-
-
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
Method Options - HTTP #5193
Comments
In addition to the OPTIONS method, HTTP also defines CONNECT and TRACE. TRACE means "please send my request back to me", and if it's going to be handled at all, should be handled by Svelte-Kit itself rather than by page endpoints. CONNECT means "please open a connection to this remote destination and then forward packets for me" and should only be implemented by proxies with appropriate security, and not by Svelte-Kit. Which leaves only OPTIONS, which could indeed be useful for Svelte-Kit projects that are implementing APIs, and should be added. Currently the node-adapter handles OPTIONS requests and doesn't pass them on to the site. Repro:
import type { RequestHandler } from './__types';
export const get: RequestHandler = async ({ locals }) => {
return { status: 200, body: 'get' }
}
export const options: RequestHandler = async ({ locals }) => {
return { status: 200, body: 'options' }}
|
I'd like to give this a shot |
I'm still figuring this out. Wonder if #5748 has any impact? Would appreciate if someone could give some pointers on where to fix this. |
Fixed sveltejs#5193 Formatted using `pnpm format`
Has there been any discussion around adding a generic “onRequest” function instead of splitting one function per HTTP method? That would also add compatibility with fetch based api frameworks like trpc and similar. |
Any workaround to access the options request? |
Would be nice to have this. I presume there is a workaround by using the svelte hooks |
I managed to make OPTIONS work. (This option works for me locally & on Vercel, but most probably it would work elsewhere as well). In your
I separated
As a Bonus - you can modify the behaviour of your OPTIONS response to set p.s. just a junior developer here trying to help fellow devs |
Just tried this for a Netlify deployment and it doesn't seem to work. |
if some got import { corsHeaders } from '$lib/server/corsHeaders'
import type { Handle } from '@sveltejs/kit'
export const handle: Handle = async ({ event, resolve }) => {
if (event.request.method !== 'OPTIONS') {
const response = await resolve(event);
// clone headers before mutating them
// (https://github.com/sveltejs/kit/pull/10030/files#diff-7a5a1441a4765c525635c31466bc75c520fb481ec56d1606894270a73986ff86R58)
const headers = new Headers(response.headers);
for (const [key, value] of Object.entries(corsHeaders)) {
headers.set(key, value);
}
return response;
}
console.log("=> HOOK | handeled OPTIONS request")
return new Response('ok', { headers: corsHeaders });
}; |
Describe the problem
I didn't see the Options method for building APIs. Is it possible to have it?
Describe the proposed solution
It is important to query the server capacity and the Options method, it provides for us
Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: