From b06c56b70bbdcbddedbfddd9862c136edd5acdcd Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 20 Oct 2016 08:47:19 -0700 Subject: [PATCH] Update "node-fetch" typings (#11834) * Update "node-fetch" typings Convert to external module and remove `namespace _fetch` * Fix test * Don't need patch version --- form-data/form-data-tests.ts | 2 +- node-fetch/index.d.ts | 175 +++++++++++++++------------------ node-fetch/node-fetch-tests.ts | 14 +-- 3 files changed, 90 insertions(+), 101 deletions(-) diff --git a/form-data/form-data-tests.ts b/form-data/form-data-tests.ts index 3a9db203da..2df21510c0 100644 --- a/form-data/form-data-tests.ts +++ b/form-data/form-data-tests.ts @@ -4,7 +4,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/node-fetch/index.d.ts b/node-fetch/index.d.ts index e96071224e..9ce5facdac 100644 --- a/node-fetch/index.d.ts +++ b/node-fetch/index.d.ts @@ -1,100 +1,89 @@ -// Type definitions for node-fetch based on whatwg-fetch +// Type definitions for node-fetch v1.6 // Project: https://github.com/bitinn/node-fetch // Definitions by: Torsten Werner // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare module _fetch { - - class Request extends Body { - constructor(input: string | Request, init?: RequestInit); - method: string; - url: string; - headers: Headers; - context: RequestContext; - referrer: string; - mode: RequestMode; - redirect: RequestRedirect; - credentials: RequestCredentials; - cache: RequestCache; - } - - interface RequestInit { - method?: string; - headers?: HeaderInit | { [index: string]: string }; - body?: BodyInit; - mode?: RequestMode; - redirect?: RequestRedirect; - credentials?: RequestCredentials; - cache?: RequestCache; - } - - 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" | - "subresource" | "style" | "track" | "video" | "worker" | - "xmlhttprequest" | "xslt"; - type RequestMode = "same-origin" | "no-cors" | "cors"; - type RequestRedirect = "follow" | "error" | "manual"; - type RequestCredentials = "omit" | "same-origin" | "include"; - type RequestCache = - "default" | "no-store" | "reload" | "no-cache" | - "force-cache" | "only-if-cached"; - - class Headers { - append(name: string, value: string): void; - delete(name: string): void; - get(name: string): string; - getAll(name: string): Array; - has(name: string): boolean; - set(name: string, value: string): void; - forEach(callback: (value: string, name: string) => void): void; - } - - class Body { - bodyUsed: boolean; - arrayBuffer(): Promise; - blob(): Promise; - formData(): Promise; - json(): Promise; - json(): Promise; - text(): Promise; - } - class Response extends Body { - constructor(body?: BodyInit, init?: ResponseInit); - static error(): Response; - static redirect(url: string, status: number): Response; - type: ResponseType; - url: string; - status: number; - ok: boolean; - statusText: string; - headers: Headers; - clone(): Response; - } - - type ResponseType = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect"; - - interface ResponseInit { - status: number; - statusText?: string; - headers?: HeaderInit; - } - - type HeaderInit = Headers | Array; - type BodyInit = ArrayBuffer | ArrayBufferView | Blob | FormData | string; - type RequestInfo = Request | string; - - interface FetchStatic { - (url: string | Request, init?: RequestInit): Promise; - } - +export class Request extends Body { + constructor(input: string | Request, init?: RequestInit); + method: string; + url: string; + headers: Headers; + context: RequestContext; + referrer: string; + mode: RequestMode; + redirect: RequestRedirect; + credentials: RequestCredentials; + cache: RequestCache; } -declare module "node-fetch" { - var fetch: _fetch.FetchStatic; - namespace fetch {} - export = fetch; +interface RequestInit { + method?: string; + headers?: HeaderInit | { [index: string]: string }; + body?: BodyInit; + mode?: RequestMode; + redirect?: RequestRedirect; + credentials?: RequestCredentials; + cache?: RequestCache; } + +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" | + "subresource" | "style" | "track" | "video" | "worker" | + "xmlhttprequest" | "xslt"; +type RequestMode = "same-origin" | "no-cors" | "cors"; +type RequestRedirect = "follow" | "error" | "manual"; +type RequestCredentials = "omit" | "same-origin" | "include"; +type RequestCache = + "default" | "no-store" | "reload" | "no-cache" | + "force-cache" | "only-if-cached"; + +export class Headers { + append(name: string, value: string): void; + delete(name: string): void; + get(name: string): string; + getAll(name: string): Array; + has(name: string): boolean; + set(name: string, value: string): void; + forEach(callback: (value: string, name: string) => void): void; +} + +export class Body { + bodyUsed: boolean; + arrayBuffer(): Promise; + blob(): Promise; + formData(): Promise; + json(): Promise; + json(): Promise; + text(): Promise; +} + +export class Response extends Body { + constructor(body?: BodyInit, init?: ResponseInit); + static error(): Response; + static redirect(url: string, status: number): Response; + type: ResponseType; + url: string; + status: number; + ok: boolean; + statusText: string; + headers: Headers; + clone(): Response; +} + +type ResponseType = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect"; + +interface ResponseInit { + status: number; + statusText?: string; + headers?: HeaderInit; +} + +type HeaderInit = Headers | Array; +type BodyInit = ArrayBuffer | ArrayBufferView | Blob | FormData | string; +type RequestInfo = Request | string; + +export default function fetch(url: string | Request, init?: RequestInit): Promise; diff --git a/node-fetch/node-fetch-tests.ts b/node-fetch/node-fetch-tests.ts index b4542209d8..52ebce4999 100644 --- a/node-fetch/node-fetch-tests.ts +++ b/node-fetch/node-fetch-tests.ts @@ -1,9 +1,9 @@ -import * as fetch from 'node-fetch'; +import fetch, { Headers, Request, RequestInit, Response } from 'node-fetch'; function test_fetchUrlWithOptions() { - var headers = new _fetch.Headers(); + var headers = new Headers(); headers.append("Content-Type", "application/json"); - var requestOptions: _fetch.RequestInit = { + var requestOptions: RequestInit = { method: "POST", headers: headers, mode: 'same-origin', @@ -15,7 +15,7 @@ function test_fetchUrlWithOptions() { } function test_fetchUrlWithHeadersObject() { - var requestOptions: _fetch.RequestInit = { + var requestOptions: RequestInit = { method: "POST", headers: { 'Content-Type': 'application/json' @@ -29,13 +29,13 @@ function test_fetchUrl() { } function test_fetchUrlWithRequestObject() { - var requestOptions: _fetch.RequestInit = { + var requestOptions: RequestInit = { method: "POST", headers: { 'Content-Type': 'application/json' } }; - var request: _fetch.Request = new _fetch.Request("http://www.andlabs.net/html5/uCOR.php", requestOptions); + var request: Request = new Request("http://www.andlabs.net/html5/uCOR.php", requestOptions); handlePromise(fetch(request)); } @@ -46,7 +46,7 @@ function test_globalFetchVar() { }); } -function handlePromise(promise: Promise<_fetch.Response>) { +function handlePromise(promise: Promise) { promise.then((response) => { if (response.type === 'basic') { // for test only