Skip to content

Commit

Permalink
provide access to request context via event.platform (#9280)
Browse files Browse the repository at this point in the history
* provide access to request context via `event.platform` - closes #8706

* cheat
  • Loading branch information
Rich-Harris committed Mar 2, 2023
1 parent 744dc81 commit 2d8c63a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 8 deletions.
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"]
}

0 comments on commit 2d8c63a

Please sign in to comment.