UNPKG

openapi-typescript

Version:

Convert OpenAPI 3.0 & 3.1 schemas to TypeScript

1 lines 6.99 kB
{"version":3,"file":"path-item-object.cjs","sources":["../../src/transform/path-item-object.ts"],"sourcesContent":["import ts from \"typescript\";\nimport { addJSDocComment, NEVER, oapiRef, QUESTION_TOKEN, tsModifiers, tsPropertyIndex } from \"../lib/ts.js\";\nimport { createRef } from \"../lib/utils.js\";\nimport type {\n OperationObject,\n ParameterObject,\n PathItemObject,\n ReferenceObject,\n TransformNodeOptions,\n} from \"../types.js\";\nimport transformOperationObject, { injectOperationObject } from \"./operation-object.js\";\nimport { transformParametersArray } from \"./parameters-array.js\";\n\nexport type Method = \"get\" | \"put\" | \"post\" | \"delete\" | \"options\" | \"head\" | \"patch\" | \"trace\";\n\n/**\n * Transform PathItem nodes (4.8.9)\n * @see https://spec.openapis.org/oas/v3.1.0#path-item-object\n */\nexport default function transformPathItemObject(pathItem: PathItemObject, options: TransformNodeOptions): ts.TypeNode {\n const type: ts.TypeElement[] = [];\n\n // parameters\n type.push(\n ...transformParametersArray(pathItem.parameters ?? [], {\n ...options,\n path: createRef([options.path, \"parameters\"]),\n }),\n );\n\n // methods\n for (const method of [\"get\", \"put\", \"post\", \"delete\", \"options\", \"head\", \"patch\", \"trace\"] as Method[]) {\n const operationObject = pathItem[method];\n if (\n !operationObject ||\n (options.ctx.excludeDeprecated &&\n (\"$ref\" in operationObject ? options.ctx.resolve<OperationObject>(operationObject.$ref) : operationObject)\n ?.deprecated)\n ) {\n type.push(\n ts.factory.createPropertySignature(\n /* modifiers */ tsModifiers({ readonly: options.ctx.immutable }),\n /* name */ tsPropertyIndex(method),\n /* questionToken */ QUESTION_TOKEN,\n /* type */ NEVER,\n ),\n );\n continue;\n }\n\n // fold top-level PathItem parameters into method-level, with the latter overriding the former\n const keyedParameters: Record<string, ParameterObject | ReferenceObject> = {};\n if (!(\"$ref\" in operationObject)) {\n // important: OperationObject parameters come last, and will override any conflicts with PathItem parameters\n for (const parameter of [...(pathItem.parameters ?? []), ...(operationObject.parameters ?? [])]) {\n // fix: #1798, use unique key\n const name =\n \"$ref\" in parameter\n ? `${options.ctx.resolve<ParameterObject>(parameter.$ref)?.in}-${options.ctx.resolve<ParameterObject>(parameter.$ref)?.name}`\n : `${parameter.in}-${parameter.name}`;\n if (name) {\n keyedParameters[name] = parameter;\n }\n }\n }\n\n let operationType: ts.TypeNode;\n if (\"$ref\" in operationObject) {\n operationType = oapiRef(operationObject.$ref);\n }\n // if operationId exists, move into an `operations` export and pass the reference in here\n else if (operationObject.operationId) {\n // workaround for issue caused by redocly ref parsing: https://github.com/openapi-ts/openapi-typescript/issues/1542\n const operationId = operationObject.operationId.replace(HASH_RE, \"/\");\n operationType = oapiRef(createRef([\"operations\", operationId]));\n injectOperationObject(\n operationId,\n { ...operationObject, parameters: Object.values(keyedParameters) },\n { ...options, path: createRef([options.path, method]) },\n );\n } else {\n operationType = ts.factory.createTypeLiteralNode(\n transformOperationObject(\n { ...operationObject, parameters: Object.values(keyedParameters) },\n { ...options, path: createRef([options.path, method]) },\n ),\n );\n }\n const property = ts.factory.createPropertySignature(\n /* modifiers */ tsModifiers({ readonly: options.ctx.immutable }),\n /* name */ tsPropertyIndex(method),\n /* questionToken */ undefined,\n /* type */ operationType,\n );\n addJSDocComment(operationObject, property);\n type.push(property);\n }\n\n return ts.factory.createTypeLiteralNode(type);\n}\n\nconst HASH_RE = /#/g;\n"],"names":["transformParametersArray","createRef","operationObject","ts","tsModifiers","tsPropertyIndex","QUESTION_TOKEN","NEVER","oapiRef","injectOperationObject","transformOperationObject","addJSDocComment"],"mappings":";;;;;;;;;;;;AAmBA,SAAwB,uBAAA,CAAwB,UAA0B,OAAA,EAA4C;AACpH,EAAA,MAAM,OAAyB,EAAC;AAGhC,EAAA,IAAA,CAAK,IAAA;AAAA,IACH,GAAGA,wCAAA,CAAyB,QAAA,CAAS,UAAA,IAAc,EAAC,EAAG;AAAA,MACrD,GAAG,OAAA;AAAA,MACH,MAAMC,eAA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