Skip to content

Commit

Permalink
[feat] provide the original request object
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Sep 3, 2021
1 parent fe642d2 commit 911b5b6
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .changeset/tricky-timers-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-vercel': patch
'@sveltejs/kit': patch
---

[feat] provide the platform request object
9 changes: 5 additions & 4 deletions documentation/docs/04-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ type ResponseHeaders = Record<string, string | string[]>;
type RequestHeaders = Record<string, string>;

export type RawBody = null | Uint8Array;
export interface IncomingRequest {
export interface IncomingRequest<PlatformRequest = any> {
method: string;
host: string;
path: string;
query: URLSearchParams;
headers: RequestHeaders;
rawBody: RawBody;
platformReq?: PlatformRequest;
}

type ParameterizedBody<Body = unknown> = Body extends FormData
? ReadOnlyFormData
: (string | RawBody | ReadOnlyFormData) & Body;
// ServerRequest is exported as Request
export interface ServerRequest<Locals = Record<string, any>, Body = unknown>
extends IncomingRequest {
export interface ServerRequest<Locals = Record<string, any>, Body = unknown, PlatformRequest = any>
extends IncomingRequest<PlatformRequest> {
params: Record<string, string>;
body: ParameterizedBody<Body>;
locals: Locals; // populated by hooks handle
locals: Locals; // populated by the handle hook
}

type StrictBody = string | Uint8Array;
Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-netlify/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export async function handler(event) {
headers,
path,
query,
rawBody
rawBody,
platformReq: event
});

if (rendered) {
Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-node/src/kit-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-vercel/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export default async (req, res) => {
headers: req.headers,
path: pathname,
query: searchParams,
rawBody: body
rawBody: body,
platformReq: req
});

if (rendered) {
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export type ParameterizedBody<Body = unknown> = Body extends FormData
? ReadOnlyFormData
: (string | RawBody | ReadOnlyFormData) & Body;

export interface IncomingRequest {
export interface IncomingRequest<PlatformRequest = any> {
method: string;
host: string;
path: string;
query: URLSearchParams;
headers: RequestHeaders;
rawBody: RawBody;
platformReq?: PlatformRequest;
}
4 changes: 2 additions & 2 deletions packages/kit/types/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { MaybePromise, ResponseHeaders } from './helper';

export type StrictBody = string | Uint8Array;

export interface ServerRequest<Locals = Record<string, any>, Body = unknown>
extends IncomingRequest {
export interface ServerRequest<Locals = Record<string, any>, Body = unknown, PlatformRequest = any>
extends IncomingRequest<PlatformRequest> {
params: Record<string, string>;
body: ParameterizedBody<Body>;
locals: Locals;
Expand Down

0 comments on commit 911b5b6

Please sign in to comment.