UNPKG

@redocly/respect-core

Version:
70 lines 3.58 kB
import { sortMethods } from '../../utils/sort.js'; import { generateWorkflowSecurityInputs } from './generate-workflow-security-inputs.js'; import { generateWorkflowSecurityParameters } from './generate-workflow-security-parameters.js'; export function generateWorkflowsFromDescription({ descriptionPaths, sourceDescriptionName, rootSecurity, inputsComponents, securitySchemes, }) { const workflows = []; for (const pathItemKey in descriptionPaths) { for (const pathItemObjectKey of Object.keys(descriptionPaths[pathItemKey]).sort(sortMethods)) { const methodToCheck = pathItemObjectKey.toLocaleLowerCase(); if ([ 'get', 'post', 'put', 'delete', 'patch', 'head', 'options', 'trace', 'connect', 'query', ].includes(methodToCheck.toLocaleLowerCase())) { const method = methodToCheck; const pathKey = pathItemKey .replace(/^\/|\/$/g, '') .split('/') .join('-'); const operation = descriptionPaths[pathItemKey][methodToCheck.toLowerCase()]; const operationSecurity = operation?.security || undefined; const operationId = generateOperationId(sourceDescriptionName, operation?.operationId); const operationPath = !operationId ? generateOperationPath(sourceDescriptionName, pathItemKey, method) : undefined; const workflowSecurityInputs = generateWorkflowSecurityInputs(inputsComponents, operationSecurity || rootSecurity || []); const workflowSecurityParameters = generateWorkflowSecurityParameters(inputsComponents, operationSecurity || rootSecurity || [], securitySchemes); workflows.push({ workflowId: pathKey ? `${method}-${pathKey}-workflow` : `${method}-workflow`, ...(workflowSecurityInputs && { inputs: workflowSecurityInputs }), ...(workflowSecurityParameters.length && { parameters: workflowSecurityParameters, }), steps: [ { stepId: pathKey ? `${method}-${pathKey}-step` : `${method}-step`, ...(operationId ? { operationId } : { operationPath }), ...generateParametersWithSuccessCriteria(descriptionPaths[pathItemKey][methodToCheck.toLowerCase()]?.responses), }, ], }); } } } return workflows; } function generateParametersWithSuccessCriteria(responses) { const responseCodesFromDescription = Object.keys(responses || {}); if (!responseCodesFromDescription.length) { return []; } const firstResponseCode = responseCodesFromDescription?.[0]; return { successCriteria: [{ condition: `$statusCode == ${firstResponseCode}` }] }; } function generateOperationId(sourceDescriptionName, operationId) { if (!operationId) { return undefined; } return `$sourceDescriptions.${sourceDescriptionName}.${operationId}`; } function generateOperationPath(sourceDescriptionName, path, method) { return `{$sourceDescriptions.${sourceDescriptionName}.url}#/paths/~1${path.replace(/^\//, '')}/${method}`; } //# sourceMappingURL=generate-workflows-from-description.js.map