ts-forged
Version:
Type-safe fake data generation from DTOs for TypeScript and NestJS
172 lines (163 loc) • 4.62 kB
TypeScript
/**
* Type alias for a class constructor
*/
type Type<T> = new (...args: unknown[]) => T;
/**
* Type alias for primitive types
*/
type Primitive = string | number | boolean | null | undefined;
/**
* Type alias for a function that generates a value
*/
type Generator<T> = () => T;
/**
* Type alias for a function that generates a value with context
*/
type ContextualGenerator<T, C = unknown> = (context: C) => T;
/**
* Type alias for field metadata
*/
interface FieldMetadata {
name: string;
type: string;
isOptional: boolean;
isArray: boolean;
elementType?: unknown;
decorators: Array<{
name: string;
args: unknown[];
}>;
}
/**
* Type alias for field generator options
*/
interface FieldGeneratorOptions {
locale?: string;
seed?: number;
overrides?: Record<string, unknown>;
}
/**
* Type alias for field generator context
*/
interface FieldGeneratorContext {
fieldName: string;
fieldType: string;
metadata: FieldMetadata;
options: FieldGeneratorOptions;
depth?: number;
target?: Type<object>;
}
/**
* Factory class for generating fake data from DTOs
*/
declare class Factory<T extends object> {
private readonly fieldGenerator;
private readonly type;
private constructor();
/**
* Creates a new factory instance for the given type
*/
static create<T extends object>(type: Type<T>): Factory<T>;
/**
* Builds a single instance with optional overrides
*/
build(overrides?: Partial<T>): T;
/**
* Builds multiple instances
*/
buildMany(count: number, overrides?: Partial<T>): T[];
/**
* Creates a sequence of instances with unique values
*/
sequence(count: number, overrides?: Partial<T>): T[];
}
/**
* Class responsible for generating fake data for fields
*/
declare class FieldGenerator {
private readonly stringGenerator;
private readonly numberGenerator;
private readonly dateGenerator;
constructor();
/**
* Generates fake data for all fields in a type
*/
generateFields<T extends object>(type: Type<T>, options?: FieldGeneratorOptions): Record<string, unknown>;
/**
* Generates a single field value based on its context
*/
private generateFieldValue;
/**
* Generates an array of values
*/
private generateArrayValue;
/**
* Generates a value based on smart field name detection
*/
private generateSmartValue;
/**
* Generates an object value
*/
private generateObjectValue;
/**
* Gets the nested type for a field
*/
private getNestedType;
}
/**
* Base class for all generators
*/
declare abstract class BaseGenerator<T> {
/**
* Generates a value based on the field context
*/
abstract generate(context: FieldGeneratorContext): T;
/**
* Gets the generator options from the context
*/
protected getOptions(context: unknown): Record<string, unknown>;
/**
* Gets a specific option value
*/
protected getOption<K extends keyof FieldGeneratorContext['options']>(context: FieldGeneratorContext, key: K): FieldGeneratorContext['options'][K] | undefined;
}
/**
* Generator for string values
*/
declare class StringGenerator extends BaseGenerator<string> {
/**
* Generates a string value based on the field context
*/
generate(context: FieldGeneratorContext): string;
}
/**
* Generator for number values
*/
declare class NumberGenerator extends BaseGenerator<number> {
/**
* Generates a number value based on the field context
*/
generate(context: FieldGeneratorContext): number;
}
/**
* Generator for date values
*/
declare class DateGenerator extends BaseGenerator<Date> {
/**
* Generates a date value based on the field context
*/
generate(context: FieldGeneratorContext): Date;
}
/**
* Decorator to mark a property for fake data generation
*/
declare function FakeProperty(typeFn?: () => unknown): PropertyDecorator;
/**
* Gets field metadata for a type
*/
declare function getFieldMetadata<T extends object>(type: Type<T>): FieldMetadata[];
/**
* Gets the element type for arrays and nested objects
*/
declare function getElementType<T extends object>(type: Type<T>, property: string): unknown;
export { BaseGenerator, type ContextualGenerator, DateGenerator, Factory, FakeProperty, FieldGenerator, type FieldGeneratorContext, type FieldGeneratorOptions, type FieldMetadata, type Generator, NumberGenerator, type Primitive, StringGenerator, type Type, getElementType, getFieldMetadata };