able-baker/api-extract.tsx

38 lines
917 B
TypeScript
Raw Permalink Normal View History

2024-05-10 10:01:57 -04:00
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")};
`;