From e6ca2c37076eceef1e9d6dd508cbdf602f119624 Mon Sep 17 00:00:00 2001 From: cirroskais Date: Sun, 28 Apr 2024 05:05:09 -0400 Subject: [PATCH] make api routes mountable --- src/server/routes/api.ts | 31 +++++++++++------------------- src/server/routes/api/apis.ts | 9 +++++++++ src/server/routes/api/v0/api.json | 2 +- src/server/routes/api/v1/api.json | 2 +- src/server/routes/api/web/api.json | 7 ++----- 5 files changed, 24 insertions(+), 27 deletions(-) create mode 100644 src/server/routes/api/apis.ts diff --git a/src/server/routes/api.ts b/src/server/routes/api.ts index a7a44a7..eec22f5 100644 --- a/src/server/routes/api.ts +++ b/src/server/routes/api.ts @@ -1,8 +1,8 @@ import { Hono } from "hono" -import { readFile, readdir } from "fs/promises" import Files from "../lib/files.js" -import {fileURLToPath} from "url" -import {dirname} from "path" +import { fileURLToPath } from "url" +import { dirname } from "path" +import apis from "./api/apis.js" const APIDirectory = dirname(fileURLToPath(import.meta.url)) + "/api" @@ -39,10 +39,12 @@ class APIVersion { async load() { for (let _mount of this.definition.mount) { - let mount = resolveMount(_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, apiRoot: Hono) => 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.apiRoot)) } } @@ -67,23 +69,12 @@ export default class APIRouter { let def = new APIVersion(definition, this.files, this.root) await def.load() - this.root.route( - definition.baseURL, - def.root - ) + this.root.route(definition.baseURL, def.root) } async loadAPIMethods() { - let files = await readdir(APIDirectory) - for (let version of files) { - let def = JSON.parse( - ( - await readFile( - `${process.cwd()}/src/server/routes/api/${version}/api.json` - ) - ).toString() - ) as APIDefinition - await this.mount(def) + for (let api of apis) { + await this.mount(api as APIDefinition) } } } diff --git a/src/server/routes/api/apis.ts b/src/server/routes/api/apis.ts new file mode 100644 index 0000000..571c156 --- /dev/null +++ b/src/server/routes/api/apis.ts @@ -0,0 +1,9 @@ +// EXTREME BANDAID SOLUTION +// +// SHOULD BE FIXED IN SVELTEKIT REWRITE + +import web from "./web/api.json" assert { type: "json" } +import v0 from "./v0/api.json" assert { type: "json" } +import v1 from "./v1/api.json" assert { type: "json" } + +export default [web, v0, v1] diff --git a/src/server/routes/api/v0/api.json b/src/server/routes/api/v0/api.json index ad0bffb..2a35e07 100644 --- a/src/server/routes/api/v0/api.json +++ b/src/server/routes/api/v0/api.json @@ -7,4 +7,4 @@ { "file": "authRoutes", "to": "/auth" }, { "file": "fileApiRoutes", "to": "/files" } ] -} \ No newline at end of file +} diff --git a/src/server/routes/api/v1/api.json b/src/server/routes/api/v1/api.json index 7e5affe..52553b5 100644 --- a/src/server/routes/api/v1/api.json +++ b/src/server/routes/api/v1/api.json @@ -13,4 +13,4 @@ "to": "/file" } ] -} \ No newline at end of file +} diff --git a/src/server/routes/api/web/api.json b/src/server/routes/api/web/api.json index a20237d..a8e17c3 100644 --- a/src/server/routes/api/web/api.json +++ b/src/server/routes/api/web/api.json @@ -1,8 +1,5 @@ { "name": "web", "baseURL": "/", - "mount": [ - { "file": "preview", "to": "/download" }, - "go" - ] -} \ No newline at end of file + "mount": [{ "file": "preview", "to": "/download" }, "go"] +}