mirror of
https://github.com/mollersuite/monofile.git
synced 2024-11-21 21:36:26 -08:00
make api routes mountable
This commit is contained in:
parent
4264ee41f4
commit
e6ca2c3707
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
9
src/server/routes/api/apis.ts
Normal file
9
src/server/routes/api/apis.ts
Normal file
|
@ -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]
|
|
@ -7,4 +7,4 @@
|
|||
{ "file": "authRoutes", "to": "/auth" },
|
||||
{ "file": "fileApiRoutes", "to": "/files" }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,4 +13,4 @@
|
|||
"to": "/file"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
{
|
||||
"name": "web",
|
||||
"baseURL": "/",
|
||||
"mount": [
|
||||
{ "file": "preview", "to": "/download" },
|
||||
"go"
|
||||
]
|
||||
}
|
||||
"mount": [{ "file": "preview", "to": "/download" }, "go"]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue