Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow client option for custom dispatcher into fetch requests (e.g. to disable certificate validation) #1631 #1636

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 4 additions & 0 deletions packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +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?: Dispatcher;
/** custom fetch (defaults to globalThis.fetch) */
fetch?: (request: Request) => ReturnType<typeof fetch>;
/** 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 @@ -28,6 +28,7 @@ class CustomRequest extends Request {
export default function createClient(clientOptions) {
let {
baseUrl = "",
dispatcher = undefined,
fetch: baseFetch = globalThis.fetch,
querySerializer: globalQuerySerializer,
bodySerializer: globalBodySerializer,
Expand Down Expand Up @@ -107,7 +108,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 @@ -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