API Reference
Complete API documentation for Interface Forge classes and types.
Core Classes
Factory<T>
Main class for generating type-safe mock data.
import { Factory } from 'interface-forge';
const userFactory = new Factory<User>((faker) => ({
id: faker.string.uuid(),
name: faker.person.fullName(),
email: faker.internet.email(),
}));
Methods:
build(overrides?)
- Generate single objectbatch(count, overrides?)
- Generate multiple objectsbuildAsync(overrides?)
- Async generationbatchAsync(count, overrides?)
- Async batch generationuse(definition)
- Update factory definitionextend(additions)
- Extend with additional fieldscompose(composition)
- Compose with other factoriesbeforeBuild(hook)
- Add pre-generation hookafterBuild(hook)
- Add post-generation hookcreate(overrides?, options?)
- Generate and persist single objectcreateMany(count, overrides?, options?)
- Generate and persist multiple objectswithAdapter(adapter)
- Set persistence adapter
ZodFactory
Generate data from Zod schemas.
import { ZodFactory } from 'interface-forge/zod';
const factory = new ZodFactory(userSchema);
const user = factory.build();
Methods:
build(overrides?)
- Generate from schemabatch(count, overrides?)
- Generate multiple from schemawithTypeHandler(typeName, handler)
- Add custom type handlerwithTypeHandlers(handlers)
- Add multiple type handlers
Utility Classes
Ref<T> - Lazy references
const userRef = new Ref<User>();
CycleGenerator - Cycle through values
const gen = new CycleGenerator(['a', 'b', 'c']);
SampleGenerator - Random sampling
const gen = new SampleGenerator(['x', 'y', 'z']);
Type Definitions
type FactoryFunction<T> = (faker: Faker, iteration: number) => T;
type FactorySchema<T> = {
[K in keyof T]: T[K] | Generator<T[K]> | Ref<T[K]>;
};
type FactoryComposition<T> = {
[K in keyof T]?: Factory<T[K]> | T[K];
};
type BeforeBuildHook<T> = (
data: Partial<T>,
) => Partial<T> | Promise<Partial<T>>;
type AfterBuildHook<T> = (data: T) => T | Promise<T>;
type ZodTypeHandler = (
schema: ZodType,
generator: ZodSchemaGenerator,
currentDepth: number,
) => unknown;
interface PersistenceAdapter<T, R = T> {
create(data: T): Promise<R>;
createMany(data: T[]): Promise<R[]>;
}
interface FixtureConfiguration {
basePath?: string;
directory?: string;
includeSource?: boolean;
useSubdirectory?: boolean;
validateSignature?: boolean;
}
Error Classes
ConfigurationError
- Invalid configurationCircularReferenceError
- Circular reference detectionValidationError
- Schema validation failuresFixtureError
- Fixture operation failuresFixtureValidationError
- Fixture signature validation failures
Examples
Basic Factory
const userFactory = new Factory<User>((faker) => ({
id: faker.string.uuid(),
name: faker.person.fullName(),
}));
const user = userFactory.build();
const users = userFactory.batch(5);
Composition
const enhancedFactory = userFactory.compose<EnhancedUser>({
profile: profileFactory,
isActive: true,
});
Persistence
const factory = userFactory.withAdapter(new MongooseAdapter(UserModel));
const user = await factory.create();
Fixtures
const user = userFactory.build({}, { generateFixture: 'test-user' });
Zod Integration
const factory = new ZodFactory(userSchema);
const user = factory.build(); // Validates against schema
For detailed usage, see the Getting Started guide.