|
| 1 | +import type ts from 'typescript'; |
| 2 | + |
| 3 | +import { compiler, type Property } from '../../compiler'; |
| 4 | +import type { IRContext } from '../../ir/context'; |
| 5 | +import type { |
| 6 | + IROperationObject, |
| 7 | + IRParameterObject, |
| 8 | + IRPathItemObject, |
| 9 | + IRPathsObject, |
| 10 | +} from '../../ir/ir'; |
| 11 | +import { irParametersToIrSchema } from '../../ir/schema'; |
| 12 | +import type { PluginHandler } from '../types'; |
| 13 | +import { |
| 14 | + componentsToType, |
| 15 | + schemaToType, |
| 16 | + type SchemaToTypeOptions, |
| 17 | +} from '../utils/types'; |
| 18 | +import type { Config } from './types'; |
| 19 | + |
| 20 | +const FILE_ID = 'index'; |
| 21 | +const ROUTE_HANDLER_NAME = 'RouteHandler'; |
| 22 | +const OPERATIONS_IDENTIFIER = 'FastifyOpenapiGlueRouteHandlers'; |
| 23 | +const ROUTE_PROPERTY_NAME = { |
| 24 | + BODY: 'Body', |
| 25 | + HEADER: 'Headers', |
| 26 | + PATH: 'Params', |
| 27 | + QUERY: 'Querystring', |
| 28 | + RESPONSE: 'Reply', |
| 29 | +}; |
| 30 | + |
| 31 | +const parameterToProperty = ({ |
| 32 | + options, |
| 33 | + parameter, |
| 34 | + name, |
| 35 | +}: { |
| 36 | + name: string; |
| 37 | + options: SchemaToTypeOptions; |
| 38 | + parameter: Record<string, IRParameterObject>; |
| 39 | +}): Property => { |
| 40 | + const schema = irParametersToIrSchema({ |
| 41 | + parameters: parameter, |
| 42 | + }); |
| 43 | + return { |
| 44 | + isRequired: !!schema.required, |
| 45 | + name, |
| 46 | + type: schemaToType({ options, schema }), |
| 47 | + }; |
| 48 | +}; |
| 49 | + |
| 50 | +const operationToProperty = ({ |
| 51 | + operation, |
| 52 | + options, |
| 53 | +}: { |
| 54 | + operation: IROperationObject; |
| 55 | + options: SchemaToTypeOptions; |
| 56 | +}): Property => { |
| 57 | + const operationProperties: Array<Property> = []; |
| 58 | + |
| 59 | + if (operation.body) { |
| 60 | + operationProperties.push({ |
| 61 | + isRequired: operation.body.required, |
| 62 | + name: ROUTE_PROPERTY_NAME.BODY, |
| 63 | + type: schemaToType({ |
| 64 | + options, |
| 65 | + schema: operation.body.schema, |
| 66 | + }), |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + if (operation.parameters) { |
| 71 | + if (operation.parameters.header) { |
| 72 | + operationProperties.push( |
| 73 | + parameterToProperty({ |
| 74 | + name: ROUTE_PROPERTY_NAME.HEADER, |
| 75 | + options, |
| 76 | + parameter: operation.parameters.header, |
| 77 | + }), |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + if (operation.parameters.query) { |
| 82 | + operationProperties.push( |
| 83 | + parameterToProperty({ |
| 84 | + name: ROUTE_PROPERTY_NAME.QUERY, |
| 85 | + options, |
| 86 | + parameter: operation.parameters.query, |
| 87 | + }), |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + if (operation.parameters.path) { |
| 92 | + operationProperties.push( |
| 93 | + parameterToProperty({ |
| 94 | + name: ROUTE_PROPERTY_NAME.PATH, |
| 95 | + options, |
| 96 | + parameter: operation.parameters.path, |
| 97 | + }), |
| 98 | + ); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (operation.responses) { |
| 103 | + const responseProperties: Array<Property> = []; |
| 104 | + for (const code in operation.responses) { |
| 105 | + const response = operation.responses[code]; |
| 106 | + responseProperties.push({ |
| 107 | + name: code, |
| 108 | + type: schemaToType({ |
| 109 | + options, |
| 110 | + schema: response?.schema ?? {}, |
| 111 | + }), |
| 112 | + }); |
| 113 | + } |
| 114 | + operationProperties.push({ |
| 115 | + name: ROUTE_PROPERTY_NAME.RESPONSE, |
| 116 | + type: compiler.typeInterfaceNode({ |
| 117 | + properties: responseProperties, |
| 118 | + useLegacyResolution: false, |
| 119 | + }), |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + const operationType = compiler.typeInterfaceNode({ |
| 124 | + properties: operationProperties, |
| 125 | + useLegacyResolution: false, |
| 126 | + }); |
| 127 | + const property: Property = { |
| 128 | + name: operation.id, |
| 129 | + type: compiler.typeNode(ROUTE_HANDLER_NAME, [operationType]), |
| 130 | + }; |
| 131 | + return property; |
| 132 | +}; |
| 133 | + |
| 134 | +const pathsToType = ({ |
| 135 | + context, |
| 136 | + options, |
| 137 | +}: { |
| 138 | + context: IRContext; |
| 139 | + options: SchemaToTypeOptions; |
| 140 | +}): ts.Node => { |
| 141 | + const operationsProperties = []; |
| 142 | + for (const path in context.ir.paths) { |
| 143 | + const pathItem = context.ir.paths[path as keyof IRPathsObject]; |
| 144 | + for (const method in pathItem) { |
| 145 | + const operation = pathItem[method as keyof IRPathItemObject]; |
| 146 | + if (operation) { |
| 147 | + const operationProperty = operationToProperty({ operation, options }); |
| 148 | + operationsProperties.push(operationProperty); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + const identifier = context.file({ id: FILE_ID })!.identifier({ |
| 154 | + $ref: OPERATIONS_IDENTIFIER, |
| 155 | + create: true, |
| 156 | + namespace: 'type', |
| 157 | + }); |
| 158 | + const paths = compiler.typeAliasDeclaration({ |
| 159 | + exportType: true, |
| 160 | + name: identifier.name || '', |
| 161 | + type: compiler.typeInterfaceNode({ |
| 162 | + properties: operationsProperties, |
| 163 | + useLegacyResolution: false, |
| 164 | + }), |
| 165 | + }); |
| 166 | + return paths; |
| 167 | +}; |
| 168 | + |
| 169 | +export const handler: PluginHandler<Config> = ({ context, plugin }) => { |
| 170 | + const file = context.createFile({ id: FILE_ID, path: plugin.output }); |
| 171 | + const options: SchemaToTypeOptions = { file }; |
| 172 | + file.import({ asType: true, module: 'fastify', name: ROUTE_HANDLER_NAME }); |
| 173 | + componentsToType({ |
| 174 | + context, |
| 175 | + options, |
| 176 | + }); |
| 177 | + file.add(pathsToType({ context, options })); |
| 178 | +}; |
0 commit comments