Edits history of script submission #22300 for ' Azure Event Grid script with preprocessor template (windmill)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    /**
     * General Trigger Preprocessor
     *
     * ⚠️ This function runs BEFORE the main function.
     *
     * It processes raw trigger data (e.g., Azure Event Grid, MQTT, HTTP, SQS)
     * before passing it to `main()`.
     * Common tasks:
     * - Extract metadata
     * - Filter messages
     * - Add timestamps/context
     *
     * The returned object determines `main()` parameters:
     * - `{a: 1, b: 2}` → `main(a, b)`
     * - `{payload}` → `main(payload)`
     *
     * @param event - Azure Event Grid trigger data. `payload` is the already-parsed
     *   CloudEvent `data` field (object / array / string / number / null) when the
     *   publisher used `data`. If the publisher used `data_base64` instead (binary
     *   payload), `payload` contains the base64 string — decode it here if needed.
     */
    export async function preprocessor(event: {
    	payload: any
    	kind: 'azure'
    	id: string
    	source: string
    	type: string
    	subject?: string
    	time?: string
    	specversion: string
    	datacontenttype?: string
    	dataschema?: string
    	headers?: Record<string, string>
    	delivery_type: 'push' | 'pull'
    	trigger_path: string
    }) {
    	if (event.kind !== 'azure') {
    		// We assume the script is triggered by an Azure Event Grid message. If you
    		// also want to wire it to other trigger kinds, extend this branch.
    		throw new Error(`Expected azure trigger kind got: ${event.kind}`)
    	}
    
    	return {
    		payload: event.payload,
    		eventType: event.type,
    		subject: event.subject,
    		source: event.source,
    		eventId: event.id,
    		time: event.time,
    		deliveryType: event.delivery_type
    	}
    }
    
    /**
     * Main Function — handles processed Azure Event Grid events.
     *
     * Called AFTER `preprocessor()` with its return values.
     *
     * @param payload - The CloudEvent `data` field (parsed JSON) or base64 string.
     * @param eventType - e.g. `Microsoft.Storage.BlobCreated`.
     * @param subject - CloudEvents subject; for Azure system topics this is often
     *   a resource path (blob path, key vault secret id, etc.).
     * @param source - CloudEvents source; usually the producer resource.
     * @param eventId - Unique event id (useful for deduplication).
     * @param time - Event time (RFC 3339).
     * @param deliveryType - 'push' when Azure delivered via webhook, 'pull' when
     *   Windmill fetched from a Namespace queue.
     */
    export async function main(
    	payload: any,
    	eventType: string,
    	subject: string | undefined,
    	source: string,
    	eventId: string,
    	time: string | undefined,
    	deliveryType: 'push' | 'pull'
    ) {
    	console.log(`[${deliveryType}] ${eventType} from ${source} (${eventId})`)
    	return { received: payload, type: eventType, subject }
    }
    

    Submitted by hugo989 19 days ago