mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
fetch exposed in global context
This commit is contained in:
@@ -300,7 +300,6 @@
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="es-collections.d.ts" />
|
||||
<TypeScriptCompile Include="es6-promise.d.ts" />
|
||||
<TypeScriptCompile Include="fetch\fetch.d.ts" />
|
||||
<TypeScriptCompile Include="file-system\file-name-resolver.d.ts" />
|
||||
<TypeScriptCompile Include="file-system\file-name-resolver.ts">
|
||||
<DependentUpon>file-name-resolver.d.ts</DependentUpon>
|
||||
@@ -1734,9 +1733,7 @@
|
||||
<ItemGroup>
|
||||
<Content Include="js-libs\esprima\README.md" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="apps\list-view-demo\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<!-- To save maintenance effort, the Visual Studio build does not rely on the Microsoft targets.
|
||||
Instead, it calls the Grunt build.
|
||||
-->
|
||||
@@ -1787,7 +1784,7 @@
|
||||
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
|
||||
</WebProjectProperties>
|
||||
</FlavorProperties>
|
||||
<UserProperties ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2linear-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" />
|
||||
<UserProperties ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2linear-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
</Project>
|
||||
@@ -1,6 +1,5 @@
|
||||
/* tslint:disable:no-unused-variable */
|
||||
import TKUnit = require("./TKUnit");
|
||||
import fetchModule = require("fetch");
|
||||
import types = require("utils/types");
|
||||
|
||||
// <snippet module="fetch" title="fetch">
|
||||
@@ -12,7 +11,7 @@ import types = require("utils/types");
|
||||
// </snippet>
|
||||
|
||||
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) {
|
||||
// <snippet module="fetch" title="fetch">
|
||||
// ### 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!
|
||||
// <hide>
|
||||
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);
|
||||
// </hide>
|
||||
}, function (e) {
|
||||
@@ -42,7 +41,7 @@ export var test_fetch_text = function (done: (err: Error, res?: string) => void)
|
||||
// <snippet module="fetch" title="fetch">
|
||||
// ### 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!
|
||||
// <hide>
|
||||
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)
|
||||
// <snippet module="fetch" title="fetch">
|
||||
// ### 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!
|
||||
// <hide>
|
||||
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)
|
||||
// <snippet module="fetch" title="fetch">
|
||||
// ### 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!
|
||||
// <hide>
|
||||
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) =
|
||||
// <snippet module="fetch" title="fetch">
|
||||
// ### 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!
|
||||
// <hide>
|
||||
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
|
||||
// <snippet module="fetch" title="fetch">
|
||||
// ### 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!
|
||||
// <hide>
|
||||
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) {
|
||||
// <snippet module="fetch" title="fetch">
|
||||
// ### 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;
|
||||
// <hide>
|
||||
@@ -189,7 +188,7 @@ export var test_fetch_response_headers = function (done) {
|
||||
// <snippet module="fetch" title="fetch">
|
||||
// ### 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();
|
||||
// <hide>
|
||||
@@ -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) {
|
||||
// <snippet module="fetch" title="fetch">
|
||||
// ### 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" })
|
||||
|
||||
82
declarations.d.ts
vendored
82
declarations.d.ts
vendored
@@ -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<string>;
|
||||
has(name: string): boolean;
|
||||
set(name: string, value: string): void;
|
||||
}
|
||||
|
||||
declare class Body {
|
||||
bodyUsed: boolean;
|
||||
/*
|
||||
arrayBuffer(): Promise<ArrayBuffer>;
|
||||
blob(): Promise<Blob>;
|
||||
*/
|
||||
formData(): Promise<FormData>;
|
||||
json(): Promise<any>;
|
||||
text(): Promise<string>;
|
||||
}
|
||||
|
||||
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<string>;
|
||||
declare type BodyInit = Blob|FormData|string;
|
||||
declare type RequestInfo = Request|string;
|
||||
|
||||
declare function fetch(url: string, init?: RequestInit): Promise<Response>;
|
||||
|
||||
interface XMLHttpRequest {
|
||||
send(data?: FormData): void;
|
||||
}
|
||||
|
||||
|
||||
90
fetch/fetch.d.ts
vendored
90
fetch/fetch.d.ts
vendored
@@ -1,90 +0,0 @@
|
||||
// Type definitions for fetch API
|
||||
// Project: https://github.com/github/fetch
|
||||
// Definitions by: Ryan Graham <https://github.com/ryan-codingintrigue>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference path="../es6-promise.d.ts" />
|
||||
|
||||
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<string>;
|
||||
has(name: string): boolean;
|
||||
set(name: string, value: string): void;
|
||||
}
|
||||
|
||||
class Body {
|
||||
bodyUsed: boolean;
|
||||
/*
|
||||
arrayBuffer(): Promise<ArrayBuffer>;
|
||||
blob(): Promise<Blob>;
|
||||
*/
|
||||
formData(): Promise<FormData>;
|
||||
json(): Promise<any>;
|
||||
text(): Promise<string>;
|
||||
}
|
||||
|
||||
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<string>;
|
||||
type BodyInit = Blob|FormData|string;
|
||||
type RequestInfo = Request|string;
|
||||
|
||||
/* tslint:disable */
|
||||
function fetch(url: string, init?: RequestInit): Promise<Response>;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user