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
5 changes: 4 additions & 1 deletion examples/express/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ const main = async () => {
unreachable(res);
}
}

{
// case-insensitive method example
await fetchT(`${origin}/users`, { method: "GET" });
}
{
// query parameter example
// TODO: Add common information for query parameter
Expand Down
1 change: 1 addition & 0 deletions src/common/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const Method = [
"head",
] as const;
export type Method = (typeof Method)[number];
export type CaseInsensitiveMethod = Method | Uppercase<Method>;

export type ApiEndpoint<Path extends string> = Partial<
Record<Method, ApiSpec<ParseUrlParams<Path>>>
Expand Down
10 changes: 6 additions & 4 deletions src/fetch/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ApiBodySchema,
ApiEndpoints,
CaseInsensitiveMethod,
MergeApiResponses,
Method,
NormalizePath,
Expand All @@ -15,11 +16,11 @@ import {
import { TypedString } from "../json";

export interface RequestInitT<
M extends Method,
InputMethod extends CaseInsensitiveMethod,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Body extends Record<string, any> | undefined,
> extends RequestInit {
method?: M;
method?: InputMethod;
body?: TypedString<Body>;
}

Expand All @@ -36,10 +37,11 @@ type FetchT<Origin extends UrlPrefixPattern, E extends ApiEndpoints> = <
""
>,
CandidatePaths extends MatchedPatterns<InputPath, keyof E & string>,
M extends Method = "get",
InputMethod extends CaseInsensitiveMethod = "get",
M extends Method = Lowercase<InputMethod>,
>(
input: Input,
init?: RequestInitT<M, ApiBodySchema<E, CandidatePaths, M>>,
init?: RequestInitT<InputMethod, ApiBodySchema<E, CandidatePaths, M>>,
// FIXME: NonNullable
) => Promise<MergeApiResponses<NonNullable<E[CandidatePaths][M]>["res"]>>;

Expand Down