|
| 1 | +import { OpenAPIV3 } from 'openapi-types'; |
| 2 | + |
| 3 | +export class GlobalParameterConsolidator { |
| 4 | + private root: OpenAPIV3.Document; |
| 5 | + private globalParamRefs: Set<string> = new Set(); |
| 6 | + private globalParamDefinitions: Map<string, any> = new Map(); |
| 7 | + |
| 8 | + constructor(root: OpenAPIV3.Document) { |
| 9 | + this.root = root; |
| 10 | + } |
| 11 | + |
| 12 | + /** |
| 13 | + * Consolidates global query parameters into a single globalParams object. |
| 14 | + * |
| 15 | + */ |
| 16 | + consolidate(): OpenAPIV3.Document { |
| 17 | + this.discoverGlobalParameters(); |
| 18 | + |
| 19 | + if (this.globalParamRefs.size === 0) { |
| 20 | + console.log('No global parameters found'); |
| 21 | + return this.root; |
| 22 | + } |
| 23 | + |
| 24 | + this.createGlobalParamsSchema(); |
| 25 | + |
| 26 | + this.createGlobalParamsParameter(); |
| 27 | + |
| 28 | + this.replaceGlobalParamsInPaths(); |
| 29 | + |
| 30 | + return this.root; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Check all parameters marked with x-global: true |
| 35 | + */ |
| 36 | + private discoverGlobalParameters(): void { |
| 37 | + if (!this.root.components?.parameters) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // Check parameters for x-global: true |
| 42 | + for (const [paramKey, paramDef] of Object.entries(this.root.components.parameters)) { |
| 43 | + if (!paramDef) continue; |
| 44 | + |
| 45 | + const param = paramDef as any; |
| 46 | + if (param['x-global'] === true || param['x-global'] === 'true') { |
| 47 | + const paramRef = `#/components/parameters/${paramKey}`; |
| 48 | + this.globalParamRefs.add(paramRef); |
| 49 | + this.globalParamDefinitions.set(paramKey, param); |
| 50 | + console.log(`Found global parameter: ${paramKey}`); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private createGlobalParamsSchema(): void { |
| 56 | + if (!this.root.components) { |
| 57 | + this.root.components = {}; |
| 58 | + } |
| 59 | + if (!this.root.components.schemas) { |
| 60 | + this.root.components.schemas = {}; |
| 61 | + } |
| 62 | + |
| 63 | + const properties: Record<string, any> = {}; |
| 64 | + |
| 65 | + for (const [paramKey, paramDef] of this.globalParamDefinitions) { |
| 66 | + const paramName = paramDef.name || paramKey; |
| 67 | + const schema = paramDef.schema || {}; |
| 68 | + |
| 69 | + const propertyObj: any = { |
| 70 | + ...schema, |
| 71 | + description: paramDef.description || schema.description, |
| 72 | + }; |
| 73 | + |
| 74 | + for (const key in paramDef) { |
| 75 | + if (key.startsWith('x-')) { |
| 76 | + propertyObj[key] = paramDef[key]; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + properties[paramName] = propertyObj; |
| 81 | + } |
| 82 | + |
| 83 | + const globalParamsSchema: OpenAPIV3.SchemaObject = { |
| 84 | + type: 'object', |
| 85 | + description: 'Global query parameters that apply to all operations', |
| 86 | + properties, |
| 87 | + }; |
| 88 | + |
| 89 | + (this.root.components.schemas as any).GlobalParams = globalParamsSchema; |
| 90 | + } |
| 91 | + |
| 92 | + private createGlobalParamsParameter(): void { |
| 93 | + if (!this.root.components) { |
| 94 | + this.root.components = {}; |
| 95 | + } |
| 96 | + if (!this.root.components.parameters) { |
| 97 | + this.root.components.parameters = {}; |
| 98 | + } |
| 99 | + |
| 100 | + const globalParamsParam: any = { |
| 101 | + name: 'globalParams', |
| 102 | + in: 'query', |
| 103 | + description: 'Global query parameters', |
| 104 | + schema: { |
| 105 | + $ref: '#/components/schemas/GlobalParams', |
| 106 | + }, |
| 107 | + 'x-global': true, |
| 108 | + }; |
| 109 | + |
| 110 | + (this.root.components.parameters as any).globalParams = globalParamsParam; |
| 111 | + } |
| 112 | + |
| 113 | + private replaceGlobalParamsInPaths(): void { |
| 114 | + if (!this.root.paths) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + for (const pathKey in this.root.paths) { |
| 119 | + const pathItem = this.root.paths[pathKey]; |
| 120 | + if (!pathItem) continue; |
| 121 | + |
| 122 | + const methods = ['get', 'post', 'put', 'delete', 'patch', 'options', 'head', 'trace']; |
| 123 | + for (const method of methods) { |
| 124 | + const operation = (pathItem as any)[method] as OpenAPIV3.OperationObject | undefined; |
| 125 | + if (!operation) continue; |
| 126 | + |
| 127 | + this.replaceGlobalParamsInOperation(operation); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private replaceGlobalParamsInOperation(operation: OpenAPIV3.OperationObject): void { |
| 133 | + if (!operation.parameters) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + const indicesToRemove: number[] = []; |
| 138 | + let hasGlobalParams = false; |
| 139 | + |
| 140 | + for (let i = 0; i < operation.parameters.length; i++) { |
| 141 | + const param = operation.parameters[i]; |
| 142 | + const paramRef = (param as any).$ref; |
| 143 | + |
| 144 | + if (paramRef && this.globalParamRefs.has(paramRef)) { |
| 145 | + indicesToRemove.push(i); |
| 146 | + hasGlobalParams = true; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + for (let i = indicesToRemove.length - 1; i >= 0; i--) { |
| 151 | + operation.parameters.splice(indicesToRemove[i], 1); |
| 152 | + } |
| 153 | + |
| 154 | + if (hasGlobalParams) { |
| 155 | + const globalParamsRef: OpenAPIV3.ReferenceObject = { |
| 156 | + $ref: '#/components/parameters/globalParams', |
| 157 | + }; |
| 158 | + operation.parameters.push(globalParamsRef as any); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments