@trapi/metadata
Version:
Generate REST-API metadata scheme from TypeScript Decorators.
72 lines • 2.31 kB
JavaScript
;
/*
* Copyright (c) 2022-2023.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DecoratorResolver = void 0;
const property_manager_1 = require("../property-manager");
const preset_1 = require("../preset");
const utils_1 = require("../../utils");
class DecoratorResolver {
// -------------------------------------------
constructor() {
this.items = [];
this.map = {};
}
// -------------------------------------------
/**
* Try to find a matching representation for a given decorator type and decorators or node.
*
* @param id
* @param data
*/
match(id, data) {
if (!(0, utils_1.hasOwnProperty)(this.map, id)) {
return undefined;
}
const decorators = Array.isArray(data) ?
data :
(0, utils_1.getNodeDecorators)(data);
if (!(0, utils_1.hasOwnProperty)(this.map, id)) {
return undefined;
}
for (let i = 0; i < this.map[id].length; i++) {
const items = decorators.filter((decorator) => decorator.text === this.map[id][i].name);
if (items.length > 0) {
return new property_manager_1.DecoratorPropertyManager(this.map[id][i], items);
}
}
return undefined;
}
// -------------------------------------------
apply(items) {
this.items.push(...items);
this.aggregate();
}
async applyPreset(name) {
const items = await (0, preset_1.loadPreset)(name);
this.items.push(...items);
this.aggregate();
}
/**
* Aggregate/group different annotation sources.
*
* @private
*/
aggregate() {
const result = {};
for (let i = 0; i < this.items.length; i++) {
const mapping = this.items[i];
if (!Object.prototype.hasOwnProperty.call(result, mapping.id)) {
result[mapping.id] = [];
}
result[mapping.id].push(mapping);
}
this.map = result;
}
}
exports.DecoratorResolver = DecoratorResolver;
//# sourceMappingURL=module.js.map