zod-to-x
Version:
Multi language types generation from Zod schemas.
127 lines (126 loc) • 4.54 kB
TypeScript
import { IZod2xLayerMetadata } from "../lib/zod_ext";
/**
* A decorator function to create a layered model by extending a class with additional metadata.
* This decorator ensures that each class instance is a singleton and associates metadata with
* transpilerable properties of the class.
*
* @param opt - The metadata options for the layer, of type `IZod2xLayerMetadata`.
*
* @returns A class decorator that extends the target class with the following features:
* - Singleton instance management.
* - Automatic assignment of metadata (`IZod2xMetadata`) to transpilerable properties.
* - Layer-specific metadata association for Zod types.
*
* ### Usage
* Apply this decorator to a class to enable layered modeling and metadata management.
*
* ### Example
* ```typescript
* @Layer({ name: "User", file: "user.ts", index: 0 })
* class ExampleClass {
* // Class implementation
* }
* ```
*
* ### Usage
* It can also be used to create custom layers like Domain, Application or Infrastructure:
* ```typescript
* function Domain(opt: Pick<IZod2xLayerMetadata, "file" | "name">) {
* return Layer({ ...opt, index: EZod2XLayer.DOMAIN });
* }
*
* @Domain({ name: "User", file: "user.ts" })
* class User {
* // Class implementation
* }
* ```
*/
export declare function Layer(opt: IZod2xLayerMetadata): <T extends {
new (...args: any[]): {};
}>(constructor: T) => {
new (...args: any[]): {
modelName: string;
layerMetadata: IZod2xLayerMetadata;
};
instance: T | null;
} & T;
/**
* The Domain layer decorator is typically used to define the core business logic or domain-specific
* rules within the application. It serves as a foundational layer in the layered architecture,
* encapsulating domain-related concerns.
*
* @param opt - An object containing the following properties:
* - `file`: The file path associated with the layer.
* - `name`: The name of the layer.
*
* @returns The configured Domain layer.
*/
export declare function Domain(opt: Omit<IZod2xLayerMetadata, "index">): <T extends {
new (...args: any[]): {};
}>(constructor: T) => {
new (...args: any[]): {
modelName: string;
layerMetadata: IZod2xLayerMetadata;
};
instance: T | null;
} & T;
/**
* The Application layer decorator is typically used to represent the application-specific logic
* or configuration in a layered architecture. It serves as a higher-level abstraction
* that interacts with other layers, such as domain or infrastructure layers.
*
* @param opt - An object containing the following properties:
* - `file`: The file associated with the layer.
* - `name`: The name of the layer.
*
* @returns The result of invoking the `Layer` function with the provided options and
* the `APPLICATION` layer index.
*/
export declare function Application(opt: Omit<IZod2xLayerMetadata, "index">): <T extends {
new (...args: any[]): {};
}>(constructor: T) => {
new (...args: any[]): {
modelName: string;
layerMetadata: IZod2xLayerMetadata;
};
instance: T | null;
} & T;
/**
* The Infrastructure layer decorator is typically responsible for handling
* low-level technical details such as database access, external APIs,
* file systems, and other system-level operations. It serves as the
* foundation for higher-level layers in the architecture.
*
* @param opt - An object containing metadata for the layer:
* - `file`: The file associated with this layer.
* - `name`: The name of the layer.
*
* @returns A configured Infrastructure layer.
*/
export declare function Infrastructure(opt: Omit<IZod2xLayerMetadata, "index">): <T extends {
new (...args: any[]): {};
}>(constructor: T) => {
new (...args: any[]): {
modelName: string;
layerMetadata: IZod2xLayerMetadata;
};
instance: T | null;
} & T;
/**
* The Presentation layer decorator is typically used to define the presentation or view-specific
* aspects of the model, such as formatting or display-related metadata.
*
* @param opt - An object containing the following properties:
* - `file`: The file associated with the layer.
* - `name`: The name of the layer.
* @returns The configured Presentation layer.
*/
export declare function Presentation(opt: Omit<IZod2xLayerMetadata, "index">): <T extends {
new (...args: any[]): {};
}>(constructor: T) => {
new (...args: any[]): {
modelName: string;
layerMetadata: IZod2xLayerMetadata;
};
instance: T | null;
} & T;