@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
80 lines (79 loc) • 3.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("../common");
var HttpClientCell_1 = require("./HttpClientCell");
var HttpClientFile_1 = require("./HttpClientFile");
var HttpClientNs_1 = require("./HttpClientNs");
function clientHeader() {
var VERSION = common_1.constants.VERSION;
var client = "client@" + VERSION['@platform/cell.client'];
var schema = "schema@" + VERSION['@platform/cell.schema'];
return "CellOS; " + client + "; " + schema;
}
var HttpClient = (function () {
function HttpClient(args) {
var _a;
this.urls = common_1.Schema.urls((_a = args.host, (_a !== null && _a !== void 0 ? _a : 8080)));
this.origin = this.urls.origin;
var headers = { client: clientHeader() };
var http = args.http ? args.http : common_1.Http.create({ headers: headers, mode: 'cors' });
http.before$.subscribe(function (e) { return e.modify.headers.merge(headers); });
this.http = http;
this.request$ = http.before$;
this.response$ = http.after$;
}
HttpClient.create = function (input) {
var args = typeof input === 'object' ? input : { host: input };
return new HttpClient(args);
};
HttpClient.isClient = function (input) {
if (typeof input !== 'object' || input === null) {
return false;
}
var value = input;
return (common_1.util.isObservable(value.request$) &&
common_1.util.isObservable(value.response$) &&
typeof value.origin === 'string' &&
typeof value.info === 'function' &&
typeof value.ns === 'function' &&
typeof value.cell === 'function' &&
typeof value.file === 'function');
};
HttpClient.prototype.info = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var http, url, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
http = this.http;
url = this.urls.sys.info.toString();
return [4, http.get(url)];
case 1:
res = _a.sent();
return [2, common_1.util.fromHttpResponse(res).toClientResponse()];
}
});
});
};
HttpClient.prototype.ns = function (input) {
var urls = this.urls;
var uri = urls.ns(input).uri;
var http = this.http;
return HttpClientNs_1.HttpClientNs.create({ uri: uri, urls: urls, http: http });
};
HttpClient.prototype.cell = function (input) {
var urls = this.urls;
var uri = urls.cell(input).uri;
var http = this.http;
return HttpClientCell_1.HttpClientCell.create({ uri: uri, urls: urls, http: http });
};
HttpClient.prototype.file = function (input) {
var urls = this.urls;
var uri = urls.file(input).uri;
var http = this.http;
return HttpClientFile_1.HttpClientFile.create({ uri: uri, urls: urls, http: http });
};
return HttpClient;
}());
exports.HttpClient = HttpClient;