|
| 1 | +import type { IR } from '@hey-api/shared'; |
| 2 | +import { toCase } from '@hey-api/shared'; |
| 3 | + |
| 4 | +import type { $ } from '../../../../py-dsl'; |
| 5 | +// import { py } from '../../../../ts-python'; |
| 6 | +import type { HeyApiSdkPlugin } from '../types'; |
| 7 | +import { getSignatureParameters } from './signature'; |
| 8 | + |
| 9 | +type OperationParameters = { |
| 10 | + bodyRef?: string; |
| 11 | + parameters: Array<ReturnType<typeof $.param>>; |
| 12 | + // parameters: Array<{ |
| 13 | + // annotation?: py.Expression; |
| 14 | + // defaultValue?: py.Expression; |
| 15 | + // name: string; |
| 16 | + // }>; |
| 17 | +}; |
| 18 | + |
| 19 | +const PYTHON_BUILTIN_TYPES: Record<string, string> = { |
| 20 | + array: 'list', |
| 21 | + boolean: 'bool', |
| 22 | + integer: 'int', |
| 23 | + number: 'float', |
| 24 | + object: 'dict', |
| 25 | + string: 'str', |
| 26 | +}; |
| 27 | + |
| 28 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 29 | +function schemaToPythonType(schema: IR.SchemaObject, plugin: HeyApiSdkPlugin['Instance']): string { |
| 30 | + if (schema.$ref) { |
| 31 | + return toCase(schema.$ref.split('/').pop()!, 'PascalCase'); |
| 32 | + } |
| 33 | + |
| 34 | + if (schema.type === 'array') { |
| 35 | + const itemsSchema = schema.items as IR.SchemaObject | undefined; |
| 36 | + const itemType = itemsSchema ? schemaToPythonType(itemsSchema, plugin) : 'Any'; |
| 37 | + return `list[${itemType}]`; |
| 38 | + } |
| 39 | + |
| 40 | + if (schema.type === 'object' || schema.additionalProperties) { |
| 41 | + if (schema.additionalProperties && typeof schema.additionalProperties === 'object') { |
| 42 | + const valueType = schemaToPythonType(schema.additionalProperties as IR.SchemaObject, plugin); |
| 43 | + return `dict[str, ${valueType}]`; |
| 44 | + } |
| 45 | + return 'dict[str, Any]'; |
| 46 | + } |
| 47 | + |
| 48 | + if (schema.type === 'tuple') { |
| 49 | + const itemsSchema = schema.items as IR.SchemaObject | IR.SchemaObject[] | undefined; |
| 50 | + const itemTypes = itemsSchema |
| 51 | + ? Array.isArray(itemsSchema) |
| 52 | + ? itemsSchema.map((item) => schemaToPythonType(item, plugin)) |
| 53 | + : [schemaToPythonType(itemsSchema, plugin)] |
| 54 | + : []; |
| 55 | + return `tuple[${itemTypes.join(', ')}]`; |
| 56 | + } |
| 57 | + |
| 58 | + const builtinType = schema.type ? PYTHON_BUILTIN_TYPES[schema.type] : 'Any'; |
| 59 | + return builtinType ?? 'Any'; |
| 60 | +} |
| 61 | + |
| 62 | +export function operationParameters({ |
| 63 | + operation, |
| 64 | + plugin, |
| 65 | +}: { |
| 66 | + operation: IR.OperationObject; |
| 67 | + plugin: HeyApiSdkPlugin['Instance']; |
| 68 | +}): OperationParameters { |
| 69 | + const result: OperationParameters = { |
| 70 | + parameters: [], |
| 71 | + }; |
| 72 | + |
| 73 | + if (plugin.config.paramsStructure === 'flat') { |
| 74 | + const signature = getSignatureParameters({ operation }); |
| 75 | + if (!signature) return result; |
| 76 | + |
| 77 | + // result.bodyRef = signature.bodyRef; |
| 78 | + |
| 79 | + // for (const param of opParameters.parameters) { |
| 80 | + // if (param.name === '*') { |
| 81 | + // continue; |
| 82 | + // } |
| 83 | + // node.param(param.name, (p) => p.type(param.annotation).default(param.defaultValue)); |
| 84 | + // } |
| 85 | + |
| 86 | + // const pathParams: OperationParameters['parameters'] = []; |
| 87 | + // const requiredParams: OperationParameters['parameters'] = []; |
| 88 | + // const optionalParams: OperationParameters['parameters'] = []; |
| 89 | + |
| 90 | + // const paramNames = Object.keys(signature.parameters); |
| 91 | + |
| 92 | + // for (const paramName of paramNames) { |
| 93 | + // const param = signature.parameters[paramName]!; |
| 94 | + |
| 95 | + // if (param.in === 'path') { |
| 96 | + // const type = schemaToPythonType(param.schema, plugin); |
| 97 | + // pathParams.push({ |
| 98 | + // annotation: py.factory.createIdentifier(type), |
| 99 | + // name: param.name, |
| 100 | + // }); |
| 101 | + // continue; |
| 102 | + // } |
| 103 | + |
| 104 | + // if (param.in === 'body' && param.schema.$ref) { |
| 105 | + // const refName = toCase(param.schema.$ref.split('/').pop()!, 'PascalCase'); |
| 106 | + // if (param.isRequired) { |
| 107 | + // requiredParams.push({ |
| 108 | + // annotation: py.factory.createIdentifier(refName), |
| 109 | + // name: param.name, |
| 110 | + // }); |
| 111 | + // } else { |
| 112 | + // optionalParams.push({ |
| 113 | + // annotation: py.factory.createIdentifier(`${refName} | None`), |
| 114 | + // defaultValue: py.factory.createLiteral(null), |
| 115 | + // name: param.name, |
| 116 | + // }); |
| 117 | + // } |
| 118 | + // continue; |
| 119 | + // } |
| 120 | + |
| 121 | + // const type = schemaToPythonType(param.schema, plugin); |
| 122 | + |
| 123 | + // if (param.isRequired) { |
| 124 | + // requiredParams.push({ |
| 125 | + // annotation: py.factory.createIdentifier(type), |
| 126 | + // name: param.name, |
| 127 | + // }); |
| 128 | + // } else { |
| 129 | + // let defaultValue: py.Expression = py.factory.createLiteral(null); |
| 130 | + // if (param.schema.default !== undefined) { |
| 131 | + // const defaultVal = param.schema.default; |
| 132 | + // if ( |
| 133 | + // typeof defaultVal === 'string' || |
| 134 | + // typeof defaultVal === 'number' || |
| 135 | + // typeof defaultVal === 'boolean' |
| 136 | + // ) { |
| 137 | + // defaultValue = py.factory.createLiteral(defaultVal); |
| 138 | + // } else { |
| 139 | + // defaultValue = py.factory.createLiteral(null); |
| 140 | + // } |
| 141 | + // } else if (type.startsWith('list') || type.startsWith('dict')) { |
| 142 | + // defaultValue = py.factory.createLiteral(null); |
| 143 | + // } |
| 144 | + |
| 145 | + // optionalParams.push({ |
| 146 | + // annotation: py.factory.createIdentifier(`${type} | None`), |
| 147 | + // defaultValue, |
| 148 | + // name: param.name, |
| 149 | + // }); |
| 150 | + // } |
| 151 | + // } |
| 152 | + |
| 153 | + // if (pathParams.length > 0) { |
| 154 | + // result.parameters.push(...pathParams); |
| 155 | + // } |
| 156 | + |
| 157 | + // if (requiredParams.length > 0 || optionalParams.length > 0) { |
| 158 | + // result.parameters.push({ name: '*' }); |
| 159 | + // result.parameters.push(...requiredParams); |
| 160 | + // result.parameters.push(...optionalParams); |
| 161 | + // } |
| 162 | + |
| 163 | + // result.parameters.push({ |
| 164 | + // annotation: py.factory.createIdentifier('float | None'), |
| 165 | + // defaultValue: py.factory.createLiteral(null), |
| 166 | + // name: 'timeout', |
| 167 | + // }); |
| 168 | + } |
| 169 | + |
| 170 | + return result; |
| 171 | +} |
0 commit comments