Skip to content

fix(openapi-fetch): dynamic access to methods using wrapAsPathBasedClient #2256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-onions-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

fix dynamic access to methods using wrapAsPathBasedClient
32 changes: 16 additions & 16 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,30 +298,30 @@ class PathCallForwarder {
this.url = url;
}

GET(init) {
GET = (init) => {
return this.client.GET(this.url, init);
}
PUT(init) {
};
PUT = (init) => {
return this.client.PUT(this.url, init);
}
POST(init) {
};
POST = (init) => {
return this.client.POST(this.url, init);
}
DELETE(init) {
};
DELETE = (init) => {
return this.client.DELETE(this.url, init);
}
OPTIONS(init) {
};
OPTIONS = (init) => {
return this.client.OPTIONS(this.url, init);
}
HEAD(init) {
};
HEAD = (init) => {
return this.client.HEAD(this.url, init);
}
PATCH(init) {
};
PATCH = (init) => {
return this.client.PATCH(this.url, init);
}
TRACE(init) {
};
TRACE = (init) => {
return this.client.TRACE(this.url, init);
}
};
}

class PathClientProxyHandler {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MediaType } from "openapi-typescript-helpers";
import { createExpect, describe, expect, expectTypeOf, test } from "vitest";
import { describe, expect, expectTypeOf, test } from "vitest";
import { createPathBasedClient, type PathBasedClient } from "../../src/index.js";
import type { paths } from "./schemas/path-based-client.js";

Expand Down Expand Up @@ -78,5 +78,21 @@ describe("createPathBasedClient", () => {
expect(data.title).toBe("Blog post title");
}
});

test('properly binds "this" inside PathCallForwarder', async () => {
let method = "";
const client = createObservedPathBasedClient<paths>({}, async (req) => {
method = req.method;
return Response.json({});
});

const getMethodByPathAndMethod = (path: "/posts", method: "GET") => {
return client[path][method];
};

await getMethodByPathAndMethod("/posts", "GET")();

expect(method).toBe("GET");
});
});
});
Loading