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
15 changes: 14 additions & 1 deletion src/fetch/index.t-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ const JSONT = JSON as JSONT;
})();
}

{
type Spec = DefineApiEndpoints<{
"/users": {
post: { responses: { 200: { body: { prop: string } } } };
};
}>;
(async () => {
const f = fetch as FetchT<"", Spec>;
// TODO: getが定義されていない場合、methodを省略したらエラーになってほしいが今はならない
await f(`/users`, {});
})();
}

{
type Spec = DefineApiEndpoints<{
"/users": {
Expand All @@ -156,7 +169,7 @@ const JSONT = JSON as JSONT;
(async () => {
const f = fetch as FetchT<"", Spec>;
// headers and body can be omitted because they are optional
await f(`/users`, {});
await f(`/users`, { method: "post" });
})();
}

Expand Down
27 changes: 13 additions & 14 deletions src/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ApiP,
AnyApiResponses,
CaseInsensitiveMethod,
FilterNever,
MatchedPatterns,
MergeApiResponseBodies,
Method,
Expand All @@ -25,19 +24,19 @@ export type RequestInitT<
HeadersObj extends Record<string, string> | undefined,
> = Omit<RequestInit, "method" | "body" | "headers"> & {
method?: InputMethod;
} & FilterNever<{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body: Body extends Record<string, any>
? IsAllOptional<Body> extends true
? Body | TypedString<Body> | undefined
: TypedString<Body>
: never;
headers: HeadersObj extends Record<string, string>
? IsAllOptional<HeadersObj> extends true
? HeadersObj | Headers | undefined
: HeadersObj | Headers
: never;
}>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} & (Body extends Record<string, any>
? IsAllOptional<Body> extends true
? { body?: Body | TypedString<Body> }
: { body: TypedString<Body> }
: // eslint-disable-next-line @typescript-eslint/ban-types
{}) &
(HeadersObj extends Record<string, string>
? IsAllOptional<HeadersObj> extends true
? { headers?: HeadersObj | Headers }
: { headers: HeadersObj | Headers }
: // eslint-disable-next-line @typescript-eslint/ban-types
{});

/**
* FetchT is a type for window.fetch like function but more strict type information
Expand Down