Skip to content

Commit

Permalink
node-fetch: agent can be a boolean
Browse files Browse the repository at this point in the history
The `agent` option is (after checking to see if it's a function, and if
so replacing itself with the return value of calling it) passed directly
to `http.request` (or `https.request`) so we should be able to pass any
type supported there. Notably, `agent: false` is distinct from `agent:
undefined`.

A PR making essentially the same change was merged upstream (the
upstream repo maintains their own types for v3 but not for v2):
node-fetch/node-fetch#1502
  • Loading branch information
glasser committed Feb 9, 2022
1 parent 2766011 commit af88e00
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 3 additions & 3 deletions types/node-fetch/index.d.ts
Expand Up @@ -16,7 +16,7 @@
/// <reference types="node" />

import FormData = require('form-data');
import { Agent } from "http";
import { RequestOptions } from "http";
import { URLSearchParams, URL } from "url";
import { AbortSignal } from "./externals";

Expand All @@ -31,7 +31,7 @@ export class Request extends Body {
url: string;

// node-fetch extensions to the whatwg/fetch spec
agent?: Agent | ((parsedUrl: URL) => Agent) | undefined;
agent?: RequestOptions['agent'] | ((parsedUrl: URL) => RequestOptions['agent']);
compress: boolean;
counter: number;
follow: number;
Expand All @@ -51,7 +51,7 @@ export interface RequestInit {
signal?: AbortSignal | null | undefined;

// node-fetch extensions
agent?: Agent | ((parsedUrl: URL) => Agent) | undefined; // =null http.Agent instance, allows custom proxy, certificate etc.
agent?: RequestOptions['agent'] | ((parsedUrl: URL) => RequestOptions['agent']); // =null http.Agent instance, allows custom proxy, certificate etc.
compress?: boolean | undefined; // =true support gzip/deflate content encoding. false to disable
follow?: number | undefined; // =20 maximum redirect count. 0 to not follow redirect
size?: number | undefined; // =0 maximum response body size in bytes. 0 to disable
Expand Down
7 changes: 4 additions & 3 deletions types/node-fetch/node-fetch-tests.ts
Expand Up @@ -19,7 +19,8 @@ function test_fetchUrlWithOptions() {
method: "POST",
redirect: "manual",
size: 100,
timeout: 5000
timeout: 5000,
agent: false,
};
handlePromise(
fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions)
Expand Down Expand Up @@ -75,7 +76,7 @@ function test_fetchUrlWithRequestObject() {
);
const timeout: number = request.timeout;
const size: number = request.size;
const agent: Agent | ((parsedUrl: URL) => Agent) | undefined = request.agent;
const agent: Agent | ((parsedUrl: URL) => boolean | Agent | undefined) | boolean | undefined = request.agent;
const protocol: string = request.protocol;

handlePromise(fetch(request));
Expand Down Expand Up @@ -127,7 +128,7 @@ function test_fetchUrlObjectWithRequestObject() {
);
const timeout: number = request.timeout;
const size: number = request.size;
const agent: Agent | ((parsedUrl: URL) => Agent) | undefined = request.agent;
const agent: Agent | ((parsedUrl: URL) => boolean | Agent | undefined) | boolean | undefined = request.agent;
const protocol: string = request.protocol;

handlePromise(fetch(request));
Expand Down

0 comments on commit af88e00

Please sign in to comment.