openapi-typescript
Version:
Convert OpenAPI 3.0 & 3.1 schemas to TypeScript
1 lines • 6.74 kB
Source Map (JSON)
{"version":3,"file":"path-item-object.mjs","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":[],"mappings":";;;;;;AAmBA,SAAwB,uBAAA,CAAwB,UAA0B,OAAA,EAA4C;AACpH,EAAA,MAAM,OAAyB,EAAC;AAGhC,EAAA,IAAA,CAAK,IAAA;AAAA,IACH,GAAG,wBAAA,CAAyB,QAAA,CAAS,UAAA,IAAc,EAAC,EAAG;AAAA,MACrD,GAAG,OAAA;AAAA,MACH,MAAM,SAAA,CAAU,CAAC,OAAA,CAAQ,IAAA,EAAM,YAAY,CAAC;AAAA,KAC7C;AAAA,GACH;AAGA,EAAA,KAAA,MAAW,MAAA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