Update "node-fetch" typings (#11834)

* Update "node-fetch" typings

Convert to external module and remove `namespace _fetch`

* Fix test

* Don't need patch version
This commit is contained in:
Andy 2016-10-20 08:47:19 -07:00 committed by GitHub
parent 65166af553
commit b06c56b70b
3 changed files with 90 additions and 101 deletions

View File

@ -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';

175
node-fetch/index.d.ts vendored
View File

@ -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 <https://github.com/torstenwerner>
// 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<string>;
has(name: string): boolean;
set(name: string, value: string): void;
forEach(callback: (value: string, name: string) => void): void;
}
class Body {
bodyUsed: boolean;
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Blob>;
formData(): Promise<FormData>;
json(): Promise<any>;
json<T>(): Promise<T>;
text(): Promise<string>;
}
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<string>;
type BodyInit = ArrayBuffer | ArrayBufferView | Blob | FormData | string;
type RequestInfo = Request | string;
interface FetchStatic {
(url: string | Request, init?: RequestInit): Promise<Response>;
}
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<string>;
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<ArrayBuffer>;
blob(): Promise<Blob>;
formData(): Promise<FormData>;
json(): Promise<any>;
json<T>(): Promise<T>;
text(): Promise<string>;
}
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<string>;
type BodyInit = ArrayBuffer | ArrayBufferView | Blob | FormData | string;
type RequestInfo = Request | string;
export default function fetch(url: string | Request, init?: RequestInit): Promise<Response>;

View File

@ -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<Response>) {
promise.then((response) => {
if (response.type === 'basic') {
// for test only