-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@dreamkit/app": patch | ||
--- | ||
|
||
Fix path matching |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { kindApp } from "./utils/kind.js"; | ||
import { createRoutePathRegex, extractPathParams } from "./utils/routing.js"; | ||
|
||
export class RequestUrl<T = string> extends URL { | ||
static { | ||
kindApp(this, "RequestUrl"); | ||
} | ||
is(...paths: (keyof T)[]): boolean { | ||
// [review] | ||
return paths.includes(this.pathname as any); | ||
return paths.some((path) => | ||
createRoutePathRegex(path as string).test(this.pathname), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
createRoutePathRegex, | ||
createRouteUrl, | ||
extractPathParams, | ||
} from "../../src/utils/routing.js"; | ||
import { describe, it, expect } from "vitest"; | ||
|
||
describe("extractPathParams", () => { | ||
it("extract optional param", () => { | ||
expect(extractPathParams("/users/:id?")).toMatchObject([ | ||
{ | ||
key: ":id?", | ||
name: "id", | ||
optional: true, | ||
spread: false, | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe("createRouteUrl", () => { | ||
it("with optional param", () => { | ||
expect(createRouteUrl("/users/:id?", { id: 1 }).pathname).toBe("/users/1"); | ||
expect(createRouteUrl("/users/:id?").pathname).toBe("/users"); | ||
}); | ||
}); | ||
|
||
describe("createRoutePathRegex", () => { | ||
it("with optional param", () => { | ||
const regex = createRoutePathRegex("/users/:id?"); | ||
expect(regex.test("/users")).toBe(true); | ||
expect(regex.test("/users/")).toBe(true); | ||
expect(regex.test("/users/1")).toBe(true); | ||
expect(regex.test("/users/1/")).toBe(true); | ||
expect(regex.test("/users/1/2")).toBe(false); | ||
}); | ||
it("with required param", () => { | ||
const regex = createRoutePathRegex("/users/:id"); | ||
expect(regex.test("/users")).toBe(false); | ||
expect(regex.test("/users/")).toBe(false); | ||
expect(regex.test("/users/1")).toBe(true); | ||
expect(regex.test("/users/1/")).toBe(true); | ||
expect(regex.test("/users/1/2")).toBe(false); | ||
}); | ||
it("with spread", () => { | ||
const regex = createRoutePathRegex("/users/*any"); | ||
expect(regex.test("/")).toBe(false); | ||
expect(regex.test("/users")).toBe(true); | ||
expect(regex.test("/users/")).toBe(true); | ||
expect(regex.test("/users/1")).toBe(true); | ||
expect(regex.test("/users/1/")).toBe(true); | ||
expect(regex.test("/users/1/2")).toBe(true); | ||
}); | ||
it("with all", () => { | ||
const regex = createRoutePathRegex("/*any"); | ||
expect(regex.test("/")).toBe(true); | ||
expect(regex.test("/users")).toBe(true); | ||
expect(regex.test("/users/")).toBe(true); | ||
expect(regex.test("/users/1")).toBe(true); | ||
expect(regex.test("/users/1/")).toBe(true); | ||
expect(regex.test("/users/1/2")).toBe(true); | ||
}); | ||
}); |