nats-micro
Version:
NATS micro compatible extra-lightweight microservice library
57 lines • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.storage = void 0;
const misc_js_1 = require("../utils/misc.js");
class ClassStorage {
constructor() {
this.items = [];
}
ensureClassAdded(target) {
const existing = this.items.find((ms) => ms.target === target);
if (existing)
return existing;
const added = {
target: target,
config: {
name: '',
description: '',
version: '0.0.0',
metadata: {},
},
methods: [],
};
this.items.push(added);
return added;
}
ensureClassMethodAdded(targetClass, classMethod) {
const storedClass = this.ensureClassAdded(targetClass);
const existing = storedClass.methods.find((m) => m.method === classMethod);
if (existing)
return existing;
const added = {
method: classMethod,
config: {},
};
storedClass.methods.push(added);
return added;
}
getConfig(target) {
var _a;
const constructor = Object.getPrototypeOf(target).constructor.prototype;
const ms = this.items.find((m) => m.target === constructor);
if (!ms)
return undefined;
const methods = {};
for (const method of ms.methods) {
const methodName = (_a = method.config.name) !== null && _a !== void 0 ? _a : (0, misc_js_1.kebabCase)(method.method.name);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const targetMethod = target[method.method.name];
if (!targetMethod)
throw new Error(`Class method ${method.method.name} is missing in target object`);
methods[methodName] = Object.assign(Object.assign({}, method.config), { handler: targetMethod });
}
return Object.assign(Object.assign({}, ms.config), { methods });
}
}
exports.storage = new ClassStorage();
//# sourceMappingURL=storage.js.map