diff --git a/api-extract.tsx b/api-extract.tsx new file mode 100644 index 0000000..48c0c5f --- /dev/null +++ b/api-extract.tsx @@ -0,0 +1,37 @@ +import * as Endpoints from ">able/api.tsx"; + +type EndpointOptions = Record; +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")}; +`; diff --git a/api.tsx b/api.tsx index 777ecae..42015f9 100644 --- a/api.tsx +++ b/api.tsx @@ -1,4 +1,9 @@ export default (req:Request):Response|false=> { return false; +} + +export const test =()=> +{ + return {test:true}; } \ No newline at end of file diff --git a/app.tsx b/app.tsx index 787f4b5..8d00389 100644 --- a/app.tsx +++ b/app.tsx @@ -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 ()=>

App

diff --git a/run-serve.tsx b/run-serve.tsx index 2a34f58..2c5ad97 100644 --- a/run-serve.tsx +++ b/run-serve.tsx @@ -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}; 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 = {}; + 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) {