Skip to content

feat: preprocessor new format #911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions docs/advanced/17_email_triggers/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ And if you use a [preprocessor](../../core_concepts/43_preprocessors/index.mdx),

```TypeScript
export async function preprocessor(
raw_email: string,
parsed_email: any,
wm_trigger: {
event: {
kind: "email",
},
raw_email: string,
parsed_email: any,
}
) {
if (event.kind !== "email") {
throw new Error("Expected an email event");
}
// return what you want to pass to the main function, for instance the sender email address and the email body
return {
sender_address: parsed_email.headers["From"][0].address,
email_body: parsed_email.text_body
sender_address: event.parsed_email.headers["From"][0].address,
email_body: event.parsed_email.text_body
}
}

Expand Down
61 changes: 31 additions & 30 deletions docs/core_concepts/39_http_routing/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,31 @@ export async function main(/* args from the request body */) {
With a preprocessor:
```ts
export async function preprocessor(
name: string,
age: number,
wm_trigger: {
kind: 'http' | 'email' | 'webhook' | 'websocket' | 'kafka' | 'nats' | 'postgres' | 'sqs' | 'gcp',
http: {
route: string;
path: string;
method: string;
params: Record<string, string>;
query: Record<string, string>;
headers: Record<string, string>;
}
event: {
kind: 'http',
body: { // assuming the body contains name and age parameters
name: string,
age: number,
},
raw_string: string | null,
route: string;
path: string;
method: string;
params: Record<string, string>;
query: Record<string, string>;
headers: Record<string, string>;
}
) {
if (wm_trigger.kind === 'http' && wm_trigger.http) {
if (event.kind === 'http') {
const { name, age } = event.body;
return {
user_id: wm_trigger.http.params.id,
user_id: event.params.id,
name,
age
age,
};
}

throw new Error(`Expected trigger of kind 'http', but received: ${wm_trigger.kind}`);
throw new Error(`Expected trigger of kind 'http', but received: ${event.kind}`);
}

export async function main(user_id: string, name: string, age: number) {
Expand Down Expand Up @@ -204,29 +206,28 @@ Example script for HMAC signature validation:
const SECRET_KEY_VARIABLE_PATH = "u/admin/well_backlit_variable";

export async function main(
wm_trigger: {
event: {
kind: 'http',
http?: {
route: string;
path: string;
method: string;
params: Record<string, string>;
query: Record<string, string>;
headers: Record<string, string>;
};
body: any
raw_string: string,
route: string;
path: string;
method: string;
params: Record<string, string>;
query: Record<string, string>;
headers: Record<string, string>;
},
raw_string: string
) {
if (!wm_trigger.http) {
throw new Error('Missing HTTP context');
if (event.kind !== 'http') {
throw new Error('Expected a http event');
}

const signature = wm_trigger.http.headers['x-signature'] || wm_trigger.http.headers['signature'];
const signature = event.headers['x-signature'] || event.headers['signature'];
if (!signature) {
throw new Error('Missing signature in request headers.');
}

const timestamp = wm_trigger.http.headers['x-timestamp'] || wm_trigger.http.headers['timestamp'];
const timestamp = event.headers['x-timestamp'] || event.headers['timestamp'];
if (timestamp) {
const timestampValue = parseInt(timestamp, 10);
const currentTime = Math.floor(Date.now() / 1000);
Expand Down
18 changes: 10 additions & 8 deletions docs/core_concepts/40_websocket_triggers/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ And if you use a [preprocessor](../43_preprocessors/index.mdx), the script could

```TypeScript
export async function preprocessor(
msg: string,
wm_trigger: {
event: {
kind: "websocket",
websocket: {
url: string // the WebSocket URL
}
},
msg: string,
url: string,
}
) {
if (event.kind !== "websocket") {
throw new Error(`Expected a websocket event`);
}

// assuming the message is a JSON object
const msg = JSON.parse(msg);
const msg = JSON.parse(event.msg);

// define args for the main function
// let's assume we want to use the message content and the url
return {
message_content: msg.content,
url: wm_trigger.websocket.url
url: event.url
};
}

Expand Down
26 changes: 14 additions & 12 deletions docs/core_concepts/41_kafka_triggers/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ The group id is automatically filled in from the current workspace and the trigg
It indicates the consumer group to which the trigger belongs. This garantees that even if the trigger stops listening for a while, it will receive the messages it missed when it starts listening again.

Once the Kafka resource and settings are set, select the runnable that should be triggered by this trigger.
The received webhook message will be passed to the runnable as a string argument called `msg`.
The received webhook base64 encoded payload will be passed to the runnable as a string argument called `payload`.

Here's an example script:

```TypeScript
export async function main(msg: string) {
export async function main(payload: string) {
// do something with the message
}
```
Expand All @@ -29,24 +29,26 @@ And if you use a [preprocessor](../43_preprocessors/index.mdx), the script could

```TypeScript
export async function preprocessor(
msg: string,
wm_trigger: {
event: {
kind: "kafka",
kafka: {
brokers: string[];
topic: string; // the specific topic the message was received from
group_id: string;
}
},
payload: string, // base64 encoded payload
brokers: string[];
topic: string; // the specific topic the message was received from
group_id: string;
}
) {
if (event.kind !== "kafka") {
throw new Error(`Expected a kafka event`);
}

// assuming the message is a JSON object
const msg = JSON.parse(msg);
const msg = JSON.parse(atob(event.payload));

// define args for the main function
// let's assume we want to use the message content and the topic
return {
message_content: msg.content,
topic: wm_trigger.kafka.topic
topic: event.topic
};
}

Expand Down
Loading