65 lines
1.8 KiB
JavaScript
65 lines
1.8 KiB
JavaScript
![]() |
function getHandlerFromModule(mod, method) {
|
||
|
if (mod[method]) {
|
||
|
return mod[method];
|
||
|
}
|
||
|
if (method === "delete" && mod["del"]) {
|
||
|
return mod["del"];
|
||
|
}
|
||
|
if (mod["all"]) {
|
||
|
return mod["all"];
|
||
|
}
|
||
|
return void 0;
|
||
|
}
|
||
|
async function renderEndpoint(mod, context, ssr) {
|
||
|
var _a;
|
||
|
const { request, params } = context;
|
||
|
const chosenMethod = (_a = request.method) == null ? void 0 : _a.toLowerCase();
|
||
|
const handler = getHandlerFromModule(mod, chosenMethod);
|
||
|
if (!ssr && ssr === false && chosenMethod && chosenMethod !== "get") {
|
||
|
console.warn(`
|
||
|
${chosenMethod} requests are not available when building a static site. Update your config to \`output: 'server'\` or \`output: 'hybrid'\` with an \`export const prerender = false\` to handle ${chosenMethod} requests.`);
|
||
|
}
|
||
|
if (!handler || typeof handler !== "function") {
|
||
|
let response = new Response(null, {
|
||
|
status: 404,
|
||
|
headers: {
|
||
|
"X-Astro-Response": "Not-Found"
|
||
|
}
|
||
|
});
|
||
|
return response;
|
||
|
}
|
||
|
if (handler.length > 1) {
|
||
|
console.warn(`
|
||
|
API routes with 2 arguments have been deprecated. Instead they take a single argument in the form of:
|
||
|
|
||
|
export function get({ params, request }) {
|
||
|
//...
|
||
|
}
|
||
|
|
||
|
Update your code to remove this warning.`);
|
||
|
}
|
||
|
const proxy = new Proxy(context, {
|
||
|
get(target, prop) {
|
||
|
if (prop in target) {
|
||
|
return Reflect.get(target, prop);
|
||
|
} else if (prop in params) {
|
||
|
console.warn(`
|
||
|
API routes no longer pass params as the first argument. Instead an object containing a params property is provided in the form of:
|
||
|
|
||
|
export function get({ params }) {
|
||
|
// ...
|
||
|
}
|
||
|
|
||
|
Update your code to remove this warning.`);
|
||
|
return Reflect.get(params, prop);
|
||
|
} else {
|
||
|
return void 0;
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
return handler.call(mod, proxy, request);
|
||
|
}
|
||
|
export {
|
||
|
renderEndpoint
|
||
|
};
|