-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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] allow EndpointOutput
response objects with toJSON
property
#2170
[chore] allow EndpointOutput
response objects with toJSON
property
#2170
Conversation
🦋 Changeset detectedLatest commit: d414f03 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 |
JSONValue
compatible with objects that have a toJSON
method
What's the motivation behind this change? How would you use it? |
When stringifying an object javascript will check if it has a I saw it was necessary because even though And this also masks the problem with interface bodies in my case because mongodb documents have a |
Okay, I see now that It's weird that |
I didn't say that Also, |
My first reaction is that it's a feature, not a bug, that |
Although, then again, if you've explicitly defined a |
That's a good point, being explicit about the shape of the body can make the results more predictable, but manually calling In my opinion, if someone does put a custom object inside the response body, they would be aware of how it's parsed (if they need to at all) on the client |
Also, I think there should be separate types for json input and output, since they have different constraints |
As you've said, it might be better to separate these types, as the input value has no use of having |
JSONValue
compatible with objects that have a toJSON
methodDefaultBody
compatible with objects that have a toJSON
method
I think just extending the |
I don't think its possible since simply extending output has the following behavior: const get: RequestHandler = () => {
return {
body: new Date() // works
}
}
const post: RequestHandler = () => {
return {
body: {
date: new Date() // fails
}
}
} |
|
Oops, totally overlooked that. In any case, we should do something about the duplication. Looking at it again, I think the separate input and output doesn't really apply here as |
Both are necessary, I didn't find a way to remove the function signature from type JSONPrimitive = string | boolean | number | null |
We can separate just the function itself into its own |
This has an edge case: The following code is valid, even though a const foo: JSONValue = {
toJSON(){
return {
toJSON(){
return 12;
}
}
}
} |
Let's reuse the output as the value then (repl) |
Ok, that works better than my solution |
there's enough edge cases here that I wonder if we should finally setup our first test for the type definitions to test this |
How do we do it tho? |
this is one project I remember seeing before that tested its types: https://github.com/chartjs/chartjs-plugin-datalabels/tree/master/types/test |
Where do you want to put the tests? |
Putting them in the types directory probably makes sense. E.g. maybe for |
package.json - added script to test the types tsconfig.json - ensured that test files are not included on compilation
Ah, I may have given you some bad advice about putting the tests in the So I don't know what you want to do given that. If you want to just drop the last few commits we could merge this without the tests and then maybe take a stab at doing testing along those lines in a new PR? |
Thanks for the reverts and comments, we don't really put comments on types other than those in helper. It's nice to also have them in other types, but it would make more sense if it's a public facing type for the comments, like what we have for ambient modules. For simple ones used internally, a meaningful word should be sufficient. |
DefaultBody
compatible with objects that have a toJSON
methodEndpointOutput
response objects with toJSON
property
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. All changesets should bepatch
until SvelteKit 1.0This pull request allows users to return objects that have a
toJSON
method (its return value defines the json that represents the object)