client-aftermath-ts-sdk
Version:
Client Aftermath TypeScript SDK
133 lines (132 loc) • 6.3 kB
JavaScript
;
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");
const transactions_2 = require("@mysten/sui.js/transactions");
class Caller {
// =========================================================================
// Constructor
// =========================================================================
constructor(config = {}, apiUrlPrefix = "") {
this.config = config;
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 + (url === "" ? "" : "/")}${url}`;
};
this.setAccessToken = (accessToken) => {
this.config.accessToken = accessToken;
};
this.apiBaseUrl =
this.config.network === undefined
? undefined
: Caller.apiBaseUrlForNetwork(this.config.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 headers = Object.assign({
// "Content-Type": "text/plain",
"Content-Type": "application/json" }, (this.config.accessToken
? { Authorization: `Bearer ${this.config.accessToken}` }
: {}));
const uncastResponse = yield (body === undefined
? fetch(apiCallUrl, {
headers,
signal,
})
: fetch(apiCallUrl, {
method: "POST",
body: JSON.stringify(body),
headers,
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 transactions_2.TransactionBlock.from(yield this.fetchApi(url, body, signal, options));
});
}
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) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const events = yield this.fetchApi(url, body, signal, options);
// TODO: handle this logic on af-fe instead (to handle max limit case)
return {
events,
nextCursor: events.length < ((_a = body.limit) !== null && _a !== void 0 ? _a : 1)
? undefined
: events.length + ((_b = body.cursor) !== null && _b !== void 0 ? _b : 0),
};
});
}
}
exports.Caller = Caller;