From 0bf5f6f985c32827ba74a45facb4780345073098 Mon Sep 17 00:00:00 2001 From: stringsplit <77242831+nbitzz@users.noreply.github.com> Date: Tue, 26 Mar 2024 16:15:58 -0700 Subject: [PATCH] Genius --- src/server/routes/api.ts | 10 ++++++---- src/server/routes/api/v0/primaryApi.ts | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/server/routes/api.ts b/src/server/routes/api.ts index ab4ca43..a7a44a7 100644 --- a/src/server/routes/api.ts +++ b/src/server/routes/api.ts @@ -26,22 +26,24 @@ function resolveMount(mount: APIMountResolvable): APIMount { class APIVersion { readonly definition: APIDefinition readonly apiPath: string + readonly apiRoot: Hono readonly root: Hono = new Hono() readonly files: Files - constructor(definition: APIDefinition, files: Files) { + constructor(definition: APIDefinition, files: Files, apiRoot: Hono) { this.definition = definition this.apiPath = APIDirectory + "/" + definition.name this.files = files + this.apiRoot = apiRoot } async load() { for (let _mount of this.definition.mount) { let mount = resolveMount(_mount); // no idea if there's a better way to do this but this is all i can think of - let { default: route } = await import(`${this.apiPath}/${mount.file}.js`) as { default: (files: Files) => Hono } + let { default: route } = await import(`${this.apiPath}/${mount.file}.js`) as { default: (files: Files, apiRoot: Hono) => Hono } - this.root.route(mount.to, route(this.files)) + this.root.route(mount.to, route(this.files, this.apiRoot)) } } } @@ -62,7 +64,7 @@ export default class APIRouter { private async mount(definition: APIDefinition) { console.log(`mounting APIDefinition ${definition.name}`) - let def = new APIVersion(definition, this.files) + let def = new APIVersion(definition, this.files, this.root) await def.load() this.root.route( diff --git a/src/server/routes/api/v0/primaryApi.ts b/src/server/routes/api/v0/primaryApi.ts index 1e313a3..992fd76 100644 --- a/src/server/routes/api/v0/primaryApi.ts +++ b/src/server/routes/api/v0/primaryApi.ts @@ -20,9 +20,9 @@ export let primaryApi = new Hono<{ primaryApi.all("*", getAccount) -export default function (files: Files) { +export default function (files: Files, apiRoot: Hono) { primaryApi.get("/file/:fileId", async (ctx) => - primaryApi.fetch( + apiRoot.fetch( new Request( (new URL( `/api/v1/file/${ctx.req.param("fileId")}`, ctx.req.raw.url)).href,