|
| 1 | +import { Effect, Schema } from 'effect'; |
| 2 | +import { |
| 3 | + onTaskDispatched, |
| 4 | + Request, |
| 5 | + TaskQueueFunction, |
| 6 | + TaskQueueOptions, |
| 7 | +} from 'firebase-functions/v2/tasks'; |
| 8 | +import { logger } from 'firebase-functions'; |
| 9 | +import { run, Runtime } from './run.js'; |
| 10 | + |
| 11 | +interface TaskDispatchedEffectOptions<R> extends TaskQueueOptions { |
| 12 | + runtime: Runtime<R>; |
| 13 | +} |
| 14 | + |
| 15 | +interface TaskDispatchedEffectOptionsWithSchema<R, S extends Schema.Schema.Any> |
| 16 | + extends TaskDispatchedEffectOptions<R | Schema.Schema.Context<S>> { |
| 17 | + dataSchema: S; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Decode task payload JSON data using the provided schema. |
| 22 | + */ |
| 23 | +function decodeTaskData<S extends Schema.Schema.Any>( |
| 24 | + schema: S, |
| 25 | + request: Request<unknown> |
| 26 | +): Effect.Effect<Schema.Schema.Type<S>, Error, Schema.Schema.Context<S>> { |
| 27 | + return Schema.decodeUnknown(schema)(request.data).pipe( |
| 28 | + Effect.mapError( |
| 29 | + (error) => new Error(`Failed to decode task payload: ${error.message}`) |
| 30 | + ) |
| 31 | + ) as Effect.Effect<Schema.Schema.Type<S>, Error, Schema.Schema.Context<S>>; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Create a Firebase Functions Cloud Tasks trigger that runs an effect when a task is dispatched. |
| 36 | + * |
| 37 | + * @param options - The options for the Cloud Tasks trigger including optional payload schema. |
| 38 | + * @param handler - The handler function that runs the effect. |
| 39 | + * @returns The Firebase Functions Cloud Tasks trigger. |
| 40 | + */ |
| 41 | +// Overload: with payload schema |
| 42 | +export function onTaskDispatchedEffect<R, S extends Schema.Schema.Any, E>( |
| 43 | + options: TaskDispatchedEffectOptionsWithSchema<R, S>, |
| 44 | + handler: ( |
| 45 | + data: Schema.Schema.Type<S>, |
| 46 | + request: Request<Schema.Schema.Encoded<S>> |
| 47 | + ) => Effect.Effect<void, E, R> |
| 48 | +): TaskQueueFunction<Schema.Schema.Encoded<S>>; |
| 49 | + |
| 50 | +// Overload: without payload schema (full control) |
| 51 | +export function onTaskDispatchedEffect<R, T, E>( |
| 52 | + options: TaskDispatchedEffectOptions<R>, |
| 53 | + handler: (request: Request<T>) => Effect.Effect<void, E, R> |
| 54 | +): TaskQueueFunction<T>; |
| 55 | + |
| 56 | +// Implementation |
| 57 | +export function onTaskDispatchedEffect<R>( |
| 58 | + options: TaskDispatchedEffectOptions<R> & { |
| 59 | + dataSchema?: Schema.Schema.Any; |
| 60 | + }, |
| 61 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 62 | + handler: (...args: any[]) => Effect.Effect<void, unknown, R> |
| 63 | +): TaskQueueFunction<unknown> { |
| 64 | + const { dataSchema } = options; |
| 65 | + |
| 66 | + return onTaskDispatched(options, async (request) => { |
| 67 | + const effect = Effect.gen(function* () { |
| 68 | + if (dataSchema) { |
| 69 | + // Decode task payload and pass both parsed payload and request to handler |
| 70 | + const taskData = yield* decodeTaskData(dataSchema, request); |
| 71 | + return yield* handler(taskData, request); |
| 72 | + } else { |
| 73 | + // Pass raw request to handler |
| 74 | + return yield* handler(request); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + await run(options.runtime, effect as Effect.Effect<void, never, R>).catch( |
| 79 | + (error) => { |
| 80 | + logger.error('Defect in onTaskDispatched', { |
| 81 | + inner: error, |
| 82 | + stack: error instanceof Error ? error.stack : undefined, |
| 83 | + }); |
| 84 | + } |
| 85 | + ); |
| 86 | + }); |
| 87 | +} |
0 commit comments