@fern-api/ui-core-utils 0.139.3-2b772b85a → 0.139.3-73574b676
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__test__/sanitizeUrl.test.d.ts +2 -0
- package/dist/__test__/sanitizeUrl.test.d.ts.map +1 -0
- package/dist/__test__/sanitizeUrl.test.js +72 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/sanitizeUrl.d.ts +6 -0
- package/dist/sanitizeUrl.d.ts.map +1 -0
- package/dist/sanitizeUrl.js +37 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitizeUrl.test.d.ts","sourceRoot":"","sources":["../../src/__test__/sanitizeUrl.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { sanitizeUrl } from "../sanitizeUrl";
|
|
2
|
+
describe("sanitizeUrl", () => {
|
|
3
|
+
describe("undefined and empty inputs", () => {
|
|
4
|
+
it("returns undefined for undefined input", () => {
|
|
5
|
+
expect(sanitizeUrl(undefined)).toBeUndefined();
|
|
6
|
+
});
|
|
7
|
+
it("returns undefined for empty string", () => {
|
|
8
|
+
expect(sanitizeUrl("")).toBeUndefined();
|
|
9
|
+
});
|
|
10
|
+
it("returns undefined for whitespace string", () => {
|
|
11
|
+
expect(sanitizeUrl(" ")).toBeUndefined();
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
describe("protocol-relative URLs", () => {
|
|
15
|
+
it("handles protocol-relative URLs starting with //", () => {
|
|
16
|
+
expect(sanitizeUrl("//example.com")).toBe("https://example.com/");
|
|
17
|
+
expect(sanitizeUrl("//api.example.com/path")).toBe("https://api.example.com/path");
|
|
18
|
+
expect(sanitizeUrl("//localhost:8080")).toBe("https://localhost:8080/");
|
|
19
|
+
});
|
|
20
|
+
it("returns undefined for invalid protocol-relative URLs", () => {
|
|
21
|
+
expect(sanitizeUrl("//")).toBeUndefined();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
describe("URLs without protocol", () => {
|
|
25
|
+
it("adds https:// to URLs without protocol", () => {
|
|
26
|
+
expect(sanitizeUrl("example.com")).toBe("https://example.com/");
|
|
27
|
+
expect(sanitizeUrl("api.example.com/path")).toBe("https://api.example.com/path");
|
|
28
|
+
expect(sanitizeUrl("localhost:3000")).toBe("https://localhost:3000/");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe("complete URLs with protocol", () => {
|
|
32
|
+
it("validates and returns complete HTTPS URLs", () => {
|
|
33
|
+
expect(sanitizeUrl("https://example.com")).toBe("https://example.com/");
|
|
34
|
+
expect(sanitizeUrl("https://api.example.com/path")).toBe("https://api.example.com/path");
|
|
35
|
+
expect(sanitizeUrl("https://localhost:8080")).toBe("https://localhost:8080/");
|
|
36
|
+
expect(sanitizeUrl("https://example.com?param=value")).toBe("https://example.com/?param=value");
|
|
37
|
+
});
|
|
38
|
+
it("validates and returns complete HTTP URLs", () => {
|
|
39
|
+
expect(sanitizeUrl("http://example.com")).toBe("http://example.com/");
|
|
40
|
+
expect(sanitizeUrl("http://api.example.com/path")).toBe("http://api.example.com/path");
|
|
41
|
+
expect(sanitizeUrl("http://localhost:8080")).toBe("http://localhost:8080/");
|
|
42
|
+
});
|
|
43
|
+
it("returns undefined for invalid complete URLs", () => {
|
|
44
|
+
expect(sanitizeUrl("https://")).toBeUndefined();
|
|
45
|
+
expect(sanitizeUrl("http://")).toBeUndefined();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
describe("edge cases", () => {
|
|
49
|
+
it("handles URLs with query parameters and fragments", () => {
|
|
50
|
+
expect(sanitizeUrl("https://example.com/path?param=value#fragment")).toBe("https://example.com/path?param=value#fragment");
|
|
51
|
+
expect(sanitizeUrl("example.com/path?param=value#fragment")).toBe("https://example.com/path?param=value#fragment");
|
|
52
|
+
});
|
|
53
|
+
it("handles URLs with special characters", () => {
|
|
54
|
+
expect(sanitizeUrl("https://example.com/path with spaces")).toBe("https://example.com/path%20with%20spaces");
|
|
55
|
+
expect(sanitizeUrl("example.com/path with spaces")).toBe("https://example.com/path%20with%20spaces");
|
|
56
|
+
});
|
|
57
|
+
it("handles URLs with authentication", () => {
|
|
58
|
+
expect(sanitizeUrl("https://user:pass@example.com")).toBe("https://user:pass@example.com/");
|
|
59
|
+
expect(sanitizeUrl("user:pass@example.com")).toBe("https://user:pass@example.com/");
|
|
60
|
+
});
|
|
61
|
+
it("handles IP addresses", () => {
|
|
62
|
+
expect(sanitizeUrl("https://192.168.1.1")).toBe("https://192.168.1.1/");
|
|
63
|
+
expect(sanitizeUrl("192.168.1.1")).toBe("https://192.168.1.1/");
|
|
64
|
+
expect(sanitizeUrl("//192.168.1.1")).toBe("https://192.168.1.1/");
|
|
65
|
+
});
|
|
66
|
+
it("handles localhost", () => {
|
|
67
|
+
expect(sanitizeUrl("https://localhost")).toBe("https://localhost/");
|
|
68
|
+
expect(sanitizeUrl("localhost")).toBe("https://localhost/");
|
|
69
|
+
expect(sanitizeUrl("//localhost")).toBe("https://localhost/");
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -20,4 +20,5 @@ export type { Digit, Letter, LowercaseLetter, UppercaseLetter } from "./types";
|
|
|
20
20
|
export { unknownToString } from "./unknownToString";
|
|
21
21
|
export { visitDiscriminatedUnion } from "./visitDiscriminatedUnion";
|
|
22
22
|
export { withDefaultProtocol } from "./withDefaultProtocol";
|
|
23
|
+
export { sanitizeUrl } from "./sanitizeUrl";
|
|
23
24
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAA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S,CAAC;AAC/E,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -19,3 +19,4 @@ export { titleCase } from "./titleCase";
|
|
|
19
19
|
export { unknownToString } from "./unknownToString";
|
|
20
20
|
export { visitDiscriminatedUnion } from "./visitDiscriminatedUnion";
|
|
21
21
|
export { withDefaultProtocol } from "./withDefaultProtocol";
|
|
22
|
+
export { sanitizeUrl } from "./sanitizeUrl";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitizeUrl.d.ts","sourceRoot":"","sources":["../src/sanitizeUrl.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAgCvE"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* validates and sanitizes a base URL.
|
|
3
|
+
* returns null if the URL cannot be coerced into a valid URL.
|
|
4
|
+
*/
|
|
5
|
+
export function sanitizeUrl(url) {
|
|
6
|
+
if (!url) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
// handle protocol-relative URLs (starting with //)
|
|
10
|
+
if (url.startsWith("//")) {
|
|
11
|
+
try {
|
|
12
|
+
const parsedUrl = new URL(`https:${url}`);
|
|
13
|
+
return parsedUrl.toString();
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
// handle URLs without protocol
|
|
20
|
+
if (!url.startsWith("http://") && !url.startsWith("https://")) {
|
|
21
|
+
try {
|
|
22
|
+
const parsedUrl = new URL(`https://${url}`);
|
|
23
|
+
return parsedUrl.toString();
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// validate complete URLs
|
|
30
|
+
try {
|
|
31
|
+
const parsedUrl = new URL(url);
|
|
32
|
+
return parsedUrl.toString();
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|