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
- `cors({ origin, methods, headers })`— enable and configure CORS headers. All options are strings.
122
125
`throttle({ rate, chunkSize, burst, key })`— throttles bandwidth per request to a specified rate (bytes per second), with optional burst capacity and chunk size. The key option allows per-client throttling. (Implemented natively, not using koa-throttle.)
123
-
- `bodyTransform({ transform })`— parse and mutate request body with a custom function.
126
+
- `bodyTransform({ request?, response? })`— parse and mutate request and/or response body with custom functions.
124
127
125
128
### Rate Limiting
126
129
@@ -191,28 +194,28 @@ This configuration throttles responses to an average of 1 KB/s, sending data in
191
194
192
195
### Body Transform
193
196
194
-
The `bodyTransform` middleware parses the incoming request body (JSON, form, or text), then replaces it with the result of your custom `transform` function. This lets you inspect, modify, or completely replace the body before it is proxied or processed by other middlewares. It uses `koa-bodyparser` under the hood.
197
+
The `bodyTransform` middleware allows you to parse and mutate both the request and response bodies using custom transformation functions. You can specify a `request` and/or `response` transform, each as either a JavaScript function string (for YAML config) or a real function (for programmatic usage). Backward compatibility with the old `transform` key is removed—use the new object shape only.
195
198
196
199
How it works:
197
200
- Parses the request body and makes it available as `ctx.request.body`.
198
-
- Calls your `transform` function with the parsed body and Koa context.
199
-
- Sets the return value as the new `ctx.request.body`.
200
-
- Subsequent middlewares and proxy logic use the mutated body.
201
+
- If a `request` transform is provided, it is called with the parsed body and Koa context, and its return value replaces `ctx.request.body`.
202
+
- After downstream middleware and proxying, if a `response` transform is provided, it is called with the response body (`ctx.body`) and Koa context, and its return value replaces `ctx.body`.
203
+
- Both transforms can be used independently or together.
This configuration adds a `foo: 'bar'` property to every JSON request body before it is proxied to the target server.
213
+
This configuration adds a `foo: 'bar'` property to every JSON request body and a `transformed: true` property to every JSON response body.
210
214
211
215
**Note:**
212
-
For maximum flexibility, the `transform` option in `bodyTransform` can be specified as a JavaScript function string in your YAML config. This allows you to define custom transformation logic directly in the config file. Be aware that evaluating JS from config can introduce security and syntax risks. Use with care and only in trusted environments.
216
+
For maximum flexibility, the `request` and `response` options in `bodyTransform` can be specified as JavaScript function strings in your YAML config. This allows you to define custom transformation logic directly in the config file. Be aware that evaluating JS from config can introduce security and syntax risks. Use with care and only in trusted environments.
213
217
214
-
215
-
If you call `startServer` programmatically, you can also pass a real function instead of a string:
218
+
If you call `startServer` programmatically, you can also pass real functions instead of strings:
216
219
217
220
```ts
218
221
import { startServer, bodyTransform } from 'chaos-proxy';
0 commit comments