-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Improve RequestHandler and EndpointOutput type declarations #1778
Improve RequestHandler and EndpointOutput type declarations #1778
Conversation
🦋 Changeset detectedLatest commit: 27d8b34 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…t or Response bodies. Added a generic parameter to `RequestHandler` and an generic parameter to `EndpointOutput`. Together, these let you specify the type of the body of the Response you want to return from a RequestHandler function. The default of both of these parameters is `DefaultBody`.
76d75eb
to
b1d5f97
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, just a couple of comments. A changeset would also be nice.
…and `RequestHandler`. Also added patch changeset
Cool. I just made the changes:
Let me know if I misunderstood any of your suggestions - I'd be happy to go back and amend any mistakes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Everything else looks good to me.
If I wanted my
post
function to return anumber
as a response body and I accidentally return astring
, there is no way I can let the type-checker prevent situations like these. There is no way I can tell it "hey, make sure the body property is always a number". So, the following typechecks:This PR changes
RequestHandler
andEndpointOutput
so that users can specify their response body type asnumber
by makingnumber
the third generic argument toRequestHandler
. As a result, this won't typecheck:I wanted to make the second generic parameter to
RequestHander
,ReqBody
be of default typeDefaultBody
, but this causes an error inpackages/kit/src/runtime/server/endpoint.js
on line 27, because therequest.body: BaseBody
is not type compatible withBaseBody & DefaultBody
, which is the type it would end up with (per the type definitions ofServerRequest
andParametrizedBody
).There might be a better way to avoid the error and give
ReqBody
stronger typing than unknown, but I don't currently know enough about the codebase to do so.Background for this PR: In the discord channel
contribute-to-svelte-kit
, I proposed this idea a few messages from the last.Before submitting the PR, please make sure you do the following
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpx changeset
and following the prompts