Skip to content
This repository was archived by the owner on Nov 12, 2024. It is now read-only.

Commit 161da67

Browse files
committed
Add RequestTypes helper
1 parent 0b05955 commit 161da67

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import type createClient from "openapi-fetch";
44
import type { FetchOptions, ParseAsResponse } from "openapi-fetch";
55
import type {
66
ErrorResponse,
7+
ErrorResponseJSON,
78
FilterKeys,
89
MediaType,
910
PathsWithMethod,
1011
ResponseObjectMap,
1112
SuccessResponse,
13+
SuccessResponseJSON,
1214
} from "openapi-typescript-helpers";
13-
import useSWR, { type SWRConfiguration } from "swr";
15+
import useSWR, { SWRResponse, type SWRConfiguration } from "swr";
1416
import useSWRInfinite, { SWRInfiniteKeyLoader } from "swr/infinite";
1517
import type { PartialDeep } from "type-fest";
1618

@@ -151,3 +153,26 @@ export function createHooks<Paths extends {}>(
151153
},
152154
};
153155
}
156+
157+
export type RequestTypes<
158+
Paths extends object,
159+
Path extends PathsWithMethod<Paths, "get">,
160+
Req = FilterKeys<Paths[Path], "get">,
161+
Data = SuccessResponseJSON<Req>,
162+
Error = ErrorResponseJSON<Req>,
163+
Params = Req extends { parameters: infer P } ? P : never,
164+
PathParams = Params extends { path: infer P } ? P : never,
165+
QueryParams = Params extends { query: infer Q } ? Q : never,
166+
HeaderParams = Params extends { header: infer H } ? H : never,
167+
CookieParams = Params extends { cookie: infer C } ? C : never,
168+
SWRConfig = SWRConfiguration<Data, Error>,
169+
> = {
170+
Data: Data;
171+
Error: Error;
172+
PathParams: PathParams;
173+
QueryParams: QueryParams;
174+
Headers: HeaderParams;
175+
Cookies: CookieParams;
176+
SWRConfig: SWRConfig;
177+
SWRResponse: SWRResponse<Data, Error, SWRConfig>;
178+
};

test/index.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expectTypeOf } from "expect-type";
22
import createClient from "openapi-fetch";
33
import { SuccessResponseJSON } from "openapi-typescript-helpers";
44
import type { paths } from "../generated/petstore";
5-
import { createHooks } from "../src/index";
5+
import { createHooks, RequestTypes } from "../src/index";
66
import { mutate } from "swr";
77

88
const petStoreApi = createClient<paths>({
@@ -71,3 +71,10 @@ usePetStoreInfinite("/store/order/{orderId}", (index, previous) => ({
7171
},
7272
},
7373
}));
74+
75+
type Req = RequestTypes<paths, "/store/order/{orderId}">;
76+
77+
expectTypeOf<Req["Data"]>().toEqualTypeOf<OrderSuccessResponse>();
78+
expectTypeOf<
79+
Parameters<NonNullable<Req["SWRConfig"]["onSuccess"]>>[0]
80+
>().toEqualTypeOf<OrderSuccessResponse>();

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"target": "ESNext",
44
"module": "ES2020",
55
"moduleResolution": "Node",
6+
"noUncheckedIndexedAccess": true,
67
"outDir": "dist",
78
"strict": true,
89
"skipLibCheck": true

0 commit comments

Comments
 (0)