Skip to content

feat(Http): Expand support for request on local files #982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 46 additions & 18 deletions nativescript-angular/http/ns-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from "@angular/core";
import {
Http,
ConnectionBackend,
Request,
RequestOptions,
RequestOptionsArgs,
ResponseOptions,
Expand All @@ -24,32 +25,59 @@ export class NSHttp extends Http {
super(backend, defaultOptions);
}

/**
* Performs a request with `request` http method.
*/
request(req: string | Request, options?: RequestOptionsArgs): Observable<Response> {
const urlString = typeof req === "string" ? req : req.url;
if (isLocalRequest(urlString)) {
return this._requestLocalUrl(urlString);
} else {
return super.request(req, options);
}
}

/**
* Performs a request with `get` http method.
* Uses a local file if `~/` resource is requested.
*/
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
if (url.indexOf("~") === 0 || url.indexOf("/") === 0) {
// normalize url
url = url.replace("~", "").replace("/", "");
// request from local app resources
return Observable.fromPromise<Response>(new Promise((resolve, reject) => {
let app = this.nsFileSystem.currentApp();
let localFile = app.getFile(url);
if (localFile) {
localFile.readText().then((data) => {
resolve(responseOptions(data, 200, url));
}, (err: Object) => {
reject(responseOptions(err, 400, url));
});
} else {
reject(responseOptions("Not Found", 404, url));
}
}));
if (isLocalRequest(url)) {
return this._requestLocalUrl(url);
} else {
return super.get(url, options);
}
}

/**
* Uses a local file if `~/` resource is requested.
* @param url
*/
private _requestLocalUrl(url: string): Observable<Response> {
// normalize url
url = normalizeLocalUrl(url);
// request from local app resources
return Observable.fromPromise<Response>(new Promise((resolve, reject) => {
let app = this.nsFileSystem.currentApp();
let localFile = app.getFile(url);
if (localFile) {
localFile.readText().then((data) => {
resolve(responseOptions(data, 200, url));
}, (err: Object) => {
reject(responseOptions(err, 400, url));
});
} else {
reject(responseOptions("Not Found", 404, url));
}
}));
}
}

function isLocalRequest(url: string): boolean {
return url.indexOf("~") === 0 || url.indexOf("/") === 0;
}

function normalizeLocalUrl(url: string): string {
return url.replace("~", "").replace("/", "");
}

function responseOptions(body: string | Object, status: number, url: string): Response {
Expand Down
21 changes: 20 additions & 1 deletion tests/app/tests/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
inject,
} from "@angular/core/testing";
import {ReflectiveInjector} from "@angular/core";
import {BaseRequestOptions, ConnectionBackend, Http, Response, ResponseOptions} from "@angular/http";
import {Request, BaseRequestOptions, ConnectionBackend, Http, Response, ResponseOptions} from "@angular/http";
import "rxjs/add/operator/map";
import {MockBackend} from "@angular/http/testing";
import {NSHttp} from "nativescript-angular/http/ns-http";
Expand Down Expand Up @@ -45,6 +45,25 @@ describe("Http", () => {
});
});

it("request method should work with local files prefixed with '~'", () => {
http.request("~/test.json").map(res => res.json()).subscribe((response: any) => {
assert.strictEqual(3, response.length);
assert.strictEqual("Alex", response[0].name);
});
});

it("request method using Request type should work with local files prefixed with '~'", () => {
const url = "~/test.json";
const req = new Request({
method: 'GET',
url
});
http.request(req).map(res => res.json()).subscribe((response: any) => {
assert.strictEqual(3, response.length);
assert.strictEqual("Alex", response[0].name);
});
});

it("should work with local files prefixed with '/'", () => {
http.get("/test.json").map(res => res.json()).subscribe((response: any) => {
assert.strictEqual(3, response.length);
Expand Down