Skip to content

Commit

Permalink
Allow client option for custom dispatcher into fetch requests (e.g. t…
Browse files Browse the repository at this point in the history
…o disable certificate validation) #1631
  • Loading branch information
mellster2012 committed Aug 10, 2024
1 parent 9c51678 commit 62003c5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/openapi-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"openapi-typescript-codegen": "^0.25.0",
"openapi-typescript-fetch": "^2.0.0",
"superagent": "^9.0.2",
"undici": "^6.14.1",
"typescript": "^5.4.5",
"vite": "^5.3.5"
}
Expand Down
4 changes: 4 additions & 0 deletions packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import type {
SuccessResponse,
} from "openapi-typescript-helpers";

import type { Dispatcher } from "undici";

/** Options for each client instance */
export interface ClientOptions extends Omit<RequestInit, "headers"> {
/** set the common root URL for all API requests */
baseUrl?: string;
/** custom dispatcher */
dispatcher?: Dispatcher;
/** custom fetch (defaults to globalThis.fetch) */
fetch?: (input: Request) => Promise<Response>;
/** global querySerializer */
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function randomID() {
export default function createClient(clientOptions) {
let {
baseUrl = "",
dispatcher = undefined,
fetch: baseFetch = globalThis.fetch,
querySerializer: globalQuerySerializer,
bodySerializer: globalBodySerializer,
Expand Down Expand Up @@ -125,7 +126,7 @@ export default function createClient(clientOptions) {
}

// fetch!
let response = await fetch(request);
let response = await fetch(request, dispatcher ? { dispatcher } : undefined);

// middleware (response)
// execute in reverse-array order (first priority gets last transform)
Expand Down
28 changes: 28 additions & 0 deletions packages/openapi-fetch/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import createClient, {
} from "../src/index.js";
import { server, baseUrl, useMockRequestHandler, toAbsoluteURL } from "./fixtures/mock-server.js";
import type { paths } from "./fixtures/api.js";
import { Agent } from "undici";

beforeAll(() => {
server.listen({
Expand Down Expand Up @@ -768,6 +769,33 @@ describe("client", () => {
expect(getRequestUrl().href).toBe(toAbsoluteURL("/self"));
});

it("dispatcher", async () => {
const dispatcher = new Agent({
connect: {
rejectUnauthorized: false,
},
});
let client = createClient<paths>({ baseUrl, dispatcher });

const { getRequestUrl } = useMockRequestHandler({
baseUrl,
method: "get",
path: "/self",
status: 200,
body: { message: "OK" },
});

await client.GET("/self");

// assert baseUrl and path mesh as expected
expect(getRequestUrl().href).toBe(toAbsoluteURL("/self"));

client = createClient<paths>({ baseUrl });
await client.GET("/self");
// assert trailing '/' was removed
expect(getRequestUrl().href).toBe(toAbsoluteURL("/self"));
});

describe("headers", () => {
it("persist", async () => {
const headers: HeadersInit = { Authorization: "Bearer secrettoken" };
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 62003c5

Please sign in to comment.