sussy-util
Version:
Util package made by me
56 lines (55 loc) • 2.78 kB
TypeScript
import { Builder, Constructor, MutableObject } from "../Types";
/**
* A generic builder class that facilitates the construction of instances for a specified class
* by setting attributes using fluent API-style method calls.
* @template T - The type of class for which instances are built.
*/
declare class GenericBuilder<T extends {}> implements Builder<T> {
readonly ClassType: Constructor<T>;
readonly Type: string;
constructorParams: {
order: Array<string | string[]>;
attributes: MutableObject<any>;
};
attributes: Partial<T>;
/**
* Creates an instance of GenericBuilder.
* @param {Constructor<T>} ClassType - The constructor function of the class for which instances will be built.
* @param {string} Type - The type of builder to create, either "constructorReading" or "objectPropertyReading".
*/
constructor(ClassType: Constructor<T>, Type: string);
private interpretConstructor;
private createSettersProperties;
private createSettersSignature;
private defineMethod;
/**
* Build and return an instance of the specified class type with the attributes set using the builder methods.
* @returns {T} - An instance of the specified class type with attributes set.
*/
build(): T;
}
/**
* 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 class for which instances are built using the GenericBuilder.
*/
export default class BuilderBuilder<T extends {}> implements Builder<Constructor<GenericBuilder<T>>> {
readonly ClassType: Constructor<T>;
readonly CreationType: "constructorReading" | "objectPropertyReading";
/**
* Creates an instance of the BuilderBuilder.
* @param {Function} ClassType - The constructor of the target class for which a builder is to be generated.
* @param {string} CreationType - Specifies the method used for creating the builder:
* - "constructorReading": Parses instance variables based on the constructor's signature.
* * IMPORTANT: The constructor must be the first function in the class.
* - "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?: "constructorReading" | "objectPropertyReading");
/**
* Builds and returns a constructor for a generic builder class associated with the target class.
* @returns {Constructor<GenericBuilder<T>>} A constructor function for the generic builder class.
*/
build(): Constructor<GenericBuilder<T>>;
}
export {};