UNPKG

hm-aftermath-ts-sdk

Version:
120 lines (119 loc) 5.52 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Caller = void 0; const transactions_1 = require("@mysten/sui/transactions"); const helpers_1 = require("./helpers"); class Caller { // ========================================================================= // Constructor // ========================================================================= constructor(network, apiUrlPrefix = "") { this.network = network; this.apiUrlPrefix = apiUrlPrefix; this.urlForApiCall = (url) => { if (this.apiBaseUrl === undefined) throw new Error("no apiBaseUrl: unable to fetch data"); // TODO: handle url prefixing and api calls based on network differently return `${this.apiBaseUrl}/api/${this.apiUrlPrefix === "" ? "" : this.apiUrlPrefix + "/"}${url}`; }; this.apiBaseUrl = network === undefined ? undefined : Caller.apiBaseUrlForNetwork(network); } // ========================================================================= // Private Methods // ========================================================================= static fetchResponseToType(response, disableBigIntJsonParsing) { return __awaiter(this, void 0, void 0, function* () { if (!response.ok) throw new Error(yield response.text()); const json = JSON.stringify(yield response.json()); const output = disableBigIntJsonParsing ? JSON.parse(json) : helpers_1.Helpers.parseJsonWithBigint(json); return output; }); } // ========================================================================= // Api Calling // ========================================================================= static apiBaseUrlForNetwork(network) { if (network === "MAINNET") return "https://aftermath.finance"; if (network === "TESTNET") return "https://testnet.aftermath.finance"; if (network === "DEVNET") return "https://devnet.aftermath.finance"; if (network === "LOCAL") return "http://localhost:3000"; const safeUrl = network.slice(-1) === "/" ? network.slice(0, -1) : network; return safeUrl; } // ========================================================================= // Protected Methods // ========================================================================= // ========================================================================= // Api Calling // ========================================================================= fetchApi(url, body, signal, options) { return __awaiter(this, void 0, void 0, function* () { if (!(options === null || options === void 0 ? void 0 : options.disableBigIntJsonParsing)) { (() => { // this allows BigInt to be JSON serialized (as string) BigInt.prototype.toJSON = function () { return this.toString() + "n"; }; })(); } const apiCallUrl = this.urlForApiCall(url); const uncastResponse = yield (body === undefined ? fetch(apiCallUrl, { headers: { "Content-Type": "text/plain", }, signal, }) : fetch(apiCallUrl, { method: "POST", headers: { "Content-Type": "text/plain", }, body: JSON.stringify(body), signal, })); const response = yield Caller.fetchResponseToType(uncastResponse, options === null || options === void 0 ? void 0 : options.disableBigIntJsonParsing); return response; }); } fetchApiTransaction(url, body, signal, options) { return __awaiter(this, void 0, void 0, function* () { return transactions_1.Transaction.from(yield this.fetchApi(url, body, signal, options)); }); } fetchApiTransactionV0(url, body, signal, options) { return __awaiter(this, void 0, void 0, function* () { return {}; }); } fetchApiEvents(url, body, signal, options) { return __awaiter(this, void 0, void 0, function* () { return this.fetchApi(url, body, signal, options); }); } fetchApiIndexerEvents(url, body, signal, options) { return __awaiter(this, void 0, void 0, function* () { return this.fetchApi(url, body, signal, options); }); } } exports.Caller = Caller;