@questlabs/core
Version:
This is the quest SDK
246 lines (245 loc) • 12.7 kB
JavaScript
"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.Badge = void 0;
const constants_1 = require("../../utils/constants");
const helperFunctions_1 = require("../../utils/helperFunctions");
class Badge {
constructor(questSdk) {
this.questSdk = questSdk;
}
getAllBadgesForUser(options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
(0, helperFunctions_1.validateParams)({ userId, token });
const url = `/badges`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.GET, headers: options === null || options === void 0 ? void 0 : options._headers, queryParams });
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
getUserCreatedBadges(options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
(0, helperFunctions_1.validateParams)({ userId, token });
const url = `/users/${userId}/created-badges`;
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.GET,
headers: options === null || options === void 0 ? void 0 : options._headers,
queryParams: options === null || options === void 0 ? void 0 : options._queryParams,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
getAllBadgesForEntity({ entityId } = {}, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId });
const url = `/entities/${defaultEntityId}/badges`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.GET, headers: options === null || options === void 0 ? void 0 : options._headers, queryParams });
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
addBadgeForEntity({ name, imageIPFS, description, entityId, skills, userIds, endsAt, xp, airdropEnabled, typeData, type, totalCount, visibility, proofType, }, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, name, imageIPFS, description, userId, token });
const url = `/entities/${defaultEntityId}/badges`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
const body = Object.assign({ name,
imageIPFS,
description,
skills,
userIds,
endsAt,
xp,
airdropEnabled,
typeData,
type,
totalCount,
visibility,
proofType }, options === null || options === void 0 ? void 0 : options._body);
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.POST,
body,
headers: options === null || options === void 0 ? void 0 : options._headers,
queryParams,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
getBadgeInfoForEntity({ entityId, badgeId, prefixBadgeIpfs }, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, badgeId, userId, token });
const url = `/entities/${defaultEntityId}/badges/${badgeId}`;
const queryParams = Object.assign(Object.assign({ userId }, (prefixBadgeIpfs && { prefixBadgeIpfs })), options === null || options === void 0 ? void 0 : options._queryParams);
return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.GET, headers: options === null || options === void 0 ? void 0 : options._headers, queryParams });
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
updateBadgesForEntity({ badgeId, entityId, name, imageIPFS, description, skills, userIds, endsAt, xp, typeData, type, }, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, badgeId, userId, token });
const url = `/entities/${defaultEntityId}/badges/${badgeId}/update`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
const body = Object.assign({ name,
imageIPFS,
description,
skills,
userIds,
endsAt,
xp,
typeData,
type }, options === null || options === void 0 ? void 0 : options._body);
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.POST,
headers: options === null || options === void 0 ? void 0 : options._headers,
queryParams,
body,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
claimBadgeForEntity({ badgeId, addToDynamicNFT = false, entityId }, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, badgeId, addToDynamicNFT, userId, token });
const url = `/entities/${defaultEntityId}/badges/${badgeId}/claim`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
const body = Object.assign({ addToDynamicNFT }, options === null || options === void 0 ? void 0 : options._body);
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.POST,
queryParams,
headers: options === null || options === void 0 ? void 0 : options._headers,
body,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
addUsersToBadge({ badgeId, userIds, entityId }, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, badgeId, userIds, userId, token });
const url = `/entities/${defaultEntityId}/badges/${badgeId}/users`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
const body = Object.assign({ userIds }, options === null || options === void 0 ? void 0 : options._body);
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.POST,
queryParams,
headers: options === null || options === void 0 ? void 0 : options._headers,
body,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
getNumberOfBadgesEarned({ entityId, forUserId } = {}, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, userId, token });
if (!userId && !forUserId) {
throw new Error("Please pass the userId or login");
}
const url = `/entities/${defaultEntityId}/users/${forUserId || userId}/badges-count`;
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.GET,
queryParams: options === null || options === void 0 ? void 0 : options._queryParams,
headers: options === null || options === void 0 ? void 0 : options._headers,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
getUserEarnedBadgesForEntity({ entityId, forUserId, limit, page } = {}, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, userId, token });
if (!userId && !forUserId) {
throw new Error("Please pass the userId or login");
}
const url = `/entities/${defaultEntityId}/users/${forUserId || userId}/badges`;
const queryParams = Object.assign(Object.assign(Object.assign({}, (page && { page })), (limit && { limit })), options === null || options === void 0 ? void 0 : options._queryParams);
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.GET,
queryParams,
headers: options === null || options === void 0 ? void 0 : options._headers,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
}
exports.Badge = Badge;