UNPKG

@trapi/metadata

Version:

Generate REST-API metadata scheme from TypeScript Decorators.

92 lines 2.82 kB
"use strict"; /* * Copyright (c) 2021-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.mergeArguments = exports.extractPropertyFromDecorator = exports.buildDecoratorPropertyConfig = void 0; const locter_1 = require("locter"); const utils_1 = require("../../utils"); function buildDecoratorPropertyConfig(property) { return { ...property, isType: property.isType ?? false, index: property.index ?? 0, amount: property.amount ?? 1, }; } exports.buildDecoratorPropertyConfig = buildDecoratorPropertyConfig; function extractPropertyFromDecorator(decorator, config) { const items = config.isType ? decorator.typeArguments : extractValueFromArgumentType(decorator.arguments); if (items.length <= config.index) { return undefined; } const data = (config.amount >= 1 ? items.slice(config.index, config.index + config.amount) : items.slice(config.index)); if (data.length === 0) { return undefined; } if (config.strategy) { if (config.strategy === 'merge') { return mergeArguments(data); } return config.strategy(data); } if (config.amount === 1) { if (data.length > 0) { return data[0]; } return undefined; } return data; } exports.extractPropertyFromDecorator = extractPropertyFromDecorator; function mergeArguments(data) { if (data.length === 0) { return undefined; } // we are merging object properties if ((0, locter_1.isObject)(data[0])) { const element = data[0]; if (data.length === 1) { return element; } for (let i = 1; i < data.length; i++) { if ((0, locter_1.isObject)(data[i])) { Object.assign(element, data[i]); } } return element; } const output = Array.isArray(data[0]) ? data[0] : [data[0]]; if (data.length === 1) { return output; } for (let i = 1; i < data.length; i++) { if (Array.isArray(data[i])) { output.push(...data[i]); } else { output.push(data[i]); } } return output; } exports.mergeArguments = mergeArguments; function extractValueFromArgumentType(argument) { const values = []; for (let i = 0; i < argument.length; i++) { if (!(0, utils_1.hasOwnProperty)(argument[i], 'kind')) { values.push(argument[i]); continue; } values.push((0, utils_1.getInitializerValue)(argument[i])); } return values; } //# sourceMappingURL=utils.js.map