Skip to content

Commit efaa1e2

Browse files
tobiasdcldrwpow
andauthored
feat(openapi-fetch): allow usage of custom Request class (#1907)
* feat(openapi-fetch): allow usage of custom Request class * chore: added changeset * fix: make linter happy * Update brave-days-invent.md Co-authored-by: Drew Powers <[email protected]> --------- Co-authored-by: Drew Powers <[email protected]>
1 parent cc83541 commit efaa1e2

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

.changeset/brave-days-invent.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+
allow usage of custom Request class

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export interface ClientOptions extends Omit<RequestInit, "headers"> {
1717
baseUrl?: string;
1818
/** custom fetch (defaults to globalThis.fetch) */
1919
fetch?: (input: Request) => Promise<Response>;
20+
/** custom Request (defaults to globalThis.Request) */
21+
Request?: typeof Request;
2022
/** global querySerializer */
2123
querySerializer?: QuerySerializer<unknown> | QuerySerializerOptions;
2224
/** global bodySerializer */

packages/openapi-fetch/src/index.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
// settings & const
22
const PATH_PARAM_RE = /\{[^{}]+\}/g;
33

4-
/** Add custom parameters to Request object */
5-
class CustomRequest extends Request {
6-
constructor(input, init) {
7-
super(input, init);
8-
9-
// add custom parameters
10-
for (const key in init) {
11-
if (!(key in this)) {
12-
this[key] = init[key];
13-
}
14-
}
15-
}
16-
}
17-
184
/**
195
* Returns a cheap, non-cryptographically-secure random ID
206
* Courtesy of @imranbarbhuiya (https://github.com/imranbarbhuiya)
@@ -30,6 +16,7 @@ export function randomID() {
3016
export default function createClient(clientOptions) {
3117
let {
3218
baseUrl = "",
19+
Request: CustomRequest = globalThis.Request,
3320
fetch: baseFetch = globalThis.fetch,
3421
querySerializer: globalQuerySerializer,
3522
bodySerializer: globalBodySerializer,
@@ -48,6 +35,7 @@ export default function createClient(clientOptions) {
4835
const {
4936
baseUrl: localBaseUrl,
5037
fetch = baseFetch,
38+
Request = CustomRequest,
5139
headers,
5240
params = {},
5341
parseAs = "json",
@@ -98,6 +86,13 @@ export default function createClient(clientOptions) {
9886
let options;
9987
let request = new CustomRequest(createFinalURL(schemaPath, { baseUrl, params, querySerializer }), requestInit);
10088

89+
/** Add custom parameters to Request object */
90+
for (const key in init) {
91+
if (!(key in request)) {
92+
request[key] = init[key];
93+
}
94+
}
95+
10196
if (middlewares.length) {
10297
id = randomID();
10398

@@ -119,7 +114,7 @@ export default function createClient(clientOptions) {
119114
id,
120115
});
121116
if (result) {
122-
if (!(result instanceof Request)) {
117+
if (!(result instanceof CustomRequest)) {
123118
throw new Error("onRequest: must return new Request() when modifying the request");
124119
}
125120
request = result;

packages/openapi-fetch/test/common/request.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,27 @@ describe("request", () => {
282282
expect(headers.get("cookie")).toEqual("session=1234");
283283
});
284284

285+
test("uses provided Request class", async () => {
286+
// santity check to make sure the profided fetch function is actually called
287+
expect.assertions(1);
288+
289+
class SpecialRequestImplementation extends Request {}
290+
291+
const specialFetch = async (input: Request) => {
292+
// make sure that the request is actually an instance of the custom request we provided
293+
expect(input).instanceOf(SpecialRequestImplementation);
294+
return Promise.resolve(Response.json({ hello: "world" }));
295+
};
296+
297+
const client = createClient<paths>({
298+
baseUrl: "https://fakeurl.example",
299+
fetch: specialFetch,
300+
Request: SpecialRequestImplementation,
301+
});
302+
303+
await client.GET("/resources");
304+
});
305+
285306
test("can attach custom properties to request", async () => {
286307
function createCustomFetch(data: any) {
287308
const response = {

0 commit comments

Comments
 (0)