UNPKG

@platform/cell.client

Version:

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

117 lines (116 loc) 4.74 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 rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); 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@".concat(VERSION['@platform/cell.client']); var schema = "schema@".concat(VERSION['@platform/cell.schema']); return "".concat(client, "; ").concat(schema); } var HttpClient = (function () { function HttpClient(args) { var _a; this._dispose$ = new rxjs_1.Subject(); this.dispose$ = this._dispose$.asObservable(); 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' }); var request$ = http.req$.pipe((0, operators_1.takeUntil)(this.dispose$)); var response$ = http.res$.pipe((0, operators_1.takeUntil)(this.dispose$)); request$.subscribe(function (e) { return e.modify.headers.merge(headers); }); this.http = http; this.request$ = request$; this.response$ = response$; } 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.isReachable = function (host) { return tslib_1.__awaiter(this, void 0, void 0, function () { var error_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4, HttpClient.create(host).info()]; case 1: _a.sent(); return [2, true]; case 2: error_1 = _a.sent(); if (error_1.code === 'ECONNREFUSED') { return [2, false]; } else { throw error_1; } return [3, 3]; case 3: return [2]; } }); }); }; HttpClient.prototype.dispose = function () { this._dispose$.next(); this._dispose$.complete(); }; 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 (0, HttpClientNs_1.HttpClientNs)({ 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 (0, HttpClientCell_1.HttpClientCell)({ 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 (0, HttpClientFile_1.HttpClientFile)({ uri: uri, urls: urls, http: http }); }; return HttpClient; }()); exports.HttpClient = HttpClient;