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

rebase from upstream/main
  • Loading branch information
mellster2012 committed May 16, 2024
1 parent 7190899 commit cbcb1ae
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1,611 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"@changesets/cli": "^2.27.1",
"del-cli": "^5.1.0",
"prettier": "^3.2.5",
"typescript": "^5.4.5",
"undici": "^6.14.1"
"typescript": "^5.4.5"
}
}
1 change: 1 addition & 0 deletions packages/openapi-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"openapi-typescript-fetch": "^2.0.0",
"superagent": "^9.0.2",
"typescript": "^5.4.5",
"undici": "^6.14.1",
"vitest": "^1.6.0"
}
}
4 changes: 3 additions & 1 deletion packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import type {
SuccessResponse,
} from "openapi-typescript-helpers";

import { 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?: unknown;
dispatcher?: Dispatcher;
/** custom fetch (defaults to globalThis.fetch) */
fetch?: (request: Request) => ReturnType<typeof fetch>;
/** global querySerializer */
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 @@ -2,6 +2,7 @@ import { HttpResponse, type StrictResponse } from "msw";
import createClient, { type Middleware, type MiddlewareRequest, type QuerySerializerOptions } from "../src/index.js";
import type { paths } from "./fixtures/api.js";
import { server, baseUrl, useMockRequestHandler, toAbsoluteURL } from "./fixtures/mock-server.js";
import { Agent } from "undici";

beforeAll(() => {
server.listen({
Expand Down Expand Up @@ -678,6 +679,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
Loading

0 comments on commit cbcb1ae

Please sign in to comment.