handle delete resrved word (#3078)
This commit is contained in:
parent
1bfdf43dca
commit
d33e177817
3 changed files with 30 additions and 5 deletions
5
.changeset/chilly-terms-smoke.md
Normal file
5
.changeset/chilly-terms-smoke.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Support the "del" API method, because "delete" is a reserved word.
|
5
.changeset/lazy-kangaroos-rush.md
Normal file
5
.changeset/lazy-kangaroos-rush.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add support for an "all" API method, to handle all requests
|
|
@ -441,17 +441,32 @@ export function defineScriptVars(vars: Record<any, any>) {
|
||||||
return markHTMLString(output);
|
return markHTMLString(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Renders an endpoint request to completion, returning the body.
|
function getHandlerFromModule(mod: EndpointHandler, method: string) {
|
||||||
export async function renderEndpoint(mod: EndpointHandler, request: Request, params: Params) {
|
// If there was an exact match on `method`, return that function.
|
||||||
const chosenMethod = request.method?.toLowerCase() ?? 'get';
|
if (mod[method]) {
|
||||||
const handler = mod[chosenMethod];
|
return mod[method];
|
||||||
|
}
|
||||||
|
// Handle `del` instead of `delete`, since `delete` is a reserved word in JS.
|
||||||
|
if (method === 'delete' && mod['del']) {
|
||||||
|
return mod['del'];
|
||||||
|
}
|
||||||
|
// If a single `all` handler was used, return that function.
|
||||||
|
if (mod['all']) {
|
||||||
|
return mod['all'];
|
||||||
|
}
|
||||||
|
// Otherwise, no handler found.
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Renders an endpoint request to completion, returning the body. */
|
||||||
|
export async function renderEndpoint(mod: EndpointHandler, request: Request, params: Params) {
|
||||||
|
const chosenMethod = request.method?.toLowerCase();
|
||||||
|
const handler = getHandlerFromModule(mod, chosenMethod);
|
||||||
if (!handler || typeof handler !== 'function') {
|
if (!handler || typeof handler !== 'function') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Endpoint handler not found! Expected an exported function for "${chosenMethod}"`
|
`Endpoint handler not found! Expected an exported function for "${chosenMethod}"`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return await handler.call(mod, params, request);
|
return await handler.call(mod, params, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue