Skip to content

Commit

Permalink
feat: aadd routes API (lucacasonato#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoXuS committed Jan 28, 2021
1 parent e175b88 commit ba024ef
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions example/pages/api/hello world with space.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { oak } from "../../../deps/mod.ts";

export default (ctx: oak.RouterContext) => {
ctx.response.body = { "hello": "world" };
};
5 changes: 5 additions & 0 deletions example/pages/api/hello-world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { oak } from "../../../deps/mod.ts";

export default (ctx: oak.RouterContext) => {
ctx.response.body = { "hello": "world" };
};
11 changes: 11 additions & 0 deletions src/apis.ts
Original file line number Diff line number Diff line change
@@ -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();
};
3 changes: 2 additions & 1 deletion src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down Expand Up @@ -57,7 +58,7 @@ export async function serve(
);
});
}

addApis(router);
app.use(router.routes());
app.use(router.allowedMethods());

Expand Down

0 comments on commit ba024ef

Please sign in to comment.