From d7189eba42d360aaef506329ae329e17b3afbae7 Mon Sep 17 00:00:00 2001 From: George Lim <21700768+george-lim@users.noreply.github.com> Date: Tue, 14 Sep 2021 01:49:40 -0400 Subject: [PATCH] [chore] expose Body generic to hook functions (#2413) * Add Body generic to hooks * Add changeset * Update hooks documentation * Remove MaybePromise helper type from hooks documentation * update changeset Co-authored-by: Ignatius Bagus --- .changeset/two-buttons-eat.md | 5 +++++ documentation/docs/04-hooks.md | 14 +++++++------- packages/kit/types/hooks.d.ts | 14 +++++++------- 3 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 .changeset/two-buttons-eat.md diff --git a/.changeset/two-buttons-eat.md b/.changeset/two-buttons-eat.md new file mode 100644 index 000000000000..d39c10241834 --- /dev/null +++ b/.changeset/two-buttons-eat.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Add a generic argument to allow typing Body from hooks diff --git a/documentation/docs/04-hooks.md b/documentation/docs/04-hooks.md index 88643c849039..dafd7093d80b 100644 --- a/documentation/docs/04-hooks.md +++ b/documentation/docs/04-hooks.md @@ -52,10 +52,10 @@ export interface ServerResponse { body?: StrictBody; } -export interface Handle> { +export interface Handle, Body = unknown> { (input: { - request: ServerRequest; - resolve(request: ServerRequest): ServerResponse | Promise; + request: ServerRequest; + resolve(request: ServerRequest): ServerResponse | Promise; }): ServerResponse | Promise; } ``` @@ -92,8 +92,8 @@ If unimplemented, SvelteKit will log the error with default formatting. ```ts // Declaration types for handleError hook -export interface HandleError> { - (input: { error: Error & { frame?: string }; request: ServerRequest }): void; +export interface HandleError, Body = unknown> { + (input: { error: Error & { frame?: string }; request: ServerRequest }): void; } ``` @@ -116,8 +116,8 @@ If unimplemented, session is `{}`. ```ts // Declaration types for getSession hook -export interface GetSession, Session = any> { - (request: ServerRequest): Session | Promise; +export interface GetSession, Body = unknown, Session = any> { + (request: ServerRequest): Session | Promise; } ``` diff --git a/packages/kit/types/hooks.d.ts b/packages/kit/types/hooks.d.ts index 60d8a8263ad4..4bbc81267b05 100644 --- a/packages/kit/types/hooks.d.ts +++ b/packages/kit/types/hooks.d.ts @@ -16,19 +16,19 @@ export interface ServerResponse { body?: StrictBody; } -export interface GetSession, Session = any> { - (request: ServerRequest): MaybePromise; +export interface GetSession, Body = unknown, Session = any> { + (request: ServerRequest): MaybePromise; } -export interface Handle> { +export interface Handle, Body = unknown> { (input: { - request: ServerRequest; - resolve(request: ServerRequest): MaybePromise; + request: ServerRequest; + resolve(request: ServerRequest): MaybePromise; }): MaybePromise; } -export interface HandleError> { - (input: { error: Error & { frame?: string }; request: ServerRequest }): void; +export interface HandleError, Body = unknown> { + (input: { error: Error & { frame?: string }; request: ServerRequest }): void; } export interface ExternalFetch {