From fc7985da11c040be37e8ffe6151fd7ba84c656ff Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Fri, 3 Sep 2021 08:48:16 -0700 Subject: [PATCH] [feat] provide the original request object --- .changeset/tricky-timers-boil.md | 8 ++++++++ documentation/docs/04-hooks.md | 9 +++++---- packages/adapter-netlify/files/entry.js | 3 ++- packages/adapter-node/src/kit-middleware.js | 3 ++- packages/adapter-vercel/files/entry.js | 3 ++- packages/kit/src/core/dev/index.js | 3 ++- packages/kit/types/app.d.ts | 3 ++- packages/kit/types/hooks.d.ts | 4 ++-- 8 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 .changeset/tricky-timers-boil.md diff --git a/.changeset/tricky-timers-boil.md b/.changeset/tricky-timers-boil.md new file mode 100644 index 000000000000..e63708ab0637 --- /dev/null +++ b/.changeset/tricky-timers-boil.md @@ -0,0 +1,8 @@ +--- +'@sveltejs/adapter-netlify': patch +'@sveltejs/adapter-node': patch +'@sveltejs/adapter-vercel': patch +'@sveltejs/kit': patch +--- + +[feat] provide the original request object diff --git a/documentation/docs/04-hooks.md b/documentation/docs/04-hooks.md index 88643c849039..f6f82188407c 100644 --- a/documentation/docs/04-hooks.md +++ b/documentation/docs/04-hooks.md @@ -24,24 +24,25 @@ type ResponseHeaders = Record; type RequestHeaders = Record; export type RawBody = null | Uint8Array; -export interface IncomingRequest { +export interface IncomingRequest { method: string; host: string; path: string; query: URLSearchParams; headers: RequestHeaders; rawBody: RawBody; + platformReq?: PlatformRequest; } type ParameterizedBody = Body extends FormData ? ReadOnlyFormData : (string | RawBody | ReadOnlyFormData) & Body; // ServerRequest is exported as Request -export interface ServerRequest, Body = unknown> - extends IncomingRequest { +export interface ServerRequest, Body = unknown, PlatformRequest = any> + extends IncomingRequest { params: Record; body: ParameterizedBody; - locals: Locals; // populated by hooks handle + locals: Locals; // populated by the handle hook } type StrictBody = string | Uint8Array; diff --git a/packages/adapter-netlify/files/entry.js b/packages/adapter-netlify/files/entry.js index e5e7fd718c77..cb47d62a1b20 100644 --- a/packages/adapter-netlify/files/entry.js +++ b/packages/adapter-netlify/files/entry.js @@ -16,7 +16,8 @@ export async function handler(event) { headers, path, query, - rawBody + rawBody, + platformReq: event }); if (rendered) { diff --git a/packages/adapter-node/src/kit-middleware.js b/packages/adapter-node/src/kit-middleware.js index 5807b3d587fb..ee81d258255b 100644 --- a/packages/adapter-node/src/kit-middleware.js +++ b/packages/adapter-node/src/kit-middleware.js @@ -23,7 +23,8 @@ export function create_kit_middleware({ render }) { headers: req.headers, // TODO: what about repeated headers, i.e. string[] path: parsed.pathname, query: parsed.searchParams, - rawBody: body + rawBody: body, + platformReq: req }); if (rendered) { diff --git a/packages/adapter-vercel/files/entry.js b/packages/adapter-vercel/files/entry.js index 35d6e3170a7c..df587c145361 100644 --- a/packages/adapter-vercel/files/entry.js +++ b/packages/adapter-vercel/files/entry.js @@ -22,7 +22,8 @@ export default async (req, res) => { headers: req.headers, path: pathname, query: searchParams, - rawBody: body + rawBody: body, + platformReq: req }); if (rendered) { diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js index 1d68339776f3..cd39a0191076 100644 --- a/packages/kit/src/core/dev/index.js +++ b/packages/kit/src/core/dev/index.js @@ -345,7 +345,8 @@ async function create_handler(vite, config, dir, cwd, get_manifest) { host, path: parsed.pathname.replace(config.kit.paths.base, ''), query: parsed.searchParams, - rawBody: body + rawBody: body, + platformReq: req }, { amp: config.kit.amp, diff --git a/packages/kit/types/app.d.ts b/packages/kit/types/app.d.ts index 4446d227cf57..0109b1465b53 100644 --- a/packages/kit/types/app.d.ts +++ b/packages/kit/types/app.d.ts @@ -27,11 +27,12 @@ export type ParameterizedBody = Body extends FormData ? ReadOnlyFormData : (string | RawBody | ReadOnlyFormData) & Body; -export interface IncomingRequest { +export interface IncomingRequest { method: string; host: string; path: string; query: URLSearchParams; headers: RequestHeaders; rawBody: RawBody; + platformReq?: PlatformRequest; } diff --git a/packages/kit/types/hooks.d.ts b/packages/kit/types/hooks.d.ts index 60d8a8263ad4..7d228cb9a495 100644 --- a/packages/kit/types/hooks.d.ts +++ b/packages/kit/types/hooks.d.ts @@ -3,8 +3,8 @@ import { MaybePromise, ResponseHeaders } from './helper'; export type StrictBody = string | Uint8Array; -export interface ServerRequest, Body = unknown> - extends IncomingRequest { +export interface ServerRequest, Body = unknown, PlatformRequest = any> + extends IncomingRequest { params: Record; body: ParameterizedBody; locals: Locals;