1 | |
2 | * General Trigger Preprocessor Function |
3 | * |
4 | * ⚠️ IMPORTANT: This preprocessor function is ALWAYS called FIRST, before the main function. |
5 | * |
6 | * This example assumes the script was triggered by an MQTT event, but the same |
7 | * preprocessor can handle other trigger types by checking wm_trigger.kind. |
8 | * |
9 | * Flow: |
10 | * 1. Trigger event occurs (MQTT message in this example) |
11 | * 2. Preprocessor function is called FIRST |
12 | * 3. Main function is called with the return values from preprocessor |
13 | * |
14 | * You can transform the raw trigger data here before it reaches your main function. |
15 | * Common transformations include: |
16 | * - Converting binary payload to string or JSON |
17 | * - Extracting metadata from the trigger source |
18 | * - Filtering messages |
19 | * - Adding timestamps or other context |
20 | * |
21 | * IMPORTANT: The object you return from this preprocessor determines the parameters passed to your main function: |
22 | * - If you return {a: 1, b: 2}, then main will be called as main(a, b) |
23 | * - If you return {payload}, then main will be called as main(payload) |
24 | * |
25 | * @param wm_trigger - Information about the trigger source (could be MQTT, HTTP, email, etc.) |
26 | * @param payload - The raw data from the trigger (type depends on the trigger kind) |
27 | * @returns An object with the arguments to pass to the main function |
28 | */ |
29 | export async function preprocessor( |
30 | wm_trigger: { |
31 | kind: 'http' | 'email' | 'webhook' | 'websocket' | 'kafka' | 'nats' | 'postgres' | 'sqs' | 'mqtt', |
32 | mqtt?: { |
33 | topic: string, |
34 | retain: boolean, |
35 | pkid: number, |
36 | qos: number, |
37 | v5?: { |
38 | payload_format_indicator?: number, |
39 | topic_alias?: number, |
40 | response_topic?: string, |
41 | correlation_data?: Array<number>, |
42 | user_properties?: Array<[string, string]>, |
43 | subscription_identifiers?: Array<number>, |
44 | content_type?: string |
45 | } |
46 | } |
47 | }, |
48 | |
49 | * Each trigger type has its own specific parameters: |
50 | * - For MQTT: 'payload' (Uint8Array of raw message bytes) |
51 | * - For SQS: 'msg' (message data) |
52 | * - Other trigger types have their own specific properties |
53 | */ |
54 | payload: Uint8Array, |
55 | ) { |
56 | if (wm_trigger.kind === 'mqtt' && wm_trigger.mqtt) { |
57 |
|
58 | |
59 | |
60 | const payloadAsString = new TextDecoder().decode(payload); |
61 |
|
62 | return { |
63 | contentType: wm_trigger.mqtt.v5?.content_type, |
64 | payload, |
65 | payloadAsString |
66 | } |
67 | } |
68 | else { |
69 | return { |
70 | kind: wm_trigger.kind |
71 | } |
72 | } |
73 | } |
74 |
|
75 | |
76 | * Main Function - Handles Trigger Events |
77 | * |
78 | * ⚠️ This function is called AFTER the preprocessor function. |
79 | * |
80 | * This example assumes we're handling an MQTT trigger event where the payload is a string. |
81 | * The main function receives arguments based on what the preprocessor returned. |
82 | * |
83 | * @param contentType - For MQTT: The content type from MQTT v5 properties if available |
84 | * @param payload - The raw binary payload from MQTT |
85 | * @param payloadAsString - The payload as a string (decoded from binary data) |
86 | */ |
87 | export async function main(payload: string, contentType?: string, payloadAsString: string) { |
88 |
|
89 | } |