All files / utils shared.ts

60.24% Statements 50/83
38.57% Branches 27/70
55.55% Functions 5/9
61.53% Lines 48/78

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 1589x             9x                 9x 6x     6x   6x                 6x                 6x 6x 5x     1x     9x                                       126x         126x   95x 95x         21x 21x   6x 6x   4x 4x                     126x     9x 19x 19x 19x       19x 19x 122x   122x     122x     122x 122x         122x 120x     122x 41x   81x       19x     9x 14x 14x           9x 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 || [];
    Iif (!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 if (value.type) {
            zodType = jsonSchemaPropertiesToTSTypes(value);
        } else E{
            throw new Error(`Missing 'type' property in JSON schema for key: ${key}`);
        }
 
        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
    }
}