seems to be working?

This commit is contained in:
Seth Trowbridge 2024-05-10 10:01:57 -04:00
parent 11f896f12b
commit 2945b72791
4 changed files with 64 additions and 2 deletions

37
api-extract.tsx Normal file
View File

@ -0,0 +1,37 @@
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")};
`;

View File

@ -2,3 +2,8 @@ export default (req:Request):Response|false=>
{
return false;
}
export const test =()=>
{
return {test:true};
}

View File

@ -1,6 +1,8 @@
import {test} from ">able/api.tsx";
import * as ISO from ">able/iso-elements.tsx";
console.log(ISO)
console.log(test());
export default ()=><div>
<h1 class="p-4 bg-red-500 text-white">App</h1>

View File

@ -2,6 +2,7 @@ import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts";
import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62";
import { HuntConfig, Root } from "./checker.tsx";
import CustomServe from ">able/api.tsx";
import { Call, Spoof } from "./api-extract.tsx";
type DenoConfig = {imports:Record<string, string>};
const ImportMap:DenoConfig = {imports:{}};
@ -26,7 +27,6 @@ const ImportMapReload =async()=>
imports[key] = "/"+key;
}
});
ImportMap.imports = Configuration.Remap(imports, Configuration);
}
@ -179,6 +179,9 @@ export default async()=>
{
/** pathname with no leading slash */
const clippedPath = decodeURI(url.pathname.substring(1));
if(clippedPath == ">able/api.tsx"){
return new Response(Spoof, {headers:{"content-type":"application/javascript"}});
}
proxy = import.meta.resolve(clippedPath);
}
@ -188,6 +191,21 @@ export default async()=>
{
return custom;
}
// auto-api
if(url.pathname.startsWith("/api/"))
{
const functionName = url.pathname.split("/")[2];
const options:Record<string, string|number|boolean> = {};
url.searchParams.forEach((value, key)=>{options[key] = value});
const response = Call(functionName, options);
if(response)
{
return response;
}
}
// full api
const api = await Configuration.Serve(req, url, ext, ImportMap, Configuration);
if(api)
{