@ssecd/jkn
Version:
JKN (BPJS) Bridging API untuk NodeJS
44 lines (43 loc) • 1.04 kB
JavaScript
export class BaseApi {
fetcher;
constructor(fetcher) {
this.fetcher = fetcher;
}
send(option) {
return this.fetcher.send(this.type, option);
}
async getConfig() {
return this.fetcher.getConfig();
}
async requiredConfig(...keys) {
const config = await this.getConfig();
for (const key of keys) {
if (!config[key]) {
const message = `The "${key}" config value must not be falsy for this request`;
throw new Error(message);
}
}
return config;
}
get name() {
return this.constructor.name + ' -> ';
}
}
export class CachedApi {
fetcher;
cached = new Map();
constructor(fetcher) {
this.fetcher = fetcher;
}
get(key, Api) {
let api = this.cached.get(key);
if (!api) {
api = new Api(this.fetcher);
this.cached.set(key, api);
}
return api;
}
clear() {
this.cached.clear();
}
}