Skip to content

Commit 1f7ad9d

Browse files
authored
Allow to select the response content type (#1597)
1 parent 92f4b96 commit 1f7ad9d

File tree

4 files changed

+79
-15
lines changed

4 files changed

+79
-15
lines changed

.changeset/few-tomatoes-try.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
Allow to select the response content type

packages/openapi-fetch/src/index.d.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,18 @@ export type RequestBodyOption<T> =
111111
export type FetchOptions<T> = RequestOptions<T> &
112112
Omit<RequestInit, "body" | "headers">;
113113

114-
export type FetchResponse<T, O> =
114+
export type FetchResponse<T, O, Media extends MediaType> =
115115
| {
116116
data: ParseAsResponse<
117-
FilterKeys<SuccessResponse<ResponseObjectMap<T>>, MediaType>,
117+
FilterKeys<SuccessResponse<ResponseObjectMap<T>>, Media>,
118118
O
119119
>;
120120
error?: never;
121121
response: Response;
122122
}
123123
| {
124124
data?: never;
125-
error: FilterKeys<ErrorResponse<ResponseObjectMap<T>>, MediaType>;
125+
error: FilterKeys<ErrorResponse<ResponseObjectMap<T>>, Media>;
126126
response: Response;
127127
};
128128

@@ -180,33 +180,37 @@ export type MaybeOptionalInit<P extends PathMethods, M extends keyof P> =
180180
export type ClientMethod<
181181
Paths extends Record<string, PathMethods>,
182182
M extends HttpMethod,
183+
Media extends MediaType,
183184
> = <
184185
P extends PathsWithMethod<Paths, M>,
185186
I extends MaybeOptionalInit<Paths[P], M>,
186187
>(
187188
url: P,
188189
...init: I
189-
) => Promise<FetchResponse<Paths[P][M], I[0]>>;
190+
) => Promise<FetchResponse<Paths[P][M], I[0], Media>>;
190191

191-
export default function createClient<Paths extends {}>(
192+
export default function createClient<
193+
Paths extends {},
194+
Media extends MediaType = MediaType,
195+
>(
192196
clientOptions?: ClientOptions,
193197
): {
194198
/** Call a GET endpoint */
195-
GET: ClientMethod<Paths, "get">;
199+
GET: ClientMethod<Paths, "get", Media>;
196200
/** Call a PUT endpoint */
197-
PUT: ClientMethod<Paths, "put">;
201+
PUT: ClientMethod<Paths, "put", Media>;
198202
/** Call a POST endpoint */
199-
POST: ClientMethod<Paths, "post">;
203+
POST: ClientMethod<Paths, "post", Media>;
200204
/** Call a DELETE endpoint */
201-
DELETE: ClientMethod<Paths, "delete">;
205+
DELETE: ClientMethod<Paths, "delete", Media>;
202206
/** Call a OPTIONS endpoint */
203-
OPTIONS: ClientMethod<Paths, "options">;
207+
OPTIONS: ClientMethod<Paths, "options", Media>;
204208
/** Call a HEAD endpoint */
205-
HEAD: ClientMethod<Paths, "head">;
209+
HEAD: ClientMethod<Paths, "head", Media>;
206210
/** Call a PATCH endpoint */
207-
PATCH: ClientMethod<Paths, "patch">;
211+
PATCH: ClientMethod<Paths, "patch", Media>;
208212
/** Call a TRACE endpoint */
209-
TRACE: ClientMethod<Paths, "trace">;
213+
TRACE: ClientMethod<Paths, "trace", Media>;
210214
/** Register middleware */
211215
use(...middleware: Middleware[]): void;
212216
/** Unregister middleware */

packages/openapi-fetch/test/fixtures/api.d.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Do not make direct changes to the file.
44
*/
55

6-
76
export interface paths {
87
"/comment": {
98
put: {
@@ -396,6 +395,13 @@ export interface paths {
396395
};
397396
};
398397
};
398+
"/multiple-response-content": {
399+
get: {
400+
responses: {
401+
200: components["responses"]["MultipleResponse"];
402+
};
403+
};
404+
};
399405
}
400406

401407
export type webhooks = Record<string, never>;
@@ -492,6 +498,20 @@ export interface components {
492498
"application/json": components["schemas"]["User"];
493499
};
494500
};
501+
MultipleResponse: {
502+
content: {
503+
"application/json": {
504+
id: string;
505+
email: string;
506+
name?: string;
507+
};
508+
"application/ld+json": {
509+
"@id": string;
510+
email: string;
511+
name?: string;
512+
};
513+
};
514+
};
495515
};
496516
parameters: never;
497517
requestBodies: {
@@ -557,7 +577,6 @@ export type $defs = Record<string, never>;
557577
export type external = Record<string, never>;
558578

559579
export interface operations {
560-
561580
getHeaderParams: {
562581
parameters: {
563582
header: {

packages/openapi-fetch/test/index.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,42 @@ describe("client", () => {
10491049

10501050
expect(data.byteLength).toBe(2);
10511051
});
1052+
1053+
it("use the selected content", async () => {
1054+
const client = createClient<paths, "application/ld+json">();
1055+
mockFetchOnce({
1056+
status: 200,
1057+
headers: { "Content-Type": "application/ld+json" },
1058+
body: JSON.stringify({
1059+
"@id": "some-resource-identifier",
1060+
1061+
name: null,
1062+
}),
1063+
});
1064+
const { data } = await client.GET("/multiple-response-content", {
1065+
headers: {
1066+
Accept: "application/ld+json",
1067+
},
1068+
});
1069+
1070+
data satisfies
1071+
| {
1072+
"@id": string;
1073+
email: string;
1074+
name?: string;
1075+
}
1076+
| undefined;
1077+
1078+
if (!data) {
1079+
throw new Error(`Missing response`);
1080+
}
1081+
1082+
expect(data).toEqual({
1083+
"@id": "some-resource-identifier",
1084+
1085+
name: null,
1086+
});
1087+
});
10521088
});
10531089
});
10541090

0 commit comments

Comments
 (0)