async-xbox-live-api
Version:
Async library to enable you to interact with the xbox live api
128 lines (127 loc) • 5.46 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.getActivityForGamer = exports.getAchievementsForGamer = exports.getScreenshotsForGamer = exports.getDetailsForClip = exports.getClipsForGamer = exports.getXuid = void 0;
const cache_1 = require("./cache");
const request_1 = require("./request");
const xblHosts_1 = require("./xblHosts");
const mockUserData = {
id: '-1',
hostId: '-1',
settings: [],
isSponsoredUser: false
};
/*
* Exchanges a gamertag for Xbox Live User Id (xuid)
* @param string gamertag
*/
function getXuid(gamertag) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const cacheXuid = yield cache_1.xlaCache.get(`xuidForGamertag-${gamertag}`);
if (cacheXuid.isCached) {
return cacheXuid.value;
}
else {
const host = xblHosts_1.XboxLiveSubdomain.PROFILE;
const uri = `/users/gt(${encodeURIComponent(gamertag)})/profile/settings`;
const data = yield (0, request_1.makeXliveApiRequest)(host, uri);
if (!((_a = data.profileUsers) === null || _a === void 0 ? void 0 : _a.length)) {
data.profileUsers = [mockUserData];
throw new Error(`Could not find ${gamertag} in Xbox Live API`);
}
yield cache_1.xlaCache.set(`xuidForGamertag-${gamertag}`, data.profileUsers[0].id);
return data.profileUsers[0].id;
}
});
}
exports.getXuid = getXuid;
/*
* Returns up to 200 GameDVR clips from Xbox Live
* @param string gamertag
*/
function getClipsForGamer(gamertag, continueToken) {
return __awaiter(this, void 0, void 0, function* () {
const xuid = yield getXuid(gamertag);
const host = xblHosts_1.XboxLiveSubdomain.CLIPS;
const uri = `/users/xuid(${xuid})/clips?maxItems=200&continuationToken=${continueToken !== null && continueToken !== void 0 ? continueToken : ''}`;
const data = yield (0, request_1.makeXliveApiRequest)(host, uri);
return data;
});
}
exports.getClipsForGamer = getClipsForGamer;
/*
* Returns clip info for a specific clip
* @param string gamertag
* @param string clipId ID of the clip to fetch details for
*/
function getDetailsForClip(gamertag, clipId) {
return __awaiter(this, void 0, void 0, function* () {
let clipData = yield getClipsForGamer(gamertag);
let continueToken = clipData.pagingInfo.continuationToken;
let counter = 0;
let foundClip;
while (continueToken && !foundClip && counter < 10) {
const { gameClips } = clipData;
foundClip = gameClips.find((clipData) => clipData.gameClipId === clipId);
if (foundClip) {
break;
}
clipData = yield getClipsForGamer(gamertag, continueToken);
continueToken = clipData.pagingInfo.continuationToken;
counter += 1;
}
return foundClip;
});
}
exports.getDetailsForClip = getDetailsForClip;
/*
* Returns up to 200 screenshots from Xbox Live.
* @param string gamertag
*/
function getScreenshotsForGamer(gamertag, continueToken) {
return __awaiter(this, void 0, void 0, function* () {
const xuid = yield getXuid(gamertag);
const host = xblHosts_1.XboxLiveSubdomain.SCREEN_SHOT;
const uri = `/users/xuid(${xuid})/screenshots?maxItems=200&continuationToken=${continueToken !== null && continueToken !== void 0 ? continueToken : ''}`;
const data = yield (0, request_1.makeXliveApiRequest)(host, uri);
return data;
});
}
exports.getScreenshotsForGamer = getScreenshotsForGamer;
/*
* Returns achivements from Xbox Live.
* @param string gamertag
*/
function getAchievementsForGamer(gamertag, continueToken) {
return __awaiter(this, void 0, void 0, function* () {
const xuid = yield getXuid(gamertag);
const host = xblHosts_1.XboxLiveSubdomain.ACHIVEMENTS;
const uri = `/users/xuid(${xuid})/history/titles?&continuationToken=${continueToken !== null && continueToken !== void 0 ? continueToken : ''}`;
const data = yield (0, request_1.makeXliveApiRequest)(host, uri);
return data;
});
}
exports.getAchievementsForGamer = getAchievementsForGamer;
/*
* Returns player activity from Xbox Live.
* @param string gamertag
*/
function getActivityForGamer(gamertag, continueToken) {
return __awaiter(this, void 0, void 0, function* () {
const xuid = yield getXuid(gamertag);
const host = xblHosts_1.XboxLiveSubdomain.AVTY;
const uri = `/users/xuid(${xuid})/activity/History`;
const data = yield (0, request_1.makeXliveApiRequest)(host, uri);
return data;
});
}
exports.getActivityForGamer = getActivityForGamer;