File tree Expand file tree Collapse file tree 7 files changed +730
-181
lines changed Expand file tree Collapse file tree 7 files changed +730
-181
lines changed Original file line number Diff line number Diff line change 44 AllFetchOptions ,
55 OpenapiPaths ,
66 FetchOptions ,
7+ FetchMethods ,
78} from "./types/fetch-options" ;
89import { queryBuilder } from "./query-builder" ;
910import { pathBuilder } from "./path-builder" ;
@@ -24,10 +25,12 @@ function fetchFactory<Paths>(options?: InitParameters) {
2425
2526 async function fetcher <
2627 Path extends keyof Paths ,
27- Method extends keyof Paths [ Path ] ,
28+ Method extends FetchMethods < Paths [ Path ] > ,
2829 Operation extends Paths [ Path ] [ Method ] ,
2930 > ( input : Path , init : { method : Method } & FetchOptions < Operation > ) {
30- const options = init as unknown as { method : Method } & AllFetchOptions ;
31+ const options = init as unknown as {
32+ method : Method ;
33+ } & AllFetchOptions ;
3134
3235 const qBuilder = queryBuilder ( {
3336 style : serialization ?. query ?. style ?? "form" ,
Original file line number Diff line number Diff line change @@ -35,6 +35,13 @@ export type OpenapiPaths<Paths> = {
3535 } ;
3636} ;
3737
38+ /**
39+ * Extracts the available HTTP methods
40+ */
41+ export type FetchMethods < T > = {
42+ [ K in keyof T & HttpMethod ] : T [ K ] extends never | undefined ? never : K ;
43+ } [ keyof T & HttpMethod ] ;
44+
3845/**
3946 * Extract available paths from openapi-typescript paths type.
4047 */
Original file line number Diff line number Diff line change 44export type OperationRequestBody <
55 Operation ,
66 ContentType extends string = string ,
7- > = Operation extends {
8- requestBody ?: { content : { [ key in ContentType ] : infer RequestBody } } ;
9- }
10- ? RequestBody
11- : never ;
7+ > = [ Operation ] extends [ { requestBody ?: never } ]
8+ ? never
9+ : Operation extends {
10+ requestBody ?: { content : { [ key in ContentType ] : infer RequestBody } } ;
11+ }
12+ ? RequestBody
13+ : never ;
Original file line number Diff line number Diff line change @@ -28,12 +28,10 @@ export type OperationHttpHeaders<Operation> = OperationParameters<
2828type OperationParameters <
2929 Operation ,
3030 ParameterType extends "path" | "query" | "header" ,
31- > = Operation extends {
32- parameters ?: infer AllParameters ;
33- }
34- ? AllParameters extends {
35- [ key in ParameterType ] ?: infer PathParameters ;
36- }
37- ? PathParameters
31+ > = Operation extends { parameters ?: infer AllParameters }
32+ ? AllParameters extends { [ key in ParameterType ] ?: infer Param }
33+ ? [ Param ] extends [ never ] | [ undefined ]
34+ ? never
35+ : Param
3836 : never
3937 : never ;
Original file line number Diff line number Diff line change @@ -29,4 +29,5 @@ export type HttpMethod =
2929 | "patch"
3030 | "delete"
3131 | "head"
32- | "options" ;
32+ | "options"
33+ | "trace" ;
Original file line number Diff line number Diff line change 1- import createFetch from "../../src/openapi-typescript" ;
1+ import createFetch , { FetchMethods } from "../../src/openapi-typescript" ;
22import { paths , components } from "./test-data/petstore-openapi3" ;
33import { IsEqual } from "../test-tools" ;
44
You can’t perform that action at this time.
0 commit comments