Skip to content

Commit b95184f

Browse files
NathanWalkerAlexander Vakrilov
authored and
Alexander Vakrilov
committed
feat(Http): expand support for request on local files (NativeScript#982)
1 parent e5cc581 commit b95184f

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

nativescript-angular/http/ns-http.ts

+46-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Injectable } from "@angular/core";
22
import {
33
Http,
44
ConnectionBackend,
5+
Request,
56
RequestOptions,
67
RequestOptionsArgs,
78
ResponseOptions,
@@ -24,32 +25,59 @@ export class NSHttp extends Http {
2425
super(backend, defaultOptions);
2526
}
2627

28+
/**
29+
* Performs a request with `request` http method.
30+
*/
31+
request(req: string | Request, options?: RequestOptionsArgs): Observable<Response> {
32+
const urlString = typeof req === "string" ? req : req.url;
33+
if (isLocalRequest(urlString)) {
34+
return this._requestLocalUrl(urlString);
35+
} else {
36+
return super.request(req, options);
37+
}
38+
}
39+
2740
/**
2841
* Performs a request with `get` http method.
29-
* Uses a local file if `~/` resource is requested.
3042
*/
3143
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
32-
if (url.indexOf("~") === 0 || url.indexOf("/") === 0) {
33-
// normalize url
34-
url = url.replace("~", "").replace("/", "");
35-
// request from local app resources
36-
return Observable.fromPromise<Response>(new Promise((resolve, reject) => {
37-
let app = this.nsFileSystem.currentApp();
38-
let localFile = app.getFile(url);
39-
if (localFile) {
40-
localFile.readText().then((data) => {
41-
resolve(responseOptions(data, 200, url));
42-
}, (err: Object) => {
43-
reject(responseOptions(err, 400, url));
44-
});
45-
} else {
46-
reject(responseOptions("Not Found", 404, url));
47-
}
48-
}));
44+
if (isLocalRequest(url)) {
45+
return this._requestLocalUrl(url);
4946
} else {
5047
return super.get(url, options);
5148
}
5249
}
50+
51+
/**
52+
* Uses a local file if `~/` resource is requested.
53+
* @param url
54+
*/
55+
private _requestLocalUrl(url: string): Observable<Response> {
56+
// normalize url
57+
url = normalizeLocalUrl(url);
58+
// request from local app resources
59+
return Observable.fromPromise<Response>(new Promise((resolve, reject) => {
60+
let app = this.nsFileSystem.currentApp();
61+
let localFile = app.getFile(url);
62+
if (localFile) {
63+
localFile.readText().then((data) => {
64+
resolve(responseOptions(data, 200, url));
65+
}, (err: Object) => {
66+
reject(responseOptions(err, 400, url));
67+
});
68+
} else {
69+
reject(responseOptions("Not Found", 404, url));
70+
}
71+
}));
72+
}
73+
}
74+
75+
function isLocalRequest(url: string): boolean {
76+
return url.indexOf("~") === 0 || url.indexOf("/") === 0;
77+
}
78+
79+
function normalizeLocalUrl(url: string): string {
80+
return url.replace("~", "").replace("/", "");
5381
}
5482

5583
function responseOptions(body: string | Object, status: number, url: string): Response {

tests/app/tests/http.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
inject,
66
} from "@angular/core/testing";
77
import {ReflectiveInjector} from "@angular/core";
8-
import {BaseRequestOptions, ConnectionBackend, Http, Response, ResponseOptions} from "@angular/http";
8+
import {Request, BaseRequestOptions, ConnectionBackend, Http, Response, ResponseOptions} from "@angular/http";
99
import "rxjs/add/operator/map";
1010
import {MockBackend} from "@angular/http/testing";
1111
import {NSHttp} from "nativescript-angular/http/ns-http";
@@ -45,6 +45,25 @@ describe("Http", () => {
4545
});
4646
});
4747

48+
it("request method should work with local files prefixed with '~'", () => {
49+
http.request("~/test.json").map(res => res.json()).subscribe((response: any) => {
50+
assert.strictEqual(3, response.length);
51+
assert.strictEqual("Alex", response[0].name);
52+
});
53+
});
54+
55+
it("request method using Request type should work with local files prefixed with '~'", () => {
56+
const url = "~/test.json";
57+
const req = new Request({
58+
method: 'GET',
59+
url
60+
});
61+
http.request(req).map(res => res.json()).subscribe((response: any) => {
62+
assert.strictEqual(3, response.length);
63+
assert.strictEqual("Alex", response[0].name);
64+
});
65+
});
66+
4867
it("should work with local files prefixed with '/'", () => {
4968
http.get("/test.json").map(res => res.json()).subscribe((response: any) => {
5069
assert.strictEqual(3, response.length);

0 commit comments

Comments
 (0)