Skip to content

Commit 7992aef

Browse files
committed
feat!: support latest openapi-typescript output
1 parent 9cd214d commit 7992aef

File tree

7 files changed

+730
-181
lines changed

7 files changed

+730
-181
lines changed

src/openapi-typescript/fetch-factory.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
AllFetchOptions,
55
OpenapiPaths,
66
FetchOptions,
7+
FetchMethods,
78
} from "./types/fetch-options";
89
import { queryBuilder } from "./query-builder";
910
import { 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",

src/openapi-typescript/types/fetch-options.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*/

src/openapi-typescript/types/request-body.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
export 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;

src/openapi-typescript/types/request-parameters.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ export type OperationHttpHeaders<Operation> = OperationParameters<
2828
type 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;

src/types/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ export type HttpMethod =
2929
| "patch"
3030
| "delete"
3131
| "head"
32-
| "options";
32+
| "options"
33+
| "trace";

test/openapi-typescript/fetch-factory.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import createFetch from "../../src/openapi-typescript";
1+
import createFetch, { FetchMethods } from "../../src/openapi-typescript";
22
import { paths, components } from "./test-data/petstore-openapi3";
33
import { IsEqual } from "../test-tools";
44

0 commit comments

Comments
 (0)