refactor: move mergePaths() to utils.ts (#2905)
This commit is contained in:
+2
-6
@@ -7,12 +7,7 @@ import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts";
|
|||||||
import * as colors from "@std/fmt/colors";
|
import * as colors from "@std/fmt/colors";
|
||||||
import { type MiddlewareFn, runMiddlewares } from "./middlewares/mod.ts";
|
import { type MiddlewareFn, runMiddlewares } from "./middlewares/mod.ts";
|
||||||
import { FreshReqContext } from "./context.ts";
|
import { FreshReqContext } from "./context.ts";
|
||||||
import {
|
import { type Method, type Router, UrlPatternRouter } from "./router.ts";
|
||||||
mergePaths,
|
|
||||||
type Method,
|
|
||||||
type Router,
|
|
||||||
UrlPatternRouter,
|
|
||||||
} from "./router.ts";
|
|
||||||
import {
|
import {
|
||||||
type FreshConfig,
|
type FreshConfig,
|
||||||
normalizeConfig,
|
normalizeConfig,
|
||||||
@@ -22,6 +17,7 @@ import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
|
|||||||
import type { ServerIslandRegistry } from "./context.ts";
|
import type { ServerIslandRegistry } from "./context.ts";
|
||||||
import { FinishSetup, ForgotBuild } from "./finish_setup.tsx";
|
import { FinishSetup, ForgotBuild } from "./finish_setup.tsx";
|
||||||
import { HttpError } from "./error.ts";
|
import { HttpError } from "./error.ts";
|
||||||
|
import { mergePaths } from "./utils.ts";
|
||||||
|
|
||||||
// TODO: Completed type clashes in older Deno versions
|
// TODO: Completed type clashes in older Deno versions
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
|
|||||||
@@ -28,17 +28,6 @@ export interface Router<T> {
|
|||||||
|
|
||||||
export const IS_PATTERN = /[*:{}+?()]/;
|
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> {
|
export class UrlPatternRouter<T> implements Router<T> {
|
||||||
readonly _routes: Route<T>[] = [];
|
readonly _routes: Route<T>[] = [];
|
||||||
readonly _middlewares: T[] = [];
|
readonly _middlewares: T[] = [];
|
||||||
|
|||||||
+1
-15
@@ -1,10 +1,5 @@
|
|||||||
import { expect } from "@std/expect";
|
import { expect } from "@std/expect";
|
||||||
import {
|
import { IS_PATTERN, pathToPattern, UrlPatternRouter } from "./router.ts";
|
||||||
IS_PATTERN,
|
|
||||||
mergePaths,
|
|
||||||
pathToPattern,
|
|
||||||
UrlPatternRouter,
|
|
||||||
} from "./router.ts";
|
|
||||||
|
|
||||||
Deno.test("IS_PATTERN", () => {
|
Deno.test("IS_PATTERN", () => {
|
||||||
expect(IS_PATTERN.test("/foo")).toEqual(false);
|
expect(IS_PATTERN.test("/foo")).toEqual(false);
|
||||||
@@ -176,12 +171,3 @@ Deno.test("pathToPattern", async (t) => {
|
|||||||
expect(() => pathToPattern("foo/[[name]]-bar")).toThrow();
|
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");
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -13,3 +13,22 @@ export function assertInDir(
|
|||||||
throw new Error(`Path "${tmp}" resolved outside of "${dir}"`);
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -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");
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user