Edits history of script submission #11460 for ' SQS trigger script with preprocessor template (windmill)'

  • bun
    /**
     * General Trigger Preprocessor
     *
     * ⚠️ This function runs BEFORE the main function.
     *
     * It processes raw trigger data (e.g., MQTT, HTTP, SQS) before passing it to main().
     * Common tasks:
     * - Convert binary payloads to string/JSON
     * - Extract metadata
     * - Filter messages
     * - Add timestamps/context
     *
     * The returned object determines main() parameters:
     * - {a: 1, b: 2} → main(a, b)
     * - {msg} → main(msg)
     *
     * @param wm_trigger - Trigger details (e.g., MQTT, HTTP, SQS)
     * @param msg - Raw trigger data (format varies by trigger type)
     * @returns Processed data for main()
     */
    
    export async function preprocessor(
      wm_trigger: {
        kind: 'http' | 'email' | 'webhook' | 'websocket' | 'kafka' | 'nats' | 'postgres' | 'sqs' | 'mqtt',
        sqs?: {
          queue_url: string,
          message_id?: string,
          receipt_handle?: string,
          attributes: Record<string, string>,
          message_attributes?: Record<string, {
            string_value?: string,
            data_type: string
          }>
        }
      },
      msg: string
    ) {
      if (wm_trigger.kind === 'sqs' && wm_trigger.sqs) {
        return {
          queueUrl: wm_trigger.sqs.queue_url,
          messageId: wm_trigger.sqs.message_id,
          receiptHandle: wm_trigger.sqs.receipt_handle,
          attributes: wm_trigger.sqs.attributes,
          messageAttributes: wm_trigger.sqs.message_attributes,
          msg
        };
      }
      
      throw new Error(`Expected sqs trigger kind, got: ${wm_trigger.kind}`);
    }
    
    /**
     * Main Function - Handles processed trigger events
     *
     * ⚠️ Called AFTER preprocessor(), with its return values.
     *
     * @param queueUrl - URL of the SQS queue
     * @param messageId - Unique message identifier (if available)
     * @param receiptHandle - Receipt handle for message deletion (if available)
     * @param attributes - Message attributes from SQS
     * @param messageAttributes - Custom message attributes (if any)
     * @param msg - Raw message content
     */
    export async function main(
      queueUrl: string,
      messageId: string | undefined,
      receiptHandle: string | undefined,
      attributes: Record<string, string>,
      messageAttributes: Record<string, { string_value?: string; data_type: string }> | undefined,
      msg: string
    ) {
      // Implement the main function logic here
    }
    

    Submitted by dieriba.pro916 435 days ago