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 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
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
133 changes: 133 additions & 0 deletions src/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* 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 {
if (!query || Object.keys(query).length === 0) {
return input;
}

Check warning on line 81 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L80-L81

Added lines #L80 - L81 were not covered by tests

const searchIndex = input.indexOf("?");

if (searchIndex === -1) {
const normalizedQuery = Object.entries(query)
.filter(([, value]) => value !== undefined)
.flatMap(([key, value]) => {
if (Array.isArray(value)) {
return value.map((item) => [key, normalizeQueryValue(item)]);
}

Check warning on line 91 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L90-L91

Added lines #L90 - L91 were not covered by tests

return [[key, normalizeQueryValue(value)]];
});
const searchParams = new URLSearchParams(normalizedQuery);
const queryString = searchParams.toString();
return queryString ? `${input}?${queryString}` : input;
}

const searchParams = new URLSearchParams(input.slice(searchIndex + 1));
const base = input.slice(0, searchIndex);

Check warning on line 101 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L100-L101

Added lines #L100 - L101 were not covered by tests

for (const [key, value] of Object.entries(query)) {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
if (value === undefined) {
searchParams.delete(key);
} else if (Array.isArray(value)) {
for (const item of value) {
searchParams.append(key, normalizeQueryValue(item));
}
} else {
searchParams.set(key, normalizeQueryValue(value));
}
}

Check warning on line 113 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L103-L113

Added lines #L103 - L113 were not covered by tests

const queryString = searchParams.toString();

Check warning on line 115 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L115

Added line #L115 was not covered by tests
return queryString ? `${base}?${queryString}` : base;
}

function normalizeQueryValue(value: QueryValue): string {
if (value === null) {
return "";
}

Check warning on line 122 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L121-L122

Added lines #L121 - L122 were not covered by tests

if (typeof value === "number" || typeof value === "boolean") {
return String(value);
}

if (typeof value === "object") {
return JSON.stringify(value);
}

Check warning on line 130 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L128-L130

Added lines #L128 - L130 were not covered by tests

return String(value);
}

Check warning on line 133 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L132-L133

Added lines #L132 - L133 were not covered by tests