-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsb.yml
More file actions
46 lines (44 loc) · 1.5 KB
/
Copy pathsb.yml
File metadata and controls
46 lines (44 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# yaml-language-server: $schema=../../schemas/sb-config.schema.json
# JavaScript body transform.
#
# Runs a JavaScript function against the raw body string. The entrypoint is
# the top-level `function transform(body)`, where `body` is a string. The
# script parses the JSON, mutates it, and returns a new JSON string. A
# `static` action seeds the input so the example runs offline.
#
# Run:
# make run CONFIG=examples/transform-javascript/sb.yml
#
# Test:
# curl -s -H 'Host: js.local' http://127.0.0.1:8080/
#
# Expected: title reversed, title_length added, body trimmed to 40 chars,
# transformed_by tag added.
proxy:
http_bind_port: 8080
origins:
"js.local":
action:
type: static
status: 200
content_type: application/json
json_body:
id: 1
title: "javascript runs on the request path"
body: "this body is longer than forty characters so it will be trimmed by the script"
userId: 7
transforms:
- type: javascript
script: |
function transform(body) {
var data = JSON.parse(body);
if (typeof data.title === "string") {
data.title_length = data.title.length;
data.title_reversed = data.title.split("").reverse().join("");
}
if (typeof data.body === "string" && data.body.length > 40) {
data.body = data.body.slice(0, 40) + "...";
}
data.transformed_by = "javascript";
return JSON.stringify(data);
}