Skip to content

Commit a4d879f

Browse files
author
Vladimir Enchev
committed
Merge pull request NativeScript#454 from NativeScript/fetch
Fetch API polyfill
2 parents 782c390 + 053d7dd commit a4d879f

File tree

10 files changed

+751
-0
lines changed

10 files changed

+751
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dist/
1111
*.js
1212
!gruntfile.js
1313
!js-libs/**/*.*
14+
!fetch/**/*.*
1415
!apps/TelerikNEXT/lib/**/*.*
1516
CrossPlatformModules.sln.ide/
1617
*.suo

CrossPlatformModules.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
<TypeScriptCompile Include="apps\tests\file-name-resolver-tests\file-name-resolver-tests.ts" />
172172
<TypeScriptCompile Include="apps\tests\frame-tests.ts" />
173173
<TypeScriptCompile Include="apps\tests\gestures-tests.ts" />
174+
<TypeScriptCompile Include="apps\tests\fetch-tests.ts" />
174175
<TypeScriptCompile Include="apps\tests\layouts\dock-layout-tests.ts" />
175176
<TypeScriptCompile Include="apps\tests\pages\app.ts" />
176177
<TypeScriptCompile Include="apps\tests\pages\file-load-test.ts" />
@@ -288,6 +289,7 @@
288289
</TypeScriptCompile>
289290
<TypeScriptCompile Include="es-collections.d.ts" />
290291
<TypeScriptCompile Include="es6-promise.d.ts" />
292+
<TypeScriptCompile Include="fetch\fetch.d.ts" />
291293
<TypeScriptCompile Include="file-system\file-name-resolver.d.ts" />
292294
<TypeScriptCompile Include="file-system\file-name-resolver.ts">
293295
<DependentUpon>file-name-resolver.d.ts</DependentUpon>
@@ -809,6 +811,7 @@
809811
<Content Include="apps\ui-tests-app\pages\text\label.xml" />
810812
<Content Include="apps\ui-tests-app\pages\text\button.xml" />
811813
<Content Include="apps\ui-tests-app\web-view\webview.xml" />
814+
<Content Include="fetch\fetch.js" />
812815
<Content Include="js-libs\reworkcss-value\reworkcss-value.js" />
813816
<Content Include="ui\layouts\stack-layout\package.json">
814817
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@@ -1670,6 +1673,8 @@
16701673
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16711674
</Content>
16721675
<Content Include="apps\action-bar-demo\package.json" />
1676+
<Content Include="fetch\package.json" />
1677+
<Content Include="fetch\README.md" />
16731678
<None Include="js-libs\esprima\LICENSE.BSD" />
16741679
<Content Include="source-control.md" />
16751680
<Content Include="ui\segmented-bar\package.json">

