poke-sdk-typescript
Version:
103 lines (102 loc) • 3.27 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.Poke = void 0;
const axios_1 = require("axios");
const path_1 = require("path");
const errors_1 = require("./errors");
class Poke {
constructor(config) {
this.config = config;
this.config = config;
}
request(path) {
return __awaiter(this, void 0, void 0, function* () {
let result = undefined;
const url = new URL(this.config.BaseUrl);
url.pathname = (0, path_1.join)(url.pathname, path);
try {
const { data } = yield axios_1.default.get(url.href, {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
'User-Agent': this.config.UserAgent
}
});
result = data;
}
catch (e) {
throw new errors_1.PokeApiError(e.message);
}
return result;
});
}
/**
*
* Get a Pokemon by ID or Name
*
* @param id numeric id or name of the Pokemon
*
* @example
* ```ts
* getStat('1')
* getStat('bulbasaur')
* ```
*
* @returns {Promise<Pokemon>} The Pokemon
* @see {@link https://pokeapi.co/docs/v2#pokemon}
*/
getPokemon(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.request(`/pokemon/${id}`);
});
}
/**
* Get a Pokemon nature by ID or Name. Natures influence how a Pokemon's stats grow.
*
* @param id numeric id or name of the nature
*
* @example
* ```ts
* getNature('1')
* getNature('hardy')
* ```
*
* returns {Promise<Pokemon>} The Pokemon Nature
* @see {@link https://pokeapi.co/docs/v2#natures}
*/
getNature(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.request(`/nature/${id}`);
});
}
/**
*
* Get a Pokemon Stat by ID or Name. Stats determine certain aspects of battles.
*
* @param id numeric id or name of the statistic
*
* @example
* ```ts
* getStat('1')
* getStat('hp')
* ```
*
* @returns {Promise<Stat>} The Stat
* @see {@link https://pokeapi.co/docs/v2#stats}
*/
getStat(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.request(`/stat/${id}`);
});
}
}
exports.Poke = Poke;