Skip to content

Commit

Permalink
fix possible error with '+' character in the route pathname
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiaz9 committed Jul 24, 2024
1 parent 70d1e5e commit 6cce900
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.DS_Store

node_modules
dist
dist

debug*.ts
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "route-fn",
"version": "1.3.2",
"version": "1.3.3",
"description": "A simple utility function for typesafe urls.",
"license": "MIT",
"type": "module",
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
RouteParams,
SearchParams,
} from "./types"
import { safeUrl } from "./safe-url"

const fakeOrigin = "http://localhost"

Expand Down Expand Up @@ -62,9 +63,9 @@ export function createRouteFn<const Routes extends string[]>(routes: Routes) {

fn.params = function (url: string): DynamicParamsDictionary<Routes> {
const urlWithOrigin = new URL(url, fakeOrigin).href
const input = urlWithOrigin.split("?")[0]
const input = safeUrl(urlWithOrigin.split("?")[0])

const patterns = sortedRoutes.map((route) => new URLPattern({ pathname: route }))
const patterns = sortedRoutes.map((route) => new URLPattern({ pathname: safeUrl(route) }))

for (const pattern of patterns) {
const patternResult = pattern.exec(input)
Expand All @@ -83,9 +84,9 @@ export function createRouteFn<const Routes extends string[]>(routes: Routes) {
const matchingRoutes = typeof routeIds === "string" ? [routeIds] : routeIds
const bestMatch = sortedRoutes.find((route) => {
const urlWithOrigin = new URL(url, fakeOrigin).href
const input = urlWithOrigin.split("?")[0]
const input = safeUrl(urlWithOrigin.split("?")[0])

const pattern = new URLPattern({ pathname: route })
const pattern = new URLPattern({ pathname: safeUrl(route) })

if (pattern.test(input)) {
return true
Expand Down
3 changes: 3 additions & 0 deletions src/safe-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function safeUrl(url: string) {
return url.replace(/[+]/g, encodeURIComponent("+"))
}
16 changes: 16 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ describe("route-fn.params", () => {
project: "route-fn",
})
})

it("should handle special characters", () => {
const route = createRouteFn(["/:org/:id/+"])

expect(route.params("/apple/iphone/+")).toEqual({
org: "apple",
id: "iphone",
})
})
})

describe("route-fn.test", () => {
Expand Down Expand Up @@ -109,4 +118,11 @@ describe("route-fn.test", () => {
expect(route.test("/apple/settings/billing", "/:org/:project/posts")).toEqual(false)
expect(route.test("/apple/settings/billing", "/:org/:project/settings")).toEqual(false)
})

it("should handle special characters", () => {
const route = createRouteFn(["/:org/+"])

expect(route.test("/apple/new", "/:org/+")).toEqual(false)
expect(route.test("/apple/+", "/:org/+")).toEqual(true)
})
})

0 comments on commit 6cce900

Please sign in to comment.