@sprucelabs/spruce-event-utils
Version:
Some helpful utilities to speed up working with Mercury! 🚅
47 lines (46 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const schema_1 = require("@sprucelabs/schema");
const constants_1 = require("../constants");
class RemoteService {
constructor(env) {
this.env = env;
}
set(remote) {
//@ts-ignore
const host = constants_1.REMOTES[remote];
if (!host) {
throw new schema_1.SchemaError({
code: 'INVALID_PARAMETERS',
friendlyMessage: `${remote} is not a valid remote. Try:\n\n${Object.keys(constants_1.REMOTES).join('\n')}`,
parameters: ['remote'],
});
}
this.env.set('HOST', host);
}
getHost() {
return this.env.get('HOST');
}
getRemote() {
// move to constants or some better mapping?
const values = Object.entries(constants_1.REMOTES);
const host = this.getHost();
if (typeof host === 'undefined') {
return null;
}
const hostStr = host?.toString?.();
const match = values.find((v) => hostStr?.indexOf?.(v[1]) > -1);
if (!match) {
if (hostStr?.startsWith?.('http')) {
return 'custom';
}
throw new schema_1.SchemaError({
code: 'INVALID_PARAMETERS',
friendlyMessage: `Mercury's env.HOST is set to '${host}', which I can't resolve to an environment.`,
parameters: ['env.HOST'],
});
}
return match?.[0];
}
}
exports.default = RemoteService;