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: currency-converter-display-js
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) {
display.push(currency + " = " + currencyCodes[currency] + json.rates[currency])
}

// final output to display
"$1 = " + display.join(", ")
Troubleshooting

You can print to the console for rapid development e.g. console.log(JSON.stringify(json)), this is only 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(", ");