UNPKG

extra-life-api

Version:

A node module to integrate with the extra-life API

227 lines (226 loc) 9.98 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.getTeamRoster = exports.getTeamDonations = exports.getTeamInfo = exports.getUserBadges = exports.getUserIncentives = exports.getUserMilestones = exports.getUserDonations = exports.getUserInfo = void 0; const node_fetch_1 = require("node-fetch"); const api_paths_1 = require("./helpers/api-paths"); exports.getUserInfo = (id) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve, reject) => { const url = api_paths_1.apiPaths.profileUrl(id); let userInfoJson = {}; node_fetch_1.default(url) .then((res) => { try { userInfoJson = res.json(); userInfoJson.avatarImageURL = 'https:' + userInfoJson.avatarImageURL; userInfoJson.donateURL = `https://www.extra-life.org/index.cfm?fuseaction=donate.participant&participantID=${id}`; if (userInfoJson.teamID) { exports.getTeamInfo(userInfoJson.teamID, false) .then((data) => { userInfoJson.teamURL = data.teamURL; resolve(userInfoJson); }).catch((reason) => { reject(reason); }); } else { resolve(userInfoJson); } } catch (e) { reject(e); } }) .catch(() => { console.log('Error parsing userInfo URL'); reject('There was an error trying to make your request'); }); }); }); exports.getUserDonations = (id, limit = 100, page = 1) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve, reject) => { const url = api_paths_1.apiPaths.userDonationUrl(id, limit, page); const userDonationsJson = {}; node_fetch_1.default(url) .then((res) => __awaiter(void 0, void 0, void 0, function* () { try { userDonationsJson.countDonations = parseInt(res.headers.get('num-records'), 10) || 0; userDonationsJson.countPages = Math.ceil(userDonationsJson.countDonations / limit); userDonationsJson.donations = yield res.json(); resolve(userDonationsJson); } catch (e) { reject(e); } })) .catch(() => { console.log('Error parsing userDonations URL'); reject('There was an error trying to make your request'); }); }); }); exports.getUserMilestones = (id, limit = 100, page = 1) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve, reject) => { const url = api_paths_1.apiPaths.userMilestonesUrl(id, limit, page); const userMilestonesJson = {}; node_fetch_1.default(url) .then((res) => __awaiter(void 0, void 0, void 0, function* () { try { userMilestonesJson.countMilestones = parseInt(res.headers.get('num-records'), 10) || 0; userMilestonesJson.countPages = Math.ceil(userMilestonesJson.countMilestones / limit); userMilestonesJson.milestones = yield res.json(); resolve(userMilestonesJson); } catch (e) { reject(e); } })) .catch(() => { console.log('Error parsing userMilestones URL'); reject('There was an error trying to make your request'); }); }); }); exports.getUserIncentives = (id, limit = 100, page = 1) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve, reject) => { const url = api_paths_1.apiPaths.userIncentivesUrl(id, limit, page); const userIncentivesJson = {}; node_fetch_1.default(url) .then((res) => __awaiter(void 0, void 0, void 0, function* () { try { userIncentivesJson.countIncentives = parseInt(res.headers.get('num-records'), 10) || 0; userIncentivesJson.countPages = Math.ceil(userIncentivesJson.countIncentives / limit); userIncentivesJson.incentives = yield res.json(); resolve(userIncentivesJson); } catch (e) { reject(e); } })) .catch(() => { console.log('Error parsing userIncentives URL'); reject('There was an error trying to make your request'); }); }); }); exports.getUserBadges = (id, limit = 100, page = 1) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve, reject) => { const url = api_paths_1.apiPaths.userBadgesUrl(id, limit, page); const userBadgesJson = {}; node_fetch_1.default(url) .then((res) => __awaiter(void 0, void 0, void 0, function* () { try { userBadgesJson.countBadges = parseInt(res.headers.get('num-records'), 10) || 0; userBadgesJson.countPages = Math.ceil(userBadgesJson.countBadges / limit); userBadgesJson.badges = yield res.json(); resolve(userBadgesJson); } catch (e) { reject(e); } })) .catch(() => { console.log('Error parsing userBadges URL'); reject('There was an error trying to make your request'); }); }); }); exports.getTeamInfo = (id, fetchRoster = true) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve, reject) => { const url = api_paths_1.apiPaths.teamProfileUrl(id); let teamInfoJson = {}; node_fetch_1.default(url) .then((res) => __awaiter(void 0, void 0, void 0, function* () { try { teamInfoJson = yield res.json(); } catch (e) { reject(e); } teamInfoJson.avatarImageURL = 'http:' + teamInfoJson.avatarImageURL; if (fetchRoster) { exports.getTeamRoster(id) .then((rosterData) => { teamInfoJson.members = rosterData.members.map((u) => { u.URL = `https://www.extra-life.org/index.cfm?fuseaction=donorDrive.participant&participantID=${u.participantID}`; return u; }); resolve(teamInfoJson); }).catch((reason) => { reject(reason); }); } else { resolve(teamInfoJson); } })) .catch(() => { console.log('Error obtaining team info'); reject('There was an error trying to make your request'); }); }); }); exports.getTeamDonations = (id, limit = 100, page = 1) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve, reject) => { const teamDonationsJson = {}; const url = api_paths_1.apiPaths.teamDonationsUrl(id, limit, page); node_fetch_1.default(url) .then((res) => __awaiter(void 0, void 0, void 0, function* () { try { teamDonationsJson.countDonations = parseInt(res.headers.get('num-records'), 10) || 0; teamDonationsJson.countPages = Math.ceil(teamDonationsJson.countDonations / limit); teamDonationsJson.donations = yield res.json(); } catch (e) { reject(e); } resolve(teamDonationsJson); })) .catch(() => { console.log('Error parsing teamDonations URL'); reject('There was an error trying to make your request'); }); }); }); exports.getTeamRoster = (id, page) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve, reject) => { const teamRosterJson = {}; const offsetCalc = (page && page !== 1) ? ((page - 1) * 100) : null; const url = api_paths_1.apiPaths.teamRosterUrl(id, offsetCalc); node_fetch_1.default(url) .then((res) => __awaiter(void 0, void 0, void 0, function* () { try { teamRosterJson.countMembers = parseInt(res.headers.get('num-records'), 10) || 0; teamRosterJson.countPages = Math.ceil(teamRosterJson.countMembers / 100); try { teamRosterJson.members = yield res.json(); } catch (e) { teamRosterJson.members = []; } } catch (e) { reject(e); } if (!teamRosterJson.members) { teamRosterJson.members = []; } teamRosterJson.members.forEach((member) => { member.avatarImageURL = 'https:' + member.avatarImageURL; }); resolve(teamRosterJson); })) .catch(() => { console.log('Error parsing teamRoster URL'); reject('There was an error trying to make your request'); }); }); });