Skip to content

Commit

Permalink
[chore] expose Body generic to hook functions (#2413)
Browse files Browse the repository at this point in the history
* Add Body generic to hooks

* Add changeset

* Update hooks documentation

* Remove MaybePromise helper type from hooks documentation

* update changeset

Co-authored-by: Ignatius Bagus <ignatius.mbs@gmail.com>
  • Loading branch information
george-lim and ignatiusmb authored Sep 14, 2021
1 parent b781b2f commit d7189eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-buttons-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add a generic argument to allow typing Body from hooks
14 changes: 7 additions & 7 deletions documentation/docs/04-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export interface ServerResponse {
body?: StrictBody;
}

export interface Handle<Locals = Record<string, any>> {
export interface Handle<Locals = Record<string, any>, Body = unknown> {
(input: {
request: ServerRequest<Locals>;
resolve(request: ServerRequest<Locals>): ServerResponse | Promise<ServerResponse>;
request: ServerRequest<Locals, Body>;
resolve(request: ServerRequest<Locals, Body>): ServerResponse | Promise<ServerResponse>;
}): ServerResponse | Promise<ServerResponse>;
}
```
Expand Down Expand Up @@ -92,8 +92,8 @@ If unimplemented, SvelteKit will log the error with default formatting.
```ts
// Declaration types for handleError hook

export interface HandleError<Locals = Record<string, any>> {
(input: { error: Error & { frame?: string }; request: ServerRequest<Locals> }): void;
export interface HandleError<Locals = Record<string, any>, Body = unknown> {
(input: { error: Error & { frame?: string }; request: ServerRequest<Locals, Body> }): void;
}
```

Expand All @@ -116,8 +116,8 @@ If unimplemented, session is `{}`.
```ts
// Declaration types for getSession hook

export interface GetSession<Locals = Record<string, any>, Session = any> {
(request: ServerRequest<Locals>): Session | Promise<Session>;
export interface GetSession<Locals = Record<string, any>, Body = unknown, Session = any> {
(request: ServerRequest<Locals, Body>): Session | Promise<Session>;
}
```

Expand Down
14 changes: 7 additions & 7 deletions packages/kit/types/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ export interface ServerResponse {
body?: StrictBody;
}

export interface GetSession<Locals = Record<string, any>, Session = any> {
(request: ServerRequest<Locals>): MaybePromise<Session>;
export interface GetSession<Locals = Record<string, any>, Body = unknown, Session = any> {
(request: ServerRequest<Locals, Body>): MaybePromise<Session>;
}

export interface Handle<Locals = Record<string, any>> {
export interface Handle<Locals = Record<string, any>, Body = unknown> {
(input: {
request: ServerRequest<Locals>;
resolve(request: ServerRequest<Locals>): MaybePromise<ServerResponse>;
request: ServerRequest<Locals, Body>;
resolve(request: ServerRequest<Locals, Body>): MaybePromise<ServerResponse>;
}): MaybePromise<ServerResponse>;
}

export interface HandleError<Locals = Record<string, any>> {
(input: { error: Error & { frame?: string }; request: ServerRequest<Locals> }): void;
export interface HandleError<Locals = Record<string, any>, Body = unknown> {
(input: { error: Error & { frame?: string }; request: ServerRequest<Locals, Body> }): void;
}

export interface ExternalFetch {
Expand Down

0 comments on commit d7189eb

Please sign in to comment.