inngest
Version:
Official SDK for Inngest.com. Inngest is the reliability layer for modern applications. Inngest combines durable execution, events, and queues into a zero-infra platform with built-in observability.
120 lines (119 loc) • 3.74 kB
JavaScript
//#region src/components/triggers/triggers.ts
/**
* Create a cron trigger for scheduled function execution.
*
* @param schedule - Cron expression (e.g., "0 0 * * *" for daily at midnight)
* @returns Cron trigger
*/
function cron(schedule) {
return { cron: schedule };
}
/**
* Represents a typed event definition that can be used both as a trigger
* and for creating events with validation.
*
* @template TName - The event name (e.g., "user.created")
* @template TSchema - Optional StandardSchema for type-safe event data
*/
var EventType = class {
/**
* The event name. This is the same as the `name` property, but is necessary
* to make the event type compatible with other features (e.g. event
* triggers).
*/
event;
name;
schema;
version;
constructor({ name, schema, version }) {
this.event = name;
this.name = name;
this.schema = schema;
this.version = version;
}
/**
* Creates an event to send.
*
* The returned event object includes a `validate()` method that can be called
* to validate the event data against the schema (if one was provided). The
* `validate()` method returns a new event object with the validated data,
* including any transforms defined in the schema.
*
* Validation is not performed within this method because validation may be async.
*
* @param data - Event data (required if schema is defined, optional otherwise)
* @param options - Optional event options including id, timestamp, and version
*/
create(...args) {
const [data, options] = args;
return {
name: this.name,
data,
id: options?.id,
ts: options?.ts,
v: options?.v ?? this.version,
validate: async () => {
if (this.schema) {
if (!data) throw new Error("data is required");
const check = await this.schema["~standard"].validate(data);
if (check.issues) throw new Error(check.issues.map((issue) => {
if (issue.path && issue.path.length > 0) return `${issue.path.join(".")}: ${issue.message}`;
return issue.message;
}).join(", "));
}
}
};
}
};
/**
* Create an event type definition that can be used as a trigger and for
* creating events.
*
* This is the primary way to define typed events in Inngest. It creates an
* EventType instance that provides type safety and optional runtime validation.
*
* @param name - The event name (e.g., "user.created")
* @param options - Optional options for the event type
* @param options.schema - Optional StandardSchema for type-safe event data validation
* @param options.version - Optional version of the event
* @returns EventType instance that can be used as a trigger or for creating events
*/
function eventType(name, { schema, version } = {}) {
return new EventType({
name,
schema,
version
});
}
/**
* Create a type-only schema that provides TypeScript types without runtime
* validation. Returns a hardcoded StandardSchemaV1 whose `validate` is a
* passthrough, so invalid data will not be rejected at runtime. Use this when
* you want event type safety without pulling in a validation library like Zod.
*/
function staticSchema() {
return { "~standard": {
version: 1,
vendor: "inngest",
validate: (value) => ({ value })
} };
}
/**
* Create an invoke trigger for function-to-function calls.
*
* This creates a trigger that allows your function to be invoked directly by
* other functions using `step.invoke()`. The schema defines the expected data
* structure for invocations.
*
* @param schema - StandardSchema defining the invoke payload structure
* @returns Invoke trigger
*/
function invoke(schema) {
return new EventType({
name: "inngest/function.invoked",
schema
});
}
//#endregion
export { EventType, cron, eventType, invoke, staticSchema };
//# sourceMappingURL=triggers.js.map