UNPKG

openapi-typescript

Version:

Convert OpenAPI 3.0 & 3.1 schemas to TypeScript

1 lines 2.45 kB
{"version":3,"file":"responses-object.cjs","sources":["../../src/transform/responses-object.ts"],"sourcesContent":["import ts from \"typescript\";\nimport { NEVER, addJSDocComment, tsModifiers, oapiRef, tsPropertyIndex } from \"../lib/ts.js\";\nimport { createRef, getEntries } from \"../lib/utils.js\";\nimport type { ResponsesObject, TransformNodeOptions } from \"../types.js\";\nimport transformResponseObject from \"./response-object.js\";\n\n/**\n * Transform ResponsesObject nodes (4.8.16)\n * @see https://spec.openapis.org/oas/v3.1.0#responses-object\n */\nexport default function transformResponsesObject(\n responsesObject: ResponsesObject,\n options: TransformNodeOptions,\n): ts.TypeNode {\n const type: ts.TypeElement[] = [];\n\n for (const [responseCode, responseObject] of getEntries(responsesObject, options.ctx)) {\n const responseType =\n \"$ref\" in responseObject\n ? oapiRef(responseObject.$ref)\n : transformResponseObject(responseObject, {\n ...options,\n path: createRef([options.path, \"responses\", responseCode]),\n });\n const property = ts.factory.createPropertySignature(\n /* modifiers */ tsModifiers({ readonly: options.ctx.immutable }),\n /* name */ tsPropertyIndex(responseCode),\n /* questionToken */ undefined,\n /* type */ responseType,\n );\n addJSDocComment(responseObject, property);\n type.push(property);\n }\n\n return type.length ? ts.factory.createTypeLiteralNode(type) : NEVER;\n}\n"],"names":["responseObject","getEntries","oapiRef","transformResponseObject","createRef","ts","tsModifiers","tsPropertyIndex","addJSDocComment","NEVER"],"mappings":";;;;;;;;;;;AAUwB,SAAA,wBAAA,CACtB,iBACA,OACa,EAAA;AACb,EAAA,MAAM,OAAyB,EAAC;AAEhC,EAAW,KAAA,MAAA,CAAC,cAAcA,gBAAc,CAAA,IAAKC,iBAAW,eAAiB,EAAA,OAAA,CAAQ,GAAG,CAAG,EAAA;AACrF,IAAM,MAAA,YAAA,GACJ,UAAUD,gBACN,GAAAE,UAAA,CAAQF,iBAAe,IAAI,CAAA,GAC3BG,eAAwBH,gBAAgB,EAAA;AAAA,MACtC,GAAG,OAAA;AAAA,MACH,MAAMI,eAAU,CAAA,CAAC,QAAQ,IAAM,EAAA,WAAA,EAAa,YAAY,CAAC;AAAA,KAC1D,CAAA;AACP,IAAM,MAAA,QAAA,GAAWC,YAAG,OAAQ,CAAA,uBAAA;AAAA;AAAA,MACNC,eAAY,EAAE,QAAA,EAAU,OAAQ,CAAA,GAAA,CAAI,WAAW,CAAA;AAAA;AAAA,MAC/CC,mBAAgB,YAAY,CAAA;AAAA;AAAA,MAC5B,MAAA;AAAA;AAAA,MACA;AAAA,KACtB;AACA,IAAAC,kBAAA,CAAgBR,kBAAgB,QAAQ,CAAA;AACxC,IAAA,IAAA,CAAK,KAAK,QAAQ,CAAA;AAAA;AAGpB,EAAA,OAAO,KAAK,MAAS,GAAAK,WAAA,CAAG,OAAQ,CAAA,qBAAA,CAAsB,IAAI,CAAI,GAAAI,QAAA;AAChE;;;;"}