UNPKG

@poprank/sdk

Version:

PopRank API client and types

340 lines 15.8 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PopRankClient = void 0; var axios_1 = __importDefault(require("axios")); var constants_1 = require("./constants"); /** If successful, returns `data`. Otherwise, throws an error. */ var handleResponse = function (response) { if (!response.success) { throw new Error(response.data); } return response.data; }; /** The PopRank API client. */ var PopRankClient = /** @class */ (function () { function PopRankClient(config) { this.client = axios_1.default.create(__assign({ baseURL: constants_1.API_BASE_URL }, config)); } /** * Get all `Collection` objects. If `slug` is provided, then the collection * is updated on the server, before the response is sent. * @summary Get all collections. * @param slug The collection identifier used in the PopRank collection page URL. * If provided, the traits object is included in the response. * @param player A player's wallet address . */ PopRankClient.prototype.getCollections = function (slug, player) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get('/collections', { params: { slug: slug, player: player } })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Get the `Collection` object that corresponds with the provided `slug`. The * collection is updated on PopRank's server before response, so this method * effectively doubles as a data refresh. * @summary Get a collection. * @param slug The collection identifier used in the PopRank collection page URL. * * If provided, the traits object is included in the response. */ PopRankClient.prototype.getCollection = function (slug) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get("/collections/".concat(slug), { params: { slug: slug } })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Get the specified collection's Overview tab data. * @summary Get collection overview. * @param slug The collection identifier used in the PopRank collection page URL. */ PopRankClient.prototype.getCollectionOverview = function (slug) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get('/collection/overview', { params: { slug: slug } })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Get the number of unique owners for `slug`. * @summary Get number of unique owners. * @param slug The collection identifier used in the PopRank collection page URL. */ PopRankClient.prototype.getNumOwners = function (slug) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get('/collection/owners', { params: { slug: slug } })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Get ranking data for numerous assets. * @summary Get a collection's NFTs. * @param slug The collection identifier used in the PopRank collection page URL. * @param options Optional object to paginate and filter response. */ PopRankClient.prototype.getNfts = function (slug, options) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get("/nfts/".concat(slug), { params: options })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Gets the ranking data for a single asset. * @summary Get an NFT. * @param slug The collection identifier used in the PopRank collection page URL. * @param id The NFT's token ID. * @param options Optional object to include more data in the response. */ PopRankClient.prototype.getNft = function (slug, id, options) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get("/nfts/".concat(slug, "/").concat(id), { params: options })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Gets a random pair of NFTs to rank. * @summary Get NFT pair. * @param slug The collection identifier used in the PopRank collection page URL. * @param player Although optional, required to validate the round. */ PopRankClient.prototype.getNftPair = function (slug, player) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get("/nfts/".concat(slug, "/double"), { params: { player: player } })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Get the total number of rounds played (valid and invalid). * @summary Get global rounds. */ PopRankClient.prototype.getGlobalRounds = function () { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get('/rounds', { params: { count: true } })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Get the number of rounds played by `player` for a specific collection, identified by `slug`. * @summary Get collection rounds. * @param slug The collection identifier used in the PopRank collection page URL. * @param player The player's wallet address. */ PopRankClient.prototype.getCollectionRounds = function (slug, player) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get("/rounds/".concat(slug), { params: { player: player, count: true } })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Get up to last 5 rounds for an NFT. * @summary Get NFT rounds. * @param slug The collection identifier used in the PopRank collection page URL. * @param id The NFT's token ID. */ PopRankClient.prototype.getNftRounds = function (slug, id) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get("/rounds/".concat(slug, "/").concat(id))]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Send HTTP PUT request containing the round's `winner` and `loser`. * @summary Send ranking game result. * @param winner The winning NFT contestant * @param loser The losing NFT contestant * @param player The player's wallet address * @param dir The direction selected by the player */ PopRankClient.prototype.postRound = function (winner, loser, player, dir) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.post('/rounds', { winner: winner, loser: loser, player: player, dir: dir })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Get the top global player leaderboard. * @summary Get global player leaderboard. * @param playerAddresses If not already in the top limit players, include the player at the end. * @param limit Limits the number of players returned in the response. Must be between 1-200, inclusive. Defaults to 50. */ PopRankClient.prototype.getGlobalPlayerLeaderboard = function (playerAddresses, limit) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get('/leaderboard/player', { params: { playerAddresses: playerAddresses, limit: limit } })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Get the top player leaderboard for a collection. * @summary Get collection player leaderboard. * @param slug collection identifier used in the PopRank collection page URL. * @param playerAddresses If not already in the top limit players, include the player at the end. * @param limit Limits the number of players returned in the response. Must be between 1-200, inclusive. Defaults to 50. */ PopRankClient.prototype.getCollectionPlayerLeaderboard = function (slug, playerAddresses, limit) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get("/leaderboard/player/".concat(slug), { params: { playerAddresses: playerAddresses, limit: limit } })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; /** * Get the Discord username of a Discord user with given `id`. * @summary Get Discord username. * @param id The Discord ID. */ PopRankClient.prototype.getDiscordUsername = function (id) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.client.get('/discord/user', { params: { id: id } })]; case 1: response = (_a.sent()).data; return [2 /*return*/, handleResponse(response)]; } }); }); }; return PopRankClient; }()); exports.PopRankClient = PopRankClient; //# sourceMappingURL=client.js.map