UNPKG

openapi-ts-json-schema

Version:

OpenAPI to JSON schema generator with TypeScript in mind

37 lines (36 loc) 1.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.replacePlaceholdersWithImportedSchemas = void 0; const __1 = require(".."); /** * Replace id placeholders with imported schemas */ function replacePlaceholdersWithImportedSchemas({ schemaAsText, absoluteDirName, schemaMetaDataMap, }) { const importStatements = new Set(); // Replace placeholder occurrences with the relevant imported schema name let output = schemaAsText.replaceAll(__1.PLACEHOLDER_REGEX, (_match, id) => { const importedSchema = schemaMetaDataMap.get(id); /* v8 ignore start */ if (!importedSchema) { throw new Error('[openapi-ts-json-schema] No matching schema found in "schemaMetaDataMap"'); } /* v8 ignore stop */ // Evaluate imported schema relative path from current schema file const importedSchemaRelativePath = (0, __1.makeRelativeModulePath)({ fromDirectory: absoluteDirName, to: importedSchema.absoluteImportPath, }); const { uniqueName } = importedSchema; importStatements.add(`import ${uniqueName} from "${importedSchemaRelativePath}"`); return uniqueName; }); if (importStatements.size > 0) { // Empty line between imports and schema 💅 output = '\n' + output; importStatements.forEach((entry) => { output = entry + '\n' + output; }); } return output; } exports.replacePlaceholdersWithImportedSchemas = replacePlaceholdersWithImportedSchemas;