Skip to content

Commit cbcb1ae

Browse files
committed
Allow client option for custom dispatcher into fetch requests (e.g. to disable certificate validation) openapi-ts#1631
rebase from upstream/main
1 parent 7190899 commit cbcb1ae

File tree

5 files changed

+33
-1611
lines changed

5 files changed

+33
-1611
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"@changesets/cli": "^2.27.1",
2121
"del-cli": "^5.1.0",
2222
"prettier": "^3.2.5",
23-
"typescript": "^5.4.5",
24-
"undici": "^6.14.1"
23+
"typescript": "^5.4.5"
2524
}
2625
}

packages/openapi-fetch/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"openapi-typescript-fetch": "^2.0.0",
7676
"superagent": "^9.0.2",
7777
"typescript": "^5.4.5",
78+
"undici": "^6.14.1",
7879
"vitest": "^1.6.0"
7980
}
8081
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import type {
1111
SuccessResponse,
1212
} from "openapi-typescript-helpers";
1313

14+
import { Dispatcher} from 'undici';
15+
1416
/** Options for each client instance */
1517
export interface ClientOptions extends Omit<RequestInit, "headers"> {
1618
/** set the common root URL for all API requests */
1719
baseUrl?: string;
1820
/** custom dispatcher */
19-
dispatcher?: unknown;
21+
dispatcher?: Dispatcher;
2022
/** custom fetch (defaults to globalThis.fetch) */
2123
fetch?: (request: Request) => ReturnType<typeof fetch>;
2224
/** global querySerializer */

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

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { HttpResponse, type StrictResponse } from "msw";
22
import createClient, { type Middleware, type MiddlewareRequest, type QuerySerializerOptions } from "../src/index.js";
33
import type { paths } from "./fixtures/api.js";
44
import { server, baseUrl, useMockRequestHandler, toAbsoluteURL } from "./fixtures/mock-server.js";
5+
import { Agent } from "undici";
56

67
beforeAll(() => {
78
server.listen({
@@ -678,6 +679,33 @@ describe("client", () => {
678679
expect(getRequestUrl().href).toBe(toAbsoluteURL("/self"));
679680
});
680681

682+
it("dispatcher", async () => {
683+
const dispatcher = new Agent({
684+
connect: {
685+
rejectUnauthorized: false,
686+
},
687+
});
688+
let client = createClient<paths>({ baseUrl, dispatcher });
689+
690+
const { getRequestUrl } = useMockRequestHandler({
691+
baseUrl,
692+
method: "get",
693+
path: "/self",
694+
status: 200,
695+
body: { message: "OK" },
696+
});
697+
698+
await client.GET("/self");
699+
700+
// assert baseUrl and path mesh as expected
701+
expect(getRequestUrl().href).toBe(toAbsoluteURL("/self"));
702+
703+
client = createClient<paths>({ baseUrl });
704+
await client.GET("/self");
705+
// assert trailing '/' was removed
706+
expect(getRequestUrl().href).toBe(toAbsoluteURL("/self"));
707+
});
708+
681709
describe("headers", () => {
682710
it("persist", async () => {
683711
const headers: HeadersInit = { Authorization: "Bearer secrettoken" };

0 commit comments

Comments
 (0)