Batched payload types

This commit is contained in:
nicktrn
2023-12-06 07:48:54 +00:00
parent 5324f9a8de
commit 6006ba00dd
5 changed files with 94 additions and 22 deletions
+2
View File
@@ -44,6 +44,8 @@ export const BatcherOptionsSchema = z.object({
export type BatcherOptions = z.infer<typeof BatcherOptionsSchema>;
export type OptionalBatcherOptions = BatcherOptions | undefined;
export const StaticTriggerMetadataSchema = z.object({
type: z.literal("static"),
title: z.union([z.string(), z.array(z.string())]),
+21 -3
View File
@@ -1,4 +1,5 @@
import {
BatcherOptions,
FailedRunNotification,
IntegrationConfig,
InvokeOptions,
@@ -21,6 +22,8 @@ import { slugifyId } from "./utils";
import { runLocalStorage } from "./runLocalStorage";
import { Prettify } from "@trigger.dev/core";
import { ConcurrencyLimit } from "./concurrencyLimit";
import { EventTrigger } from "./triggers/eventTrigger";
import { WebhookTrigger } from "./triggers/webhook";
export type JobOptions<
TTrigger extends Trigger<EventSpecification<any>>,
@@ -80,21 +83,36 @@ export type JobOptions<
* @param context An object that contains information about the Organization, Job, Run and more.
*/
run: (
payload: TriggerEventType<TTrigger>,
payload: ArrayIfBatched<TTrigger, TriggerEventType<TTrigger>>,
io: IOWithIntegrations<TIntegrations>,
context: TriggerContext
) => Promise<TOutput>;
onSuccess?: (
notification: SuccessfulRunNotification<TOutput, TriggerEventType<TTrigger>>
notification: SuccessfulRunNotification<
TOutput,
ArrayIfBatched<TTrigger, TriggerEventType<TTrigger>>
>
) => void;
onFailure?: (notification: FailedRunNotification<TriggerEventType<TTrigger>>) => void;
onFailure?: (
notification: FailedRunNotification<ArrayIfBatched<TTrigger, TriggerEventType<TTrigger>>>
) => void;
// @internal
__internal?: boolean;
};
type HasBatchingEnabled<TBatcherOptions> = TBatcherOptions extends BatcherOptions ? true : false;
type ArrayIfTrue<TBool, T> = TBool extends true ? T[] : T;
type ArrayIfBatched<TTrigger extends Trigger<any>, T> = TTrigger extends EventTrigger<any, infer U>
? ArrayIfTrue<HasBatchingEnabled<U>, T>
: TTrigger extends WebhookTrigger<any, any, any, infer U>
? ArrayIfTrue<HasBatchingEnabled<U>, T>
: T;
export type JobPayload<TJob> = TJob extends Job<Trigger<EventSpecification<infer TEvent>>, any>
? TEvent
: never;
@@ -1,4 +1,10 @@
import { BatcherOptions, EventFilter, TriggerMetadata, deepMergeFilters } from "@trigger.dev/core";
import {
BatcherOptions,
EventFilter,
OptionalBatcherOptions,
TriggerMetadata,
deepMergeFilters,
} from "@trigger.dev/core";
import { Job } from "../job";
import { TriggerClient } from "../triggerClient";
import {
@@ -12,21 +18,26 @@ import { formatSchemaErrors } from "../utils/formatSchemaErrors";
import { ParsedPayloadSchemaError } from "../errors";
import { VerifyCallback } from "../httpEndpoint";
type EventTriggerOptions<TEventSpecification extends EventSpecification<any>> = {
type EventTriggerOptions<
TEventSpecification extends EventSpecification<any>,
TBatcherOptions extends OptionalBatcherOptions = undefined,
> = {
event: TEventSpecification;
name?: string | string[];
source?: string;
filter?: EventFilter;
verify?: EventTypeFromSpecification<TEventSpecification> extends Request ? VerifyCallback : never;
batch?: BatcherOptions;
batch?: TBatcherOptions;
};
export class EventTrigger<TEventSpecification extends EventSpecification<any>>
implements Trigger<TEventSpecification>
export class EventTrigger<
TEventSpecification extends EventSpecification<any>,
TBatcherOptions extends OptionalBatcherOptions = undefined,
> implements Trigger<TEventSpecification>
{
#options: EventTriggerOptions<TEventSpecification>;
#options: EventTriggerOptions<TEventSpecification, TBatcherOptions>;
constructor(options: EventTriggerOptions<TEventSpecification>) {
constructor(options: EventTriggerOptions<TEventSpecification, TBatcherOptions>) {
this.#options = options;
}
@@ -49,7 +60,14 @@ export class EventTrigger<TEventSpecification extends EventSpecification<any>>
attachToJob(triggerClient: TriggerClient, job: Job<Trigger<TEventSpecification>, any>): void {}
batch(options?: BatcherOptions): EventTrigger<TEventSpecification> {
/**
* Used to configure batching options. An empty object will enable batching with server defaults.
*
* @param options - Is an object containing the following properties:
* @param {number} options.maxPayloads - The `maxPayloads` property defines how many event payloads you will receive at most per batch. This is affected by server limits, but you should never receive more than this.
* @param {number} options.maxInterval - The `maxInterval` property defines how many seconds to wait before sending out batches that aren't full yet. This is affected by server limits, but you should never receive more than this.
*/
batch(options?: BatcherOptions): EventTrigger<TEventSpecification, {}> {
const { batch, ...rest } = this.#options;
return new EventTrigger({
@@ -75,7 +93,7 @@ export class EventTrigger<TEventSpecification extends EventSpecification<any>>
}
/** Configuration options for an EventTrigger */
type TriggerOptions<TEvent> = {
type TriggerOptions<TEvent, TBatcherOptions extends OptionalBatcherOptions = undefined> = {
/** The name of the event you are subscribing to. Must be an exact match (case sensitive). To trigger on multiple possible events, pass in an array of event names */
name: string | string[];
/** A [Zod](https://trigger.dev/docs/documentation/guides/zod) schema that defines the shape of the event payload.
@@ -104,8 +122,14 @@ type TriggerOptions<TEvent> = {
* ```
*/
filter?: EventFilter;
/** Used to set batching options. */
batch?: BatcherOptions;
/**
* Used to configure batching options. An empty object will enable batching with server defaults.
*
* @param {number} maxPayloads - The `maxPayloads` property defines how many event payloads you will receive at most per batch. This is affected by server limits, but you should never receive more than this.
* @param {number} maxInterval - The `maxInterval` property defines how many seconds to wait before sending out batches that aren't full yet. This is affected by server limits, but you should never receive more than this.
*/
batch?: TBatcherOptions;
examples?: EventSpecificationExample[];
};
@@ -113,9 +137,12 @@ type TriggerOptions<TEvent> = {
/** `eventTrigger()` is set as a [Job's trigger](https://trigger.dev/docs/sdk/job) to subscribe to an event a Job from [a sent event](https://trigger.dev/docs/sdk/triggerclient/instancemethods/sendevent)
* @param options options for the EventTrigger
*/
export function eventTrigger<TEvent extends any = any>(
options: TriggerOptions<TEvent>
): EventTrigger<EventSpecification<TEvent>> {
export function eventTrigger<
TEvent extends any = any,
TBatcherOptions extends OptionalBatcherOptions = undefined,
>(
options: TriggerOptions<TEvent, TBatcherOptions>
): EventTrigger<EventSpecification<TEvent>, TBatcherOptions> {
return new EventTrigger({
name: options.name,
filter: options.filter,
+28 -3
View File
@@ -3,6 +3,7 @@ import {
DisplayProperty,
EventFilter,
HandleTriggerSource,
OptionalBatcherOptions,
RegisterWebhookSource,
TriggerMetadata,
deepMergeFilters,
@@ -262,20 +263,37 @@ export type WebhookTriggerOptions<
TEventSpecification extends EventSpecification<any>,
TEventSource extends WebhookSource<any, any, any>,
TConfig extends Record<string, string[]> = Record<string, string[]>,
TBatcherOptions extends OptionalBatcherOptions = undefined,
> = {
event: TEventSpecification;
source: TEventSource;
params: GetWebhookParams<TEventSource>;
config: TConfig;
batch?: BatcherOptions;
/**
* Used to configure batching options. An empty object will enable batching with server defaults.
*
* @param {number} maxPayloads - The `maxPayloads` property defines how many event payloads you will receive at most per batch. This is affected by server limits, but you should never receive more than this.
* @param {number} maxInterval - The `maxInterval` property defines how many seconds to wait before sending out batches that aren't full yet. This is affected by server limits, but you should never receive more than this.
*/
batch?: TBatcherOptions;
};
export class WebhookTrigger<
TEventSpecification extends EventSpecification<any>,
TEventSource extends WebhookSource<any, any, any>,
TConfig extends Record<string, string[]> = Record<string, string[]>,
TBatcherOptions extends OptionalBatcherOptions = undefined,
> implements Trigger<TEventSpecification>
{
constructor(private options: WebhookTriggerOptions<TEventSpecification, TEventSource>) {}
constructor(
private options: WebhookTriggerOptions<
TEventSpecification,
TEventSource,
TConfig,
TBatcherOptions
>
) {}
get event() {
return this.options.event;
@@ -307,7 +325,14 @@ export class WebhookTrigger<
};
}
batch(options?: BatcherOptions): WebhookTrigger<TEventSpecification, TEventSource> {
/**
* Used to configure batching options. An empty object will enable batching with server defaults.
*
* @param options - Is an object containing the following properties:
* @param {number} options.maxPayloads - The `maxPayloads` property defines how many event payloads you will receive at most per batch. This is affected by server limits, but you should never receive more than this.
* @param {number} options.maxInterval - The `maxInterval` property defines how many seconds to wait before sending out batches that aren't full yet. This is affected by server limits, but you should never receive more than this.
*/
batch(options?: BatcherOptions): WebhookTrigger<TEventSpecification, TEventSource, TConfig, {}> {
const { batch, ...rest } = this.options;
return new WebhookTrigger({
+2 -2
View File
@@ -124,8 +124,8 @@ export type TypedEventSpecificationExample<TEvent> = {
id: string;
name: string;
icon?: string;
payload: TEvent
}
payload: TEvent;
};
export interface EventSpecification<TEvent extends any, TInvoke extends any = TEvent> {
name: string | string[];