Skip to content
This repository was archived by the owner on May 6, 2023. It is now read-only.

Commit ddd6f98

Browse files
committed
add nested routes; add nested routes with parameters
1 parent 66de4ff commit ddd6f98

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed

__tests__/route-match.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { describe, it, expect } from "bun:test";
2+
3+
describe("Route matching tests", () => {
4+
it("will match a single, non-nested route", async () => {
5+
//this needs to be testable
6+
});
7+
});

__tests__/scan-paths.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, it, expect } from "bun:test";
2+
import { RoutePath } from "buxt";
23
import { join } from "path";
34
import { cwd } from "process";
45
import ScanPaths from "src/utils/ScanPaths";
@@ -8,11 +9,11 @@ const root = join(cwd(), "__tests__", "routes");
89
describe("Route mapper tests", () => {
910
it("will recursively fetch the paths of files inside the 'routes' folder", async () => {
1011
const paths: Array<RoutePath> = [
11-
{ AbsolutePath: "messages.ts", FullPath: root + "/messages.ts", EffectiveRoute: "messages", },
12-
{ AbsolutePath: "messages/[id].ts", FullPath: root + "/messages/[id].ts", EffectiveRoute: "messages/[id]" },
13-
{ AbsolutePath: "messages/new_message.ts", FullPath: root + "/messages/new_message.ts", EffectiveRoute: "messages/new_message" },
14-
{ AbsolutePath: "user/[user]/[likes].ts", FullPath: root + "/user/[user]/[likes].ts", EffectiveRoute: "user/[user]/[likes]" },
15-
{ AbsolutePath: "user/[user].ts", FullPath: root + "/user/[user].ts", EffectiveRoute: "user/[user]" }
12+
{ AbsolutePath: "messages.ts", FullPath: root + "/messages.ts" },
13+
{ AbsolutePath: "messages/[id].ts", FullPath: root + "/messages/[id].ts" },
14+
{ AbsolutePath: "messages/new_message.ts", FullPath: root + "/messages/new_message.ts" },
15+
{ AbsolutePath: "user/[user]/[likes].ts", FullPath: root + "/user/[user]/[likes].ts" },
16+
{ AbsolutePath: "user/[user].ts", FullPath: root + "/user/[user].ts" }
1617
];
1718

1819
const routePaths = await ScanPaths(root, "");

index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use strict";
22

33
import { BuxtServer } from "./src/BuxtServer";
4-
import { BuxtRequest, BuxtResponse } from "buxt";
4+
import type { BuxtRequest, BuxtResponse, Route, RoutePath } from "./index.d";
55

66
const factoryFunction = BuxtServer.createServer;
77

88
export { factoryFunction as CreateServer };
99

10-
export type { BuxtResponse, BuxtRequest };
10+
export type { BuxtResponse, BuxtRequest, Route, RoutePath };
1111
export default BuxtServer.createServer;

src/BuxtServer.ts

+44-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,47 @@ export class BuxtServer {
3232
}));
3333
}
3434

35+
private matchRoute(routeToMatch: string): Route {
36+
let currentDepth = 0;
37+
const routeToMatchSections = routeToMatch.split("/");
38+
for (let route of this.routes) {
39+
const mappedRoute = route.route.split("/");
40+
for (let section of mappedRoute) {
41+
if (section === routeToMatchSections[currentDepth]) {
42+
if (mappedRoute.length > currentDepth + 1 &&
43+
routeToMatchSections.length > currentDepth + 1) {
44+
currentDepth++;
45+
continue;
46+
} else if (mappedRoute.length > routeToMatchSections.length ||
47+
mappedRoute.length > routeToMatchSections.length) {
48+
break;
49+
} else {
50+
return route;
51+
}
52+
}
53+
if (currentDepth > 0) {
54+
if (section.match(/(?<=\[)(.*?)(?=\])/g)) {
55+
if (mappedRoute.length > currentDepth + 1 &&
56+
routeToMatchSections.length > currentDepth + 1) {
57+
currentDepth++;
58+
continue;
59+
} else if (mappedRoute.length > routeToMatchSections.length ||
60+
mappedRoute.length > routeToMatchSections.length) {
61+
break;
62+
} else {
63+
return route;
64+
}
65+
}
66+
}
67+
}
68+
currentDepth = 0;
69+
}
70+
}
71+
72+
private async handleRequest(): Promise<void> {
73+
74+
}
75+
3576
async listen(): Promise<void> {
3677
console.info("Serving the following endpoints");
3778
this.routes.forEach(async (route, index) => {
@@ -42,12 +83,9 @@ export class BuxtServer {
4283
fetch: async (req) => {
4384
const bReq = await BuxtRequest.buildRequest(req);
4485
const bRes = new BuxtResponse();
45-
for (let route of this.routes) {
46-
if (route.route === bReq.matchPath) {
47-
await route.delegate(bReq, bRes);
48-
return bRes.getResponse();
49-
}
50-
}
86+
const r = this.matchRoute(bReq.matchPath);
87+
88+
console.log(r);
5189

5290
return new Response("Not Found", { status: 404 });
5391
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"bun-types"
1010
],
1111
"paths": {
12-
"buxt": ["index.d.ts"]
12+
"buxt": ["index.ts"]
1313
}
1414
}
1515
}

0 commit comments

Comments
 (0)