@sprucelabs/spruce-event-utils
Version:
Some helpful utilities to speed up working with Mercury! 🚅
45 lines (44 loc) • 1.81 kB
JavaScript
import { SchemaError } from '@sprucelabs/schema';
import { REMOTES } from '../constants.js';
export default class RemoteService {
constructor(env) {
this.env = env;
}
set(remote) {
//@ts-ignore
const host = REMOTES[remote];
if (!host) {
throw new SchemaError({
code: 'INVALID_PARAMETERS',
friendlyMessage: `${remote} is not a valid remote. Try:\n\n${Object.keys(REMOTES).join('\n')}`,
parameters: ['remote'],
});
}
this.env.set('HOST', host);
}
getHost() {
return this.env.get('HOST');
}
getRemote() {
var _a, _b;
// move to constants or some better mapping?
const values = Object.entries(REMOTES);
const host = this.getHost();
if (typeof host === 'undefined') {
return null;
}
const hostStr = (_a = host === null || host === void 0 ? void 0 : host.toString) === null || _a === void 0 ? void 0 : _a.call(host);
const match = values.find((v) => { var _a; return ((_a = hostStr === null || hostStr === void 0 ? void 0 : hostStr.indexOf) === null || _a === void 0 ? void 0 : _a.call(hostStr, v[1])) > -1; });
if (!match) {
if ((_b = hostStr === null || hostStr === void 0 ? void 0 : hostStr.startsWith) === null || _b === void 0 ? void 0 : _b.call(hostStr, 'http')) {
return 'custom';
}
throw new 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 === null || match === void 0 ? void 0 : match[0];
}
}