jsev
Version:
Environment for building Web API's.
44 lines • 1.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utilities_1 = require("../utilities");
exports.default = (env) => {
if (!env.cfg.services) {
return null;
}
const cfg = env.cfg.services;
const { log } = env;
const serviceModules = utilities_1.exportModulesSync(cfg.path);
const services = Object.entries(serviceModules).reduce((a, x) => {
const [svcName, svcLoader] = x;
if (!utilities_1.isFunction(svcLoader.init)) {
log.warn(`No init function found for ${svcName}`);
return a;
}
const svc = svcLoader.init(env);
if (!svc) {
log.debug(`${svcName} was not loaded`);
return a;
}
a[svcName] = svc;
log.info(`Loaded ${svcName}`);
return a;
}, {});
return {
func: (ctx, next) => {
ctx.services = Object.entries(services).reduce((a, x) => {
const [name, Svc] = x;
try {
a[name] = new Svc(ctx);
}
catch (err) {
err.message = `Failed to load ${name}`;
throw err;
}
return a;
}, {});
return next();
},
rank: 20,
};
};
//# sourceMappingURL=services.js.map