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!: remove ufo dependency #440

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Readable } from "node:stream";
import destr from "destr";
import { withBase, withQuery } from "ufo";
import { withBase, withQuery } from "./path";
import { createFetchError } from "./error";
import {
isPayloadMethod,
Expand Down
119 changes: 119 additions & 0 deletions src/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* eslint-disable unicorn/prefer-at */
export type QueryValue =
| string
| number
| boolean
| QueryValue[]
| Record<string, any>
| null
| undefined;
export type QueryObject = Record<string, QueryValue | QueryValue[]>;

/**
* Removes the leading slash from the given path if it has one.
*/
export function withoutLeadingSlash(path?: string): string {
if (!path || path === "/") {
return "/";
}

Check warning on line 18 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L16-L18

Added lines #L16 - L18 were not covered by tests

return path[0] === "/" ? path.slice(1) : path;
}

Check warning on line 21 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L20-L21

Added lines #L20 - L21 were not covered by tests

/**
* Removes the trailing slash from the given path if it has one.
*/
export function withoutTrailingSlash(path?: string): string {
if (!path || path === "/") {
return "/";
}

Check warning on line 29 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L28-L29

Added lines #L28 - L29 were not covered by tests

return path[path.length - 1] === "/" ? path.slice(0, -1) : path;
}

/**
* Joins the given base URL and path, ensuring that there is only one slash between them.
*/
export function joinURL(base?: string, path?: string): string {
if (!base || base === "/") {
return path || "/";
}

Check warning on line 40 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L39-L40

Added lines #L39 - L40 were not covered by tests

if (!path || path === "/") {
return base || "/";
}

const baseHasTrailing = base[base.length - 1] === "/";
const pathHasLeading = path[0] === "/";
if (baseHasTrailing && pathHasLeading) {
return base + path.slice(1);

Check warning on line 49 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L49

Added line #L49 was not covered by tests
}

if (!baseHasTrailing && !pathHasLeading) {
return `${base}/${path}`;

Check warning on line 53 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L53

Added line #L53 was not covered by tests
}

return base + path;
}

/**
* Adds the base path to the input path, if it is not already present.
*/
export function withBase(input = "", base = ""): string {
if (!base || base === "/") {
return input;
}

Check warning on line 65 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L64-L65

Added lines #L64 - L65 were not covered by tests

const _base = withoutTrailingSlash(base);
if (input.startsWith(_base)) {
return input;
}

Check warning on line 70 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L69-L70

Added lines #L69 - L70 were not covered by tests

return joinURL(_base, input);
}

/**
* Returns the URL with the given query parameters. If a query parameter is undefined, it is omitted.
*/
export function withQuery(input: string, query: QueryObject): string {
let url: URL | undefined;
let searchParams: URLSearchParams;

if (input.includes("?")) {
url = new URL(input, "http://localhost");
searchParams = new URLSearchParams(url.search);

Check warning on line 84 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L83-L84

Added lines #L83 - L84 were not covered by tests
pi0 marked this conversation as resolved.
Show resolved Hide resolved
} else {
searchParams = new URLSearchParams();
}

for (const [key, value] of Object.entries(query)) {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
if (value === undefined) {
searchParams.delete(key);

Check warning on line 91 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L91

Added line #L91 was not covered by tests
} else if (typeof value === "number" || typeof value === "boolean") {
searchParams.set(key, String(value));
} else if (!value) {
searchParams.set(key, "");
} else if (Array.isArray(value)) {
for (const item of value) {
searchParams.append(key, String(item));
}
} else if (typeof value === "object") {
searchParams.set(key, JSON.stringify(value));
} else {
searchParams.set(key, String(value));
}

Check warning on line 104 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L95-L104

Added lines #L95 - L104 were not covered by tests
}

const queryString = searchParams.toString();

if (url) {
url.search = queryString;
let urlWithQuery = url.toString();
if (urlWithQuery.startsWith("http://localhost")) {
urlWithQuery = urlWithQuery.slice(16);
}
return urlWithQuery;
}

Check warning on line 116 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L110-L116

Added lines #L110 - L116 were not covered by tests

return queryString ? `${input}?${queryString}` : input;
}