able-baker/api-extract.tsx

38 lines
917 B
TypeScript

import * as Endpoints from ">able/api.tsx";
type EndpointOptions = Record<string, string|number|boolean>;
type EndpointHandler = (options:EndpointOptions)=>unknown;
export function Call(functionName:string, options:EndpointOptions)
{
try
{
// @ts-ignore we're catchig this anyway
const results = Endpoints[functionName](options);
return new Response(JSON.stringify(results));
}
catch(e)
{
return false;
}
}
function proxy(functionName:string)
{
return async( ...args:string[])=>
{
const resp = await fetch(`/api/${functionName}/${args.join("/")}`);
const json = await resp.json();
return json;
}
}
const parts:string[] = [];
for(const member in Endpoints)
{
member != "default" && parts.push(`export const ${member} = ${proxy.name}("${member}")`);
}
export const Spoof = `${proxy.toString()}
${parts.join("\n")};
`;