diff --git a/types/form-data/form-data-tests.ts b/types/form-data/form-data-tests.ts index 77d7ec390a..86f2a49937 100644 --- a/types/form-data/form-data-tests.ts +++ b/types/form-data/form-data-tests.ts @@ -2,7 +2,7 @@ import FormData = require('form-data'); import fs = require('fs'); import http = require('http'); import request = require('request'); -import fetch = require('node-fetch'); +import fetch from 'node-fetch'; import * as ImportUsingES6Syntax from 'form-data'; diff --git a/types/node-fetch/index.d.ts b/types/node-fetch/index.d.ts index 1d1e0556de..4b12734363 100644 --- a/types/node-fetch/index.d.ts +++ b/types/node-fetch/index.d.ts @@ -9,169 +9,166 @@ import { Agent } from "http"; import { URLSearchParams } from "url"; -declare namespace fetch { - class Request extends Body { - constructor(input: string | { href: string } | Request, init?: RequestInit); - clone(): Request; - context: RequestContext; - headers: Headers; - method: string; - redirect: RequestRedirect; - referrer: string; - url: string; +export class Request extends Body { + constructor(input: string | { href: string } | Request, 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?: Agent; - compress: boolean; - counter: number; - follow: number; - hostname: string; - port?: number; - protocol: string; - size: number; - timeout: number; - } - - interface RequestInit { - // whatwg/fetch standard options - body?: BodyInit; - headers?: HeaderInit | { [index: string]: string }; - method?: string; - redirect?: RequestRedirect; - - // node-fetch extensions - agent?: Agent; // =null http.Agent instance, allows custom proxy, certificate etc. - compress?: boolean; // =true support gzip/deflate content encoding. false to disable - follow?: number; // =20 maximum redirect count. 0 to not follow redirect - size?: number; // =0 maximum response body size in bytes. 0 to disable - timeout?: number; // =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 - } - - 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"; - type RequestMode = "cors" | "no-cors" | "same-origin"; - type RequestRedirect = "error" | "follow" | "manual"; - type RequestCredentials = "omit" | "include" | "same-origin"; - - type RequestCache = - "default" - | "force-cache" - | "no-cache" - | "no-store" - | "only-if-cached" - | "reload"; - - class Headers implements Iterable<[string, string]> { - constructor(init?: Headers | { [k: string]: string }); - forEach(callback: (value: string, name: string) => void): void; - append(name: string, value: string): void; - delete(name: string): void; - get(name: string): string | null; - getAll(name: string): string[]; - has(name: string): boolean; - raw(): { [k: string]: string[] }; - set(name: string, value: string): void; - - // Iterator methods - entries(): Iterator<[string, string]>; - keys(): Iterator; - values(): Iterator<[string]>; - [Symbol.iterator](): Iterator<[string, string]>; - } - - class Blob { - type: string; - size: number; - slice(start?: number, end?: number): Blob; - } - - class Body { - constructor(body?: any, opts?: { size?: number; timeout?: number }); - arrayBuffer(): Promise; - blob(): Promise; - body: NodeJS.ReadableStream; - bodyUsed: boolean; - buffer(): Promise; - json(): Promise; - text(): Promise; - textConverted(): Promise; - } - - class FetchError extends Error { - name: "FetchError"; - constructor(message: string, type: string, systemError: string); - } - - 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; - size: number; - status: number; - statusText: string; - timeout: number; - type: ResponseType; - url: string; - } - - type ResponseType = - "basic" - | "cors" - | "default" - | "error" - | "opaque" - | "opaqueredirect"; - - interface ResponseInit { - headers?: HeaderInit; - status: number; - statusText?: string; - } - - type HeaderInit = Headers | string[]; - type BodyInit = ArrayBuffer | ArrayBufferView | NodeJS.ReadableStream | string | URLSearchParams; - type RequestInfo = string | Request; + // node-fetch extensions to the whatwg/fetch spec + agent?: Agent; + compress: boolean; + counter: number; + follow: number; + hostname: string; + port?: number; + protocol: string; + size: number; + timeout: number; } -declare function fetch( - url: string | fetch.Request, - init?: fetch.RequestInit -): Promise; -export = fetch; +export interface RequestInit { + // whatwg/fetch standard options + body?: BodyInit; + headers?: HeaderInit | { [index: string]: string }; + method?: string; + redirect?: RequestRedirect; + + // node-fetch extensions + agent?: Agent; // =null http.Agent instance, allows custom proxy, certificate etc. + compress?: boolean; // =true support gzip/deflate content encoding. false to disable + follow?: number; // =20 maximum redirect count. 0 to not follow redirect + size?: number; // =0 maximum response body size in bytes. 0 to disable + timeout?: number; // =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?: Headers | { [k: string]: string }); + forEach(callback: (value: string, name: string) => void): void; + append(name: string, value: string): void; + delete(name: string): void; + get(name: string): string | null; + getAll(name: string): string[]; + has(name: string): boolean; + raw(): { [k: string]: string[] }; + set(name: string, value: string): void; + + // Iterator methods + entries(): Iterator<[string, string]>; + keys(): Iterator; + values(): Iterator<[string]>; + [Symbol.iterator](): Iterator<[string, string]>; +} + +export class Blob { + type: string; + size: number; + slice(start?: number, end?: number): Blob; +} + +export class Body { + constructor(body?: any, opts?: { size?: number; timeout?: number }); + arrayBuffer(): Promise; + blob(): Promise; + body: NodeJS.ReadableStream; + bodyUsed: boolean; + buffer(): Promise; + json(): Promise; + text(): Promise; + textConverted(): Promise; +} + +export class FetchError extends Error { + name: "FetchError"; + constructor(message: string, type: string, systemError: string); +} + +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; + size: number; + status: number; + statusText: string; + timeout: number; + type: ResponseType; + url: string; +} + +export type ResponseType = + "basic" + | "cors" + | "default" + | "error" + | "opaque" + | "opaqueredirect"; + +export interface ResponseInit { + headers?: HeaderInit; + status: number; + statusText?: string; +} + +export type HeaderInit = Headers | string[]; +export type BodyInit = ArrayBuffer | ArrayBufferView | NodeJS.ReadableStream | string | URLSearchParams; +export type RequestInfo = string | Request; + +export default function fetch( + url: string | Request, + init?: RequestInit +): Promise; diff --git a/types/node-fetch/node-fetch-tests.ts b/types/node-fetch/node-fetch-tests.ts index e7d9ff5c9a..7561ae1b0f 100644 --- a/types/node-fetch/node-fetch-tests.ts +++ b/types/node-fetch/node-fetch-tests.ts @@ -1,5 +1,4 @@ -import fetch = require("node-fetch"); -import { Headers, Request, RequestInit, Response } from 'node-fetch'; +import fetch, { Headers, Request, RequestInit, Response } from 'node-fetch'; import { Agent } from "http"; function test_fetchUrlWithOptions() { diff --git a/types/p-retry/p-retry-tests.ts b/types/p-retry/p-retry-tests.ts index fb013bd167..1b58960e95 100644 --- a/types/p-retry/p-retry-tests.ts +++ b/types/p-retry/p-retry-tests.ts @@ -1,5 +1,5 @@ import pRetry = require('p-retry'); -import fetch = require('node-fetch'); +import fetch from 'node-fetch'; const run = () => fetch('https://sindresorhus.com/unicorn') .then(response => { diff --git a/types/zipkin-instrumentation-fetch/index.d.ts b/types/zipkin-instrumentation-fetch/index.d.ts index e98850b64a..390cb80bcc 100644 --- a/types/zipkin-instrumentation-fetch/index.d.ts +++ b/types/zipkin-instrumentation-fetch/index.d.ts @@ -5,7 +5,7 @@ // TypeScript Version: 2.3 import { Tracer } from 'zipkin'; -import fetch = require('node-fetch'); +import fetch from 'node-fetch'; interface Options { tracer: Tracer; diff --git a/types/zipkin-instrumentation-fetch/zipkin-instrumentation-fetch-tests.ts b/types/zipkin-instrumentation-fetch/zipkin-instrumentation-fetch-tests.ts index 80736c49ef..5790413e0a 100644 --- a/types/zipkin-instrumentation-fetch/zipkin-instrumentation-fetch-tests.ts +++ b/types/zipkin-instrumentation-fetch/zipkin-instrumentation-fetch-tests.ts @@ -1,5 +1,5 @@ import wrapFetch = require('zipkin-instrumentation-fetch'); -import fetch = require('node-fetch'); +import fetch from 'node-fetch'; import { Tracer,