Source: index.js

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Helius = exports.Types = void 0;
const axios_1 = __importDefault(require("axios"));
const API_URL_V0 = "https://api.helius.xyz/v0";
const API_URL_V1 = "https://api.heliuys.xyz/v1";
exports.Types = __importStar(require("./types"));
/**
 * This is the base level class for interfacing with all Helius API methods.
 * @class
 */
class Helius {
    /**
     * Initializes Helius API client with an API key
     * @constructor
     * @param apiKey - API key generated at dev.helius.xyz
     */
    constructor(apiKey) {
        this.apiKey = apiKey;
    }
    /**
    * Retrieves a list of all webhooks associated with the current API key
    * @returns {Promise<Webhook[]>} a promise that resolves to an array of webhook objects
    * @throws {Error} if there is an error calling the webhooks endpoint or if the response contains an error
    */
    getAllWebhooks() {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const { data } = yield axios_1.default.get(`${API_URL_V0}/webhooks?api-key=${this.apiKey}`);
                return data;
            }
            catch (err) {
                if (axios_1.default.isAxiosError(err)) {
                    throw new Error(`error calling getWebhooks: ${(_a = err.response) === null || _a === void 0 ? void 0 : _a.data.error}`);
                }
                else {
                    throw new Error(`error calling getWebhooks: ${err}`);
                }
            }
        });
    }
    /**
    * Retrieves a single webhook by its ID, associated with the current API key
    * @param {string} webhookID - the ID of the webhook to retrieve
    * @returns {Promise<Webhook>} a promise that resolves to a webhook object
    * @throws {Error} if there is an error calling the webhooks endpoint or if the response contains an error
    */
    getWebhookByID(webhookID) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const { data } = yield axios_1.default.get(`${API_URL_V0}/webhooks/${webhookID}?api-key=${this.apiKey}`);
                return data;
            }
            catch (err) {
                if (axios_1.default.isAxiosError(err)) {
                    throw new Error(`error during getWebhookByID: ${(_a = err.response) === null || _a === void 0 ? void 0 : _a.data.error}`);
                }
                else {
                    throw new Error(`error during getWebhookByID: ${err}`);
                }
            }
        });
    }
    /**
    * Creates a new webhook with the provided request
    * @param {CreateWebhookRequest} createWebhookRequest - the request object containing the webhook information
    * @returns {Promise<Webhook>} a promise that resolves to the created webhook object
    * @throws {Error} if there is an error calling the webhooks endpoint or if the response contains an error
    */
    createWebhook(createWebhookRequest) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const { data } = yield axios_1.default.post(`${API_URL_V0}/webhooks?api-key=${this.apiKey}`, Object.assign({}, createWebhookRequest));
                return data;
            }
            catch (err) {
                if (axios_1.default.isAxiosError(err)) {
                    throw new Error(`error during createWebhook: ${(_a = err.response) === null || _a === void 0 ? void 0 : _a.data.error}`);
                }
                else {
                    throw new Error(`error during createWebhook: ${err}`);
                }
            }
        });
    }
    /**
    * Deletes a webhook by its ID
    * @param {string} webhookID - the ID of the webhook to delete
    * @returns {Promise<boolean>} a promise that resolves to true if the webhook was successfully deleted, or false otherwise
    * @throws {Error} if there is an error calling the webhooks endpoint or if the response contains an error
    */
    deleteWebhook(webhookID) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            try {
                yield axios_1.default.delete(`${API_URL_V0}/webhooks/${webhookID}?api-key=${this.apiKey}`);
                return true;
            }
            catch (err) {
                if (axios_1.default.isAxiosError(err)) {
                    throw new Error(`error during deleteWebhook: ${(_a = err.response) === null || _a === void 0 ? void 0 : _a.data.error}`);
                }
                else {
                    throw new Error(`error during deleteWebhook: ${err}`);
                }
            }
        });
    }
    /**
    * Edits an existing webhook by its ID with the provided request
    * @param {string} webhookID - the ID of the webhook to edit
    * @param {EditWebhookRequest} editWebhookRequest - the request object containing the webhook information
    * @returns {Promise<Webhook>} a promise that resolves to the edited webhook object
    * @throws {Error} if there is an error calling the webhooks endpoint or if the response contains an error
    */
    editWebhook(webhookID, editWebhookRequest) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const webhook = yield this.getWebhookByID(webhookID);
                const { data } = yield axios_1.default.put(`${API_URL_V0}/webhooks/${webhookID}?api-key=${this.apiKey}`, Object.assign(Object.assign({}, webhook), editWebhookRequest));
                return data;
            }
            catch (err) {
                if (axios_1.default.isAxiosError(err)) {
                    throw new Error(`error during editWebhook: ${(_a = err.response) === null || _a === void 0 ? void 0 : _a.data.error}`);
                }
                else {
                    throw new Error(`error during editWebhook: ${err}`);
                }
            }
        });
    }
    /**
    * Appends an array of addresses to an existing webhook by its ID
    * @param {string} webhookID - the ID of the webhook to edit
    * @param {string[]} newAccountAddresses - the array of addresses to be added to the webhook
    * @returns {Promise<Webhook>} a promise that resolves to the edited webhook object
    * @throws {Error} if there is an error calling the webhooks endpoint, if the response contains an error, or if the number of addresses exceeds 10,000
    */
    appendAddressesToWebhook(webhookID, newAccountAddresses) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const webhook = yield this.getWebhookByID(webhookID);
                const accountAddresses = webhook.accountAddresses.concat(newAccountAddresses);
                webhook.accountAddresses = accountAddresses;
                if (accountAddresses.length > 10000) {
                    throw new Error(`a single webhook cannot contain more than 10,000 addresses`);
                }
                const { data } = yield axios_1.default.put(`${API_URL_V0}/webhooks/${webhookID}?api-key=${this.apiKey}`, Object.assign({}, webhook));
                return data;
            }
            catch (err) {
                if (axios_1.default.isAxiosError(err)) {
                    throw new Error(`error during appendAddressesToWebhook: ${(_a = err.response) === null || _a === void 0 ? void 0 : _a.data.error}`);
                }
                else {
                    throw new Error(`error during appendAddressesToWebhook: ${err}`);
                }
            }
        });
    }
}
exports.Helius = Helius;