Skip to content
Merged
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
10 changes: 7 additions & 3 deletions src/common/url.t-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ type ToUrlParamPatternTestCases = [
Expect<Equal<ToUrlParamPattern<"/:a/b">, `/${string}/b`>>,
Expect<Equal<ToUrlParamPattern<"/:a/:b">, `/${string}/${string}`>>,
Expect<
// @ts-expect-error URL is not supported
Equal<ToUrlParamPattern<"https://example.com">, `"https://example.com}`>
Equal<ToUrlParamPattern<"https://example.com">, "https://example.com">
>,
Expect<
Equal<
ToUrlParamPattern<"https://example.com/:a">,
`https://example.com/${string}`
>
>,
];

Expand All @@ -45,7 +50,6 @@ type ToUrlPatternTestCases = [
`/users/${string}?key=value`
>
>,
// @ts-expect-error URL is not supported
Expect<Equal<ToUrlPattern<"https://example.com">, "https://example.com">>,
];

Expand Down
12 changes: 7 additions & 5 deletions src/common/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ export type UrlPrefixPattern = `${UrlPrefix}${string}`;
* // => "/users/${string}"
* ```
*/
export type ToUrlParamPattern<T> = T extends `${infer O}:${infer R}`
? R extends `${string}/${infer L}`
? `${O}${string}/${ToUrlParamPattern<L>}`
: `${O}${string}`
: T;
export type ToUrlParamPattern<T> = T extends `${infer O}://${infer R}`
? `${O}://${ToUrlParamPattern<R>}`
: T extends `${infer O}:${infer R}`
? R extends `${string}/${infer L}`
? `${O}${string}/${ToUrlParamPattern<L>}`
: `${O}${string}`
: T;

/**
* Convert URL definition with query to acceptable URL pattern
Expand Down
26 changes: 26 additions & 0 deletions src/fetch/index.t-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ import JSONT from "../json";
await f("/users");
}

{
await f("/users?a=1", {
headers: { "Content-Type": "application/json" },
});
}

{
const res = await f("/users", {
method: "post",
Expand Down Expand Up @@ -103,3 +109,23 @@ import JSONT from "../json";
}
})();
}

{
type Spec = DefineApiEndpoints<{
"/users": {
get: {
headers: { Cookie: `a=${string}` };
resBody: {
200: { prop: string };
};
};
};
}>;
(async () => {
const basePath = "https://example.com/api";
const f = fetch as FetchT<typeof basePath, Spec>;
await f(`${basePath}/users`, {
headers: { Cookie: "a=b" },
});
})();
}
16 changes: 7 additions & 9 deletions src/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
MergeApiResponses,
Method,
NormalizePath,
ParseURL,
Replace,
} from "../common";
import {
MatchedPatterns,
UrlPrefixPattern,
ParseURL,
ToUrlParamPattern,
} from "../common";
import { TypedString } from "../json";
Expand All @@ -32,15 +32,13 @@ export interface RequestInitT<
/**
* FetchT is a type for window.fetch like function but more strict type information
*/
type FetchT<Origin extends UrlPrefixPattern, E extends ApiEndpoints> = <
type FetchT<UrlPrefix extends UrlPrefixPattern, E extends ApiEndpoints> = <
Input extends
| `${ToUrlParamPattern<Origin>}${ToUrlParamPattern<keyof E & string>}`
| `${ToUrlParamPattern<Origin>}${ToUrlParamPattern<keyof E & string>}?${string}`,
InputPath extends Replace<
NormalizePath<ParseURL<Input>["path"]>,
ToUrlParamPattern<Origin>,
""
>,
| `${ToUrlParamPattern<UrlPrefix>}${ToUrlParamPattern<keyof E & string>}`
| `${ToUrlParamPattern<UrlPrefix>}${ToUrlParamPattern<keyof E & string>}?${string}`,
InputPath extends ParseURL<
Replace<NormalizePath<Input>, NormalizePath<UrlPrefix>, "">
>["path"],
CandidatePaths extends MatchedPatterns<InputPath, keyof E & string>,
InputMethod extends CaseInsensitiveMethod = "get",
M extends Method = Lowercase<InputMethod>,
Expand Down