apps/tests/fetch-tests.ts

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
/* tslint:disable:no-unused-variable */
2+
import TKUnit = require("./TKUnit");
3+
import fetchModule = require("fetch");
4+
import types = require("utils/types");
5+
6+
// <snippet module="fetch" title="fetch">
7+
// # Fetch module
8+
// Using fetch methods requires to load "fetch" module.
9+
// ``` JavaScript
10+
// var fetch = require("fetch");
11+
// ```
12+
// </snippet>
13+
14+
export var test_fetch_defined = function () {
15+
TKUnit.assert(types.isDefined((fetchModule.fetch)), "Method fetch() should be defined!");
16+
};
17+
18+
export var test_fetch = function (done: (err: Error, res?: string) => void) {
19+
var result;
20+
// <snippet module="fetch" title="fetch">
21+
// ### Get Response from URL
22+
// ``` JavaScript
23+
fetchModule.fetch("https://httpbin.org/get").then(function (r) {
24+
//// Argument (r) is Response!
25+
// <hide>
26+
TKUnit.assert(r instanceof fetchModule.Response, "Result from fetch() should be valid Response object! Actual result is: " + result);
27+
done(null);
28+
// </hide>
29+
}, function (e) {
30+
//// Argument (e) is Error!
31+
// <hide>
32+
done(e);
33+
// </hide>
34+
});
35+
// ```
36+
// </snippet>
37+
};
38+
39+
export var test_fetch_text = function (done: (err: Error, res?: string) => void) {
40+
var result;
41+
42+
// <snippet module="fetch" title="fetch">
43+
// ### Get string from URL
44+
// ``` JavaScript
45+
fetchModule.fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) {
46+
//// Argument (r) is string!
47+
// <hide>
48+
TKUnit.assert(types.isString(r), "Result from text() should be string! Actual result is: " + r);
49+
done(null);
50+
// </hide>
51+
}, function (e) {
52+
//// Argument (e) is Error!
53+
// <hide>
54+
done(e);
55+
// </hide>
56+
});
57+
// ```
58+
// </snippet>
59+
};
60+
61+
export var test_fetch_json = function (done: (err: Error, res?: string) => void) {
62+
var result;
63+
64+
// <snippet module="fetch" title="fetch">
65+
// ### Get JSON from URL
66+
// ``` JavaScript
67+
fetchModule.fetch("https://httpbin.org/get").then(response => { return response.json(); }).then(function (r) {
68+
//// Argument (r) is JSON object!
69+
// <hide>
70+
TKUnit.assert(types.isString(JSON.stringify(r)), "Result from json() should be JSON object! Actual result is: " + r);
71+
done(null);
72+
// </hide>
73+
}, function (e) {
74+
//// Argument (e) is Error!
75+
// <hide>
76+
done(e);
77+
// </hide>
78+
});
79+
// ```
80+
// </snippet>
81+
};
82+
/*
83+
export var test_fetch_blob = function (done: (err: Error, res?: string) => void) {
84+
var result;
85+
86+
// <snippet module="fetch" title="fetch">
87+
// ### Get Blob from URL
88+
// ``` JavaScript
89+
fetchModule.fetch("https://httpbin.org/get").then(response => { return response.blob(); }).then(function (r) {
90+
//// Argument (r) is Blob object!
91+
// <hide>
92+
TKUnit.assert(r instanceof Blob, "Result from blob() should be Blob object! Actual result is: " + r);
93+
done(null);
94+
// </hide>
95+
}, function (e) {
96+
//// Argument (e) is Error!
97+
// <hide>
98+
done(e);
99+
// </hide>
100+
});
101+
// ```
102+
// </snippet>
103+
};
104+
105+
export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) => void) {
106+
var result;
107+
108+
// <snippet module="fetch" title="fetch">
109+
// ### Get ArrayBuffer from URL
110+
// ``` JavaScript
111+
fetchModule.fetch("https://httpbin.org/get").then(response => { return response.arrayBuffer(); }).then(function (r) {
112+
//// Argument (r) is ArrayBuffer object!
113+
// <hide>
114+
TKUnit.assert(r instanceof ArrayBuffer, "Result from arrayBuffer() should be ArrayBuffer object! Actual result is: " + r);
115+
done(null);
116+
// </hide>
117+
}, function (e) {
118+
//// Argument (e) is Error!
119+
// <hide>
120+
done(e);
121+
// </hide>
122+
});
123+
// ```
124+
// </snippet>
125+
};
126+
127+
export var test_fetch_formData = function (done: (err: Error, res?: string) => void) {
128+
var result;
129+
130+
// <snippet module="fetch" title="fetch">
131+
// ### Get FormData from URL
132+
// ``` JavaScript
133+
fetchModule.fetch("https://httpbin.org/get").then(response => { return response.formData(); }).then(function (r) {
134+
//// Argument (r) is FormData object!
135+
// <hide>
136+
TKUnit.assert(r instanceof FormData, "Result from formData() should be FormData object! Actual result is: " + r);
137+
done(null);
138+
// </hide>
139+
}, function (e) {
140+
//// Argument (e) is Error!
141+
// <hide>
142+
done(e);
143+
// </hide>
144+
});
145+
// ```
146+
// </snippet>
147+
};
148+
*/
149+
export var test_fetch_fail_invalid_url = function (done) {
150+
var completed: boolean;
151+
var isReady = function () { return completed; }
152+
153+
fetchModule.fetch("hgfttp://httpbin.org/get").catch(function (e) {
154+
completed = true;
155+
done(null)
156+
});
157+
};
158+
159+
export var test_fetch_response_status = function (done) {
160+
161+
// <snippet module="fetch" title="fetch">
162+
// ### Get Response status
163+
// ``` fetch
164+
fetchModule.fetch("https://httpbin.org/get").then(function (response) {
165+
//// Argument (response) is Response!
166+
var statusCode = response.status;
167+
// <hide>
168+
try {
169+
TKUnit.assert(types.isDefined(statusCode), "response.status should be defined! Actual result is: " + statusCode);
170+
done(null);
171+
}
172+
catch (err) {
173+
done(err);
174+
}
175+
// </hide>
176+
}, function (e) {
177+
//// Argument (e) is Error!
178+
// <hide>
179+
done(e);
180+
// </hide>
181+
});
182+
// ```
183+
// </snippet>
184+
};
185+
186+
export var test_fetch_response_headers = function (done) {
187+
188+
// <snippet module="fetch" title="fetch">
189+
// ### Get response headers
190+
// ``` JavaScript
191+
fetchModule.fetch("https://httpbin.org/get").then(function (response) {
192+
//// Argument (response) is Response!
193+
// var all = response.headers.getAll();
194+
// <hide>
195+
try {
196+
TKUnit.assert(types.isDefined(response.headers), "response.headers should be defined! Actual result is: " + response.headers);
197+
done(null);
198+
}
199+
catch (err) {
200+
done(err);
201+
}
202+
// </hide>
203+
}, function (e) {
204+
//// Argument (e) is Error!
205+
// <hide>
206+
done(e);
207+
// </hide>
208+
});
209+
// ```
210+
// </snippet>
211+
};
212+
213+
export var test_fetch_headers_sent = function (done) {
214+
var result: fetchModule.Headers;
215+
216+
fetchModule.fetch("https://httpbin.org/get", {
217+
method: "GET",
218+
headers: { "Content-Type": "application/json" }
219+
}).then(function (response) {
220+
result = response.headers;
221+
try {
222+
TKUnit.assert(result.get("Content-Type") === "application/json", "Headers not sent/received properly! Actual result is: " + result);
223+
done(null);
224+
}
225+
catch (err) {
226+
done(err);
227+
}
228+
}, function (e) {
229+
done(e);
230+
});
231+
};
232+
233+
export var test_fetch_post_form_data = function (done) {
234+
fetchModule.fetch("https://httpbin.org/post", {
235+
method: "POST",
236+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
237+
body: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
238+
}).then(r => {
239+
// return r.formData(); Uncomment this when FormData is available!
240+
return r.json();
241+
}).then(function (r) {
242+
try {
243+
TKUnit.assert(r.form["MyVariableOne"] === "ValueOne" && r.form["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Actual result is: " + r.form);
244+
done(null);
245+
}
246+
catch (err) {
247+
done(err);
248+
}
249+
}, function (e) {
250+
done(e);
251+
});
252+
};
253+
254+
export var test_fetch_post_json = function (done) {
255+
// <snippet module="fetch" title="fetch">
256+
// ### Post JSON
257+
// ``` JavaScript
258+
fetchModule.fetch("https://httpbin.org/post", {
259+
method: "POST",
260+
headers: { "Content-Type": "application/json" },
261+
body: JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })
262+
}).then(r => { return r.json(); }).then(function (r) {
263+
// <hide>
264+
try {
265+
TKUnit.assert(r.json["MyVariableOne"] === "ValueOne" && r.json["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Actual result is: " + r.json);
266+
done(null);
267+
}
268+
catch (err) {
269+
done(err);
270+
}
271+
// </hide>
272+
// console.log(result);
273+
}, function (e) {
274+
// <hide>
275+
done(e);
276+
// </hide>
277+
// console.log("Error occurred " + e);
278+
});
279+
// ```
280+
// </snippet>
281+
};

apps/tests/testRunner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ allTests["STYLE-PROPERTIES"] = require("./ui/style/style-properties-tests");
3636
allTests["SCROLL-VIEW"] = require("./ui/scroll-view/scroll-view-tests");
3737
allTests["FILE SYSTEM"] = require("./file-system-tests");
3838
allTests["HTTP"] = require("./http-tests");
39+
allTests["FETCH"] = require("./fetch-tests");
3940
allTests["APPLICATION SETTINGS"] = require("./application-settings-tests");
4041
allTests["IMAGE SOURCE"] = require("./image-source-tests");
4142
allTests["TIMER"] = require("./timer-tests");

fetch/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2014-2015 GitHub, Inc.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

fetch/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Isomorphic Fetch Implementation
2+
3+
## status
4+
5+
WIP
6+
7+
## motivation
8+
9+
implementation of [fetch API](https://fetch.spec.whatwg.org/) in pure javascript.
10+
polyfill for browser, and implemnt for node.js.
11+
make network http access isomorphic.
12+
13+
14+
## License
15+
16+
The MIT License (MIT)
17+
Copyright (c) 2013 Jxck

0 commit comments

Comments
 (0)