Skip to main content

Javascript

javascript expressions use to the otto Javascript VM

display-with-javascript.yaml
apiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: http-check
spec:
http:
- name: USD
url: https://api.frankfurter.app/latest?from=USD&to=GBP,EUR,ILS
display:
javascript: |
currencyCodes = { "EUR": "€", "GBP": "£", "ILS": "₪"}
display = ""
for (var currency in json.rates) {
if (display != "") {
display += ", "
}
display += currency + " = " + currencyCodes[currency] + json.rates[currency] + ", "
}
"$1 = " + display
Troubleshooting

You can print to the console for rapid development e.g. console.log(JSON.stringify(json)), this will only be shown in the logs

Underscore

The underscore library is automatically imported so you can do the following:

display-format-with-underscore.yaml
apiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: http-check
spec:
http:
- name: USD
url: https://api.frankfurter.app/latest?from=USD&to=GBP,EUR,ILS
display:
javascript: |
currencyCodes = { "EUR": "€", "GBP": "£", "ILS": "₪"}
"$1 = " + _.map(json.rates, function(rate, currency) {
return currencyCodes[currency] + rate;
}).join(", ");