Skip to content

Commit 5935cd2

Browse files
authored
feat(openapi-fetch): add support for arbitrary method (#2063)
1 parent 9e4f61c commit 5935cd2

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

.changeset/hot-pants-sit.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
add support for arbitrary method

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

+11
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,24 @@ export type ClientMethod<
199199
...init: InitParam<Init>
200200
) => Promise<FetchResponse<Paths[Path][Method], Init, Media>>;
201201

202+
export type ClientRequestMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
203+
Method extends HttpMethod,
204+
Path extends PathsWithMethod<Paths, Method>,
205+
Init extends MaybeOptionalInit<Paths[Path], Method>,
206+
>(
207+
method: Method,
208+
url: Path,
209+
...init: InitParam<Init>
210+
) => Promise<FetchResponse<Paths[Path][Method], Init, Media>>;
211+
202212
export type ClientForPath<PathInfo extends Record<string | number, any>, Media extends MediaType> = {
203213
[Method in keyof PathInfo as Uppercase<string & Method>]: <Init extends MaybeOptionalInit<PathInfo, Method>>(
204214
...init: InitParam<Init>
205215
) => Promise<FetchResponse<PathInfo[Method], Init, Media>>;
206216
};
207217

208218
export interface Client<Paths extends {}, Media extends MediaType = MediaType> {
219+
request: ClientRequestMethod<Paths, Media>;
209220
/** Call a GET endpoint */
210221
GET: ClientMethod<Paths, "get", Media>;
211222
/** Call a PUT endpoint */

packages/openapi-fetch/src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ export default function createClient(clientOptions) {
227227
}
228228

229229
return {
230+
request(method, url, init) {
231+
return coreFetch(url, { ...init, method: method.toUpperCase() });
232+
},
230233
/** Call a GET endpoint */
231234
GET(url, init) {
232235
return coreFetch(url, { ...init, method: "GET" });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, expect, test } from "vitest";
2+
import { createObservedClient } from "../helpers.js";
3+
import type { paths as get_paths } from "./schemas/get.js";
4+
import type { paths as post_paths } from "./schemas/post.js";
5+
6+
describe("request", () => {
7+
test("sends correct method", async () => {
8+
let method = "";
9+
const client = createObservedClient<get_paths>({}, async (req) => {
10+
method = req.method;
11+
return Response.json({});
12+
});
13+
14+
await client.request("get", "/posts");
15+
expect(method).toBe("GET");
16+
});
17+
18+
test("sends correct method with params", async () => {
19+
let method = "";
20+
const client = createObservedClient<post_paths>({}, async (req) => {
21+
method = req.method;
22+
return Response.json({});
23+
});
24+
25+
await client.request("post", "/posts", {
26+
body: { title: "My Post", body: "Post body", publish_date: new Date("2024-06-06T12:00:00Z").getTime() },
27+
});
28+
29+
expect(method).toBe("POST");
30+
});
31+
});

0 commit comments

Comments
 (0)