ionic-native-http-connection-backend
Version:
A solution to CORS problem with Ionic and WKWebView
110 lines • 5.51 kB
JavaScript
import { HttpErrorResponse, HttpHeaders, HttpResponse, } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { getRequestOptions } from './utils/request-options';
import * as i0 from "@angular/core";
import * as i1 from "@awesome-cordova-plugins/http/ngx";
var XSSI_PREFIX = /^\)]}',?\n/;
var NativeHttpBackend = /** @class */ (function () {
function NativeHttpBackend(nativeHttp) {
this.nativeHttp = nativeHttp;
}
NativeHttpBackend.prototype.handle = function (req) {
var _this = this;
var allowedRequestMethods = [
'GET',
'POST',
'PUT',
'DELETE',
'PATCH',
'HEAD',
];
if (allowedRequestMethods.indexOf(req.method.toUpperCase()) === -1) {
throw 'Only GET, POST, PUT, DELETE, PATCH and HEAD methods are supported by the current Native HTTP version';
}
return new Observable(function (observer) {
var fireResponse = function (response) {
// ok determines whether the response will be transmitted on the event or
// error channel. Unsuccessful status codes (not 2xx) will always be errors,
// but a successful status code can still result in an error if the user
// asked for JSON data and the body cannot be parsed as such.
var ok = response.status >= 200 && response.status < 300;
var body = response.body;
// Check whether the body needs to be parsed as JSON (in many cases the browser
// will have done that already).
if (req.responseType === 'json' && typeof body === 'string') {
// Save the original body, before attempting XSSI prefix stripping.
var originalBody = body;
body = body.replace(XSSI_PREFIX, '');
try {
// Attempt the parse. If it fails, a parse error should be delivered to the user.
body = body !== '' ? JSON.parse(body) : null;
}
catch (error) {
// Since the JSON.parse failed, it's reasonable to assume this might not have been a
// JSON response. Restore the original body (including any XSSI prefix) to deliver
// a better error response.
body = originalBody;
// If this was an error request to begin with, leave it as a string, it probably
// just isn't JSON. Otherwise, deliver the parsing error to the user.
if (ok) {
// Even though the response status was 2xx, this is still an error.
ok = false;
// The parse error contains the text of the body that failed to parse.
body = { error: error, text: body };
}
}
}
if (ok) {
// A successful response is delivered on the event stream.
observer.next(new HttpResponse({
body: body,
headers: new HttpHeaders(response.headers),
status: response.status,
url: response.url,
}));
// The full body has been received and delivered, no further events
// are possible. This request is complete.
observer.complete();
}
else {
// An unsuccessful request is delivered on the error channel.
observer.error(new HttpErrorResponse({
// The error in this case is the response body (error from the server).
error: body,
headers: new HttpHeaders(response.headers),
status: response.status,
url: response.url,
}));
}
};
_this.nativeHttp
// @ts-ignore
.sendRequest(req.urlWithParams, getRequestOptions(req))
.then(function (response) {
fireResponse({
body: response.data,
status: response.status,
headers: response.headers,
url: response.url,
});
})
.catch(function (error) {
fireResponse({
body: error.error,
status: error.status || 599,
headers: error.headers,
url: error.url,
});
});
});
};
NativeHttpBackend.ɵfac = function NativeHttpBackend_Factory(t) { return new (t || NativeHttpBackend)(i0.ɵɵinject(i1.HTTP)); };
NativeHttpBackend.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: NativeHttpBackend, factory: NativeHttpBackend.ɵfac });
return NativeHttpBackend;
}());
export { NativeHttpBackend };
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(NativeHttpBackend, [{
type: Injectable
}], function () { return [{ type: i1.HTTP }]; }, null); })();
//# sourceMappingURL=native-http-backend.js.map