zod-to-x
Version:
Multi language types generation from Zod schemas.
50 lines (49 loc) • 2.12 kB
TypeScript
import { IZod2AstOpt } from "../core";
import * as Transpilers from "../transpilers";
type TargetKeys = keyof typeof Transpilers;
type Target<T extends TargetKeys> = (typeof Transpilers)[T];
type TargetOpt<K extends TargetKeys> = ConstructorParameters<Target<K>>[0];
type AstOpt = Pick<IZod2AstOpt, "strict">;
/**
* Class that provides the foundational structure for defining layered models, managing metadata,
* and exposing transpile method for all supported languages.
* It is intended to be extended by specific model implementations.
*
* @remarks
* - Children classes require the `@Domain` or other layer decorators to define the layer metadata.
* - It automatically collects properties of the model that are instances of `ZodType` and processes
* them into AST nodes.
*
* @example
* ```typescript
* @Domain({ name: 'User', file: 'user.ts' })
* class UserModel extends Zod2XModel {
* username = z.string();
* age = z.number();
* }
*
* const userModel = new UserModel({ modelName: 'User' });
* const userModelTypescript = userModel.transpile(Transpilers.Zod2Ts);
* ```
*/
export declare class Zod2XModel {
private astNodes;
private getModelName;
private getLayerMetadata;
private getAstNode;
/**
* Transpiles the model into a target language using the specified transpiler.
*
* @param target - The constructor of the target transpiler.
* @param opt - The options required to initialize the target transpiler.
* @param astOpt - Options for the AST generation.
*
* @returns A string representing the transpiled model in the target language.
*/
transpile(target: Target<"Zod2Cpp">, opt?: TargetOpt<"Zod2Cpp">, astOpt?: AstOpt): string;
transpile(target: Target<"Zod2Cpp17">, opt?: TargetOpt<"Zod2Cpp17">, astOpt?: AstOpt): string;
transpile(target: Target<"Zod2Ts">, opt?: TargetOpt<"Zod2Ts">, astOpt?: AstOpt): string;
transpile(target: Target<"Zod2Py">, opt?: TargetOpt<"Zod2Py">, astOpt?: AstOpt): string;
transpile(target: Target<"Zod2Go">, opt?: TargetOpt<"Zod2Go">, astOpt?: AstOpt): string;
}
export {};