/**
* MQTT Preprocessor Function
*
* This function is called when an MQTT message is received, BEFORE your main handler function.
* It allows you to transform or validate the incoming data before processing.
*
* Flow: MQTT message → preprocessor function → main function
*
* IMPORTANT: The object you return from this preprocessor determines the parameters passed to your main function:
* - If you return {a: 1, b: 2}, then main will be called as main(a, b)
* - If you return {payload}, then main will be called as main(payload)
* - If you return nothing or {}, then main will only receive the raw payload as main(payload)
*
* @param wm_trigger - Information about the trigger source (MQTT in this case)
* @param payload - The raw bytes of the MQTT message (as Uint8Array)
* @returns An object with the arguments to pass to the main function
*/
export async function preprocessor(
wm_trigger: {
kind: 'http' | 'email' | 'webhook' | 'websocket' | 'kafka' | 'nats' | 'postgres' | 'sqs' | 'mqtt',
mqtt: {
topic: string,
retain: boolean,
pkid: number,
qos: number,
v5?: {
payload_format_indicator?: number,
topic_alias?: number,
response_topic?: string,
correlation_data?: Array<number>,
user_properties?: Array<[string, string]>,
subscription_identifiers?: Array<number>,
content_type?: string
}
}
},
/**
* The raw message payload from the MQTT broker as bytes
* This preprocessor function is called before your main function
* The object you return from this function will be passed as arguments to your main function
* You can transform the payload here before it reaches your main handler
*/
payload: Uint8Array,
) {
return {
content_type: wm_trigger.mqtt.v5?.content_type,
payload,
payloadAsString: new TextDecoder().decode(payload)
}
}
/**
* Main Function - Handles MQTT Messages
*
* This function receives arguments based on what was returned by the preprocessor:
* - If preprocessor returns {payload, content_type}, main is called as main(payload, content_type)
* - If preprocessor returns {payload, someValue}, main is called as main(payload, someValue)
* - If preprocessor returns {}, main is called as main(payload) with the raw payload only
*
* IMPORTANT:
* - The 'payload' parameter will always be available to the main function
* - Any other parameters only exist if explicitly returned from the preprocessor
* - There are no "special" parameters - all additional parameter names come directly
* from what you return in the preprocessor
*
* @param content_type - The content type from MQTT v5 properties if available
* @param payload - The raw binary payload from MQTT
* @param payloadAsString - The payload converted to a string
* @returns The result of processing the MQTT message
*/
export async function main(payload: string, content_type?: string, payloadAsString: string) {
}Submitted by dieriba.pro916 442 days ago