diff --git a/example/pages/api/hello world with space.ts b/example/pages/api/hello world with space.ts new file mode 100644 index 0000000..ad10186 --- /dev/null +++ b/example/pages/api/hello world with space.ts @@ -0,0 +1,5 @@ +import { oak } from "../../../deps/mod.ts"; + +export default (ctx: oak.RouterContext) => { + ctx.response.body = { "hello": "world" }; +}; diff --git a/example/pages/api/hello-world.ts b/example/pages/api/hello-world.ts new file mode 100644 index 0000000..ad10186 --- /dev/null +++ b/example/pages/api/hello-world.ts @@ -0,0 +1,5 @@ +import { oak } from "../../../deps/mod.ts"; + +export default (ctx: oak.RouterContext) => { + ctx.response.body = { "hello": "world" }; +}; diff --git a/src/apis.ts b/src/apis.ts new file mode 100644 index 0000000..e05ff2b --- /dev/null +++ b/src/apis.ts @@ -0,0 +1,11 @@ +import { oak, path } from "../deps/mod.ts"; + +export const addApis = async (router: oak.Router) => { + const apis = Deno.readDir("pages/api"); + for await (const api of apis) { + const page = await import(path.resolve(`pages/api/${api.name}`)); + const url = `/${api.name.replace(/(\.ts|\s)+/g, "")}`; + router.get(url, page.default); + } + return Promise.resolve(); +}; diff --git a/src/serve.ts b/src/serve.ts index 046d8b2..246632e 100644 --- a/src/serve.ts +++ b/src/serve.ts @@ -6,6 +6,7 @@ import { WebSocket, } from "../deps/mod.ts"; import type { Page } from "./util.ts"; +import { addApis } from "./apis.ts"; export async function serve( pages: Page[], @@ -57,7 +58,7 @@ export async function serve( ); }); } - + addApis(router); app.use(router.routes()); app.use(router.allowedMethods());