Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] expose Body generic to hook functions #2413

Merged
merged 5 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Body generic to hooks
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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inserting the argument in the middle will be a breaking change, but it does make sense considering the usage order and keeps it consistent with the order from other types like Load. Let's make sure to also update the docs accordingly so users can crosscheck properly.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, one could use variadic tuples if BC should be kept: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html

This would result in more work and bloat in the implementation, so I would personally vote against it. Just wanted to point out that it would be possible.

Copy link
Contributor Author

@george-lim george-lim Sep 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ignatiusmb I updated the docs to reflect the changes.
@benbender I agree with you there, it's cool that it's possible but I don't think it's worth the bloat.

(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