Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | 17x 17x 17x 6x 6x 6x 6x 6x 6x 5x 1x 17x 11699x 11699x 7762x 7762x 2821x 2821x 597x 597x 405x 405x 114x 114x 11699x 17x 2974x 2974x 2974x 114x 2860x 2860x 11294x 11294x 11294x 11294x 11294x 11294x 11292x 11294x 6364x 4930x 2860x 17x 107x 107x 17x 2x 2x 2x 1x | import z from "zod"; import logger from "./logger"; type SchemaTypeToTsType = { [key: string]: any; }; const PYDANTIC_TYPE_TO_TS_TYPE: SchemaTypeToTsType = { string: String, integer: Number, number: Number, boolean: Boolean, null: null, }; export function jsonSchemaToTsType(jsonSchema: Record<string, any>): any { Iif (!jsonSchema.type) { jsonSchema.type = "string"; } const type = jsonSchema.type as string; Iif (type === "array") { const itemsSchema = jsonSchema.items; Iif (itemsSchema) { const ItemType = jsonSchemaToTsType(itemsSchema); return ItemType; } return Array; } Iif (type === "object") { const properties = jsonSchema.properties; Iif (properties) { const nestedModel = jsonSchemaToModel(jsonSchema); return nestedModel; } return Object; } const tsType = PYDANTIC_TYPE_TO_TS_TYPE[type]; if (tsType !== undefined) { return tsType; } throw new Error(`Unsupported JSON schema type: ${type}`); } export function jsonSchemaToTsField( name: string, jsonSchema: Record<string, any>, required: string[] ): [any, any] { const description = jsonSchema.description; const examples = jsonSchema.examples || []; return [ jsonSchemaToTsType(jsonSchema), { description: description, examples: examples, required: required.includes(name), default: required.includes(name) ? undefined : null, }, ]; } function jsonSchemaPropertiesToTSTypes(value: any): z.ZodTypeAny { Iif (!value.type) { return z.object({}); } let zodType; switch (value.type) { case "string": zodType = z .string() .describe( (value.description || "") + (value.examples ? `\nExamples: ${value.examples.join(", ")}` : "") ); break; case "number": zodType = z .number() .describe( (value.description || "") + (value.examples ? `\nExamples: ${value.examples.join(", ")}` : "") ); break; case "integer": zodType = z .number() .int() .describe( (value.description || "") + (value.examples ? `\nExamples: ${value.examples.join(", ")}` : "") ); break; case "boolean": zodType = z .boolean() .describe( (value.description || "") + (value.examples ? `\nExamples: ${value.examples.join(", ")}` : "") ); break; case "array": zodType = z .array(jsonSchemaPropertiesToTSTypes(value.items)) .describe( (value.description || "") + (value.examples ? `\nExamples: ${value.examples.join(", ")}` : "") ); break; case "object": zodType = jsonSchemaToModel(value).describe( (value.description || "") + (value.examples ? `\nExamples: ${value.examples.join(", ")}` : "") ); break; case "null": zodType = z.null().describe(value.description || ""); break; default: throw new Error(`Unsupported JSON schema type: ${value.type}`); } return zodType; } export function jsonSchemaToModel( jsonSchema: Record<string, any> ): z.ZodObject<any> { const properties = jsonSchema.properties; const requiredFields = jsonSchema.required || []; if (!properties) { return z.object({}); } const zodSchema: Record<string, any> = {}; for (const [key, _] of Object.entries(properties)) { const value = _ as any; let zodType; Iif (value.anyOf) { const anyOfTypes = value.anyOf.map((schema: any) => jsonSchemaPropertiesToTSTypes(schema) ); zodType = z .union(anyOfTypes) .describe( (value.description || "") + (value.examples ? `\nExamples: ${value.examples.join(", ")}` : "") ); } else Iif (value.allOf) { const allOfTypes = value.allOf.map((schema: any) => jsonSchemaPropertiesToTSTypes(schema) ); zodType = z .intersection( allOfTypes[0], allOfTypes .slice(1) .reduce( (acc: z.ZodTypeAny, schema: z.ZodTypeAny) => acc.and(schema), allOfTypes[0] ) ) .describe( (value.description || "") + (value.examples ? `\nExamples: ${value.examples.join(", ")}` : "") ); } else { Iif (!value.type) { value.type = "string"; } zodType = jsonSchemaPropertiesToTSTypes(value); } if (value.description) { zodType = zodType.describe(value.description); } if (requiredFields.includes(key)) { zodSchema[key] = zodType; } else { zodSchema[key] = zodType.optional(); } } return z.object(zodSchema); } export const getEnvVariable = ( name: string, defaultValue: string | undefined = undefined ): string | undefined => { try { return process.env[name] || defaultValue; } catch (_e) { return defaultValue; } }; export const nodeExternalRequire = (name: string) => { try { if (typeof process !== "undefined") { return require(name); } else E{ return require(`external:${name}`); } } catch (err) { return null; } }; |