Skip to content

Commit 4f4253a

Browse files
authored
add custom properties to request object (#1653)
* add custom properties to request object * fix typing for accept custom properties * added unit test * add changeset * fix unused types
1 parent 37885c2 commit 4f4253a

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

.changeset/red-pants-cover.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
Let request object have custom properties

packages/openapi-fetch/src/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export type ClientMethod<Paths extends Record<string, PathMethods>, M extends Ht
166166
I extends MaybeOptionalInit<Paths[P], M>,
167167
>(
168168
url: P,
169-
...init: HasRequiredKeys<I> extends never ? [I?] : [I]
169+
...init: HasRequiredKeys<I> extends never ? [(I & { [key: string]: unknown })?] : [I]
170170
) => Promise<FetchResponse<Paths[P][M], I, Media>>;
171171

172172
export default function createClient<Paths extends {}, Media extends MediaType = MediaType>(

packages/openapi-fetch/src/index.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ const DEFAULT_HEADERS = {
55

66
const PATH_PARAM_RE = /\{[^{}]+\}/g;
77

8+
/**
9+
* Add custom parameters to Request object
10+
*/
11+
class CustomRequest extends Request {
12+
constructor(input, init) {
13+
super(input, init);
14+
15+
// add custom parameters
16+
for (const key in init) {
17+
if (!this[key]) {
18+
this[key] = init[key];
19+
}
20+
}
21+
}
22+
}
23+
824
/**
925
* Create an openapi-fetch client.
1026
* @type {import("./index.js").default}
@@ -67,7 +83,7 @@ export default function createClient(clientOptions) {
6783
if (requestInit.body instanceof FormData) {
6884
requestInit.headers.delete("Content-Type");
6985
}
70-
let request = new Request(createFinalURL(url, { baseUrl, params, querySerializer }), requestInit);
86+
let request = new CustomRequest(createFinalURL(url, { baseUrl, params, querySerializer }), requestInit);
7187
// middleware (request)
7288
const mergedOptions = {
7389
baseUrl,

packages/openapi-fetch/test/index.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,29 @@ describe("client", () => {
854854
expect(req.headers.get("foo")).toBe("bar");
855855
});
856856

857+
it("can attach custom properties to request", async () => {
858+
function createCustomFetch(data: any) {
859+
const response = {
860+
clone: () => ({ ...response }),
861+
headers: new Headers(),
862+
json: async () => data,
863+
status: 200,
864+
ok: true,
865+
} as Response;
866+
return async (input: Request) => {
867+
expect(input).toHaveProperty("customProperty", "value");
868+
return Promise.resolve(response);
869+
};
870+
}
871+
872+
const customFetch = createCustomFetch({});
873+
const client = createClient<paths>({ fetch: customFetch, baseUrl });
874+
875+
client.GET("/self", {
876+
customProperty: "value",
877+
});
878+
});
879+
857880
it("can modify response", async () => {
858881
const toUnix = (date: string) => new Date(date).getTime();
859882

0 commit comments

Comments
 (0)