Skip to content

Commit 1482d4b

Browse files
committed
feat(api): prices added
1 parent 9ded443 commit 1482d4b

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/pages/api/prices.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
interface EndpointInterface {
2+
name: string;
3+
values: {
4+
ask: number;
5+
totalAsk: number;
6+
bid: number;
7+
totalBid: number;
8+
time: number;
9+
};
10+
}
11+
12+
interface PricesInterface {
13+
success: boolean;
14+
data?: Array<EndpointInterface>;
15+
}
16+
17+
export async function getPrices(): Promise<PricesInterface> {
18+
const currency = 'usd';
19+
const tokens = ['eth', 'dai'];
20+
21+
let promises = [];
22+
23+
tokens.map((token) => {
24+
promises.push({ name: token, url: `https://criptoya.com/api/tiendacrypto/${token}/${currency}/0.5` });
25+
});
26+
27+
const data = await Promise.all(
28+
promises.map(async ({ name, url }) => {
29+
const resp = await fetch(url);
30+
const body = await resp.text();
31+
32+
return { name, values: JSON.parse(body) };
33+
}),
34+
);
35+
36+
if (data) {
37+
return {
38+
success: true,
39+
data,
40+
};
41+
} else {
42+
return {
43+
success: false,
44+
};
45+
}
46+
}

0 commit comments

Comments
 (0)