UNPKG

sussy-util

Version:
79 lines (78 loc) 3.51 kB
import { Builder, Constructor } from '../Types'; /** * A builder for creating instances of the `GenericBuilder` class, * allowing construction of instances of a specified class using fluent API-style method calls. * * @template T - The type of the class for which a builder is to be generated. * @template Mode - The mode for the builder: 'constructorReading' or 'objectPropertyReading'. */ export default class BuilderBuilder<T extends object, Mode extends 'constructorReading' | 'objectPropertyReading' = 'objectPropertyReading'> implements Builder<Constructor<GenericBuilder<T, Mode>>> { readonly ClassType: Constructor<T>; readonly CreationType: Mode; /** * Creates an instance of the BuilderBuilder. * @param {Function} ClassType - The constructor of the target class for which a builder is to be generated. * @param {Mode} [CreationType='objectPropertyReading'] - Specifies the method used for creating the builder: * - "constructorReading": Parses instance variables based on the constructor's signature. * - "objectPropertyReading": Generates setters based on the class's properties. * * IMPORTANT: The target class's constructor should work with no properties given. */ constructor(ClassType: Constructor<T>, CreationType?: Mode); /** * Builds and returns a constructor for a generic builder class associated with the target class. * * @returns {Constructor<GenericBuilder<T, Mode>>} A constructor function for the generic builder class. */ build(): Constructor<GenericBuilder<T, Mode>>; } /** * A generic builder class for creating instances of a given class using a fluent API. * * @template T - The target class type for which an instance is being built. * @template Mode - Determines the builder's mode: * - 'constructorReading': Uses constructor arguments for building. * - 'objectPropertyReading': Uses object properties for building. */ export declare class GenericBuilder<T extends object, Mode extends 'constructorReading' | 'objectPropertyReading'> implements Builder<T> { private readonly ClassType; private readonly Type; private attributes; private args; /** * Initializes the builder for the specified class and mode. * * @param {Constructor<T>} ClassType - The constructor of the class to build. * @param {Mode} Type - The mode for the builder: * - "objectPropertyReading": Builds using object properties. * - "constructorReading": Builds using constructor parameters. */ constructor(ClassType: Constructor<T>, Type: Mode); /** * Generates setter methods based on the properties of the class instance. * This is used when the builder mode is 'objectPropertyReading'. * * @private */ private createSettersFromProperties; /** * Generates setter methods based on the constructor parameters of the class. * This is used when the builder mode is 'constructorReading'. * * @private */ private createSettersFromConstructorParameters; /** * Dynamically defines a method on the builder. * * @param {string} name - The name of the method to define. * @param {AnyFunction} method - The function to assign to the method. * @private */ private defineMethod; /** * Builds and returns an instance of the target class. * * @returns {T} An instance of the class specified by `ClassType`. */ build(): T; }