Created
August 18, 2019 03:32
-
-
Save luandevpro/1babdab2c526a8a5f07141258c1a41fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener('fetch', event => { | |
event.respondWith(fetchAndApply(event.request)) | |
}) | |
async function fetchAndApply(request) { | |
const init = { | |
method: 'GET', | |
headers: {'Authorization': 'XXXXXX'} | |
} | |
const [btcResp, ethResp, ltcResp] = await Promise.all([ | |
fetch('https://api.coinbase.com/v2/prices/BTC-USD/spot', init), | |
fetch('https://api.coinbase.com/v2/prices/ETH-USD/spot', init), | |
fetch('https://api.coinbase.com/v2/prices/LTC-USD/spot', init) | |
]) | |
const btc = await btcResp.json() | |
const eth = await ethResp.json() | |
const ltc = await ltcResp.json() | |
let combined = {} | |
combined['btc'] = btc['data'].amount | |
combined['ltc'] = ltc['data'].amount | |
combined['eth'] = eth['data'].amount | |
const responseInit = { | |
headers: {'Content-Type': 'application/json'} | |
} | |
return new Response(JSON.stringify(combined), responseInit) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment