Skip to content

Commit 30c6da8

Browse files
authored
fix(openapi-fetch): dynamic access to methods using wrapAsPathBasedClient (#2256)
* fix dynamic access to methods using wrapAsPathBasedClient * fix formatting issues
1 parent 4840571 commit 30c6da8

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

.changeset/heavy-onions-bake.md

+5
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

+16-16
Original file line numberDiff line numberDiff line change
@@ -298,30 +298,30 @@ class PathCallForwarder {
298298
this.url = url;
299299
}
300300

301-
GET(init) {
301+
GET = (init) => {
302302
return this.client.GET(this.url, init);
303-
}
304-
PUT(init) {
303+
};
304+
PUT = (init) => {
305305
return this.client.PUT(this.url, init);
306-
}
307-
POST(init) {
306+
};
307+
POST = (init) => {
308308
return this.client.POST(this.url, init);
309-
}
310-
DELETE(init) {
309+
};
310+
DELETE = (init) => {
311311
return this.client.DELETE(this.url, init);
312-
}
313-
OPTIONS(init) {
312+
};
313+
OPTIONS = (init) => {
314314
return this.client.OPTIONS(this.url, init);
315-
}
316-
HEAD(init) {
315+
};
316+
HEAD = (init) => {
317317
return this.client.HEAD(this.url, init);
318-
}
319-
PATCH(init) {
318+
};
319+
PATCH = (init) => {
320320
return this.client.PATCH(this.url, init);
321-
}
322-
TRACE(init) {
321+
};
322+
TRACE = (init) => {
323323
return this.client.TRACE(this.url, init);
324-
}
324+
};
325325
}
326326

327327
class PathClientProxyHandler {

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

+17-1
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,21 @@ 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
});

0 commit comments

Comments
 (0)