|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Fixes OCAPI OpenAPI spec for proper openapi-fetch typing: |
| 4 | + * 1. Converts 'default' responses to '200' for success typing |
| 5 | + * 2. Adds fault schema for error responses |
| 6 | + * 3. Adds 'default' error response referencing fault schema |
| 7 | + */ |
| 8 | +import fs from 'node:fs'; |
| 9 | +import path from 'node:path'; |
| 10 | +import {fileURLToPath} from 'node:url'; |
| 11 | + |
| 12 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 13 | +const specFile = path.join(__dirname, '..', 'specs', 'data-api.json'); |
| 14 | + |
| 15 | +console.log(`Reading ${specFile}...`); |
| 16 | +const spec = JSON.parse(fs.readFileSync(specFile, 'utf-8')); |
| 17 | + |
| 18 | +// Add fault schema based on OCAPI documentation |
| 19 | +const faultSchema = { |
| 20 | + type: 'object', |
| 21 | + description: 'OCAPI error/fault response returned for 4xx/5xx status codes', |
| 22 | + properties: { |
| 23 | + _v: { |
| 24 | + type: 'string', |
| 25 | + description: 'API version', |
| 26 | + }, |
| 27 | + fault: { |
| 28 | + type: 'object', |
| 29 | + required: ['type', 'message'], |
| 30 | + properties: { |
| 31 | + type: { |
| 32 | + type: 'string', |
| 33 | + description: 'Error type identifier (e.g., NotFoundException, CodeVersionIdNotFoundException)', |
| 34 | + }, |
| 35 | + message: { |
| 36 | + type: 'string', |
| 37 | + description: 'Human-readable error message', |
| 38 | + }, |
| 39 | + arguments: { |
| 40 | + type: 'object', |
| 41 | + description: 'Map of argument values integrated into the message', |
| 42 | + additionalProperties: { |
| 43 | + type: 'object', |
| 44 | + properties: { |
| 45 | + type: { |
| 46 | + type: 'string', |
| 47 | + enum: ['boolean', 'date', 'datetime', 'decimal', 'integer', 'string', 'time'], |
| 48 | + }, |
| 49 | + value: {}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | +}; |
| 57 | + |
| 58 | +// Add fault schema to components |
| 59 | +if (!spec.components) spec.components = {}; |
| 60 | +if (!spec.components.schemas) spec.components.schemas = {}; |
| 61 | +spec.components.schemas.fault = faultSchema; |
| 62 | + |
| 63 | +let transformedCount = 0; |
| 64 | + |
| 65 | +// Transform all paths |
| 66 | +for (const pathItem of Object.values(spec.paths || {})) { |
| 67 | + for (const [method, operation] of Object.entries(pathItem)) { |
| 68 | + if (method === 'parameters') continue; |
| 69 | + if (!operation.responses) continue; |
| 70 | + |
| 71 | + // Convert 'default' to '200' if no 200 exists |
| 72 | + if (operation.responses.default && !operation.responses['200']) { |
| 73 | + operation.responses['200'] = operation.responses.default; |
| 74 | + delete operation.responses.default; |
| 75 | + } |
| 76 | + |
| 77 | + // Add default error response if not present |
| 78 | + if (!operation.responses.default) { |
| 79 | + operation.responses.default = { |
| 80 | + description: 'Error response', |
| 81 | + content: { |
| 82 | + 'application/json': { |
| 83 | + schema: { |
| 84 | + $ref: '#/components/schemas/fault', |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }; |
| 89 | + transformedCount++; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +console.log(`Added fault schema and ${transformedCount} default error responses`); |
| 95 | + |
| 96 | +fs.writeFileSync(specFile, JSON.stringify(spec, null, 2)); |
| 97 | +console.log(`Updated ${specFile}`); |
0 commit comments