UNPKG

@quinck/fastify-openapi-typescript-generator

Version:

Contains utilities to generate fastify types from openapi definition for the fastify framework.

80 lines (79 loc) 2.79 kB
export class ParserBase { constructor() { this.config = { generic: {}, routes: [], contentTypes: new Set() }; } makeOperationId(operation, path) { const firstUpper = (str) => str.substr(0, 1).toUpperCase() + str.substr(1); const by = (matched, p1) => `By${firstUpper(p1)}`; const parts = path.split('/').slice(1); parts.unshift(operation); const opId = parts .map((item, i) => (i > 0 ? firstUpper(item) : item)) .join('') .replace(/{(\w+)}/g, by) .replace(/[^a-z]/gi, ''); return opId; } makeURL(path) { return path.replace(/{(\w+)}/g, ':$1'); } copyProps(source, target, list, copyXprops = false) { Object.keys(source).forEach(item => { if (list.includes(item) || (copyXprops && item.startsWith('x-'))) { target[item] = source[item]; } }); } parseSecurity(schemes) { return schemes ? schemes.map(item => { const name = Object.keys(item)[0]; return { name, parameters: item[name], }; }) : undefined; } removeRecursion(schemas) { function escapeJsonPointer(str) { return str.replace(/~/g, '~0').replace(/\//g, '~1'); } function processSchema(obj) { let refAdded = false; function inspectNode(obj, path, paths) { if (typeof obj === 'object' && obj !== null) { if (paths.has(obj)) { return paths.get(obj); } const newPaths = new Map(paths); newPaths.set(obj, path); for (const key in obj) { const $ref = inspectNode(obj[key], `${path}/${escapeJsonPointer(key)}`, newPaths); if (typeof $ref === 'string') { obj[key] = { $ref }; refAdded = true; } } } return undefined; } const paths = new Map(); inspectNode(obj, '#', paths); if (refAdded && typeof obj['$id'] === 'undefined') { obj['$id'] = 'http://example.com/fastifySchema'; } } for (const item in schemas) { const schema = schemas[item]; if (item === 'response') { for (const responseCode in schema) { processSchema(schema[responseCode]); } } else { processSchema(schema); } } } }