@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
65 lines (64 loc) • 2.38 kB
JavaScript
import { Schema, Http, constants, util } from '../common';
import { HttpClientCell } from './HttpClientCell';
import { HttpClientFile } from './HttpClientFile';
import { HttpClientNs } from './HttpClientNs';
function clientHeader() {
const VERSION = constants.VERSION;
const client = `client@${VERSION['@platform/cell.client']}`;
const schema = `schema@${VERSION['@platform/cell.schema']}`;
return `CellOS; ${client}; ${schema}`;
}
export class HttpClient {
constructor(args) {
var _a;
this.urls = Schema.urls((_a = args.host, (_a !== null && _a !== void 0 ? _a : 8080)));
this.origin = this.urls.origin;
const headers = { client: clientHeader() };
const http = args.http ? args.http : Http.create({ headers, mode: 'cors' });
http.before$.subscribe(e => e.modify.headers.merge(headers));
this.http = http;
this.request$ = http.before$;
this.response$ = http.after$;
}
static create(input) {
const args = typeof input === 'object' ? input : { host: input };
return new HttpClient(args);
}
static isClient(input) {
if (typeof input !== 'object' || input === null) {
return false;
}
const value = input;
return (util.isObservable(value.request$) &&
util.isObservable(value.response$) &&
typeof value.origin === 'string' &&
typeof value.info === 'function' &&
typeof value.ns === 'function' &&
typeof value.cell === 'function' &&
typeof value.file === 'function');
}
async info() {
const http = this.http;
const url = this.urls.sys.info.toString();
const res = await http.get(url);
return util.fromHttpResponse(res).toClientResponse();
}
ns(input) {
const urls = this.urls;
const uri = urls.ns(input).uri;
const http = this.http;
return HttpClientNs.create({ uri, urls, http });
}
cell(input) {
const urls = this.urls;
const uri = urls.cell(input).uri;
const http = this.http;
return HttpClientCell.create({ uri, urls, http });
}
file(input) {
const urls = this.urls;
const uri = urls.file(input).uri;
const http = this.http;
return HttpClientFile.create({ uri, urls, http });
}
}