Repository navigation
Expand file tree
/
Copy pathns-http.ts
More file actions
63 lines (59 loc) · 1.81 KB
/
Copy pathns-http.ts
File metadata and controls
63 lines (59 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Injectable } from "@angular/core";
import {
Http,
ConnectionBackend,
RequestOptions,
RequestOptionsArgs,
ResponseOptions,
ResponseType,
Response
} from "@angular/http";
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/fromPromise";
import { NSFileSystem } from "../file-system/ns-file-system";
export class NSXSRFStrategy {
public configureRequest(_req: any) {
// noop
}
}
@Injectable()
export class NSHttp extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private nsFileSystem: NSFileSystem) {
super(backend, defaultOptions);
}
/**
* 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));
}
}));
} else {
return super.get(url, options);
}
}
}
function responseOptions(body: string | Object, status: number, url: string): Response {
return new Response(new ResponseOptions({
body: body,
status: status,
statusText: "OK",
type: status === 200 ? ResponseType.Default : ResponseType.Error,
url: url
}));
}