You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+77Lines changed: 77 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,7 @@ Inhooks listens to HTTP webhooks and saves the messages to Redis. A processing m
26
26
- Supports delayed processing
27
27
- Supports retries on failure with configurable number of attempts, interval and constant or exponential backoff
28
28
- Supports different HTTP payloads types: JSON, x-www-form-urlencoded, multipart/form-data
29
+
- Supports message transformation using JavaScript ECMAScript 5.1
29
30
- ... more features planned
30
31
31
32
## Usage
@@ -79,6 +80,82 @@ flows:
79
80
currentSecretEnvVar: VERIFICATION_FLOW_1_CURRENT_SECRET # the name of the environment variable containing the verification secret
80
81
previousSecretEnvVar: VERIFICATION_FLOW_1_PREVIOUS_SECRET # optional env var that allows rotating secrets without service interruption
81
82
```
83
+
84
+
### Message transformation
85
+
86
+
#### Transform definition
87
+
88
+
Message transformation allows you to modify the payload and headers of messages before they are sent to the sinks (destinations). This can be useful for tasks such as adding or removing fields, changing the format of the data, or adding custom headers.
89
+
90
+
Currently, only JavaScript transformations are supported. The JavaScript function should be named `transform` and should take two parameters: `bodyStr`(the message body as a string) and `headers` (the message headers as a JSON object). The function should return an array with two elements: the transformed payload as a string and the transformed headers as a JSON object.
91
+
The `headers` fields has the following format:
92
+
```
93
+
{
94
+
"header-name": ["value1", "value2"]
95
+
}
96
+
```
97
+
98
+
Only JavaScript ECMAScript 5.1 is supported at the moment. We use the [goja](https://github.com/dop251/goja) library to execute the JavaScript code. You can read about the limitations on goja's documentation pages.
99
+
100
+
Here is an example configuration:
101
+
```yaml
102
+
flows:
103
+
- id: flow-1
104
+
source:
105
+
id: source-1
106
+
slug: source-1-slug
107
+
type: http
108
+
sinks:
109
+
- id: sink-1
110
+
type: http
111
+
url: https://example.com/target
112
+
transform:
113
+
id: js-transform-1
114
+
transform_definitions:
115
+
- id: js-transform-1
116
+
type: javascript
117
+
script: |
118
+
function transform(bodyStr, headers) {
119
+
const body = JSON.parse(bodyStr);
120
+
121
+
// add a header
122
+
headers["X-INHOOKS-TRANSFORMED"] = ["1"];
123
+
// capitalize the message if present
124
+
if (body.msg) {
125
+
body.msg = body.msg.toUpperCase();
126
+
}
127
+
// delete a key from the body
128
+
delete body.my_dummy_key;
129
+
130
+
return [JSON.stringify(body), headers];
131
+
}
132
+
```
133
+
134
+
135
+
#### Testing transform scripts
136
+
137
+
You can use the `/api/v1/transform` endpoint to test your transform scripts before adding them to your flow configuration. This endpoint allows you to simulate the transformation process and see the results immediately.
138
+
139
+
To use this endpoint, send a POST request with a JSON payload containing the following fields:
140
+
- `body`: The message body as a string
141
+
- `headers`: The message headers as a JSON object
142
+
- `transformDefinition`: An object containing the `type` and `script` of your transformation
143
+
144
+
Here's an example of how to use the `/api/v1/transform` endpoint:
145
+
```shell
146
+
curl -X POST http://localhost:3000/api/v1/transform \
0 commit comments