|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { |
| 4 | + FetchingJSONSchemaStore, |
| 5 | + InputData, |
| 6 | + JSONSchemaInput, |
| 7 | + quicktype, |
| 8 | +} from 'quicktype-core'; |
| 9 | +import { schemaForTypeScriptSources } from 'quicktype-typescript-input'; |
| 10 | + |
| 11 | +import type { MethodSignature } from '../types.js'; |
| 12 | + |
| 13 | +export interface NavigationModelsOptions { |
| 14 | + specPath: string; |
| 15 | + methods: MethodSignature[]; |
| 16 | + kotlinPackageName: string; |
| 17 | +} |
| 18 | + |
| 19 | +export interface GeneratedNavigationModels { |
| 20 | + swiftModels?: string; |
| 21 | + kotlinModels?: string; |
| 22 | + modelTypeNames: string[]; |
| 23 | +} |
| 24 | + |
| 25 | +const SKIP_TYPE_TOKENS = new Set([ |
| 26 | + 'Array', |
| 27 | + 'Date', |
| 28 | + 'Map', |
| 29 | + 'Object', |
| 30 | + 'Promise', |
| 31 | + 'ReadonlyArray', |
| 32 | + 'Record', |
| 33 | + 'Set', |
| 34 | + 'any', |
| 35 | + 'boolean', |
| 36 | + 'false', |
| 37 | + 'null', |
| 38 | + 'number', |
| 39 | + 'object', |
| 40 | + 'string', |
| 41 | + 'true', |
| 42 | + 'undefined', |
| 43 | + 'unknown', |
| 44 | + 'void', |
| 45 | +]); |
| 46 | + |
| 47 | +function collectReferencedTypes(methods: MethodSignature[]): Set<string> { |
| 48 | + const referenced = new Set<string>(); |
| 49 | + |
| 50 | + for (const method of methods) { |
| 51 | + const typeTexts = [ |
| 52 | + method.returnType, |
| 53 | + ...method.params.map((param) => param.type), |
| 54 | + ]; |
| 55 | + for (const typeText of typeTexts) { |
| 56 | + const matches = typeText.match(/\b[A-Za-z_]\w*\b/g); |
| 57 | + if (!matches) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + for (const match of matches) { |
| 61 | + if (!SKIP_TYPE_TOKENS.has(match)) { |
| 62 | + referenced.add(match); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return referenced; |
| 69 | +} |
| 70 | + |
| 71 | +async function generateModelsForLanguage({ |
| 72 | + typeNames, |
| 73 | + schema, |
| 74 | + lang, |
| 75 | + kotlinPackageName, |
| 76 | +}: { |
| 77 | + typeNames: string[]; |
| 78 | + schema: object; |
| 79 | + lang: 'swift' | 'kotlin'; |
| 80 | + kotlinPackageName: string; |
| 81 | +}): Promise<string> { |
| 82 | + const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore()); |
| 83 | + |
| 84 | + for (const typeName of typeNames) { |
| 85 | + const rootSchema = JSON.parse(JSON.stringify(schema)) as { |
| 86 | + $ref?: string; |
| 87 | + }; |
| 88 | + rootSchema.$ref = `#/definitions/${typeName}`; |
| 89 | + |
| 90 | + await schemaInput.addSource({ |
| 91 | + name: typeName, |
| 92 | + schema: JSON.stringify(rootSchema), |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + const inputData = new InputData(); |
| 97 | + inputData.addInput(schemaInput); |
| 98 | + |
| 99 | + const rendererOptions = |
| 100 | + lang === 'swift' |
| 101 | + ? { |
| 102 | + 'access-level': 'public', |
| 103 | + 'mutable-properties': 'true', |
| 104 | + initializers: 'false', |
| 105 | + 'swift-5-support': 'true', |
| 106 | + } |
| 107 | + : { |
| 108 | + framework: 'just-types', |
| 109 | + package: kotlinPackageName, |
| 110 | + }; |
| 111 | + |
| 112 | + const { lines } = await quicktype({ |
| 113 | + inputData, |
| 114 | + lang, |
| 115 | + rendererOptions, |
| 116 | + }); |
| 117 | + |
| 118 | + return lines.join('\n'); |
| 119 | +} |
| 120 | + |
| 121 | +export async function generateNavigationModels({ |
| 122 | + specPath, |
| 123 | + methods, |
| 124 | + kotlinPackageName, |
| 125 | +}: NavigationModelsOptions): Promise<GeneratedNavigationModels> { |
| 126 | + const absoluteSpecPath = path.resolve(process.cwd(), specPath); |
| 127 | + |
| 128 | + if (!fs.existsSync(absoluteSpecPath)) { |
| 129 | + throw new Error(`Spec file not found: ${absoluteSpecPath}`); |
| 130 | + } |
| 131 | + |
| 132 | + const schemaData = schemaForTypeScriptSources([absoluteSpecPath]); |
| 133 | + if (!schemaData.schema) { |
| 134 | + throw new Error('Failed to generate schema from TypeScript spec'); |
| 135 | + } |
| 136 | + |
| 137 | + const parsedSchema = JSON.parse(schemaData.schema) as { |
| 138 | + definitions?: Record<string, unknown>; |
| 139 | + }; |
| 140 | + const referencedTypes = collectReferencedTypes(methods); |
| 141 | + const definitions = parsedSchema.definitions ?? {}; |
| 142 | + const modelTypeNames = [...referencedTypes].filter((typeName) => |
| 143 | + Object.hasOwn(definitions, typeName) |
| 144 | + ); |
| 145 | + |
| 146 | + if (modelTypeNames.length === 0) { |
| 147 | + return { modelTypeNames: [] }; |
| 148 | + } |
| 149 | + |
| 150 | + const [swiftModels, kotlinModels] = await Promise.all([ |
| 151 | + generateModelsForLanguage({ |
| 152 | + typeNames: modelTypeNames, |
| 153 | + schema: parsedSchema, |
| 154 | + lang: 'swift', |
| 155 | + kotlinPackageName, |
| 156 | + }), |
| 157 | + generateModelsForLanguage({ |
| 158 | + typeNames: modelTypeNames, |
| 159 | + schema: parsedSchema, |
| 160 | + lang: 'kotlin', |
| 161 | + kotlinPackageName, |
| 162 | + }), |
| 163 | + ]); |
| 164 | + |
| 165 | + return { |
| 166 | + swiftModels, |
| 167 | + kotlinModels, |
| 168 | + modelTypeNames, |
| 169 | + }; |
| 170 | +} |
0 commit comments