TypeScript Bricks
Purpose | Explore this guide to gain a deeper understanding of TypeScript Bricks. We will walk you through various elements you can use for configuration and provide example instructions for setting them up. |
---|
Last Updated | May 20, 2024 |
---|
TypeScript scripting bricks give you access to Deno's extensive list of third-party modules and allow you to process your messages with only a few lines of code. We use Deno for our TypeScript bricks, which is a secure Runtime for JavaScript and TypeScript.
Configuring a TypeScript brick
TypeScript bricks have the following configuration parameters:
Parameter | Description |
---|---|
Script | You can add and edit your TypeScript script in this field |
Script (URL) | Alternatively you can put a link to a gist of your script in this field |
TypeScript scripting bricks can be used for all brick types (source, transform, and action.)
Name | Brick Type |
---|---|
typescript-source-ng | Source |
typescript-transform-ng | Transform |
typescript-action-ng | Action |
TypeScript example
typescript-transform
import { transform, update, flatten, grok, getValue, setValue, copy, withNamespace } from 'https://gist.githubusercontent.com/nl5887/f16bdf0a9ef2881aa8d1b4bc159ced18/raw/653e4e0b8fee317419c1bf7bdda009c474265379/mod.ts';
// Connect to Workflow
const { Consumer, Producer, Message } = Workflow;
let consumer = await Consumer.connect({});
const producer = await Producer.connect({});
let count = 0;
const start = Date.now();
try {
for await (let msg of consumer) {
// Use getValue() to get a value from the payload, or access directly.
const someValue = getValue(['someValue'], msg.payload)
// Use consumer.ack(msg) to acknowledge a message before producing it.
// Effectively not "producing" it to used in the next brick in the flow.
if (!someValue) {
consumer.ack(msg);
continue
}
// Use console.log("thing") statements to log anything
// Use producer.produce() to pass on a message to the next brick
await producer.produce({
payload: {
someNewValue: someValue,
...msg.payload,
},
});
// Acknowledge the message & increase count of processed messages.
consumer.ack(msg);
count++;
}
} catch (e) {
console.error(e); // Log errors
}
// Calculate and log execution time of the script:
const diff = (Date.now() - start) / 1000; /* seconds */
console.info('duration=%f rate=%f', diff, count / diff);
await consumer.close();