Skip to content

Commit

Permalink
feat(openapi-fetch): add support for arbitrary method
Browse files Browse the repository at this point in the history
  • Loading branch information
kerwanp committed Dec 21, 2024
1 parent 0f76cc4 commit f23ebc0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-pants-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": minor
---

add support for arbitrary method
14 changes: 14 additions & 0 deletions packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,27 @@ export type ClientMethod<
...init: InitParam<Init>
) => Promise<FetchResponse<Paths[Path][Method], Init, Media>>;

export type ClientRequestMethod<
Paths extends Record<string, Record<HttpMethod, {}>>,
Media extends MediaType,
> = <
Method extends HttpMethod,
Path extends PathsWithMethod<Paths, Method>,
Init extends MaybeOptionalInit<Paths[Path], Method>
>(
method: Method,
url: Path,
...init: InitParam<Init>
) => Promise<FetchResponse<Paths[Path][Method], Init, Media>>;

export type ClientForPath<PathInfo extends Record<string | number, any>, Media extends MediaType> = {
[Method in keyof PathInfo as Uppercase<string & Method>]: <Init extends MaybeOptionalInit<PathInfo, Method>>(
...init: InitParam<Init>
) => Promise<FetchResponse<PathInfo[Method], Init, Media>>;
};

export interface Client<Paths extends {}, Media extends MediaType = MediaType> {
request: ClientRequestMethod<Paths, Media>
/** Call a GET endpoint */
GET: ClientMethod<Paths, "get", Media>;
/** Call a PUT endpoint */
Expand Down
3 changes: 3 additions & 0 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ export default function createClient(clientOptions) {
}

return {
request(method, url, init) {
return coreFetch(url, { ...init, method: method.toUpperCase() })
},
/** Call a GET endpoint */
GET(url, init) {
return coreFetch(url, { ...init, method: "GET" });
Expand Down
31 changes: 31 additions & 0 deletions packages/openapi-fetch/test/http-methods/request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, test } from "vitest";
import { createObservedClient } from "../helpers.js";
import type { paths as get_paths } from "./schemas/get.js";
import type { paths as post_paths } from "./schemas/post.js";

describe("request", () => {
test("sends correct method", async () => {
let method = "";
const client = createObservedClient<get_paths>({}, async (req) => {
method = req.method;
return Response.json({});
});

await client.request('get', '/posts')
expect(method).toBe("GET");
});

test("sends correct method with params", async () => {
let method = "";
const client = createObservedClient<post_paths>({}, async (req) => {
method = req.method;
return Response.json({});
});

await client.request('post', "/posts", {
body: { title: "My Post", body: "Post body", publish_date: new Date("2024-06-06T12:00:00Z").getTime() },
});

expect(method).toBe("OPTIONS");
});
});

0 comments on commit f23ebc0

Please sign in to comment.