UNPKG

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.

65 lines (63 loc) 2.39 kB
import { internalEvents } from "../helpers/consts.js"; import { markerKey } from "../helpers/marker.js"; import { InngestFunction } from "./InngestFunction.js"; //#region src/components/DeferredFunction.ts const idDenyRegex = /['\\\n\r]/; /** * EXPERIMENTAL: This API is not yet stable and may change in the future without * a major version bump. * * A defer (companion) function created via `createDefer(...)`. Real * `InngestFunction` at runtime, but with the trigger pinned to * `inngest/deferred.schedule` (see `getConfigTriggers`), `triggers` and * `onFailure` disallowed, and the schema carried as a typed instance * property so callers of `defer(id, { function, data })` can extract it. * * Identify a defer function at runtime via `isDeferredFunction(value)` from * `helpers/marker.ts`. Prefer that over `instanceof`, which fails across * duplicate SDK copies in the same process. * * @public */ var DeferredFunction = class extends InngestFunction { schema; [markerKey] = { kind: "deferredFunction" }; constructor(client, opts, handler, schema) { if (idDenyRegex.test(opts.id)) throw new Error(`invalid id "${opts.id}"; must match ${idDenyRegex.source}`); super(client, { ...opts, triggers: [] }, handler); this.schema = schema; } getConfigTriggers(fnId) { return [{ event: internalEvents.DeferredSchedule, expression: `event.data._inngest.fn_slug == '${fnId}'` }]; } }; /** * EXPERIMENTAL: This API is not yet stable and may change in the future without * a major version bump. * * Create a typed defer function. One `createDefer` call = one Inngest * function. Returns a `DeferredFunction<TSchema>` so callers of `defer(id, * { function, data })` get the data type inferred from the schema. * * Mirrors `inngest.createFunction(opts, handler)`, with three differences: * the client is the first positional arg, `triggers` is not accepted (the * SDK emits an implicit `inngest/deferred.schedule` trigger), and `schema` * describes the payload that callers will send via `defer(id, { function, * data })`. * * Pass the result to `serve()` alongside regular functions so the SDK * registers it. */ function createDefer(client, options, handler) { const { schema, ...rest } = options; return new DeferredFunction(client, rest, handler, schema); } //#endregion export { DeferredFunction, createDefer }; //# sourceMappingURL=DeferredFunction.js.map