-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdolarito.ts
43 lines (37 loc) · 935 Bytes
/
dolarito.ts
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
let resultado:string | undefined
const dolarApi = {
*request(this: any, self: any) : any {
resultado = undefined
try {
buscarCotizacion((res: string) => {
resultado = res
})
}
catch (error) {
resultado = `Error: ${error}`
}
},
*response(this:any, self:any): any {
return this.reify(resultado)
},
}
function buscarCotizacion(callback: (resultado: string) => void): void {
const apiUrl = 'https://api.bluelytics.com.ar/v2/latest'
fetch(apiUrl)
.then(response => {
if (!response.ok) {
callback(`Error ${response}`)
return
}
return response.json()
})
.then(data => {
const compraBlue = data.blue.value_buy
const ventaBlue = data.blue.value_sell
callback(`Dólar Blue - Compra: $${compraBlue}, Venta: $${ventaBlue}`)
})
.catch(error => {
callback(`Error: ${error}`)
})
}
export { dolarApi }