async-xbox-live-api
Version:
Async library to enable you to interact with the xbox live api
49 lines (48 loc) • 2.22 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeXliveApiRequest = void 0;
const axios_1 = __importDefault(require("axios"));
const flow_1 = require("./auth/flow");
/*
* Performs requests, this is an internal method.
*/
const makeXliveApiRequest = (host, uri) => __awaiter(void 0, void 0, void 0, function* () {
const useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36';
const { cookies, authorizationHeader } = yield (0, flow_1.fetchCookiesAndAuthorizationDetails)();
const requestOptions = {
headers: {
Cookie: cookies,
'Content-Type': 'application/json',
'x-xbl-contract-version': '2',
'User-Agent': `${useragent} Like SmartGlass/2.105.0415 CFNetwork/711.3.18 Darwin/14.0.0`,
Authorization: authorizationHeader
}
};
try {
const { data } = yield axios_1.default.get(`https://${host}${uri}`, requestOptions);
return data;
}
catch (error) {
const axiosError = error;
if (axiosError.status === 429) {
throw new Error('Rate limit');
}
if (axiosError.status === 404) {
throw new Error('Player not found');
}
throw axiosError;
}
});
exports.makeXliveApiRequest = makeXliveApiRequest;