Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide access to request context via event.platform #9280

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-files-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': minor
---

feat: provide access to request context via `event.platform`
16 changes: 10 additions & 6 deletions packages/adapter-vercel/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
declare module 'SERVER' {
export { Server } from '@sveltejs/kit';
}
import { RequestContext } from './index.js';

declare module 'MANIFEST' {
import { SSRManifest } from '@sveltejs/kit';
export const manifest: SSRManifest;
declare global {
namespace App {
export interface Platform {
/**
* `context` is only available in Edge Functions
*/
context?: RequestContext;
}
}
}
6 changes: 5 additions & 1 deletion packages/adapter-vercel/files/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ const initialized = server.init({

/**
* @param {Request} request
* @param {import('../index.js').RequestContext} context
*/
export default async (request) => {
export default async (request, context) => {
await initialized;
return server.respond(request, {
getClientAddress() {
return /** @type {string} */ (request.headers.get('x-forwarded-for'));
},
platform: {
context
}
});
};
57 changes: 57 additions & 0 deletions packages/adapter-vercel/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Adapter } from '@sveltejs/kit';
import './ambient.js';

export default function plugin(config?: Config): Adapter;

Expand Down Expand Up @@ -77,3 +78,59 @@ export interface EdgeConfig {
}

export type Config = EdgeConfig | ServerlessConfig;

// we copy the RequestContext interface from `@vercel/edge` because that package can't co-exist with `@types/node`.
// see https://github.com/sveltejs/kit/pull/9280#issuecomment-1452110035

/**
* An extension to the standard `Request` object that is passed to every Edge Function.
*
* @example
* ```ts
* import type { RequestContext } from '@vercel/edge';
*
* export default async function handler(request: Request, ctx: RequestContext): Promise<Response> {
* // ctx is the RequestContext
* }
* ```
*/
export interface RequestContext {
/**
* A method that can be used to keep the function running after a response has been sent.
* This is useful when you have an async task that you want to keep running even after the
* response has been sent and the request has ended.
*
* @example
*
* <caption>Sending an internal error to an error tracking service</caption>
*
* ```ts
* import type { RequestContext } from '@vercel/edge';
*
* export async function handleRequest(request: Request, ctx: RequestContext): Promise<Response> {
* try {
* return await myFunctionThatReturnsResponse();
* } catch (e) {
* ctx.waitUntil((async () => {
* // report this error to your error tracking service
* await fetch('https://my-error-tracking-service.com', {
* method: 'POST',
* body: JSON.stringify({
* stack: e.stack,
* message: e.message,
* name: e.name,
* url: request.url,
* }),
* });
* })());
* return new Response('Internal Server Error', { status: 500 });
* }
* }
* ```
*/
waitUntil(
/**
* A promise that will be kept alive until it resolves or rejects.
*/ promise: Promise<unknown>
): void;
}
8 changes: 8 additions & 0 deletions packages/adapter-vercel/internal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare module 'SERVER' {
export { Server } from '@sveltejs/kit';
}

declare module 'MANIFEST' {
import { SSRManifest } from '@sveltejs/kit';
export const manifest: SSRManifest;
}
2 changes: 1 addition & 1 deletion packages/adapter-vercel/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"@sveltejs/kit": ["../kit/types/index"]
}
},
"include": ["**/*.js", "ambient.d.ts"]
"include": ["**/*.js", "index.d.ts", "internal.d.ts"]
}