()
| 144 | } |
| 145 | |
| 146 | function extractEndpointInputs(): Map<string, EndpointFields> { |
| 147 | const program = ts.createProgram([ENDPOINTS_DTS], { |
| 148 | target: ts.ScriptTarget.ES2022, |
| 149 | skipLibCheck: true, |
| 150 | }) |
| 151 | const checker = program.getTypeChecker() |
| 152 | const source = program.getSourceFile(ENDPOINTS_DTS) |
| 153 | if (!source) throw new Error(`Could not load ${ENDPOINTS_DTS}`) |
| 154 | |
| 155 | let mapType: ts.Type | undefined |
| 156 | source.forEachChild((node) => { |
| 157 | if ( |
| 158 | ts.isTypeAliasDeclaration(node) && |
| 159 | node.name.text === 'EndpointTypeMap' |
| 160 | ) { |
| 161 | mapType = checker.getTypeAtLocation(node.name) |
| 162 | } |
| 163 | }) |
| 164 | if (!mapType) throw new Error('EndpointTypeMap not found in endpoints.d.ts') |
| 165 | |
| 166 | const endpoints = new Map<string, EndpointFields>() |
| 167 | for (const endpoint of mapType.getProperties()) { |
| 168 | const endpointType = checker.getTypeOfSymbol(endpoint) |
| 169 | const inputSymbol = endpointType.getProperty('input') |
| 170 | if (!inputSymbol) continue |
| 171 | const inputType = checker.getTypeOfSymbol(inputSymbol) |
| 172 | |
| 173 | const fields = new Set<string>() |
| 174 | const isList = new Map<string, boolean>() |
| 175 | for (const field of inputType.getProperties()) { |
| 176 | const name = field.getName() |
| 177 | fields.add(name) |
| 178 | const fieldType = checker.getTypeOfSymbol(field) |
| 179 | isList.set(name, typeAcceptsArray(checker, fieldType)) |
| 180 | } |
| 181 | |
| 182 | const outputSymbol = endpointType.getProperty('output') |
| 183 | const producesVideo = outputSymbol |
| 184 | ? checker |
| 185 | .getTypeOfSymbol(outputSymbol) |
| 186 | .getProperties() |
| 187 | .some((p) => p.getName() === 'video' || p.getName() === 'videos') |
| 188 | : false |
| 189 | |
| 190 | endpoints.set(endpoint.getName(), { fields, isList, producesVideo }) |
| 191 | } |
| 192 | return endpoints |
| 193 | } |
| 194 | |
| 195 | function typeAcceptsArray(checker: ts.TypeChecker, type: ts.Type): boolean { |
| 196 | const parts = type.isUnion() ? type.types : [type] |
no test coverage detected