UNPKG

@platform/cell.client

Version:

A strongly typed HTTP client for operating with a CellOS service end-point.

82 lines (81 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpClient = void 0; var tslib_1 = require("tslib"); var Http_1 = require("@platform/http/lib/http/Http"); 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) !== null && _a !== void 0 ? _a : 8080); this.origin = this.urls.origin; var headers = { client: clientHeader() }; var http = args.http ? args.http : Http_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 http = this.http; var urls = this.urls; var uri = common_1.Uri.toNs(input); return HttpClientNs_1.HttpClientNs.create({ uri: uri, urls: urls, http: http }); }; HttpClient.prototype.cell = function (input) { var http = this.http; var urls = this.urls; var uri = common_1.Uri.cell(input); return HttpClientCell_1.HttpClientCell.create({ uri: uri, urls: urls, http: http }); }; HttpClient.prototype.file = function (input) { var http = this.http; var urls = this.urls; var uri = common_1.Uri.file(input); return HttpClientFile_1.HttpClientFile.create({ uri: uri, urls: urls, http: http }); }; return HttpClient; }()); exports.HttpClient = HttpClient;