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