( apiSchema: ApiSchema, nonDefaultableTypes: Iterable<string> = [], )
| 1396 | } |
| 1397 | |
| 1398 | function generateApiTypesCode( |
| 1399 | apiSchema: ApiSchema, |
| 1400 | nonDefaultableTypes: Iterable<string> = [], |
| 1401 | ): string { |
| 1402 | const definitions = collectDefinitions(apiSchema as Record<string, unknown>); |
| 1403 | const defCollections = collectDefinitionCollections( |
| 1404 | apiSchema as Record<string, unknown>, |
| 1405 | ); |
| 1406 | const ctx = makeCtx(defCollections, { nonDefaultableTypes }); |
| 1407 | |
| 1408 | // Collect all RPC methods before emitting shared definitions so method stability |
| 1409 | // can propagate to referenced data types. |
| 1410 | const methodEntries: { method: RpcMethod; isSession: boolean }[] = []; |
| 1411 | for (const { group, isSession } of [ |
| 1412 | { group: apiSchema.server, isSession: false }, |
| 1413 | { group: apiSchema.session, isSession: true }, |
| 1414 | { group: apiSchema.clientSession, isSession: false }, |
| 1415 | ]) { |
| 1416 | if (group) { |
| 1417 | methodEntries.push( |
| 1418 | ...collectRpcMethods(group as Record<string, unknown>).map((method) => ({ |
| 1419 | method, |
| 1420 | isSession, |
| 1421 | })), |
| 1422 | ); |
| 1423 | } |
| 1424 | } |
| 1425 | const allMethods = methodEntries.map(({ method }) => method); |
| 1426 | const inlineMethodParamSchemas = new Map<string, JSONSchema7>(); |
| 1427 | const sortedNames = (names: Iterable<string> | undefined): string[] => |
| 1428 | [...(names ?? [])].sort(); |
| 1429 | const schemaPropertyNames = (schema: JSONSchema7): string[] => |
| 1430 | sortedNames(Object.keys(schema.properties ?? {})); |
| 1431 | const shouldPreferMethodParamSchema = ( |
| 1432 | typeName: string, |
| 1433 | paramsSchema: JSONSchema7, |
| 1434 | ): boolean => { |
| 1435 | const definition = definitions[typeName]; |
| 1436 | if (typeof definition !== "object" || definition === null) return false; |
| 1437 | const definitionSchema = asGeneratedObjectSchema( |
| 1438 | definition as JSONSchema7, |
| 1439 | defCollections, |
| 1440 | ); |
| 1441 | if (!definitionSchema) return false; |
| 1442 | |
| 1443 | return ( |
| 1444 | JSON.stringify(schemaPropertyNames(paramsSchema)) !== |
| 1445 | JSON.stringify(schemaPropertyNames(definitionSchema)) || |
| 1446 | JSON.stringify(sortedNames(paramsSchema.required)) !== |
| 1447 | JSON.stringify(sortedNames(definitionSchema.required)) |
| 1448 | ); |
| 1449 | }; |
| 1450 | for (const { method, isSession } of methodEntries) { |
| 1451 | const params = method.params as (JSONSchema7 & { $ref?: string }) | undefined; |
| 1452 | if (!params || typeof params.$ref === "string") continue; |
| 1453 | const paramsSchema = getMethodParamsObjectSchema( |
| 1454 | method, |
| 1455 | defCollections, |
no test coverage detected
searching dependent graphs…