Skip to content

Commit

Permalink
feat: Add dynamic route updates
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCommieAxolotl committed Aug 1, 2023
1 parent 4fcaf97 commit d18c5fa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/router/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export type Route = {
path: string;
method: Method | Method[];
handler: (request: PyotrRequest) => ResponseData | Promise<ResponseData>;

/**
* Update the handler for this route.
*/
update: (handler: Route["handler"]) => void;
};

/**
Expand All @@ -26,8 +31,16 @@ export type Route = {
* () => html`<h1>Hello, world!</h1>`
* );
*/
export const route = (path: Route["path"], handler: Route["handler"], method?: Route["method"]): Route => ({
path,
method: method || "get",
handler,
});
export const route = (path: Route["path"], handler: Route["handler"], method?: Route["method"]): Route => {
const self: Partial<Route> = {
path,
method: method || "get",
handler,
};

self.update = (handler: Route["handler"]) => {
self.handler = handler;
};

return self as Route;
};
16 changes: 15 additions & 1 deletion test/byos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ myOwnServer.listen(4000);

const server = app(myOwnServer);

server.attach(route("/", () => html`<h1>Hello World</h1>`));
const index = route("/", () => html`<h1>Hello World</h1>`);

server.attach(index);
server.attach(route("/json", () => json({ hello: "world" })));

test("byos", async (t) => {
server.attach(
route("/[path]", (req) => {
// passes params to the handler
t.expect(req.params).toEqual({ path: "epic" });

return status(404);
Expand All @@ -25,17 +28,28 @@ test("byos", async (t) => {

await fetch("http://localhost:4000/epic");

// server is a Server instance
t.expect(server).toBeTruthy();

// response sends accurate status codes
const response = await fetch("http://localhost:4000/");
t.expect(response.status).toBe(200);

// response sends accurate content
const text = await response.text();
t.expect(text).toBe("<h1>Hello World</h1>");

// response sends accurate MIME types
const json = await fetch("http://localhost:4000/json");
t.expect(json.headers.get("content-type")).toBe("application/json");

// updating a route works
index.update(() => html`<h1>Goodbye World</h1>`);

const newRes = await (await fetch("http://localhost:4000/")).text();
t.expect(newRes).toBe("<h1>Goodbye World</h1>");

// disposing of a server works
await server.dispose();
t.expect(await server.dispose()).toBe(false);
});
16 changes: 15 additions & 1 deletion test/standalone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { app, route } from "../src";

const server = app(3000);

server.attach(route("/", () => html`<h1>Hello World</h1>`));
const index = route("/", () => html`<h1>Hello World</h1>`);

server.attach(index);
server.attach(route("/json", () => json({ hello: "world" })));

test("standalone", async (t) => {
server.attach(
route("/[path]", (req) => {
// passes params to the handler
t.expect(req.params).toEqual({ path: "epic" });

return status(404);
Expand All @@ -19,17 +22,28 @@ test("standalone", async (t) => {

await fetch("http://localhost:3000/epic");

// server is a Server instance
t.expect(server).toBeTruthy();

// response sends accurate status codes
const response = await fetch("http://localhost:3000/");
t.expect(response.status).toBe(200);

// response sends accurate content
const text = await response.text();
t.expect(text).toBe("<h1>Hello World</h1>");

// response sends accurate MIME types
const json = await fetch("http://localhost:3000/json");
t.expect(json.headers.get("content-type")).toBe("application/json");

// updating a route works
index.update(() => html`<h1>Goodbye World</h1>`);

const newRes = await (await fetch("http://localhost:3000/")).text();
t.expect(newRes).toBe("<h1>Goodbye World</h1>");

// disposing of a server works
await server.dispose();
t.expect(await server.dispose()).toBe(false);
});

0 comments on commit d18c5fa

Please sign in to comment.