Skip to content

Commit 9307e16

Browse files
committed
feat(openapi-fetch): allow usage of custom Request class
1 parent fdc6d22 commit 9307e16

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,29 @@ 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+
static special = "special";
291+
}
292+
293+
const specialFetch = async (input: Request) => {
294+
// make sure that the request is actually an instance of the custom request we provided
295+
expect(input).instanceOf(SpecialRequestImplementation);
296+
return Promise.resolve(Response.json({ hello: "world" }));
297+
};
298+
299+
const client = createClient<paths>({
300+
baseUrl: "https://fakeurl.example",
301+
fetch: specialFetch,
302+
Request: SpecialRequestImplementation,
303+
});
304+
305+
await client.GET("/resources");
306+
});
307+
285308
test("can attach custom properties to request", async () => {
286309
function createCustomFetch(data: any) {
287310
const response = {

0 commit comments

Comments
 (0)