From 5935cd25a05d3ec1f4c8bf222d65395a0b9ae5b4 Mon Sep 17 00:00:00 2001 From: Martin Paucot Date: Fri, 3 Jan 2025 19:22:01 +0100 Subject: [PATCH] feat(openapi-fetch): add support for arbitrary method (#2063) --- .changeset/hot-pants-sit.md | 5 +++ packages/openapi-fetch/src/index.d.ts | 11 +++++++ packages/openapi-fetch/src/index.js | 3 ++ .../test/http-methods/request.test.ts | 31 +++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 .changeset/hot-pants-sit.md create mode 100644 packages/openapi-fetch/test/http-methods/request.test.ts diff --git a/.changeset/hot-pants-sit.md b/.changeset/hot-pants-sit.md new file mode 100644 index 000000000..5765d616b --- /dev/null +++ b/.changeset/hot-pants-sit.md @@ -0,0 +1,5 @@ +--- +"openapi-fetch": patch +--- + +add support for arbitrary method diff --git a/packages/openapi-fetch/src/index.d.ts b/packages/openapi-fetch/src/index.d.ts index e3e91b805..06d01d400 100644 --- a/packages/openapi-fetch/src/index.d.ts +++ b/packages/openapi-fetch/src/index.d.ts @@ -199,6 +199,16 @@ export type ClientMethod< ...init: InitParam ) => Promise>; +export type ClientRequestMethod>, Media extends MediaType> = < + Method extends HttpMethod, + Path extends PathsWithMethod, + Init extends MaybeOptionalInit, +>( + method: Method, + url: Path, + ...init: InitParam +) => Promise>; + export type ClientForPath, Media extends MediaType> = { [Method in keyof PathInfo as Uppercase]: >( ...init: InitParam @@ -206,6 +216,7 @@ export type ClientForPath, Media e }; export interface Client { + request: ClientRequestMethod; /** Call a GET endpoint */ GET: ClientMethod; /** Call a PUT endpoint */ diff --git a/packages/openapi-fetch/src/index.js b/packages/openapi-fetch/src/index.js index 16bb50c2f..5e0c2fcd4 100644 --- a/packages/openapi-fetch/src/index.js +++ b/packages/openapi-fetch/src/index.js @@ -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" }); diff --git a/packages/openapi-fetch/test/http-methods/request.test.ts b/packages/openapi-fetch/test/http-methods/request.test.ts new file mode 100644 index 000000000..ec5625ee8 --- /dev/null +++ b/packages/openapi-fetch/test/http-methods/request.test.ts @@ -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({}, 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({}, 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("POST"); + }); +});