@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
35 lines (34 loc) • 1.28 kB
JavaScript
import { HttpClient } from '../Client.http';
import { MemoryCache, TypeSystem } from '../common';
export function typesystem(input) {
const args = typeof input === 'object' ? input : { http: input };
const event$ = args.event$ ? args.event$ : undefined;
const cache = args.cache || MemoryCache.create();
let change;
const http = HttpClient.isClient(args.http)
? args.http
: HttpClient.create(args.http);
const fetch = TypeSystem.fetcher.fromClient(http);
const api = {
http,
fetch,
cache,
get changes() {
return change || (change = TypeSystem.ChangeMonitor.create());
},
async defs(ns) {
const uris = Array.isArray(ns) ? ns : [ns];
const client = TypeSystem.client(http);
const defs = (await Promise.all(uris.map((ns) => client.load(ns)))).map(({ defs }) => defs);
return defs.reduce((acc, next) => acc.concat(next), []);
},
async typescript(ns, options = {}) {
const defs = await api.defs(ns);
return TypeSystem.Client.typescript(defs, options);
},
sheet(ns) {
return TypeSystem.Sheet.load({ ns, fetch, cache, event$ });
},
};
return api;
}