@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
66 lines (65 loc) • 2.42 kB
JavaScript
import { Http } from '@platform/http/lib/http/Http';
import { Schema, constants, util, Uri } 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) !== 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 http = this.http;
const urls = this.urls;
const uri = Uri.toNs(input);
return HttpClientNs.create({ uri, urls, http });
}
cell(input) {
const http = this.http;
const urls = this.urls;
const uri = Uri.cell(input);
return HttpClientCell.create({ uri, urls, http });
}
file(input) {
const http = this.http;
const urls = this.urls;
const uri = Uri.file(input);
return HttpClientFile.create({ uri, urls, http });
}
}