diff --git a/CrossPlatformModules.csproj b/CrossPlatformModules.csproj index c6fd5807f..7f9a72e2b 100644 --- a/CrossPlatformModules.csproj +++ b/CrossPlatformModules.csproj @@ -300,7 +300,6 @@ - file-name-resolver.d.ts @@ -1734,9 +1733,7 @@ - - - + @@ -1787,7 +1784,7 @@ False - + \ No newline at end of file diff --git a/apps/tests/fetch-tests.ts b/apps/tests/fetch-tests.ts index 6d71a4bf5..de541975a 100644 --- a/apps/tests/fetch-tests.ts +++ b/apps/tests/fetch-tests.ts @@ -1,6 +1,5 @@ /* tslint:disable:no-unused-variable */ import TKUnit = require("./TKUnit"); -import fetchModule = require("fetch"); import types = require("utils/types"); // @@ -12,7 +11,7 @@ import types = require("utils/types"); // export var test_fetch_defined = function () { - TKUnit.assert(types.isDefined((fetchModule.fetch)), "Method fetch() should be defined!"); + TKUnit.assert(types.isDefined((fetch)), "Method fetch() should be defined!"); }; export var test_fetch = function (done: (err: Error, res?: string) => void) { @@ -20,10 +19,10 @@ export var test_fetch = function (done: (err: Error, res?: string) => void) { // // ### Get Response from URL // ``` JavaScript - fetchModule.fetch("https://httpbin.org/get").then(function (r) { + fetch("https://httpbin.org/get").then(function (r) { //// Argument (r) is Response! // - TKUnit.assert(r instanceof fetchModule.Response, "Result from fetch() should be valid Response object! Actual result is: " + result); + TKUnit.assert(r instanceof Response, "Result from fetch() should be valid Response object! Actual result is: " + result); done(null); // }, function (e) { @@ -42,7 +41,7 @@ export var test_fetch_text = function (done: (err: Error, res?: string) => void) // // ### Get string from URL // ``` JavaScript - fetchModule.fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) { + fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) { //// Argument (r) is string! // TKUnit.assert(types.isString(r), "Result from text() should be string! Actual result is: " + r); @@ -64,7 +63,7 @@ export var test_fetch_json = function (done: (err: Error, res?: string) => void) // // ### Get JSON from URL // ``` JavaScript - fetchModule.fetch("https://httpbin.org/get").then(response => { return response.json(); }).then(function (r) { + fetch("https://httpbin.org/get").then(response => { return response.json(); }).then(function (r) { //// Argument (r) is JSON object! // TKUnit.assert(types.isString(JSON.stringify(r)), "Result from json() should be JSON object! Actual result is: " + r); @@ -86,7 +85,7 @@ export var test_fetch_blob = function (done: (err: Error, res?: string) => void) // // ### Get Blob from URL // ``` JavaScript - fetchModule.fetch("https://httpbin.org/get").then(response => { return response.blob(); }).then(function (r) { + fetch("https://httpbin.org/get").then(response => { return response.blob(); }).then(function (r) { //// Argument (r) is Blob object! // TKUnit.assert(r instanceof Blob, "Result from blob() should be Blob object! Actual result is: " + r); @@ -108,7 +107,7 @@ export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) = // // ### Get ArrayBuffer from URL // ``` JavaScript - fetchModule.fetch("https://httpbin.org/get").then(response => { return response.arrayBuffer(); }).then(function (r) { + fetch("https://httpbin.org/get").then(response => { return response.arrayBuffer(); }).then(function (r) { //// Argument (r) is ArrayBuffer object! // TKUnit.assert(r instanceof ArrayBuffer, "Result from arrayBuffer() should be ArrayBuffer object! Actual result is: " + r); @@ -131,7 +130,7 @@ export var test_fetch_formData = function (done: (err: Error, res?: string) => v // // ### Get FormData from URL // ``` JavaScript - fetchModule.fetch("https://httpbin.org/get").then(response => { return response.formData(); }).then(function (r) { + fetch("https://httpbin.org/get").then(response => { return response.formData(); }).then(function (r) { //// Argument (r) is FormData object! // TKUnit.assert(r instanceof FormData, "Result from formData() should be FormData object! Actual result is: " + r); @@ -151,7 +150,7 @@ export var test_fetch_fail_invalid_url = function (done) { var completed: boolean; var isReady = function () { return completed; } - fetchModule.fetch("hgfttp://httpbin.org/get").catch(function (e) { + fetch("hgfttp://httpbin.org/get").catch(function (e) { completed = true; done(null) }); @@ -162,7 +161,7 @@ export var test_fetch_response_status = function (done) { // // ### Get Response status // ``` fetch - fetchModule.fetch("https://httpbin.org/get").then(function (response) { + fetch("https://httpbin.org/get").then(function (response) { //// Argument (response) is Response! var statusCode = response.status; // @@ -189,7 +188,7 @@ export var test_fetch_response_headers = function (done) { // // ### Get response headers // ``` JavaScript - fetchModule.fetch("https://httpbin.org/get").then(function (response) { + fetch("https://httpbin.org/get").then(function (response) { //// Argument (response) is Response! // var all = response.headers.getAll(); // @@ -212,9 +211,9 @@ export var test_fetch_response_headers = function (done) { }; export var test_fetch_headers_sent = function (done) { - var result: fetchModule.Headers; + var result: Headers; - fetchModule.fetch("https://httpbin.org/get", { + fetch("https://httpbin.org/get", { method: "GET", headers: { "Content-Type": "application/json" } }).then(function (response) { @@ -236,7 +235,7 @@ export var test_fetch_post_form_data = function (done) { data.append("MyVariableOne", "ValueOne"); data.append("MyVariableTwo", "ValueTwo"); - fetchModule.fetch("https://httpbin.org/post", { + fetch("https://httpbin.org/post", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: data @@ -259,7 +258,7 @@ export var test_fetch_post_json = function (done) { // // ### Post JSON // ``` JavaScript - fetchModule.fetch("https://httpbin.org/post", { + fetch("https://httpbin.org/post", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }) diff --git a/declarations.d.ts b/declarations.d.ts index 1bb97cf9f..61295bc03 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -1,5 +1,85 @@ /* tslint:disable:no-unused-variable */ -interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { +declare class Request { + constructor(input: string|Request, init?: RequestInit); + method: string; + url: string; + headers: Headers; + context: RequestContext; + referrer: string; + mode: RequestMode; + credentials: RequestCredentials; + cache: RequestCache; +} + +interface RequestInit { + method?: string; + headers?: HeaderInit|{ [index: string]: string }; + body?: BodyInit; + mode?: RequestMode; + credentials?: RequestCredentials; + cache?: RequestCache; +} + +declare enum 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" +} + +declare enum RequestMode { "same-origin", "no-cors", "cors" } +declare enum RequestCredentials { "omit", "same-origin", "include" } +declare enum RequestCache { "default", "no-store", "reload", "no-cache", "force-cache", "only-if-cached" } + +declare 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; +} + +declare class Body { + bodyUsed: boolean; + /* + arrayBuffer(): Promise; + blob(): Promise; + */ + formData(): Promise; + json(): Promise; + text(): Promise; +} + +declare class Response extends Body { + constructor(body?: BodyInit, init?: ResponseInit); + error(): Response; + redirect(url: string, status: number): Response; + type: ResponseType; + url: string; + status: number; + ok: boolean; + statusText: string; + headers: Headers; + clone(): Response; +} + +declare enum ResponseType { "basic", "cors", "default", "error", "opaque" } + +declare class ResponseInit { + status: number; + statusText: string; + headers: HeaderInit; +} + +declare type HeaderInit = Headers|Array; +declare type BodyInit = Blob|FormData|string; +declare type RequestInfo = Request|string; + +declare function fetch(url: string, init?: RequestInit): Promise; + +interface XMLHttpRequest { send(data?: FormData): void; } diff --git a/fetch/fetch.d.ts b/fetch/fetch.d.ts deleted file mode 100644 index d67dd5ecd..000000000 --- a/fetch/fetch.d.ts +++ /dev/null @@ -1,90 +0,0 @@ -// Type definitions for fetch API -// Project: https://github.com/github/fetch -// Definitions by: Ryan Graham -// Definitions: https://github.com/borisyankov/DefinitelyTyped - -/// - -declare module "fetch" { - - class Request { - constructor(input: string|Request, init?: RequestInit); - method: string; - url: string; - headers: Headers; - context: RequestContext; - referrer: string; - mode: RequestMode; - credentials: RequestCredentials; - cache: RequestCache; - } - - interface RequestInit { - method?: string; - headers?: HeaderInit|{ [index: string]: string }; - body?: BodyInit; - mode?: RequestMode; - credentials?: RequestCredentials; - cache?: RequestCache; - } - - enum 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" - } - - enum RequestMode { "same-origin", "no-cors", "cors" } - enum RequestCredentials { "omit", "same-origin", "include" } - enum 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; - } - - class Body { - bodyUsed: boolean; -/* - arrayBuffer(): Promise; - blob(): Promise; -*/ - formData(): Promise; - json(): Promise; - text(): Promise; - } - - class Response extends Body { - constructor(body?: BodyInit, init?: ResponseInit); - error(): Response; - redirect(url: string, status: number): Response; - type: ResponseType; - url: string; - status: number; - ok: boolean; - statusText: string; - headers: Headers; - clone(): Response; - } - - enum ResponseType { "basic", "cors", "default", "error", "opaque" } - - class ResponseInit { - status: number; - statusText: string; - headers: HeaderInit; - } - - type HeaderInit = Headers|Array; - type BodyInit = Blob|FormData|string; - type RequestInfo = Request|string; - - /* tslint:disable */ - function fetch(url: string, init?: RequestInit): Promise; -} \ No newline at end of file diff --git a/globals/globals.ts b/globals/globals.ts index 66e682b20..c7b69747a 100644 --- a/globals/globals.ts +++ b/globals/globals.ts @@ -18,6 +18,9 @@ global.XMLHttpRequest = xhr.XMLHttpRequest; global.FormData = xhr.FormData; global.alert = dialogs.alert; +var fetchModule = require("fetch"); +require("utils/module-merge").merge(fetchModule, global); + export function Deprecated(target: Object, key?: string | symbol, descriptor?: any) { if (descriptor) { var originalMethod = descriptor.value;