UNPKG

kartrider.js

Version:
75 lines (74 loc) 3.42 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Client = void 0; const got_1 = __importDefault(require("got")); const MatchList_1 = require("./MatchList"); const NexonError_1 = require("./NexonError"); const MatchDetail_1 = require("./MatchDetail"); const MetaData_1 = require("./MetaData"); const isOK = (body) => body.status !== 404; class Client { constructor(token, options) { var _a; this._token = token; this.api = got_1.default.extend({ prefixUrl: 'https://api.nexon.co.kr/kart/v1.0', headers: { 'Authorization': this._token }, responseType: 'json', handlers: [NexonError_1.StatusCodeErrorHandler] }); if ((_a = options === null || options === void 0 ? void 0 : options.metaData) === null || _a === void 0 ? void 0 : _a.folderPath) { MetaData_1.MetaData.init(options.metaData.folderPath).then(data => this.metadata = data); } else { console.warn("Can't find options.metaData.folderPath! Client.MetaData is now undefined"); } } getUserBasicInfoByName(name) { return __awaiter(this, void 0, void 0, function* () { const encodeName = encodeURIComponent(name); const json = yield this.api.get(`users/nickname/${encodeName}`).then(res => res.body); return isOK(json) ? json : null; }); } getUserBasicInfoByID(id) { return __awaiter(this, void 0, void 0, function* () { const json = yield this.api.get(`users/${id}`).then(res => res.body); return isOK(json) ? json : null; }); } getMatchList(info, limit = 20, offset = 0) { return __awaiter(this, void 0, void 0, function* () { const searchParams = new URLSearchParams([['limit', limit.toString()], ["offset", offset.toString()]]); const json = yield this.api.get(`users/${info.accessId}/matches`, { searchParams }).then(res => res.body); if (!isOK(json)) return null; const data = json; return new MatchList_1.MatchList(this, info, data, { limit, offset }); }); } getMatch(MatchID) { return __awaiter(this, void 0, void 0, function* () { const json = yield this.api.get(`matches/${MatchID}`).then(res => res.body); if (!isOK(json)) return null; const data = json; return new MatchDetail_1.MatchDetail(this, data); }); } } exports.Client = Client;