Skip to content

Commit

Permalink
feat(api/coinmarketcap): init api/coinmarketcap (+server.ts)
Browse files Browse the repository at this point in the history
initialize api/coinmarketcap (+server.ts) endpoint (essentially middleware) to align with imposed
CORS configuration.

fix #1841;
  • Loading branch information
migbash committed Nov 29, 2023
1 parent ac06b58 commit c028dde
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/routes/api/coinmarketcap/+server.ts
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

0 comments on commit c028dde

Please sign in to comment.