Skip to content

Commit 493446b

Browse files
committed
feat: add rejectUnauthorized to RequestOptions
Move `rejectUnauthorized` from the private `UrllibRequestOptions` interface to the public `RequestOptions` type so it can be passed to the top-level `request()`/`curl()` wrapper without a TypeScript error and without constructing a custom HttpClient. Since `allowH2` is already part of `RequestOptions`, the private `UrllibRequestOptions` interface became redundant and is removed; `request()`/`curl()` now use `RequestOptions` directly.
1 parent e96fec9 commit 493446b

4 files changed

Lines changed: 61 additions & 14 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ console.log('status: %s, body size: %d, headers: %j', res.status, data.length, r
8181
- **_timing_** Boolean - Enable timing or not, default is `true`.
8282
- **_socketPath_** String | null - request a unix socket service, default is `null`.
8383
- **_highWaterMark_** Number - default is `67108864`, 64 KiB.
84+
- **_rejectUnauthorized_** Boolean - If `true`, the server certificate is verified against the list of supplied CAs and the request is rejected on failure. Set `false` to allow self-signed certificates. Default is `true`. Only effective on the top-level `request`/`curl` wrapper with its default dispatcher; it is ignored when calling `HttpClient.request()` directly or when a custom `dispatcher` is provided.
8485

8586
#### Options: `options.data`
8687

src/Request.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ export type RequestOptions = {
156156
/** Default: `64 KiB` */
157157
highWaterMark?: number;
158158
signal?: globalThis.AbortSignal | EventEmitter;
159+
/**
160+
* If `true`, the server certificate is verified against the list of supplied CAs. If verification fails, the request
161+
* promise is rejected.
162+
*
163+
* This option is only effective when using the top-level request/curl wrapper with its default dispatcher. It may be
164+
* ignored when using `HttpClient.request()` directly or when `dispatcher` is explicitly provided.
165+
*
166+
* Default: `true`
167+
*/
168+
rejectUnauthorized?: boolean;
159169
};
160170

161171
export type RequestMeta = {

src/index.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,7 @@ export function getDefaultHttpClient(rejectUnauthorized?: boolean, allowH2?: boo
3131
return client;
3232
}
3333

34-
interface UrllibRequestOptions extends RequestOptions {
35-
/**
36-
* If `true`, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if
37-
* verification fails. Default: `true`
38-
*/
39-
rejectUnauthorized?: boolean;
40-
// `allowH2` is inherited from RequestOptions.
41-
}
42-
43-
export async function request<T = any>(
44-
url: RequestURL,
45-
options?: UrllibRequestOptions,
46-
): Promise<HttpClientResponse<T>> {
34+
export async function request<T = any>(url: RequestURL, options?: RequestOptions): Promise<HttpClientResponse<T>> {
4735
if (options?.socketPath) {
4836
let domainSocketHttpclient = domainSocketHttpClients.get<HttpClient>(options.socketPath);
4937
if (!domainSocketHttpclient) {
@@ -63,7 +51,7 @@ export async function request<T = any>(
6351
// import * as urllib from 'urllib';
6452
// urllib.curl(url);
6553
// ```
66-
export async function curl<T = any>(url: RequestURL, options?: UrllibRequestOptions): Promise<HttpClientResponse<T>> {
54+
export async function curl<T = any>(url: RequestURL, options?: RequestOptions): Promise<HttpClientResponse<T>> {
6755
return await request<T>(url, options);
6856
}
6957

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { strict as assert } from 'node:assert';
2+
3+
import { describe, it, beforeAll, afterAll } from 'vite-plus/test';
4+
5+
import urllib from '../src/index.js';
6+
import type { RequestOptions } from '../src/index.js';
7+
import { startServer } from './fixtures/server.js';
8+
9+
describe('options.rejectUnauthorized.typecheck.test.ts', () => {
10+
let close: any;
11+
let _url: string;
12+
beforeAll(async () => {
13+
const { closeServer, url } = await startServer({ https: true });
14+
close = closeServer;
15+
_url = url;
16+
});
17+
18+
afterAll(async () => {
19+
await close();
20+
});
21+
22+
it('should accept rejectUnauthorized on RequestOptions type', async () => {
23+
// Type check: rejectUnauthorized should be assignable on RequestOptions
24+
const options: RequestOptions = {
25+
rejectUnauthorized: false,
26+
dataType: 'json',
27+
};
28+
const response = await urllib.request(_url, options);
29+
assert.equal(response.status, 200);
30+
assert.equal(response.data.method, 'GET');
31+
});
32+
33+
it('should accept rejectUnauthorized = true on RequestOptions type', async () => {
34+
const options: RequestOptions = {
35+
rejectUnauthorized: true,
36+
dataType: 'json',
37+
};
38+
await assert.rejects(
39+
async () => {
40+
await urllib.request(_url, options);
41+
},
42+
(err: any) => {
43+
assert.match(err.message, /signed certificate/);
44+
return true;
45+
},
46+
);
47+
});
48+
});

0 commit comments

Comments
 (0)