-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
189 additions
and
287 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,29 @@ | ||
import got from "got"; | ||
|
||
export async function fetchCryptoCurrency(cryptoSymbol) { | ||
if (typeof cryptoSymbol !== "string") { | ||
throw "Simbol mata uang crypto harus berupa string"; | ||
} | ||
if (!/^[A-Z]{3,8}(?:IDR|USDT)$/.test(cryptoSymbol)) { | ||
throw `Simbol mata uang crypto ${cryptoSymbol} tidak valid`; | ||
} | ||
const requestSearchParams = new URLSearchParams(); | ||
requestSearchParams.set("symbol", cryptoSymbol.toUpperCase()); | ||
const response = await fetch(`https://gold.teknologiumum.com/crypto?${requestSearchParams.toString()}`); | ||
|
||
const { statusCode, body } = await got.get( | ||
`https://indodax.com/api/ticker/${cryptoSymbol.toLowerCase()}`, | ||
{ | ||
responseType: "json", | ||
throwHttpErrors: false | ||
} | ||
); | ||
|
||
if (statusCode !== 200) { | ||
if (response.status !== 200) { | ||
throw `Gagal mendapatkan data crypto ${cryptoSymbol}`; | ||
} | ||
|
||
|
||
if (body.error_description !== undefined) { | ||
throw body.error_description; | ||
} | ||
|
||
const body = await response.json(); | ||
|
||
return { | ||
last: parseFloat(body.ticker.last), | ||
buy: parseFloat(body.ticker.buy), | ||
sell: parseFloat(body.ticker.sell), | ||
high: parseFloat(body.ticker.high), | ||
low: parseFloat(body.ticker.low) | ||
symbol: body.symbol, | ||
base_currency: body.base_currency, | ||
quote_currency: body.quote_currency, | ||
open: parseFloat(body.open), | ||
high: parseFloat(body.high), | ||
low: parseFloat(body.low), | ||
last: parseFloat(body.last), | ||
volume: parseFloat(body.volume), | ||
date: new Date(body.date) | ||
}; | ||
} |
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
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
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
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