'use strict'; var chunkABOQ73FL_cjs = require('./chunk-ABOQ73FL.cjs'); var chunk2TGWPVZN_cjs = require('./chunk-2TGWPVZN.cjs'); require('./chunk-PADR76WZ.cjs'); var chunk2RH3ZCNB_cjs = require('./chunk-2RH3ZCNB.cjs'); var chunkQAFBZLJA_cjs = require('./chunk-QAFBZLJA.cjs'); require('./chunk-ZWHQ54JM.cjs'); var core = require('@kubb/core'); var transformers = require('@kubb/core/transformers'); var path = require('path'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var transformers__default = /*#__PURE__*/_interopDefault(transformers); var path__default = /*#__PURE__*/_interopDefault(path); var OperationGenerator = class extends core.BaseGenerator { #operationsByMethod = {}; get operationsByMethod() { return this.#operationsByMethod; } set operationsByMethod(paths) { this.#operationsByMethod = paths; } #getOptions(operation, method) { const { override = [] } = this.context; return override.find(({ pattern, type }) => { if (type === "tag") { return !!operation.getTags()[0]?.name.match(pattern); } if (type === "operationId") { return !!operation.getOperationId().match(pattern); } if (type === "path") { return !!operation.path.match(pattern); } if (type === "method") { return !!method.match(pattern); } return false; })?.options || {}; } #isExcluded(operation, method) { const { exclude = [] } = this.context; let matched = false; exclude.forEach(({ pattern, type }) => { if (type === "tag" && !matched) { matched = !!operation.getTags()[0]?.name.match(pattern); } if (type === "operationId" && !matched) { matched = !!operation.getOperationId().match(pattern); } if (type === "path" && !matched) { matched = !!operation.path.match(pattern); } if (type === "method" && !matched) { matched = !!method.match(pattern); } }); return matched; } #isIncluded(operation, method) { const { include = [] } = this.context; let matched = false; include.forEach(({ pattern, type }) => { if (type === "tag" && !matched) { matched = !!operation.getTags()[0]?.name.match(pattern); } if (type === "operationId" && !matched) { matched = !!operation.getOperationId().match(pattern); } if (type === "path" && !matched) { matched = !!operation.path.match(pattern); } if (type === "method" && !matched) { matched = !!method.match(pattern); } }); return matched; } getSchemas(operation, { resolveName = (name) => name } = {}) { const pathParamsSchema = this.context.oas.getParametersSchema(operation, "path"); const queryParamsSchema = this.context.oas.getParametersSchema(operation, "query"); const headerParamsSchema = this.context.oas.getParametersSchema(operation, "header"); const requestSchema = this.context.oas.getRequestSchema(operation); const statusCodes = operation.getResponseStatusCodes().map((statusCode) => { let name = statusCode; if (name === "default") { name = "error"; } const schema = this.context.oas.getResponseSchema(operation, statusCode); return { name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} ${name}`)), description: operation.getResponseByStatusCode(statusCode)?.description, schema, operation, operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`), statusCode: name === "error" ? void 0 : Number(statusCode), keys: schema?.properties ? Object.keys(schema.properties) : void 0, keysToOmit: schema?.properties ? Object.keys(schema.properties).filter((key) => { const item = schema.properties?.[key]; return item?.writeOnly; }) : void 0 }; }); const hasResponses = statusCodes.some((item) => item.statusCode?.toString().startsWith("2")); return { pathParams: pathParamsSchema ? { name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} PathParams`)), operation, operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`), schema: pathParamsSchema, keys: pathParamsSchema.properties ? Object.keys(pathParamsSchema.properties) : void 0 } : void 0, queryParams: queryParamsSchema ? { name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} QueryParams`)), operation, operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`), schema: queryParamsSchema, keys: queryParamsSchema.properties ? Object.keys(queryParamsSchema.properties) : [] } : void 0, headerParams: headerParamsSchema ? { name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} HeaderParams`)), operation, operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`), schema: headerParamsSchema, keys: headerParamsSchema.properties ? Object.keys(headerParamsSchema.properties) : void 0 } : void 0, request: requestSchema ? { name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} ${operation.method === "get" ? "queryRequest" : "mutationRequest"}`)), description: operation.schema.requestBody?.description, operation, operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`), schema: requestSchema, keys: requestSchema.properties ? Object.keys(requestSchema.properties) : void 0, keysToOmit: requestSchema.properties ? Object.keys(requestSchema.properties).filter((key) => { const item = requestSchema.properties?.[key]; return item?.readOnly; }) : void 0 } : void 0, response: { name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} ${operation.method === "get" ? "queryResponse" : "mutationResponse"}`)), operation, operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`), schema: { oneOf: hasResponses ? statusCodes.filter((item) => item.statusCode?.toString().startsWith("2")).map((item) => { return { ...item.schema, $ref: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} ${item.statusCode}`)) }; }) : void 0 } }, responses: statusCodes.filter((item) => item.statusCode?.toString().startsWith("2")), errors: statusCodes.filter((item) => item.statusCode?.toString().startsWith("4") || item.statusCode?.toString().startsWith("5")), statusCodes }; } #methods = ["get", "post", "patch", "put", "delete"]; async build(...generators) { const { oas } = this.context; const paths = oas.getPaths(); this.operationsByMethod = Object.entries(paths).reduce((acc, [path2, method]) => { const methods = Object.keys(method); methods.forEach((method2) => { const operation = oas.operation(path2, method2); if (operation && [this.#methods].some((methods2) => method2 === operation.method)) { const isExcluded = this.#isExcluded(operation, method2); const isIncluded = this.context.include ? this.#isIncluded(operation, method2) : true; if (isIncluded && !isExcluded) { if (!acc[path2]) { acc[path2] = {}; } acc[path2] = { ...acc[path2], [method2]: { operation, schemas: this.getSchemas(operation) } }; } } }); return acc; }, {}); const promises = Object.keys(this.operationsByMethod).reduce((acc, path2) => { const methods = this.operationsByMethod[path2] ? Object.keys(this.operationsByMethod[path2]) : []; methods.forEach((method) => { const { operation } = this.operationsByMethod[path2]?.[method]; const options = this.#getOptions(operation, method); const methodToCall = this[method]; if (typeof methodToCall === "function") { const promiseMethod = methodToCall?.call(this, operation, { ...this.options, ...options }); if (promiseMethod) { acc.push(promiseMethod); } } const promiseOperation = this.operation.call(this, operation, { ...this.options, ...options }); if (promiseOperation) { acc.push(promiseOperation); } generators?.forEach((generator) => { const promise = generator.operation?.({ instance: this, operation, options: { ...this.options, ...options } }); if (promise) { acc.push(promise); } }); }); return acc; }, []); const operations = Object.values(this.operationsByMethod).map((item) => Object.values(item).map((item2) => item2.operation)); promises.push(this.all(operations.flat().filter(Boolean), this.operationsByMethod)); generators?.forEach((generator) => { const promise = generator.operations?.({ instance: this, operations: operations.flat().filter(Boolean), operationsByMethod: this.operationsByMethod, options: this.options }); if (promise) { promises.push(promise); } }); const files = await Promise.all(promises); return files.flat().filter(Boolean); } /** * Operation */ async operation(operation, options) { return []; } /** * GET */ async get(operation, options) { return []; } /** * POST */ async post(operation, options) { return []; } /** * PATCH */ async patch(operation, options) { return []; } /** * PUT */ async put(operation, options) { return []; } /** * DELETE */ async delete(operation, options) { return []; } /** * Combination of GET, POST, PATCH, PUT, DELETE */ async all(operations, paths) { return []; } }; var pluginOasName = "plugin-oas"; var pluginOas = core.createPlugin((options) => { const { output = { path: "schemas" }, validate = true, generators = [chunk2TGWPVZN_cjs.jsonGenerator], serverIndex, contentType, oasClass } = options; const getOas = async ({ config, logger }) => { try { const oas = await chunkABOQ73FL_cjs.parseFromConfig(config, oasClass); if (validate) { await oas.valdiate(); } return oas; } catch (e) { const error = e; logger.emit("warning", error?.message); return chunkABOQ73FL_cjs.parseFromConfig(config, oasClass); } }; return { name: pluginOasName, options: { output, ...options }, context() { const { config, logger } = this; return { getOas() { return getOas({ config, logger }); }, async getSchemas({ includes } = {}) { const oas = await this.getOas(); return chunkQAFBZLJA_cjs.getSchemas({ oas, contentType, includes }); }, async getBaseURL() { const oasInstance = await this.getOas(); if (serverIndex) { return oasInstance.api.servers?.at(serverIndex)?.url; } return void 0; }, contentType }; }, resolvePath(baseName, pathMode, options2) { const root = path__default.default.resolve(this.config.root, this.config.output.path); const mode = pathMode ?? core.FileManager.getMode(path__default.default.resolve(root, output.path)); if (mode === "single") { return path__default.default.resolve(root, output.path); } return path__default.default.resolve(root, output.path, baseName); }, async buildStart() { if (!output) { return; } const oas = await getOas({ config: this.config, logger: this.logger }); await oas.dereference(); const schemaGenerator = new chunk2RH3ZCNB_cjs.SchemaGenerator( { unknownType: "unknown", dateType: "date", transformers: {}, ...this.plugin.options }, { oas, pluginManager: this.pluginManager, plugin: this.plugin, contentType, include: void 0, override: void 0, mode: "split", output: output.path } ); const schemaFiles = await schemaGenerator.build(...generators); await this.addFile(...schemaFiles); const operationGenerator = new OperationGenerator(this.plugin.options, { oas, pluginManager: this.pluginManager, plugin: this.plugin, contentType, exclude: void 0, include: void 0, override: void 0, mode: "split" }); const operationFiles = await operationGenerator.build(...generators); await this.addFile(...operationFiles); } }; }); Object.defineProperty(exports, "createGenerator", { enumerable: true, get: function () { return chunk2TGWPVZN_cjs.createGenerator; } }); Object.defineProperty(exports, "createReactGenerator", { enumerable: true, get: function () { return chunk2TGWPVZN_cjs.createReactGenerator; } }); Object.defineProperty(exports, "SchemaGenerator", { enumerable: true, get: function () { return chunk2RH3ZCNB_cjs.SchemaGenerator; } }); Object.defineProperty(exports, "isKeyword", { enumerable: true, get: function () { return chunk2RH3ZCNB_cjs.isKeyword; } }); Object.defineProperty(exports, "schemaKeywords", { enumerable: true, get: function () { return chunk2RH3ZCNB_cjs.schemaKeywords; } }); exports.OperationGenerator = OperationGenerator; exports.pluginOas = pluginOas; exports.pluginOasName = pluginOasName; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map