Skip to content

Commit fbb8101

Browse files
committed
fix dynamic access to methods using wrapAsPathBasedClient
1 parent 1f6c0e8 commit fbb8101

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

.changeset/heavy-onions-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
fix dynamic access to methods using wrapAsPathBasedClient

packages/openapi-fetch/src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,28 +298,28 @@ class PathCallForwarder {
298298
this.url = url;
299299
}
300300

301-
GET(init) {
301+
GET = (init) => {
302302
return this.client.GET(this.url, init);
303303
}
304-
PUT(init) {
304+
PUT = (init) => {
305305
return this.client.PUT(this.url, init);
306306
}
307-
POST(init) {
307+
POST = (init) => {
308308
return this.client.POST(this.url, init);
309309
}
310-
DELETE(init) {
310+
DELETE = (init) => {
311311
return this.client.DELETE(this.url, init);
312312
}
313-
OPTIONS(init) {
313+
OPTIONS = (init) => {
314314
return this.client.OPTIONS(this.url, init);
315315
}
316-
HEAD(init) {
316+
HEAD = (init) => {
317317
return this.client.HEAD(this.url, init);
318318
}
319-
PATCH(init) {
319+
PATCH = (init) => {
320320
return this.client.PATCH(this.url, init);
321321
}
322-
TRACE(init) {
322+
TRACE = (init) => {
323323
return this.client.TRACE(this.url, init);
324324
}
325325
}

packages/openapi-fetch/test/path-based-client/path-based-client.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MediaType } from "openapi-typescript-helpers";
2-
import { createExpect, describe, expect, expectTypeOf, test } from "vitest";
2+
import { describe, expect, expectTypeOf, test } from "vitest";
33
import { createPathBasedClient, type PathBasedClient } from "../../src/index.js";
44
import type { paths } from "./schemas/path-based-client.js";
55

@@ -78,5 +78,23 @@ describe("createPathBasedClient", () => {
7878
expect(data.title).toBe("Blog post title");
7979
}
8080
});
81+
82+
test('properly binds "this" inside PathCallForwarder', async () => {
83+
let method = "";
84+
const client = createObservedPathBasedClient<paths>({}, async (req) => {
85+
method = req.method;
86+
return Response.json({});
87+
});
88+
89+
const getMethodByPathAndMethod = (path: "/posts", method: 'GET') => {
90+
return client[path][method];
91+
}
92+
93+
await getMethodByPathAndMethod("/posts", "GET")();
94+
95+
expect(method).toBe("GET");
96+
});
8197
});
8298
});
99+
100+

0 commit comments

Comments
 (0)