From 3dc0c0b54c19c69784c1ece0ad72c0c9b78cc134 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Nov 2018 14:16:19 -0800 Subject: [PATCH] node-fetch: Is itself a function, not an object with a 'default' property --- types/form-data/form-data-tests.ts | 2 +- types/node-fetch/index.d.ts | 323 +++++++++--------- types/node-fetch/node-fetch-tests.ts | 3 +- types/p-retry/p-retry-tests.ts | 2 +- types/zipkin-instrumentation-fetch/index.d.ts | 2 +- .../zipkin-instrumentation-fetch-tests.ts | 2 +- 6 files changed, 169 insertions(+), 165 deletions(-) diff --git a/types/form-data/form-data-tests.ts b/types/form-data/form-data-tests.ts index 86f2a49937..77d7ec390a 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 from 'node-fetch'; +import fetch = require('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 4b12734363..1d1e0556de 100644 --- a/types/node-fetch/index.d.ts +++ b/types/node-fetch/index.d.ts @@ -9,166 +9,169 @@ import { Agent } from "http"; import { URLSearchParams } from "url"; -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; +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; - // 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; + // 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; } -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; +declare function fetch( + url: string | fetch.Request, + init?: fetch.RequestInit +): Promise; +export = fetch; diff --git a/types/node-fetch/node-fetch-tests.ts b/types/node-fetch/node-fetch-tests.ts index 7561ae1b0f..e7d9ff5c9a 100644 --- a/types/node-fetch/node-fetch-tests.ts +++ b/types/node-fetch/node-fetch-tests.ts @@ -1,4 +1,5 @@ -import fetch, { Headers, Request, RequestInit, Response } from 'node-fetch'; +import fetch = require("node-fetch"); +import { 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 1b58960e95..fb013bd167 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 from 'node-fetch'; +import fetch = require('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 390cb80bcc..e98850b64a 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 from 'node-fetch'; +import fetch = require('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 5790413e0a..80736c49ef 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 from 'node-fetch'; +import fetch = require('node-fetch'); import { Tracer,