@klippa/nativescript-http
Version:
The best way to do HTTP requests in NativeScript, a drop-in replacement for the core HTTP with important improvements and additions like proper connection pooling, form data support and certificate pinning
49 lines • 1.83 kB
JavaScript
import { Injectable } from "@angular/core";
import { HttpResponse, HttpErrorResponse, HttpXhrBackend } from "@angular/common/http";
import { NSFileSystem } from "@nativescript/angular";
import { isLocalRequest, processLocalFileRequest } from "./http-utils";
import { NSHTTPXhr } from "./xhr";
import * as i0 from "@angular/core";
import * as i1 from "./xhr";
import * as i2 from "@nativescript/angular";
export class NsHttpBackEnd extends HttpXhrBackend {
constructor(xhrFactory, nsFileSystem) {
super(xhrFactory);
this.nsFileSystem = nsFileSystem;
}
handle(req) {
let result;
if (isLocalRequest(req.url)) {
result = this.handleLocalFileRequest(req.url);
}
else {
result = super.handle(req);
}
return result;
}
handleLocalFileRequest(url) {
return processLocalFileRequest(url, this.nsFileSystem, createSuccessResponse, createErrorResponse);
}
}
NsHttpBackEnd.ɵfac = function NsHttpBackEnd_Factory(t) { return new (t || NsHttpBackEnd)(i0.ɵɵinject(i1.NSHTTPXhr), i0.ɵɵinject(i2.NSFileSystem)); };
NsHttpBackEnd.ɵprov = i0.ɵɵdefineInjectable({ token: NsHttpBackEnd, factory: NsHttpBackEnd.ɵfac });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(NsHttpBackEnd, [{
type: Injectable
}], function () { return [{ type: i1.NSHTTPXhr }, { type: i2.NSFileSystem }]; }, null); })();
function createSuccessResponse(url, body, status) {
return new HttpResponse({
url,
body,
status,
statusText: "OK"
});
}
function createErrorResponse(url, body, status) {
return new HttpErrorResponse({
url,
error: body,
status,
statusText: "ERROR"
});
}
//# sourceMappingURL=ns-http-backend.js.map