Skip to content

Commit 20790c4

Browse files
author
Vladimir Enchev
committed
Merge pull request NativeScript#460 from NativeScript/xhr2
FormData support added and fetch exposed in global context
2 parents dea2e69 + 78781b9 commit 20790c4

File tree

16 files changed

+679
-575
lines changed

16 files changed

+679
-575
lines changed

CrossPlatformModules.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
<TypeScriptCompile Include="apps\tests\frame-tests.ts" />
183183
<TypeScriptCompile Include="apps\tests\gestures-tests.ts" />
184184
<TypeScriptCompile Include="apps\tests\fetch-tests.ts" />
185+
<TypeScriptCompile Include="apps\tests\xhr-tests.ts" />
185186
<TypeScriptCompile Include="apps\tests\layouts\dock-layout-tests.ts" />
186187
<TypeScriptCompile Include="apps\tests\pages\app.ts" />
187188
<TypeScriptCompile Include="apps\tests\pages\file-load-test.ts" />
@@ -299,7 +300,6 @@
299300
</TypeScriptCompile>
300301
<TypeScriptCompile Include="es-collections.d.ts" />
301302
<TypeScriptCompile Include="es6-promise.d.ts" />
302-
<TypeScriptCompile Include="fetch\fetch.d.ts" />
303303
<TypeScriptCompile Include="file-system\file-name-resolver.d.ts" />
304304
<TypeScriptCompile Include="file-system\file-name-resolver.ts">
305305
<DependentUpon>file-name-resolver.d.ts</DependentUpon>
@@ -1032,6 +1032,7 @@
10321032
</TypeScriptCompile>
10331033
<TypeScriptCompile Include="apps\xml-demo\app.ts" />
10341034
<TypeScriptCompile Include="apps\xml-demo\mainPage.ts" />
1035+
<TypeScriptCompile Include="xhr\xhr.ts" />
10351036
<TypeScriptCompile Include="xml\xml.d.ts" />
10361037
<TypeScriptCompile Include="data\observable-array\observable-array.d.ts" />
10371038
<TypeScriptCompile Include="apps\tests\observable-array-tests.ts" />
@@ -1688,6 +1689,10 @@
16881689
<Content Include="apps\list-view-demo\package.json">
16891690
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16901691
</Content>
1692+
<Content Include="xhr\package.json">
1693+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1694+
</Content>
1695+
<Content Include="xhr\Readme.md" />
16911696
<None Include="js-libs\esprima\LICENSE.BSD" />
16921697
<Content Include="source-control.md" />
16931698
<Content Include="ui\segmented-bar\package.json">

apps/tests/fetch-tests.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* tslint:disable:no-unused-variable */
22
import TKUnit = require("./TKUnit");
3-
import fetchModule = require("fetch");
43
import types = require("utils/types");
54

65
// <snippet module="fetch" title="fetch">
@@ -12,18 +11,18 @@ import types = require("utils/types");
1211
// </snippet>
1312

1413
export var test_fetch_defined = function () {
15-
TKUnit.assert(types.isDefined((fetchModule.fetch)), "Method fetch() should be defined!");
14+
TKUnit.assert(types.isDefined((fetch)), "Method fetch() should be defined!");
1615
};
1716

