UNPKG

@trapi/metadata

Version:

Generate REST-API metadata scheme from TypeScript Decorators.

151 lines 5.7 kB
"use strict"; /* * Copyright (c) 2021. * 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.AbstractGenerator = void 0; const locter_1 = require("locter"); const decorator_1 = require("../decorator"); const resolver_1 = require("../resolver"); const utils_1 = require("../utils"); class AbstractGenerator { // ------------------------------------------- constructor(node, current) { this.node = node; this.current = current; } // -------------------------------------------------------------------- buildPath() { const representation = this.current.decoratorResolver.match(decorator_1.DecoratorID.MOUNT, this.node); if (typeof representation !== 'undefined') { const value = representation.get('value'); if (typeof value === 'string') { return (0, utils_1.normalizePath)(value); } } return ''; } // ------------------------------------------- getSecurity() { const representation = this.current.decoratorResolver.match(decorator_1.DecoratorID.SECURITY, this.node); if (typeof representation === 'undefined') { return []; } const securities = []; for (let i = 0; i < representation.decorators.length; i++) { let name = representation.get('key'); // todo: check if Record<string, string[]> if ((0, locter_1.isObject)(name)) { securities.push(name); } else { if (typeof name !== 'string' || !name) { name = 'default'; } const scopes = representation.get('value'); if ((0, utils_1.isStringArray)(scopes)) { securities.push({ [name]: scopes, }); } } } return securities; } // ------------------------------------------- getExamplesValue(argument) { let example = {}; if (typeof argument === 'undefined') { return example; } if (argument.properties) { argument.properties.forEach((p) => { example[p.name.text] = (0, utils_1.getInitializerValue)(p.initializer, this.current.typeChecker); }); } else { example = (0, utils_1.getInitializerValue)(argument, this.current.typeChecker); } return example; } // ------------------------------------------- buildResponses() { const representation = this.current.decoratorResolver.match(decorator_1.DecoratorID.DESCRIPTION, this.node); if (typeof representation === 'undefined') { return []; } const responses = []; for (let i = 0; i < representation.decorators.length; i++) { const description = representation.get('description', i) || 'Ok'; const status = representation.get('statusCode', i) || '200'; const payload = representation.get('payload', i); const examples = []; if (typeof payload !== 'undefined') { examples.push({ value: payload, }); } const type = representation.get('type'); const response = { description, examples, schema: type ? new resolver_1.TypeNodeResolver(type, this.current).resolve() : undefined, status: status, name: status, }; responses.push(response); } return responses; } // ------------------------------------------- getProduces() { const representation = this.current.decoratorResolver.match(decorator_1.DecoratorID.PRODUCES, this.node); if (typeof representation === 'undefined') { return []; } const value = representation.get('value'); if (typeof value === 'undefined') { return []; } return Array.isArray(value) ? value : [value]; } getConsumes() { const representation = this.current.decoratorResolver.match(decorator_1.DecoratorID.CONSUMES, this.node); if (typeof representation === 'undefined') { return []; } let value = representation.get('value'); if (typeof value === 'undefined') { return []; } value = Array.isArray(value) ? value : [value]; return value; } getTags() { const representation = this.current.decoratorResolver.match(decorator_1.DecoratorID.TAGS, this.node); if (typeof representation === 'undefined') { return []; } let value = representation.get('value'); if (typeof value === 'undefined') { return []; } value = Array.isArray(value) ? value : [value]; return value; } // ------------------------------------------- isHidden(node) { return typeof this.current.decoratorResolver.match(decorator_1.DecoratorID.HIDDEN, node) !== 'undefined'; } isDeprecated(node) { if ((0, utils_1.hasJSDocTag)(node, utils_1.JSDocTagName.DEPRECATED)) { return true; } return typeof this.current.decoratorResolver.match(decorator_1.DecoratorID.DEPRECATED, node) !== 'undefined'; } } exports.AbstractGenerator = AbstractGenerator; //# sourceMappingURL=abstract.js.map