openapi-typescript
Version:
Convert OpenAPI 3.0 & 3.1 schemas to TypeScript
1 lines • 6.95 kB
Source Map (JSON)
{"version":3,"file":"path-item-object.cjs","sources":["../../src/transform/path-item-object.ts"],"sourcesContent":["import ts from \"typescript\";\nimport { NEVER, QUESTION_TOKEN, addJSDocComment, oapiRef, 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":";;;;;;;;;;;;AAmBwB,SAAA,uBAAA,CAAwB,UAA0B,OAA4C,EAAA;AACpH,EAAA,MAAM,OAAyB,EAAC;AAGhC,EAAK,IAAA,CAAA,IAAA;AAAA,IACH,GAAGA,wCAAA,CAAyB,QAAS,CAAA,UAAA,IAAc,EAAI,EAAA;AAAA,MACrD,GAAG,OAAA;AAAA,MACH,MAAM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