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

node-fetch: restore types (for v2), update version, allow agent: false #58693

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions notNeededPackages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3069,10 +3069,6 @@
"libraryName": "node-downloader-helper",
"asOfVersion": "1.0.16"
},
"node-fetch": {
"libraryName": "node-fetch",
"asOfVersion": "3.0.0"
},
"node-json-db": {
"libraryName": "node-json-db",
"asOfVersion": "0.9.2"
Expand Down
6 changes: 0 additions & 6 deletions types/file-fetch/package.json

This file was deleted.

1 change: 0 additions & 1 deletion types/frisby/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"private": true,
"dependencies": {
"@types/node-fetch": "^2.5.12",
"joi": "^17.3.0"
}
}
6 changes: 0 additions & 6 deletions types/libnpmpublish/package.json

This file was deleted.

6 changes: 0 additions & 6 deletions types/make-fetch-happen/package.json

This file was deleted.

21 changes: 21 additions & 0 deletions types/node-fetch/externals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// `AbortSignal` is defined here to prevent a dependency on a particular
// implementation like the `abort-controller` package, and to avoid requiring
// the `dom` library in `tsconfig.json`.

export interface AbortSignal {
aborted: boolean;

addEventListener: (type: "abort", listener: ((this: AbortSignal, event: any) => any), options?: boolean | {
capture?: boolean | undefined,
once?: boolean | undefined,
passive?: boolean | undefined
}) => void;

removeEventListener: (type: "abort", listener: ((this: AbortSignal, event: any) => any), options?: boolean | {
capture?: boolean | undefined
}) => void;

dispatchEvent: (event: any) => boolean;

onabort: null | ((this: AbortSignal, event: any) => void);
}
224 changes: 224 additions & 0 deletions types/node-fetch/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// Type definitions for node-fetch 2.6
// Project: https://github.com/bitinn/node-fetch
// Definitions by: Torsten Werner <https://github.com/torstenwerner>
// Niklas Lindgren <https://github.com/nikcorg>
// Vinay Bedre <https://github.com/vinaybedre>
// Antonio Román <https://github.com/kyranet>
// Andrew Leedham <https://github.com/AndrewLeedham>
// Jason Li <https://github.com/JasonLi914>
// Steve Faulkner <https://github.com/southpolesteve>
// ExE Boss <https://github.com/ExE-Boss>
// Alex Savin <https://github.com/alexandrusavin>
// Alexis Tyler <https://github.com/OmgImAlexis>
// Jakub Kisielewski <https://github.com/kbkk>
glasser marked this conversation as resolved.
Show resolved Hide resolved
// David Glasser <https://github.com/glasser>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/// <reference types="node" />

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

export class Request extends Body {
constructor(input: RequestInfo, init?: RequestInit);
clone(): Request;
context: RequestContext;
headers: Headers;
method: string;
redirect: RequestRedirect;
referrer: string;
url: string;

// node-fetch extensions to the whatwg/fetch spec
agent?: RequestOptions['agent'] | ((parsedUrl: URL) => RequestOptions['agent']);
compress: boolean;
counter: number;
follow: number;
hostname: string;
port?: number | undefined;
protocol: string;
size: number;
timeout: number;
}

export interface RequestInit {
// whatwg/fetch standard options
body?: BodyInit | undefined;
headers?: HeadersInit | undefined;
method?: string | undefined;
redirect?: RequestRedirect | undefined;
signal?: AbortSignal | null | undefined;

// node-fetch extensions
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
timeout?: number | undefined; // =0 req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies)

// node-fetch does not support mode, cache or credentials options
}

export type RequestContext =
"audio"
| "beacon"
| "cspreport"
| "download"
| "embed"
| "eventsource"
| "favicon"
| "fetch"
| "font"
| "form"
| "frame"
| "hyperlink"
| "iframe"
| "image"
| "imageset"
| "import"
| "internal"
| "location"
| "manifest"
| "object"
| "ping"
| "plugin"
| "prefetch"
| "script"
| "serviceworker"
| "sharedworker"
| "style"
| "subresource"
| "track"
| "video"
| "worker"
| "xmlhttprequest"
| "xslt";
export type RequestMode = "cors" | "no-cors" | "same-origin";
export type RequestRedirect = "error" | "follow" | "manual";
export type RequestCredentials = "omit" | "include" | "same-origin";

export type RequestCache =
"default"
| "force-cache"
| "no-cache"
| "no-store"
| "only-if-cached"
| "reload";

export class Headers implements Iterable<[string, string]> {
constructor(init?: HeadersInit);
forEach(callback: (value: string, name: string) => void): void;
append(name: string, value: string): void;
delete(name: string): void;
get(name: string): string | null;
has(name: string): boolean;
raw(): { [k: string]: string[] };
set(name: string, value: string): void;

// Iterable methods
entries(): IterableIterator<[string, string]>;
keys(): IterableIterator<string>;
values(): IterableIterator<string>;
[Symbol.iterator](): Iterator<[string, string]>;
}

type BlobPart = ArrayBuffer | ArrayBufferView | Blob | string;

interface BlobOptions {
type?: string | undefined;
endings?: "transparent" | "native" | undefined;
}

export class Blob {
constructor(blobParts?: BlobPart[], options?: BlobOptions);
readonly type: string;
readonly size: number;
slice(start?: number, end?: number): Blob;
text(): Promise<string>;
}

export class Body {
constructor(body?: any, opts?: { size?: number | undefined; timeout?: number | undefined });
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Blob>;
body: NodeJS.ReadableStream;
bodyUsed: boolean;
buffer(): Promise<Buffer>;
json(): Promise<any>;
size: number;
text(): Promise<string>;
textConverted(): Promise<string>;
timeout: number;
}

interface SystemError extends Error {
code?: string | undefined;
}

export class FetchError extends Error {
name: "FetchError";
constructor(message: string, type: string, systemError?: SystemError);
type: string;
code?: string | undefined;
errno?: string | undefined;
}

export class Response extends Body {
constructor(body?: BodyInit, init?: ResponseInit);
static error(): Response;
static redirect(url: string, status: number): Response;
clone(): Response;
headers: Headers;
ok: boolean;
redirected: boolean;
status: number;
statusText: string;
type: ResponseType;
url: string;
}

export type ResponseType =
"basic"
| "cors"
| "default"
| "error"
| "opaque"
| "opaqueredirect";

export interface ResponseInit {
headers?: HeadersInit | undefined;
size?: number | undefined;
status?: number | undefined;
statusText?: string | undefined;
timeout?: number | undefined;
url?: string | undefined;
}

interface URLLike {
href: string;
}

export type HeadersInit = Headers | string[][] | { [key: string]: string };
// HeaderInit is exported to support backwards compatibility. See PR #34382
export type HeaderInit = HeadersInit;
export type BodyInit =
ArrayBuffer
| ArrayBufferView
| NodeJS.ReadableStream
| string
| URLSearchParams
| FormData;
export type RequestInfo = string | URLLike | Request;

declare function fetch(
url: RequestInfo,
init?: RequestInit
): Promise<Response>;

declare namespace fetch {
function isRedirect(code: number): boolean;
}

export default fetch;