node-angular-http-client
Version:
The Angular 4.3 HttpClient for node.js
197 lines • 8.29 kB
JavaScript
;
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
// const xhr2: any = require('xhr2');
var xhr2_cookies_1 = require("xhr2-cookies");
var injection_js_1 = require("injection-js");
var index_1 = require("./http/index");
var common_http_1 = require("./common-http");
var Observable_1 = require("rxjs/Observable");
var isAbsoluteUrl = /^[a-zA-Z\-\+.]+:\/\//;
function validateRequestUrl(url) {
if (!isAbsoluteUrl.test(url)) {
throw new Error("URLs requested via Http on the server must be absolute. URL: " + url);
}
}
var ServerXhr = /** @class */ (function () {
function ServerXhr() {
}
// build(): XMLHttpRequest { return new xhr2.XMLHttpRequest(); }
ServerXhr.prototype.build = function () { return new xhr2_cookies_1.XMLHttpRequest(); };
ServerXhr = __decorate([
injection_js_1.Injectable()
], ServerXhr);
return ServerXhr;
}());
exports.ServerXhr = ServerXhr;
var ServerXsrfStrategy = /** @class */ (function () {
function ServerXsrfStrategy() {
}
ServerXsrfStrategy.prototype.configureRequest = function (req) { };
ServerXsrfStrategy = __decorate([
injection_js_1.Injectable()
], ServerXsrfStrategy);
return ServerXsrfStrategy;
}());
exports.ServerXsrfStrategy = ServerXsrfStrategy;
var ZoneMacroTaskWrapper = /** @class */ (function () {
function ZoneMacroTaskWrapper() {
}
ZoneMacroTaskWrapper.prototype.wrap = function (request) {
var _this = this;
return new Observable_1.Observable(function (observer) {
var task = null;
var scheduled = false;
var sub = null;
var savedResult = null;
var savedError = null;
var scheduleTask = function (_task) {
task = _task;
scheduled = true;
var delegate = _this.delegate(request);
sub = delegate.subscribe(function (res) { return savedResult = res; }, function (err) {
if (!scheduled) {
throw new Error('An http observable was completed twice. This shouldn\'t happen, please file a bug.');
}
savedError = err;
scheduled = false;
task.invoke();
}, function () {
if (!scheduled) {
throw new Error('An http observable was completed twice. This shouldn\'t happen, please file a bug.');
}
scheduled = false;
task.invoke();
});
};
var cancelTask = function (_task) {
if (!scheduled) {
return;
}
scheduled = false;
if (sub) {
sub.unsubscribe();
sub = null;
}
};
var onComplete = function () {
if (savedError !== null) {
observer.error(savedError);
}
else {
observer.next(savedResult);
observer.complete();
}
};
// MockBackend for Http is synchronous, which means that if scheduleTask is by
// scheduleMacroTask, the request will hit MockBackend and the response will be
// sent, causing task.invoke() to be called.
var _task = Zone.current.scheduleMacroTask('ZoneMacroTaskWrapper.subscribe', onComplete, {}, function () { return null; }, cancelTask);
scheduleTask(_task);
return function () {
if (scheduled && task) {
task.zone.cancelTask(task);
scheduled = false;
}
if (sub) {
sub.unsubscribe();
sub = null;
}
};
});
};
return ZoneMacroTaskWrapper;
}());
exports.ZoneMacroTaskWrapper = ZoneMacroTaskWrapper;
var ZoneMacroTaskConnection = /** @class */ (function (_super) {
__extends(ZoneMacroTaskConnection, _super);
function ZoneMacroTaskConnection(request, backend) {
var _this = _super.call(this) || this;
_this.request = request;
_this.backend = backend;
validateRequestUrl(request.url);
_this.response = _this.wrap(request);
return _this;
}
ZoneMacroTaskConnection.prototype.delegate = function (request) {
this.lastConnection = this.backend.createConnection(request);
return this.lastConnection.response;
};
Object.defineProperty(ZoneMacroTaskConnection.prototype, "readyState", {
get: function () {
return !!this.lastConnection ? this.lastConnection.readyState : index_1.ReadyState.Unsent;
},
enumerable: true,
configurable: true
});
return ZoneMacroTaskConnection;
}(ZoneMacroTaskWrapper));
exports.ZoneMacroTaskConnection = ZoneMacroTaskConnection;
var ZoneMacroTaskBackend = /** @class */ (function () {
function ZoneMacroTaskBackend(backend) {
this.backend = backend;
}
ZoneMacroTaskBackend.prototype.createConnection = function (request) {
return new ZoneMacroTaskConnection(request, this.backend);
};
return ZoneMacroTaskBackend;
}());
exports.ZoneMacroTaskBackend = ZoneMacroTaskBackend;
var ZoneClientBackend = /** @class */ (function (_super) {
__extends(ZoneClientBackend, _super);
function ZoneClientBackend(backend) {
var _this = _super.call(this) || this;
_this.backend = backend;
return _this;
}
ZoneClientBackend.prototype.handle = function (request) { return this.wrap(request); };
ZoneClientBackend.prototype.delegate = function (request) {
return this.backend.handle(request);
};
return ZoneClientBackend;
}(ZoneMacroTaskWrapper));
exports.ZoneClientBackend = ZoneClientBackend;
function httpFactory(xhrBackend, options) {
var macroBackend = new ZoneMacroTaskBackend(xhrBackend);
return new index_1.Http(macroBackend, options);
}
exports.httpFactory = httpFactory;
function zoneWrappedInterceptingHandler(backend, interceptors) {
var realBackend = common_http_1.interceptingHandler(backend, interceptors);
return new ZoneClientBackend(realBackend);
}
exports.zoneWrappedInterceptingHandler = zoneWrappedInterceptingHandler;
exports.SERVER_HTTP_PROVIDERS = [
{ provide: index_1.Http, useFactory: httpFactory, deps: [index_1.XHRBackend, index_1.RequestOptions] },
{ provide: index_1.BrowserXhr, useClass: ServerXhr },
{ provide: index_1.XSRFStrategy, useClass: ServerXsrfStrategy },
{ provide: common_http_1.XhrFactory, useClass: ServerXhr },
{
provide: common_http_1.HttpHandler,
useFactory: zoneWrappedInterceptingHandler,
deps: [common_http_1.HttpBackend, [new injection_js_1.Optional(), common_http_1.HTTP_INTERCEPTORS]]
}
];
//# sourceMappingURL=http.js.map