@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
39 lines (38 loc) • 1.45 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();
const pool = args.pool || TypeSystem.Pool.create();
let change;
const http = HttpClient.isClient(args.http)
? args.http
: HttpClient.create(args.http);
const host = http.origin;
const fetch = TypeSystem.Cache.wrapFetch(TypeSystem.fetcher.fromClient(http), { event$ });
const api = {
host,
http,
fetch,
cache,
pool,
get changes() {
return change || (change = TypeSystem.ChangeMonitor.create());
},
async typeDefs(ns) {
const uris = Array.isArray(ns) ? ns : [ns];
const client = TypeSystem.client(fetch);
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.typeDefs(ns);
return TypeSystem.Client.typescript(defs, options);
},
sheet(ns) {
return TypeSystem.Sheet.load({ ns, fetch, cache, event$, pool });
},
};
return api;
}