Skip to content
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
8 changes: 2 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts";
import * as colors from "@std/fmt/colors";
import { type MiddlewareFn, runMiddlewares } from "./middlewares/mod.ts";
import { FreshReqContext } from "./context.ts";
import {
mergePaths,
type Method,
type Router,
UrlPatternRouter,
} from "./router.ts";
import { type Method, type Router, UrlPatternRouter } from "./router.ts";
import {
type FreshConfig,
normalizeConfig,
Expand All @@ -22,6 +17,7 @@ import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
import type { ServerIslandRegistry } from "./context.ts";
import { FinishSetup, ForgotBuild } from "./finish_setup.tsx";
import { HttpError } from "./error.ts";
import { mergePaths } from "./utils.ts";

// TODO: Completed type clashes in older Deno versions
// deno-lint-ignore no-explicit-any
Expand Down
11 changes: 0 additions & 11 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ export interface Router<T> {

export const IS_PATTERN = /[*:{}+?()]/;

export function mergePaths(a: string, b: string) {
if (a === "" || a === "/" || a === "/*") return b;
if (b === "/") return a;
if (a.endsWith("/")) {
return a.slice(0, -1) + b;
} else if (!b.startsWith("/")) {
return a + "/" + b;
}
return a + b;
}

export class UrlPatternRouter<T> implements Router<T> {
readonly _routes: Route<T>[] = [];
readonly _middlewares: T[] = [];
Expand Down
16 changes: 1 addition & 15 deletions src/router_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { expect } from "@std/expect";
import {
IS_PATTERN,
mergePaths,
pathToPattern,
UrlPatternRouter,
} from "./router.ts";
import { IS_PATTERN, pathToPattern, UrlPatternRouter } from "./router.ts";

Deno.test("IS_PATTERN", () => {
expect(IS_PATTERN.test("/foo")).toEqual(false);
Expand Down Expand Up @@ -176,12 +171,3 @@ Deno.test("pathToPattern", async (t) => {
expect(() => pathToPattern("foo/[[name]]-bar")).toThrow();
});
});

Deno.test("mergePaths", () => {
expect(mergePaths("", "")).toEqual("");
expect(mergePaths("/", "/foo")).toEqual("/foo");
expect(mergePaths("/*", "/foo")).toEqual("/foo");
expect(mergePaths("/foo/bar", "/baz")).toEqual("/foo/bar/baz");
expect(mergePaths("/foo/bar/", "/baz")).toEqual("/foo/bar/baz");
expect(mergePaths("/foo/bar", "baz")).toEqual("/foo/bar/baz");
});
19 changes: 19 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,22 @@ export function assertInDir(
throw new Error(`Path "${tmp}" resolved outside of "${dir}"`);
}
}

/**
* Joins two path segments into a single normalized path.
* @example
* ```ts
* mergePaths("/api", "users"); // "/api/users"
* mergePaths("/api/", "/users"); // "/api/users"
* mergePaths("/", "/users"); // "/users"
* mergePaths("", "/users"); // "/users"
* mergePaths("/api", "/users"); // "/api/users"
* ```
*/
export function mergePaths(a: string, b: string) {
if (a === "" || a === "/" || a === "/*") return b;
if (b === "/") return a;
if (a.endsWith("/")) return a.slice(0, -1) + b;
if (!b.startsWith("/")) return a + "/" + b;
return a + b;
}
11 changes: 11 additions & 0 deletions src/utils_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect } from "@std/expect";
import { mergePaths } from "./utils.ts";

Deno.test("mergePaths", () => {
expect(mergePaths("", "")).toEqual("");
expect(mergePaths("/", "/foo")).toEqual("/foo");
expect(mergePaths("/*", "/foo")).toEqual("/foo");
expect(mergePaths("/foo/bar", "/baz")).toEqual("/foo/bar/baz");
expect(mergePaths("/foo/bar/", "/baz")).toEqual("/foo/bar/baz");
expect(mergePaths("/foo/bar", "baz")).toEqual("/foo/bar/baz");
});
Loading