|
1 | 1 | import { Equal, Expect } from "./type-test"; |
2 | | -import { ParseOrigin, ParseURL } from "./url"; |
| 2 | +import { |
| 3 | + MatchedPatterns, |
| 4 | + ParseHostAndPort, |
| 5 | + ParseOriginAndPath, |
| 6 | + ParseURL, |
| 7 | + ParseUrlParams, |
| 8 | + ToUrlParamPattern, |
| 9 | + ToUrlPattern, |
| 10 | +} from "./url"; |
3 | 11 |
|
4 | 12 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
5 | | -type cases = [ |
6 | | - Expect<Equal<ParseOrigin<undefined>, never>>, |
| 13 | +type ParseUrlParamsTestCases = [ |
| 14 | + // @ts-expect-error undefined is not a string |
| 15 | + Expect<Equal<ParseUrlParams<undefined>, never>>, |
| 16 | + Expect<Equal<ParseUrlParams<"">, never>>, |
| 17 | + Expect<Equal<ParseUrlParams<"">, never>>, |
| 18 | + Expect<Equal<ParseUrlParams<":a">, "a">>, |
| 19 | + Expect<Equal<ParseUrlParams<"/:a">, "a">>, |
| 20 | + Expect<Equal<ParseUrlParams<"/:a/:b">, "a" | "b">>, |
| 21 | + Expect<Equal<ParseUrlParams<"/a/:b">, "b">>, |
| 22 | +]; |
| 23 | + |
| 24 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 25 | +type ToUrlParamPatternTestCases = [ |
| 26 | + Expect<Equal<ToUrlParamPattern<"">, "">>, |
| 27 | + Expect<Equal<ToUrlParamPattern<"/">, "/">>, |
| 28 | + Expect<Equal<ToUrlParamPattern<":a">, string>>, |
| 29 | + Expect<Equal<ToUrlParamPattern<"/:a/b">, `/${string}/b`>>, |
| 30 | + Expect<Equal<ToUrlParamPattern<"/:a/:b">, `/${string}/${string}`>>, |
| 31 | + Expect< |
| 32 | + // @ts-expect-error URL is not supported |
| 33 | + Equal<ToUrlParamPattern<"https://example.com">, `"https://example.com}`> |
| 34 | + >, |
| 35 | +]; |
| 36 | + |
| 37 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 38 | +type ToUrlPatternTestCases = [ |
| 39 | + Expect<Equal<ToUrlPattern<"">, "">>, |
| 40 | + Expect<Equal<ToUrlPattern<"/">, "/">>, |
| 41 | + Expect<Equal<ToUrlPattern<"/users/:userId">, `/users/${string}`>>, |
7 | 42 | Expect< |
8 | 43 | Equal< |
9 | | - ParseOrigin<"">, |
10 | | - { schema: undefined; host: undefined; port: undefined; path: "" } |
| 44 | + ToUrlPattern<"/users/:userId?key=value">, |
| 45 | + `/users/${string}?key=value` |
11 | 46 | > |
12 | 47 | >, |
| 48 | + // @ts-expect-error URL is not supported |
| 49 | + Expect<Equal<ToUrlPattern<"https://example.com">, "https://example.com">>, |
| 50 | +]; |
| 51 | + |
| 52 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 53 | +type MatchedPatternsTestCases = [ |
| 54 | + Expect<Equal<MatchedPatterns<"", "">, "">>, |
| 55 | + Expect<Equal<MatchedPatterns<"/1", "/:userId">, "/:userId">>, |
| 56 | + Expect< |
| 57 | + Equal<MatchedPatterns<"/1", "/:userId" | "/:orgId">, "/:userId" | "/:orgId"> |
| 58 | + >, |
| 59 | + Expect< |
| 60 | + Equal< |
| 61 | + MatchedPatterns<"/users/1", "/users/:userId" | "/:userId">, |
| 62 | + "/users/:userId" | "/:userId" |
| 63 | + > |
| 64 | + >, |
| 65 | + Expect< |
| 66 | + Equal< |
| 67 | + MatchedPatterns<"/users/1", "/users/:userId" | "/org/:orgId">, |
| 68 | + "/users/:userId" |
| 69 | + > |
| 70 | + >, |
| 71 | +]; |
| 72 | + |
| 73 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 74 | +type ParseHostAndPortTestCases = [ |
13 | 75 | Expect< |
14 | 76 | Equal< |
15 | | - ParseOrigin<"https://example.com">, |
16 | | - { host: "example.com"; port: undefined } & { schema: "https"; path: "" } |
| 77 | + ParseHostAndPort<"example.com">, |
| 78 | + { host: "example.com"; port: undefined } |
17 | 79 | > |
18 | 80 | >, |
19 | 81 | Expect< |
20 | 82 | Equal< |
21 | | - ParseOrigin<"https://example.com/">, |
| 83 | + ParseHostAndPort<"example.com:8080">, |
| 84 | + { host: "example.com"; port: "8080" } |
| 85 | + > |
| 86 | + >, |
| 87 | + // If invalid port is specified, it should return never |
| 88 | + Expect<Equal<ParseHostAndPort<"example.com:xxx">, never>>, |
| 89 | +]; |
| 90 | + |
| 91 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 92 | +type ParseOriginAndPathCases = [ |
| 93 | + Expect<Equal<ParseOriginAndPath<undefined>, never>>, |
| 94 | + Expect< |
| 95 | + Equal< |
| 96 | + ParseOriginAndPath<"">, |
| 97 | + { schema: undefined; host: undefined; port: undefined; path: "" } |
| 98 | + > |
| 99 | + >, |
| 100 | + Expect< |
| 101 | + Equal< |
| 102 | + ParseOriginAndPath<"https://example.com/">, |
22 | 103 | { host: "example.com"; port: undefined } & { schema: "https"; path: "/" } |
23 | 104 | > |
24 | 105 | >, |
25 | 106 | Expect< |
26 | 107 | Equal< |
27 | | - ParseOrigin<"https://example.com/user">, |
| 108 | + ParseOriginAndPath<"https://example.com/user">, |
28 | 109 | { host: "example.com"; port: undefined } & { |
29 | 110 | schema: "https"; |
30 | 111 | path: "/user"; |
31 | 112 | } |
32 | 113 | > |
33 | 114 | >, |
| 115 | + |
| 116 | + Expect< |
| 117 | + Equal< |
| 118 | + ParseOriginAndPath<"https://example.com/users/:userId">, |
| 119 | + { host: "example.com"; port: undefined } & { |
| 120 | + schema: "https"; |
| 121 | + path: "/users/:userId"; |
| 122 | + } |
| 123 | + > |
| 124 | + >, |
| 125 | + |
34 | 126 | Expect< |
35 | 127 | Equal< |
36 | | - ParseOrigin<"https://example.com:8080/user">, |
| 128 | + ParseOriginAndPath<"https://example.com:8080/user">, |
37 | 129 | { host: "example.com"; port: "8080" } & { schema: "https"; path: "/user" } |
38 | 130 | > |
39 | 131 | >, |
| 132 | + |
40 | 133 | Expect< |
41 | 134 | Equal< |
42 | | - ParseOrigin<"/user">, |
| 135 | + ParseOriginAndPath<"/user">, |
43 | 136 | { schema: undefined; host: undefined; port: undefined; path: "/user" } |
44 | 137 | > |
45 | 138 | >, |
| 139 | +]; |
46 | 140 |
|
47 | | - Expect<Equal<ParseURL<"/user">["path"], "/user">>, |
| 141 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 142 | +type ParseURLTestCases = [ |
| 143 | + Expect<Equal<ParseURL<"/user?a=b">["path"], "/user">>, |
48 | 144 | Expect<Equal<ParseURL<"https://example.com/user">["path"], "/user">>, |
49 | 145 | ]; |
0 commit comments