1817
export var test_fetch = function (done: (err: Error, res?: string) => void) {
1918
var result;
2019
// <snippet module="fetch" title="fetch">
2120
// ### Get Response from URL
2221
// ``` JavaScript
23-
fetchModule.fetch("https://httpbin.org/get").then(function (r) {
22+
fetch("https://httpbin.org/get").then(function (r) {
2423
//// Argument (r) is Response!
2524
// <hide>
26-
TKUnit.assert(r instanceof fetchModule.Response, "Result from fetch() should be valid Response object! Actual result is: " + result);
25+
TKUnit.assert(r instanceof Response, "Result from fetch() should be valid Response object! Actual result is: " + result);
2726
done(null);
2827
// </hide>
2928
}, function (e) {
@@ -42,7 +41,7 @@ export var test_fetch_text = function (done: (err: Error, res?: string) => void)
4241
// <snippet module="fetch" title="fetch">
4342
// ### Get string from URL
4443
// ``` JavaScript
45-
fetchModule.fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) {
44+
fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) {
4645
//// Argument (r) is string!
4746
// <hide>
4847
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)
6463
// <snippet module="fetch" title="fetch">
6564
// ### Get JSON from URL
6665
// ``` JavaScript
67-
fetchModule.fetch("https://httpbin.org/get").then(response => { return response.json(); }).then(function (r) {
66+
fetch("https://httpbin.org/get").then(response => { return response.json(); }).then(function (r) {
6867
//// Argument (r) is JSON object!
6968
// <hide>
7069
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)
8685
// <snippet module="fetch" title="fetch">
8786
// ### Get Blob from URL
8887
// ``` JavaScript
89-
fetchModule.fetch("https://httpbin.org/get").then(response => { return response.blob(); }).then(function (r) {
88+
fetch("https://httpbin.org/get").then(response => { return response.blob(); }).then(function (r) {
9089
//// Argument (r) is Blob object!
9190
// <hide>
9291
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) =
108107
// <snippet module="fetch" title="fetch">
109108
// ### Get ArrayBuffer from URL
110109
// ``` JavaScript
111-
fetchModule.fetch("https://httpbin.org/get").then(response => { return response.arrayBuffer(); }).then(function (r) {
110+
fetch("https://httpbin.org/get").then(response => { return response.arrayBuffer(); }).then(function (r) {
112111
//// Argument (r) is ArrayBuffer object!
113112
// <hide>
114113
TKUnit.assert(r instanceof ArrayBuffer, "Result from arrayBuffer() should be ArrayBuffer object! Actual result is: " + r);
@@ -123,14 +122,15 @@ export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) =
123122
// ```
124123
// </snippet>
125124
};
125+
*/
126126

127127
export var test_fetch_formData = function (done: (err: Error, res?: string) => void) {
128128
var result;
129129

130130
// <snippet module="fetch" title="fetch">
131131
// ### Get FormData from URL
132132
// ``` JavaScript
133-
fetchModule.fetch("https://httpbin.org/get").then(response => { return response.formData(); }).then(function (r) {
133+
fetch("https://httpbin.org/get").then(response => { return response.formData(); }).then(function (r) {
134134
//// Argument (r) is FormData object!
135135
// <hide>
136136
TKUnit.assert(r instanceof FormData, "Result from formData() should be FormData object! Actual result is: " + r);
@@ -145,12 +145,12 @@ export var test_fetch_formData = function (done: (err: Error, res?: string) => v
145145
// ```
146146
// </snippet>
147147
};
148-
*/
148+
149149
export var test_fetch_fail_invalid_url = function (done) {
150150
var completed: boolean;
151151
var isReady = function () { return completed; }
152152

153-
fetchModule.fetch("hgfttp://httpbin.org/get").catch(function (e) {
153+
fetch("hgfttp://httpbin.org/get").catch(function (e) {
154154
completed = true;
155155
done(null)
156156
});
@@ -161,7 +161,7 @@ export var test_fetch_response_status = function (done) {
161161
// <snippet module="fetch" title="fetch">
162162
// ### Get Response status
163163
// ``` fetch
164-
fetchModule.fetch("https://httpbin.org/get").then(function (response) {
164+
fetch("https://httpbin.org/get").then(function (response) {
165165
//// Argument (response) is Response!
166166
var statusCode = response.status;
167167
// <hide>
@@ -188,7 +188,7 @@ export var test_fetch_response_headers = function (done) {
188188
// <snippet module="fetch" title="fetch">
189189
// ### Get response headers
190190
// ``` JavaScript
191-
fetchModule.fetch("https://httpbin.org/get").then(function (response) {
191+
fetch("https://httpbin.org/get").then(function (response) {
192192
//// Argument (response) is Response!
193193
// var all = response.headers.getAll();
194194
// <hide>
@@ -211,9 +211,9 @@ export var test_fetch_response_headers = function (done) {
211211
};
212212

213213
export var test_fetch_headers_sent = function (done) {
214-
var result: fetchModule.Headers;
214+
var result: Headers;
215215

216-
fetchModule.fetch("https://httpbin.org/get", {
216+
fetch("https://httpbin.org/get", {
217217
method: "GET",
218218
headers: { "Content-Type": "application/json" }
219219
}).then(function (response) {
@@ -231,16 +231,19 @@ export var test_fetch_headers_sent = function (done) {
231231
};
232232

233233
export var test_fetch_post_form_data = function (done) {
234-
fetchModule.fetch("https://httpbin.org/post", {
234+
var data = new FormData();
235+
data.append("MyVariableOne", "ValueOne");
236+
data.append("MyVariableTwo", "ValueTwo");
237+
238+
fetch("https://httpbin.org/post", {
235239
method: "POST",
236240
headers: { "Content-Type": "application/x-www-form-urlencoded" },
237-
body: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
241+
body: data
238242
}).then(r => {
239-
// return r.formData(); Uncomment this when FormData is available!
240-
return r.json();
243+
return r.formData();
241244
}).then(function (r) {
242245
try {
243-
TKUnit.assert(r.form["MyVariableOne"] === "ValueOne" && r.form["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Actual result is: " + r.form);
246+
TKUnit.assert(r instanceof FormData, "Content not sent/received properly! Actual result is: " + r);
244247
done(null);
245248
}
246249
catch (err) {
@@ -255,7 +258,7 @@ export var test_fetch_post_json = function (done) {
255258
// <snippet module="fetch" title="fetch">
256259
// ### Post JSON
257260
// ``` JavaScript
258-
fetchModule.fetch("https://httpbin.org/post", {
261+
fetch("https://httpbin.org/post", {
259262
method: "POST",
260263
headers: { "Content-Type": "application/json" },
261264
body: JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })

0 commit comments

Comments
 (0)