import { Factory, type Context } from "./factory.ts"; export interface ConstructFn> { (ctx: Context): Shape; } /** * Shorthand for creating a new factory based on the given `construct` function. * This is useful for quick and basic factories. For creating advanced factories, * you should consider extending the {@link Factory} class instead. * * ```ts * interface Person { * name: string; * age: number; * } * * const factory = defineFactory(() => ({ * name: "Example Person", * age: 18, * })); * * const person = factory.build({ age: 21 }); * ``` * * --- * * If you extend {@link Factory} with your own base factory, you can create a * factory shorthand that is based on your base factory. The following code * snippet is a great starting point for this. * * ```ts * abstract class CustomFactory extends Factory { * // ...your custom base overrides * } * * export function defineFactory( * construct: ConstructFn>, * ): CustomFactory { * class InlineFactory extends CustomFactory { * protected override construct(): Shape { * return construct(this.ctx); * } * } * return InlineFactory.create(); * } * ``` */ export function defineFactory( construct: ConstructFn>, ): Factory { class InlineFactory extends Factory { protected override construct(): Shape { return construct(this.ctx); } } return InlineFactory.create(); }