-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api/coinmarketcap): init api/coinmarketcap (+server.ts)
initialize api/coinmarketcap (+server.ts) endpoint (essentially middleware) to align with imposed CORS configuration. fix #1841;
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// ### ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ | ||
// ### 📝 DESCRIPTION ◼️ | ||
// ### Application Server Endpoint for Featured Match Data Fetch + Handle ◼️ | ||
// ### ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ | ||
|
||
// #region ➤ 📦 Package Imports | ||
|
||
import { json } from '@sveltejs/kit'; | ||
|
||
import dotenv from 'dotenv'; | ||
import LZString from 'lz-string'; | ||
|
||
import { get } from '$lib/api/utils.js'; | ||
import { checkNull } from '$lib/utils/platform-functions.js'; | ||
|
||
import type { ICoinMarketCapDataMain } from '@betarena/scores-lib/types/_WEB3_.js'; | ||
|
||
// #endregion ➤ 📦 Package Imports | ||
|
||
// #region ➤ 📌 VARIABLES | ||
|
||
dotenv.config(); | ||
|
||
// #endregion ➤ 📌 VARIABLES | ||
|
||
// #region ➤ 🛠️ METHODS | ||
|
||
// ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ | ||
// ENDPOINT ENTRY ◼️ | ||
// ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ | ||
|
||
export async function GET | ||
( | ||
req: any | ||
): Promise < unknown > | ||
{ | ||
try | ||
{ | ||
// ▓ NOTE: | ||
// ▓ > extract request data. | ||
const tickers: string = req?.url?.searchParams?.get('tickers'); | ||
|
||
// ▓ NOTE: | ||
// ▓ > obtain target ticker/symbol data. | ||
const if_M_0: boolean = | ||
!checkNull(tickers) | ||
; | ||
if (if_M_0) | ||
{ | ||
console.log('HERE'); | ||
|
||
let data: unknown; | ||
let loadType: string = "endpoint"; | ||
|
||
const cryptoPrices = await get | ||
( | ||
'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=USDT,USDC&CMC_PRO_API_KEY=af3ceee8-f47b-4dba-80f1-242f5aa1156c' | ||
) as ICoinMarketCapDataMain; | ||
|
||
if (cryptoPrices != null) | ||
{ | ||
const compressed: string = LZString.compress(JSON.stringify(cryptoPrices)); | ||
|
||
// ▓ [🐞] | ||
// console.log(JSON.parse(LZString.decompress(compressed))); | ||
|
||
return json | ||
( | ||
{ | ||
data: compressed, | ||
loadType | ||
} | ||
); | ||
}; | ||
} | ||
|
||
// ### IMPORTANT | ||
return json | ||
( | ||
null | ||
); | ||
} | ||
catch (ex) | ||
{ | ||
// ▓ [🐞] | ||
console.error | ||
( | ||
'❌ Err: ', | ||
ex | ||
); | ||
return json | ||
( | ||
null, | ||
{ | ||
status: 400, | ||
statusText: 'Uh-oh! There has been an error' | ||
} | ||
); | ||
} | ||
} | ||
|
||
// #endregion ➤ 🛠️ METHODS |