zerion-sdk
Version:
A Typed Interface for ZerionAPI
42 lines (41 loc) • 2.14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZerionService = exports.STATIC_NATIVE_TOKENS_TESTNET = exports.STATIC_CHAINS_TESTNET = exports.STATIC_NATIVE_TOKENS_MAINNET = exports.STATIC_CHAINS_MAINNET = void 0;
const config_1 = require("../config");
const mainnet_chains_json_1 = __importDefault(require("../../data/mainnet-chains.json"));
const mainnet_native_tokens_json_1 = __importDefault(require("../../data/mainnet-native-tokens.json"));
const testnet_chains_json_1 = __importDefault(require("../../data/testnet-chains.json"));
const testnet_native_tokens_json_1 = __importDefault(require("../../data/testnet-native-tokens.json"));
// A lot of ceremony, but better than dumping that whole file here.
exports.STATIC_CHAINS_MAINNET = mainnet_chains_json_1.default;
exports.STATIC_NATIVE_TOKENS_MAINNET = mainnet_native_tokens_json_1.default;
exports.STATIC_CHAINS_TESTNET = testnet_chains_json_1.default;
exports.STATIC_NATIVE_TOKENS_TESTNET = testnet_native_tokens_json_1.default;
class ZerionService {
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 = `${config_1.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;
}
}
exports.ZerionService = ZerionService;