zerion-sdk
Version:
A Typed Interface for ZerionAPI
37 lines (36 loc) • 1.57 kB
JavaScript
import { ZERION_CONFIG } from "../config";
import mainnetChainData from "../../data/mainnet-chains.json";
import mainnetNativeTokens from "../../data/mainnet-native-tokens.json";
import testnetChainData from "../../data/testnet-chains.json";
import testnetNativeTokens from "../../data/testnet-native-tokens.json";
// A lot of ceremony, but better than dumping that whole file here.
export const STATIC_CHAINS_MAINNET = mainnetChainData;
export const STATIC_NATIVE_TOKENS_MAINNET = mainnetNativeTokens;
export const STATIC_CHAINS_TESTNET = testnetChainData;
export const STATIC_NATIVE_TOKENS_TESTNET = testnetNativeTokens;
export class ZerionService {
apiKey;
env;
constructor(apiKey, testnet) {
this.apiKey = apiKey.startsWith("zk_")
? Buffer.from(`${apiKey}:`).toString("base64")
: apiKey;
this.env = testnet ? "testnet" : undefined;
}
// Utility function to make API requests with error handling
async fetchFromZerion(endpoint) {
const headers = {
accept: "application/json",
authorization: `Basic ${this.apiKey}`,
// Optionally add the "X-Env" header for testnet or other environments
...(this.env ? { "X-Env": this.env } : {}),
};
const url = `${ZERION_CONFIG.BASE_URL}${endpoint}`;
const response = await fetch(url, { headers });
if (!response.ok) {
throw new Error(`Failed to fetch ${endpoint}: ${response.statusText}`);
}
const data = await response.json();
return data;
}
}