UNPKG

zod-to-x

Version:

Multi language types generation from Zod schemas.

246 lines (245 loc) 10.8 kB
import { ASTAliasedTypes, ASTEnum, ASTIntersection, ASTNode, ASTNodes, ASTObject, ASTType, ASTUnion } from "../core"; import { TIndentationLevels } from "../utils/string_utils"; /** * Optional user settings */ export interface IZodToXOpt extends Record<string, any> { /** * Text to add as a comment at the beginning of the output. * Useful for adding headers or documentation notes. */ header?: string; /** * Number of spaces to use for indentation in the generated code. * Defaults to 4 if not specified. */ indent?: number; /** * Determines whether to include comments in the transpiled code. * Defaults to `true`. */ includeComments?: boolean; /** * Every external type will be imported from the file where it is defined. Default: true */ useImports?: boolean; } /** * Abstract base class for transpiling Zod schemas into other programming languages. * Extend this class and implement the abstract methods to define how each Zod type * should be converted to the target language's syntax. */ export declare abstract class Zod2X<T extends IZodToXOpt> { protected output: string[]; protected indent: TIndentationLevels; protected preImports: Set<string>; protected imports: Set<string>; protected postImports: Set<string>; protected abstract readonly commentKey: string; protected opt: Partial<T>; protected constructor(opt: Partial<T>); /** * Executes custom logic before and the transpilation process begins. * Useful for additional behaviour required by transpilation procedures. */ protected abstract runBefore(): void; protected abstract runAfter(): void; /** * Adds an import statement for a specific file. * @param filename - The name of the file to import from. * @param namespace - The namespace to use for the imported types. */ protected abstract addImportFromFile(filename: string, namespace: string): string; /** * Returns the accessible type name from an external namespace. * @param namespace * @param typeName */ protected abstract getTypeFromExternalNamespace(namespace: string, typeName: string): string; /** * Returns the translation of generic templates for a given AST node. */ protected abstract getGenericTemplatesTranslation(data: ASTNode): string | undefined; /** * For Layered Modeling. * If a property type is an imported type, without any modification, the transpiled type will be * an inherited type from the imported type. * @param name * @param parentNamespace * @param aliasOf */ protected abstract addExtendedType(name: string, parentNamespace: string, aliasOf: string): void; /** * Returns the keyword representing a string type in the target language. */ protected abstract getStringType(): string; /** * Returns the keyword representing a boolean type in the target language. */ protected abstract getBooleanType(): string; /** * Returns the keyword representing a number type in the target language. * @param isInt - Indicates if the number is an integer. * @param range - Optional range constraints for the number. */ protected abstract getNumberType(isInt: boolean, range: { min?: number; max?: number; }): string; /** * Returns the representation of a literal string or number in the target language. * @param value - The literal value to represent. * @param parentEnumNameKey - Optional tuple containing the parent enum name and key reference. */ protected abstract getLiteralStringType(value: string | number | boolean, parentEnumNameKey?: [string, string]): string | number; /** * Returns the keyword representing a generic 'any' type in the target language. */ protected abstract getAnyType(): string; /** * Returns the keyword representing a date type in the target language. * @returns A string representing the date type. */ protected abstract getDateType(): string; /** * Returns the representation of a tuple type in the target language. * @param itemsType - An array of strings representing the types of the tuple's elements. * @returns A string representing the tuple type. */ protected abstract getTupleType(itemsType: string[]): string; /** * Returns the representation of a set type in the target language. * @param itemType - A string representing the type of the set's items. * @returns A string representing the set type. */ protected abstract getSetType(itemType: string): string; /** * Returns the representation of a map type in the target language. * @param keyType - A string representing the type of the map's keys. * @param valueType - A string representing the type of the map's values. * @returns A string representing the map type. */ protected abstract getMapType(keyType: string, valueType: string): string; /** * Returns the representation of a record type in the target language. * @param keyType - A string representing the type of the record's keys. * @param valueType - A string representing the type of the record's values. * @returns A string representing the record type. */ protected abstract getRecordType(keyType: string, valueType: string): string; /** * Returns the representation of an intersection type in the target language. * @param itemsType - An array of strings representing the types to intersect. * @returns A string representing the intersection type. */ protected abstract getIntersectionType(itemsType: string[]): string; /** * Returns the representation of a union type in the target language. * @param itemsType - An array of strings representing the types to union. * @returns A string representing the union type. */ protected abstract getUnionType(itemsType: string[]): string; /** * Returns the representation of an array type in the target language, accounting for array depth. * @param arrayType - A string representing the type of the array's elements. * @param arrayDeep - The number of dimensions for multi-dimensional arrays. * @returns A string representing the array type. */ protected abstract getArrayType(arrayType: string, arrayDeep: number): string; /** * Transpiles an enum type from the AST to the target language. * @param data - The AST node representing the enum. */ protected abstract transpileEnum(data: ASTEnum): void; /** * Transpiles a struct (object) type from the AST to the target language. * @param data - The AST node representing the struct. */ protected abstract transpileStruct(data: ASTObject): void; /** * Transpiles a union type from the AST to the target language. * @param data - The AST node representing the union. */ protected abstract transpileUnion(data: ASTUnion): void; /** * Transpiles an intersection type from the AST to the target language. * @param data - The AST node representing the intersection. */ protected abstract transpileIntersection(data: ASTIntersection): void; /** * Transpiles an aliased type (e.g., array) from the AST to the target language. * @param data - The AST node representing the aliased type. */ protected abstract transpileAliasedType(data: ASTAliasedTypes): void; /** * Returns a comment. */ protected getComment: (data: string, indent?: string) => string; /** * Determines if the given type token can be transpiled into the target language. * @param token - The type token to check. * @returns `true` if the type is transpilerable; otherwise, `false`. */ protected isTranspilerable(token: ASTNode): boolean; /** * Determines if the given AST node represents an aliased type. * * @param token - The AST node to evaluate. * @returns `true` if the token is an instance of an aliased type, otherwise `false`. */ protected isAliasedType(token: ASTNode): boolean; protected push0: (data: string) => number; protected push1: (data: string) => number; protected push2: (data: string) => number; protected push3: (data: string) => number; /** * Adds a comment to the transpiled output. * @param data - The comment text to add. * @param indent - Optional indentation to apply before the comment. */ protected addComment(data?: string, indent?: string): void; /** * Retrieves the equivalent type representation of an AST node in the target language. * @param token - The AST node or transpilerable type to convert. * @returns A string representing the type in the target language. */ protected getAttributeType(token: ASTType): string; /** * Determines whether a given type is an external type import. * * @param item - An object containing the `parentFile` and `parentNamespace` * properties of the type to evaluate. * @returns `true` if the type is an external type import; otherwise, `false`. */ protected isExternalTypeImport(item: ASTNode): boolean; /** * Adds an external type import to the transpiler's imports if the provided transpiled item * is located into another file and namespace, and if the `useImports` option is not disabled. * * @param item - An object of type `TranspilerableTypes` containing information * about the type to be imported, including its parent file and namespace. * @returns `true` if the import was successfully added, otherwise `false`. */ protected addExternalTypeImport(item: ASTNode): boolean; /** * Transpiles a single item from the transpiler queue. * @param item - The transpilerable type to transpile. */ private _transpileItem; /** * Constructs and returns an array of strings representing the header section * of the transpiled output. The header may include custom comments, pre-imports, * imports, and post-imports, depending on the provided options and internal state. * * Each section is separated by an empty string for readability. * * @returns An array of strings representing the header section. * */ private _getHeader; /** * Transpiles a queue of AST nodes into the target language. * @param transpilerQueue - An array of transpilerable types (AST nodes with names). * @returns The transpiled code as a string. */ transpile(transpilerQueue: ASTNodes): string; }