-
-
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
Should ReadOnlyFormData type be exported? #1215
Comments
I'd be happy to send a PR, but I'm not sure if directly exporting a type from |
That's probably just strict TS being TS. Although it's true that if we know ahead what the body type is, we can pass it in through its generics to avoid narrowing and writing code like this if (typeof body !== 'string' && !(body instanceof Buffer)) {
// body: ReadOnlyFormData
} else {
// body: string | Buffer
} Still, it's probably better to explicitly handle those cases where body would be export const post: RequestHandler = async ({ body }) => {
if (typeof body === 'string' || body instanceof Buffer) {
return { status: 400 }; // add your own custom error message
}
// code for body as ReadOnlyFormData
}; |
Explicitly exhausting body types is good advice, thanks. I do hope svelte kit could somehow provide a way to more easily narrow the body to a specific type: Instead of if (typeof body === 'string' || body instanceof Buffer) {
return { status: 400 };
} We can just do if (!(body instanceof ReadOnlyFormData)) {
return { status: 400 };
} In the previous case, if svelte kit ever decides to support additional body types (e.g., |
Alternatively, we can provide a type guard function to check Maybe it's not needed either. Since the user can also define a function by ton their own and check if methods exist. The concrete type doesn't matter to the user. The users only need to know if the object looks like it. |
To clarify and supplement my previous comment. I don't think the I think it makes sense to export the interface import type { BaseBody } from '@sveltejs/kit/types/helper';
type ReadOnlyFormData = Exclude<BaseBody, string | Buffer> or just copy the interface into your own codebase. But I guess it may still have a lot of PR or issues in the future to ask for it to be exported. Like those store and transition types in the main repo. |
Agreed, and I vote for exporting a type guard function.
FormData has many methods with common names, userland's duck typing is probably going to be handwavy. If this type guard is exported by svelte kit, it can export a function that directly does |
I also wonder, why not directly use FormData in svelte kit? Users can directly use But I guess there is probably a reason I'm not aware of that calls for custom implementation. |
I agree, and on that note, exporting a type guard function means Exporting |
First off, thanks @ignatiusmb for the PR. However, on second thought, I'm in the opinion that this addresses the wrong problem. An user agent can post any kind of data it wants to the endpoint, so a defense mechanism to check for body type as suggested in @ignatiusmb's first comment is really good. The currently solution encourages people to directly use I still think we should provide a mechanism to allow users to easily and explicitly check body type instead. @Rich-Harris is it a bad idea to just use vanilla FormData instead of |
@ignatiusmb fix works for |
Is your feature request related to a problem? Please describe.
Currently in the demo app, typescript complains about this method access:
kit/packages/create-svelte/templates/default/src/routes/todos/index.json.ts
Line 25 in 399220d
Describe the solution you'd like
The
export
statement should probably start withBut that requires
ReadOnlyFormData
being able to be importedDescribe alternatives you've considered
How important is this feature to you?
Additional context
The text was updated successfully, but these errors were encountered: