Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: unify apis with method, path order #114

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ import { createRouter, addRoute } from "rou3";

const router = createRouter(/* options */);

addRoute(router, "/path", "GET", { payload: "this path" });
addRoute(router, "/path/:name", "GET", { payload: "named route" });
addRoute(router, "/path/foo/**", "GET", { payload: "wildcard route" });
addRoute(router, "/path/foo/**:name", "GET", {
addRoute(router, "GET", "/path", { payload: "this path" });
addRoute(router, "POST", "/path/:name", { payload: "named route" });
addRoute(router, "GET", "/path/foo/**", { payload: "wildcard route" });
addRoute(router, "GET", "/path/foo/**:name", {
payload: "named wildcard route",
});
```
Expand All @@ -99,16 +99,16 @@ addRoute(router, "/path/foo/**:name", "GET", {

```js
// Returns { payload: 'this path' }
findRoute(router, "/path", "GET");
findRoute(router, "GET", "/path");

// Returns { payload: 'named route', params: { name: 'fooval' } }
findRoute(router, "/path/fooval", "GET");
findRoute(router, "POST", "/path/fooval");

// Returns { payload: 'wildcard route' }
findRoute(router, "/path/foo/bar/baz", "GET");
findRoute(router, "GET", "/path/foo/bar/baz");

// Returns undefined (no route matched for/)
findRoute(router, "/", "GET");
findRoute(router, "GET", "/");
```

## Benchmarks
Expand Down
4 changes: 2 additions & 2 deletions benchmark/routers/rou3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export class Rou3 extends BaseRouter {
init() {
this.router = createRouter();
for (const route of this.routes) {
addRoute(this.router, route.path, route.method, noop);
addRoute(this.router, route.method, route.path, noop);
}
}
match(request) {
const match = findRoute(this.router, request.path, request.method, {
const match = findRoute(this.router, request.method, request.path, {
ignoreParams: !this.withParams,
});
if (!match) return undefined; // 404
Expand Down
2 changes: 1 addition & 1 deletion src/operations/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { splitPath } from "./_utils";
*/
export function addRoute<T>(
ctx: RouterContext<T>,
path: string,
method: string = "",
path: string,
data?: T,
) {
const segments = splitPath(path);
Expand Down
2 changes: 1 addition & 1 deletion src/operations/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { splitPath } from "./_utils";
*/
export function findRoute<T = unknown>(
ctx: RouterContext<T>,
path: string,
method: string = "",
path: string,
opts?: { ignoreParams?: boolean },
): MatchedRoute<T> | undefined {
if (path[path.length - 1] === "/") {
Expand Down
4 changes: 2 additions & 2 deletions src/operations/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { splitPath } from "./_utils";
*/
export function matchAllRoutes<T>(
ctx: RouterContext<T>,
method: string = "",
path: string,
method?: string,
): T[] {
return _matchAll(ctx, ctx.root, method || "", splitPath(path), 0) as T[];
return _matchAll(ctx, ctx.root, method, splitPath(path), 0) as T[];
}

function _matchAll<T>(
Expand Down
2 changes: 1 addition & 1 deletion src/operations/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { splitPath } from "./_utils";
*/
export function removeRoute<T>(
ctx: RouterContext<T>,
method: string,
path: string,
method?: string,
) {
const segments = splitPath(path);
return _remove(ctx.root, method || "", segments, 0);
Expand Down
10 changes: 5 additions & 5 deletions tests/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export function createRouter<
const router = _createRouter<T>();
if (Array.isArray(routes)) {
for (const route of routes) {
addRoute(router, route, "", { path: route } as unknown as T);
addRoute(router, "GET", route, { path: route } as unknown as T);
}
} else {
for (const [route, data] of Object.entries(routes)) {
addRoute(router, route, "", data);
addRoute(router, "GET", route, data);
}
}
return router;
Expand Down Expand Up @@ -51,11 +51,11 @@ function _formatMethods(node: Node) {
if (!node.methods) {
return "";
}
return ` ┈> [${Object.entries(node.methods)
return ` ┈> ${Object.entries(node.methods)
// @ts-expect-error
.map(([method, [data, _params]]) => {
const val = (data as any)?.path || JSON.stringify(data);
return method ? `[${method}]: ${val}` : val;
return `[${method || "*"}] ${val}`;
})
.join(", ")}]`;
.join(", ")}`;
}
76 changes: 39 additions & 37 deletions tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,93 +21,95 @@ describe("Basic router", () => {
it("snapshot", () => {
expect(formatTree(router.root)).toMatchInlineSnapshot(`
"<root>
├── /test ┈> [/test]
│ ├── /foo ┈> [/test/foo]
├── /test ┈> [GET] /test
│ ├── /foo ┈> [GET] /test/foo
│ │ ├── /bar
│ │ │ ├── /qux ┈> [/test/foo/bar/qux]
│ │ ├── /baz ┈> [/test/foo/baz]
│ │ ├── /* ┈> [/test/foo/*]
│ │ ├── /** ┈> [/test/foo/**]
│ ├── /fooo ┈> [/test/fooo]
│ ├── /* ┈> [/test/:id]
│ │ ├── /y ┈> [/test/:idY/y]
│ │ │ ├── /z ┈> [/test/:idYZ/y/z]
│ │ │ ├── /qux ┈> [GET] /test/foo/bar/qux
│ │ ├── /baz ┈> [GET] /test/foo/baz
│ │ ├── /* ┈> [GET] /test/foo/*
│ │ ├── /** ┈> [GET] /test/foo/**
│ ├── /fooo ┈> [GET] /test/fooo
│ ├── /* ┈> [GET] /test/:id
│ │ ├── /y ┈> [GET] /test/:idY/y
│ │ │ ├── /z ┈> [GET] /test/:idYZ/y/z
├── /another
│ ├── /path ┈> [/another/path]
│ ├── /path ┈> [GET] /another/path
├── /wildcard
│ ├── /** ┈> [/wildcard/**]"
│ ├── /** ┈> [GET] /wildcard/**"
`);
});

it("lookup works", () => {
// Static
expect(findRoute(router, "/test")).toEqual({ data: { path: "/test" } });
expect(findRoute(router, "/test/foo")).toEqual({
expect(findRoute(router, "GET", "/test")).toEqual({
data: { path: "/test" },
});
expect(findRoute(router, "GET", "/test/foo")).toEqual({
data: { path: "/test/foo" },
});
expect(findRoute(router, "/test/fooo")).toEqual({
expect(findRoute(router, "GET", "/test/fooo")).toEqual({
data: { path: "/test/fooo" },
});
expect(findRoute(router, "/another/path")).toEqual({
expect(findRoute(router, "GET", "/another/path")).toEqual({
data: { path: "/another/path" },
});
// Param
expect(findRoute(router, "/test/123")).toEqual({
expect(findRoute(router, "GET", "/test/123")).toEqual({
data: { path: "/test/:id" },
params: { id: "123" },
});
expect(findRoute(router, "/test/123/y")).toEqual({
expect(findRoute(router, "GET", "/test/123/y")).toEqual({
data: { path: "/test/:idY/y" },
params: { idY: "123" },
});
expect(findRoute(router, "/test/123/y/z")).toEqual({
expect(findRoute(router, "GET", "/test/123/y/z")).toEqual({
data: { path: "/test/:idYZ/y/z" },
params: { idYZ: "123" },
});
expect(findRoute(router, "/test/foo/123")).toEqual({
expect(findRoute(router, "GET", "/test/foo/123")).toEqual({
data: { path: "/test/foo/*" },
params: { _0: "123" },
});
// Wildcard
expect(findRoute(router, "/test/foo/123/456")).toEqual({
expect(findRoute(router, "GET", "/test/foo/123/456")).toEqual({
data: { path: "/test/foo/**" },
params: { _: "123/456" },
});
expect(findRoute(router, "/wildcard/foo")).toEqual({
expect(findRoute(router, "GET", "/wildcard/foo")).toEqual({
data: { path: "/wildcard/**" },
params: { _: "foo" },
});
expect(findRoute(router, "/wildcard/foo/bar")).toEqual({
expect(findRoute(router, "GET", "/wildcard/foo/bar")).toEqual({
data: { path: "/wildcard/**" },
params: { _: "foo/bar" },
});
expect(findRoute(router, "/wildcard")).toEqual({
expect(findRoute(router, "GET", "/wildcard")).toEqual({
data: { path: "/wildcard/**" },
params: { _: "" },
});
});

it("remove works", () => {
removeRoute(router, "/test");
removeRoute(router, "/test/*");
removeRoute(router, "/test/foo/*");
removeRoute(router, "/test/foo/**");
removeRoute(router, "GET", "/test");
removeRoute(router, "GET", "/test/*");
removeRoute(router, "GET", "/test/foo/*");
removeRoute(router, "GET", "/test/foo/**");
expect(formatTree(router.root)).toMatchInlineSnapshot(`
"<root>
├── /test
│ ├── /foo ┈> [/test/foo]
│ ├── /foo ┈> [GET] /test/foo
│ │ ├── /bar
│ │ │ ├── /qux ┈> [/test/foo/bar/qux]
│ │ ├── /baz ┈> [/test/foo/baz]
│ ├── /fooo ┈> [/test/fooo]
│ │ │ ├── /qux ┈> [GET] /test/foo/bar/qux
│ │ ├── /baz ┈> [GET] /test/foo/baz
│ ├── /fooo ┈> [GET] /test/fooo
│ ├── /*
│ │ ├── /y ┈> [/test/:idY/y]
│ │ │ ├── /z ┈> [/test/:idYZ/y/z]
│ │ ├── /y ┈> [GET] /test/:idY/y
│ │ │ ├── /z ┈> [GET] /test/:idYZ/y/z
├── /another
│ ├── /path ┈> [/another/path]
│ ├── /path ┈> [GET] /another/path
├── /wildcard
│ ├── /** ┈> [/wildcard/**]"
│ ├── /** ┈> [GET] /wildcard/**"
`);
expect(findRoute(router, "/test")).toBeUndefined();
expect(findRoute(router, "GET", "/test")).toBeUndefined();
});
});
Loading