Skip to content

Commit f42b3ca

Browse files
authored
chore(util-endpoints): add normalizedPath which ends with a / (#3941)
1 parent f8bc8b3 commit f42b3ca

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

packages/types/src/endpoint.ts

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export interface EndpointURL {
3636
*/
3737
path: string;
3838

39+
/**
40+
* The parsed path segment of the URL.
41+
* This value is guranteed to start and end with a "/".
42+
*/
43+
normalizedPath: string;
44+
3945
/**
4046
* A boolean indicating whether the authority is an IP address.
4147
*/

packages/util-endpoints/src/lib/parseURL.spec.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ import { parseURL } from "./parseURL";
22

33
describe(parseURL.name, () => {
44
it.each([
5-
["https://example.com", { scheme: "https", authority: "example.com", path: "/", isIp: false }],
6-
["http://example.com:80/foo/bar", { scheme: "http", authority: "example.com:80", path: "/foo/bar", isIp: false }],
7-
["https://127.0.0.1", { scheme: "https", authority: "127.0.0.1", path: "/", isIp: true }],
8-
["https://127.0.0.1:8443", { scheme: "https", authority: "127.0.0.1:8443", path: "/", isIp: true }],
9-
["https://[fe80::1]", { scheme: "https", authority: "[fe80::1]", path: "/", isIp: true }],
10-
["https://[fe80::1]:8443", { scheme: "https", authority: "[fe80::1]:8443", path: "/", isIp: true }],
5+
["https://example.com", { scheme: "https", authority: "example.com", path: "/", normalizedPath: "/", isIp: false }],
6+
[
7+
"http://example.com:80/foo/bar",
8+
{ scheme: "http", authority: "example.com:80", path: "/foo/bar", normalizedPath: "/foo/bar/", isIp: false },
9+
],
10+
["https://127.0.0.1", { scheme: "https", authority: "127.0.0.1", path: "/", normalizedPath: "/", isIp: true }],
11+
[
12+
"https://127.0.0.1:8443",
13+
{ scheme: "https", authority: "127.0.0.1:8443", path: "/", normalizedPath: "/", isIp: true },
14+
],
15+
["https://[fe80::1]", { scheme: "https", authority: "[fe80::1]", path: "/", normalizedPath: "/", isIp: true }],
16+
[
17+
"https://[fe80::1]:8443",
18+
{ scheme: "https", authority: "[fe80::1]:8443", path: "/", normalizedPath: "/", isIp: true },
19+
],
1120
])("test '%s'", (input, output) => {
1221
expect(parseURL(input)).toEqual(output);
1322
});

packages/util-endpoints/src/lib/parseURL.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export const parseURL = (value: string): EndpointURL | null => {
4343
scheme,
4444
authority,
4545
path: pathname,
46+
normalizedPath: pathname.endsWith("/") ? pathname : `${pathname}/`,
4647
isIp,
4748
};
4849
};

0 commit comments

Comments
 (0)