Skip to content

Builder Functions

Source: packages/core/src/core/generators/builder.ts, packages/core/src/core/generators/fields.ts, packages/core/src/core/generators/persona.ts, packages/core/src/core/generators/dataset.ts, packages/core/src/core/monitoring/event-validation.ts

All builder functions are identity functions that exist for TypeScript type narrowing and editor IntelliSense. They return their input unchanged (except defineAction, which compiles fields into a handler when no custom handler is provided).

defineJourney

typescript
function defineJourney(config: Journey): Journey;

Defines a journey. See types.md for the Journey interface.

typescript
const journey = defineJourney({
  id: 'onboarding',
  name: 'Onboarding',
  adventures: [welcomeAdventure],
});

defineAdventure

typescript
function defineAdventure(config: Adventure): Adventure;

Defines an adventure (session/interaction period within a journey).

typescript
const adventure = defineAdventure({
  id: 'browse',
  name: 'Browse Pages',
  timeSpan: { min: 500, max: 3000 },
  actions: [viewHome, viewProduct],
});

defineAction

typescript
function defineAction(config: ActionDefinition): Action;

Defines an action. Accepts either a fields map (compiled to a single-event handler) or a custom handler function that returns Event[].

typescript
// Declarative (fields)
const viewHome = defineAction({
  id: 'view-home',
  name: 'page_view',
  fields: { page: '/home', referrer: (ctx) => ctx.faker.internet.url() },
});

// Imperative (handler)
const addToCart = defineAction({
  id: 'add-to-cart',
  name: 'add_to_cart',
  handler: (ctx) => {
    const product = ctx.typedDataset<Product>('products').randomRow();
    return [
      {
        id: ctx.generateId('event'),
        userId: ctx.userId,
        sessionId: ctx.sessionId,
        name: 'add_to_cart',
        timestamp: ctx.now(),
        payload: { productId: product.id, price: product.price },
      },
    ];
  },
});

definePersona

typescript
function definePersona(config: PersonaDefinition): PersonaDefinition;

Defines a persona with weighted attribute distributions.

typescript
const shoppers = definePersona({
  id: 'shoppers',
  name: 'Online Shoppers',
  attributes: {
    locale: weighted({ en: 0.6, de: 0.2, fr: 0.2 }),
    tier: weighted({ free: 0.7, pro: 0.2, enterprise: 0.1 }),
    age: fake((f) => f.number.int({ min: 18, max: 65 })),
    device: oneOf(['mobile', 'desktop', 'tablet']),
  },
});

The PersonaDefinition interface:

typescript
interface PersonaDefinition {
  id: string;
  name: string;
  attributes: Record<string, FieldGenerator>;
}

defineDataset

typescript
function defineDataset<TFields>(config: DatasetDefinition<TFields>): DatasetDefinition<TFields>;

Defines a dataset. Use InferDatasetRow<typeof def> to extract the row type.

typescript
const productsDef = defineDataset({
  id: 'products',
  name: 'Products',
  count: 200,
  fields: {
    id: (_ctx, row) => `prod-${row.index}`,
    name: (ctx) => ctx.faker.commerce.productName(),
    price: (ctx) => ctx.faker.number.float({ min: 10, max: 500 }),
  },
});

type Product = InferDatasetRow<typeof productsDef>;

defineEventSchema

typescript
function defineEventSchema<T extends z.ZodRawShape>(shape: T): z.ZodObject<T>;

Convenience wrapper around z.object() for defining event payload schemas. Used with RunOptions.eventSchema for runtime validation.

typescript
import { z } from 'zod';
import { defineEventSchema } from '@synode/core';

const pageViewSchema = defineEventSchema({
  url: z.string().url(),
  referrer: z.string().optional(),
});

Field Helpers

oneOf

typescript
function oneOf<T>(options: T[]): FieldGenerator<T>;

Returns one of the provided options at random with equal probability.

weighted

typescript
function weighted<T extends string>(options: Record<T, number>): FieldGenerator<T>;

Returns a value based on weighted probabilities. Weights are normalized if they do not sum to 1.

chance

typescript
function chance(probability: number): FieldGenerator<boolean>;

Returns true with the given probability (0-1).

fake

typescript
function fake<T>(generator: (faker: Faker) => T): FieldGenerator<T>;

Returns a value generated by the context's locale-aware Faker instance.