UNPKG

@himenon/openapi-typescript-code-generator

Version:
134 lines (131 loc) 4.53 kB
import { FileSystem, OpenApiTools_exports, ResolveReference_exports, TsGenerator_exports, Validator_exports } from "./chunk-IRTN2Z2Z.js"; import "./chunk-PH24P4KQ.js"; import "./chunk-R3KKVR43.js"; import "./chunk-PZ5AY32C.js"; // src/index.ts import { EOL } from "os"; // src/generateValidRootSchema.ts var generateValidRootSchema = (input) => { if (!input.paths) { return input; } for (const [path, methods] of Object.entries(input.paths || {})) { const targets = { get: methods.get, put: methods.put, post: methods.post, delete: methods.delete, options: methods.options, head: methods.head, patch: methods.patch, trace: methods.trace }; for (const [method, operation] of Object.entries(targets)) { if (!operation) { continue; } if ("$ref" in operation) { continue; } if (!operation.operationId) { operation.operationId = `${method.toLowerCase()}${path.charAt(0).toUpperCase() + path.slice(1)}`; } } } return input; }; // src/index.ts var CodeGenerator = class { constructor(entryPointOrDocument, option) { this.entryPointOrDocument = entryPointOrDocument; this.option = option; if (typeof entryPointOrDocument === "string") { this.rootSchema = generateValidRootSchema(FileSystem.loadJsonOrYaml(entryPointOrDocument)); this.resolvedReferenceDocument = ResolveReference_exports.resolve( entryPointOrDocument, entryPointOrDocument, JSON.parse(JSON.stringify(this.rootSchema)) ); } else { this.rootSchema = entryPointOrDocument; this.resolvedReferenceDocument = ResolveReference_exports.resolve(".", ".", JSON.parse(JSON.stringify(this.rootSchema))); } this.parser = this.createParser(); } rootSchema; resolvedReferenceDocument; parser; createParser() { const entryPoint = typeof this.entryPointOrDocument === "string" ? this.entryPointOrDocument : "."; return new OpenApiTools_exports.Parser(entryPoint, this.rootSchema, this.resolvedReferenceDocument, this.option?.convertOption); } /** * Validate the OpenAPI Schema */ validateOpenApiSchema(option) { if (!option) { Validator_exports.validate(this.resolvedReferenceDocument); } else { Validator_exports.validate(this.resolvedReferenceDocument, option.logger); } } /** * Provides TypeScript typedefs generated from OpenAPI Schema. * * @param generatorTemplate Template for when you want to change the code following a type definition * @returns String of generated code */ generateTypeDefinition(generatorTemplates, allowOperationIds) { const create = () => { const statements = this.parser.getOpenApiTypeDefinitionStatements(); generatorTemplates?.forEach((generatorTemplate) => { const payload = this.parser.getCodeGeneratorParamsArray(allowOperationIds); const extraStatements = TsGenerator_exports.Utils.convertIntermediateCodes(generatorTemplate.generator(payload, generatorTemplate.option)); statements.push(...extraStatements); }); return statements; }; return [OpenApiTools_exports.Comment.generateLeading(this.resolvedReferenceDocument), TsGenerator_exports.generate(create)].join(EOL + EOL + EOL); } /** * Generate code using a template * * @param generatorTemplate * @returns String of generated code */ generateCode(generatorTemplates, allowOperationIds) { const payload = this.parser.getCodeGeneratorParamsArray(allowOperationIds); const create = () => { return generatorTemplates.flatMap((generatorTemplate) => { return TsGenerator_exports.Utils.convertIntermediateCodes(generatorTemplate?.generator(payload, generatorTemplate.option)); }); }; return [OpenApiTools_exports.Comment.generateLeading(this.resolvedReferenceDocument), TsGenerator_exports.generate(create)].join(EOL + EOL + EOL); } /** * Provides parameters extracted from OpenApi Schema */ getCodeGeneratorParamsArray(allowOperationIds) { return this.parser.getCodeGeneratorParamsArray(allowOperationIds); } /** * Provides types for parameters for Templates.FunctionalApiClient. * * This API will be moved to Templates in the future. */ getAdditionalTypeDefinitionCustomCodeGenerator() { return { generator: () => this.parser.getAdditionalTypeStatements() }; } }; export { CodeGenerator }; //# sourceMappingURL=index.js.map