|
| 1 | +import { OpenApi } from './openApi/v3/interfaces/OpenApi'; |
| 2 | +import { OpenApiMediaType } from './openApi/v3/interfaces/OpenApiMediaType'; |
| 3 | +import { OpenApiOperation } from './openApi/v3/interfaces/OpenApiOperation'; |
| 4 | +import { OpenApiParameter } from './openApi/v3/interfaces/OpenApiParameter'; |
| 5 | +import { OpenApiSchema } from './openApi/v3/interfaces/OpenApiSchema'; |
| 6 | +import { OpenApiServer } from './openApi/v3/interfaces/OpenApiServer'; |
| 7 | +import { getOpenApiSpec } from './utils/getOpenApiSpec'; |
| 8 | +import { Dictionary } from './utils/types'; |
| 9 | + |
| 10 | +export const generateCustomSpec = async (gen: any, input: string, output: string, config: Record<string, unknown>) => { |
| 11 | + const getSchemaRefFromContent = (content: OpenApiMediaType): string => { |
| 12 | + let ref: string = ''; |
| 13 | + |
| 14 | + ref = content.$ref || content.schema?.$ref || content.schema?.items?.$ref || ''; |
| 15 | + |
| 16 | + return ref.split('/').slice(-1)[0]; |
| 17 | + }; |
| 18 | + |
| 19 | + const list: OpenApi = await getOpenApiSpec(input); |
| 20 | + |
| 21 | + const requiredPathsList: string[] = ['/api/agreement', '/api/agreement/{id}']; |
| 22 | + |
| 23 | + const requiredPaths: OpenApi['paths'] = {}; |
| 24 | + |
| 25 | + for (const path in list.paths) { |
| 26 | + if (requiredPathsList.some(it => it === path)) { |
| 27 | + requiredPaths[path] = list.paths[path]; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + const requiredSchemasSet: Set<string> = new Set(); |
| 32 | + |
| 33 | + for (const pathName in requiredPaths) { |
| 34 | + const pathElement = requiredPaths[pathName]; |
| 35 | + |
| 36 | + const openApiPathValues = Object.values(pathElement) as ( |
| 37 | + | OpenApiOperation |
| 38 | + | OpenApiServer |
| 39 | + | OpenApiParameter |
| 40 | + | string |
| 41 | + )[]; |
| 42 | + |
| 43 | + openApiPathValues.forEach(requestMethodData => { |
| 44 | + if (typeof requestMethodData !== 'string') { |
| 45 | + if (!('url' in requestMethodData)) { |
| 46 | + if ('parameters' in requestMethodData) { |
| 47 | + // add schemas from {apiPath}/{method}/parameters |
| 48 | + requestMethodData.parameters?.forEach(parameter => |
| 49 | + requiredSchemasSet.add(getSchemaRefFromContent(parameter)) |
| 50 | + ); |
| 51 | + } |
| 52 | + if ('responses' in requestMethodData) { |
| 53 | + const responsesCodeData = Object.values(requestMethodData.responses); |
| 54 | + |
| 55 | + responsesCodeData.forEach(response => { |
| 56 | + const contentTypeData = Object.values(response.content ?? {}); |
| 57 | + |
| 58 | + // add schemas from {apiPath}/{method}/responses/{responseType}/content |
| 59 | + contentTypeData.forEach(content => { |
| 60 | + requiredSchemasSet.add(getSchemaRefFromContent(content)); |
| 61 | + }); |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + const requiredSchemas: Dictionary<OpenApiSchema> = {}; |
| 70 | + |
| 71 | + if (list && list.components && list.components.schemas) { |
| 72 | + for (const schema in list.components.schemas) { |
| 73 | + if (requiredSchemasSet.has(schema)) { |
| 74 | + requiredSchemas[schema] = list.components.schemas[schema]; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const listWithRequiredPaths: OpenApi = { |
| 80 | + ...list, |
| 81 | + paths: requiredPaths, |
| 82 | + components: { |
| 83 | + schemas: requiredSchemas, |
| 84 | + }, |
| 85 | + }; |
| 86 | + |
| 87 | + await gen(listWithRequiredPaths, output); |
| 88 | +}; |
0 commit comments