diff --git a/node-fetch/node-fetch-tests.ts b/node-fetch/node-fetch-tests.ts new file mode 100644 index 0000000000..28d7ff7766 --- /dev/null +++ b/node-fetch/node-fetch-tests.ts @@ -0,0 +1,60 @@ +/// + +import fetch = require('node-fetch'); + +function test_fetchUrlWithOptions() { + var headers = new _fetch.Headers(); + headers.append("Content-Type", "application/json"); + var requestOptions: _fetch.RequestInit = { + method: "POST", + headers: headers, + mode: 'same-origin', + credentials: 'omit', + cache: 'default', + redirect: 'manual' + }; + handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions)); +} + +function test_fetchUrlWithHeadersObject() { + var requestOptions: _fetch.RequestInit = { + method: "POST", + headers: { + 'Content-Type': 'application/json' + } + }; + handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions)); +} + +function test_fetchUrl() { + handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php")); +} + +function test_fetchUrlWithRequestObject() { + var requestOptions: _fetch.RequestInit = { + method: "POST", + headers: { + 'Content-Type': 'application/json' + } + }; + var request: _fetch.Request = new _fetch.Request("http://www.andlabs.net/html5/uCOR.php", requestOptions); + handlePromise(fetch(request)); +} + +function test_globalFetchVar() { + fetch('http://test.com', {}) + .then(response => { + // for test only + }); +} + +function handlePromise(promise: Promise<_fetch.Response>) { + promise.then((response) => { + if (response.type === 'basic') { + // for test only + } + return response.text(); + }).then((text) => { + console.log(text); + }); +} diff --git a/node-fetch/node-fetch.d.ts b/node-fetch/node-fetch.d.ts new file mode 100644 index 0000000000..5cf41ea992 --- /dev/null +++ b/node-fetch/node-fetch.d.ts @@ -0,0 +1,95 @@ +// Type definitions for node-fetch based on whatwg-fetch +// 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; + + function fetch(url: string | Request, init?: RequestInit): Promise; +} + +declare module "node-fetch" { + export = _fetch.fetch; +}