Comparing version 1.2.0 to 1.2.2
@@ -10,3 +10,3 @@ declare module 'unb-api' { | ||
public version: string; | ||
public maxRetries: Number; | ||
public maxRetries: number; | ||
@@ -19,3 +19,3 @@ public getUserBalance(guild_id: string, user_id: string): Promise<User>; | ||
public getGuildLeaderboard(guild_id: string, query?: Object): Promise<User[]> | Promise<{ users: User[], totalPages: Number }>; | ||
public getGuildLeaderboard(guild_id: string, query?: object): Promise<User[]> | Promise<{ users: User[], totalPages: number }>; | ||
@@ -49,3 +49,3 @@ public getGuild(guild_id: string): Promise<Guild>; | ||
version?: number; | ||
maxRetries?: Number; | ||
maxRetries?: number; | ||
} | ||
@@ -52,0 +52,0 @@ |
{ | ||
"name": "unb-api", | ||
"version": "1.2.0", | ||
"version": "1.2.2", | ||
"description": "API wrapper for UnbelievaBoat Discord Bot API", | ||
@@ -25,4 +25,4 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"request": "^2.87.0" | ||
"axios": "^0.20.0" | ||
} | ||
} |
@@ -7,10 +7,17 @@ const { Guild, Permission, User } = require('./structures'); | ||
* | ||
* @param {string} token - API Token. Get yours from https://unbelievaboat.com/api/docs | ||
* @param {?Object} [options] - Options | ||
* @param {?string} [options.baseURL] - API hostname. Defaults to https://unbelievaboat.com/api | ||
* @param {?number} [options.version] - API version. Defaults to the latest version | ||
* @param {?number} [options.maxRetries] - Maximum number of times to retry a request if it's ratelimited. Defaults to 3 | ||
* @constructs Client | ||
* @param {string} token - Your api token. You may get one from https://unbelievaboat.com/api/docs. | ||
* @param {object} [options={}] - The options for the api client. | ||
* @param {string} [options.baseURL="https://unbelievaboat.com/api"] - The api hostname to use. Defaults to https://unbelievaboat.com/api. | ||
* @param {number} [options.version=""] - The api version to use. Defaults to the latest version. | ||
* @param {number} [options.maxRetries=3] - The maximum number of times to retry a request if it's ratelimited. Defaults to 3. | ||
* @throws {TypeError} | ||
*/ | ||
constructor(token, options = {}) { | ||
if (!token) throw new Error('The API token must be specified'); | ||
if (typeof token !== 'string') throw new TypeError('The API token must be a string'); | ||
if (typeof options !== 'object') throw new TypeError('options must be an object'); | ||
if (options.baseURL !== undefined && typeof options.baseURL !== 'string') throw new TypeError('baseURL must be a string'); | ||
if (options.version !== undefined && typeof options.version !== 'number') throw new TypeError('version must be a number'); | ||
if (options.maxRetries !== undefined && typeof options.maxRetries !== 'number') throw new TypeError('maxRetries must be a number'); | ||
this.token = token; | ||
@@ -24,10 +31,16 @@ this.baseURL = options.baseURL ? options.baseURL : 'https://unbelievaboat.com/api'; | ||
/** | ||
* Get a user's balance | ||
* @param guildId - Guild ID | ||
* @param userId - User ID | ||
* @returns {Promise<User>} User object | ||
* Get a users balance. | ||
* | ||
* @public | ||
* @param {string} guildId - The guild id to get from. | ||
* @param {string} userId - The user id to get. | ||
* @throws {TypeError} | ||
* @returns {Promise<User>} User object. | ||
* | ||
* @see {@link https://unbelievaboat.com/api/docs#get-user-balance|Get User Balance} | ||
*/ | ||
getUserBalance(guildId, userId) { | ||
if (!guildId) throw new Error('guildId must be specified'); | ||
if (!userId) throw new Error('userId must be specified'); | ||
if (typeof guildId !== 'string') throw new TypeError('guildId must be a string'); | ||
if (typeof userId !== 'string') throw new TypeError('userId must be a string'); | ||
return this._request('GET', `guilds/${guildId}/users/${userId}`) | ||
@@ -38,17 +51,26 @@ .then(data => new User(data)); | ||
/** | ||
* Set a user's balance to the given params | ||
* @param {string} guildId - Guild ID | ||
* @param {string} userId - User ID | ||
* @param {number|string} [cash] Value to set the cash balance to | ||
* @param {number|string} [bank] Value to set the bank balance to | ||
* @param {string} [reason] Reason for the audit log | ||
* @returns {Promise<User>} User object | ||
* Set a users balance to the provided values. | ||
* | ||
* @public | ||
* @param {string} guildId - The guild id to use. | ||
* @param {string} userId - The user id to set the balance of. | ||
* @param {object} [data={}] - The balance options to set the users balance to. | ||
* @param {number | string} [data.cash] - The value to set the users cash balance to. | ||
* @param {number | string} [data.bank] - The value to set the users bank balance to. | ||
* @param {string} [reason] - The reason for changing the users balance to show in the audit log. | ||
* @throws {Error | TypeError} | ||
* @returns {Promise<User>} The updated user object. | ||
* | ||
* @see {@link https://unbelievaboat.com/api/docs#put-user-balance|Put User Balance} | ||
*/ | ||
setUserBalance(guildId, userId, { cash, bank } = {}, reason) { | ||
if (!guildId) throw new Error('guildId must be specified'); | ||
if (!userId) throw new Error('userId must be specified'); | ||
if (!cash && !bank) throw new Error('cash or bank must be specified'); | ||
if (cash === Infinity) cash = 'Infinity'; | ||
if (bank === Infinity) bank = 'Infinity'; | ||
return this._request('PUT', `guilds/${guildId}/users/${userId}`, { cash, bank, reason }) | ||
setUserBalance(guildId, userId, data = {}, reason) { | ||
if (typeof guildId !== 'string') throw new TypeError('guildId must be a string'); | ||
if (typeof userId !== 'string') throw new TypeError('userId must be a string'); | ||
if (typeof data !== 'object' || (data.cash === undefined && data.bank === undefined)) throw new Error('either cash or bank must be specified'); | ||
if (data.cash !== undefined && ((typeof data.cash !== 'number' && typeof data.cash !== 'string') || isNaN(data.cash))) throw new TypeError('cash must be a string or number'); | ||
if (data.bank !== undefined && ((typeof data.bank !== 'number' && typeof data.bank !== 'string') || isNaN(data.bank))) throw new TypeError('bank must be a string or number'); | ||
if (data.cash === Infinity) data.cash = 'Infinity'; | ||
if (data.bank === Infinity) data.bank = 'Infinity'; | ||
return this._request('PUT', `guilds/${guildId}/users/${userId}`, { cash: data.cash, bank: data.bank, reason }) | ||
.then(data => new User(data)); | ||
@@ -58,18 +80,27 @@ } | ||
/** | ||
* Increase or decrease a user's balance by the cash and bank values given. | ||
* Increase or decrease a users balance by the cash and bank values given. | ||
* Use negative values to decrease. | ||
* @param {string} guildId - Guild ID | ||
* @param {string} userId - User ID | ||
* @param {number|string} [cash] Value to increase/decrease the cash balance by | ||
* @param {number|string} [bank] Value to increase/decrease the bank balance by | ||
* @param {string} [reason] Reason for the audit log | ||
* @returns {Promise<User>} | ||
* | ||
* @public | ||
* @param {string} guildId - The guild id to use. | ||
* @param {string} userId - The user id to modify the balance of. | ||
* @param {object} [data={}] - The balance options to increase/decrease the users balance by. | ||
* @param {number | string} [data.cash] - The amount to increase/decrease the users cash balance by. | ||
* @param {number | string} [data.bank] - The amount to increase/decrease the users bank balance by. | ||
* @param {string} [reason] - The reason for changing the users balance to show in the audit log. | ||
* @throws {Error | TypeError} | ||
* @returns {Promise<User>} The updated user object. | ||
* | ||
* @see {@link https://unbelievaboat.com/api/docs#patch-user-balance|Patch User Balance} | ||
*/ | ||
editUserBalance(guildId, userId, { cash, bank } = {}, reason) { | ||
if (!guildId) throw new Error('guildId must be specified'); | ||
if (!userId) throw new Error('userId must be specified'); | ||
if (!cash && !bank) throw new Error('cash or bank must be specified'); | ||
if (cash === Infinity) cash = 'Infinity'; | ||
if (bank === Infinity) bank = 'Infinity'; | ||
return this._request('PATCH', `guilds/${guildId}/users/${userId}`, { cash, bank, reason }) | ||
editUserBalance(guildId, userId, data = {}, reason) { | ||
if (typeof guildId !== 'string') throw new TypeError('guildId must be a string'); | ||
if (typeof userId !== 'string') throw new TypeError('userId must be a string'); | ||
if (typeof data !== 'object' || (data.cash === undefined && data.bank === undefined)) throw new Error('either cash or bank must be specified'); | ||
if (data.cash !== undefined && ((typeof data.cash !== 'number' && typeof data.cash !== 'string') || isNaN(data.cash))) throw new TypeError('cash must be a string or number'); | ||
if (data.bank !== undefined && ((typeof data.bank !== 'number' && typeof data.bank !== 'string') || isNaN(data.bank))) throw new TypeError('bank must be a string or number'); | ||
if (data.cash === Infinity) data.cash = 'Infinity'; | ||
if (data.bank === Infinity) data.bank = 'Infinity'; | ||
return this._request('PATCH', `guilds/${guildId}/users/${userId}`, { cash: data.cash, bank: data.bank, reason }) | ||
.then(data => new User(data)); | ||
@@ -79,9 +110,24 @@ } | ||
/** | ||
* Get a guild leaderboard | ||
* @param {string} guildId - Guild ID | ||
* @param {Object} [query] - Query string parameters (sort, limit, offset, page) | ||
* @returns {Promise<Array<User>|{ users: Array<User>, totalPages: number }>} | ||
* Get the leaderboard for a guild. | ||
* | ||
* @public | ||
* @param {string} guildId - The guild id to get the leaderboard for. | ||
* @param {object} [query={}] - The query string parameters (sort, limit, offset, page). | ||
* @param {'total' | 'cash' | 'bank'} [query.sort] - The value to sort the leaderboard by. | ||
* @param {number} [query.limit] - The limit of items to return. | ||
* @param {number} [query.offset] - The index at which to start retrieving items from the leaderboard. | ||
* @param {number} [query.page] - The page to get items from. If provided the return value will be `{ users: Array, totalPages: Number }`. | ||
* @throws {TypeError} | ||
* @returns {Promise<User[] | { users: User[], totalPages: number }>} The leaderboard data. | ||
* | ||
* @see {@link https://unbelievaboat.com/api/docs#get-guild-leaderboard|Get Guild Leaderboard} | ||
*/ | ||
getGuildLeaderboard(guildId, query) { | ||
if (!guildId) throw new Error('guildId must be specified'); | ||
getGuildLeaderboard(guildId, query = {}) { | ||
if (typeof guildId !== 'string') throw new TypeError('guildId must be a string'); | ||
if (typeof query !== 'object') throw new TypeError('query must be an object'); | ||
if (query.sort !== undefined && !['total', 'cash', 'bank'].includes(query.sort)) throw new TypeError('sort must be one of total, cash or bank'); | ||
if (query.limit !== undefined && typeof query.limit !== 'number') throw new TypeError('limit must be a number'); | ||
if (query.offset !== undefined && typeof query.offset !== 'number') throw new TypeError('offset must be a number'); | ||
if (query.page !== undefined && typeof query.page !== 'number') throw new TypeError('page must be a number'); | ||
return this._request('GET', `guilds/${guildId}/users`, {}, query) | ||
@@ -101,8 +147,14 @@ .then(data => { | ||
/** | ||
* Retrieve basic guild information for the given ID | ||
* @param {string} guildId - Guild ID | ||
* @returns {Promise<{Guild}>} | ||
* Retrieve basic information for a guild. | ||
* | ||
* @public | ||
* @param {string} guildId - The guild id to get basic information of. | ||
* @throws {TypeError} | ||
* @returns {Promise<Guild>} | ||
* | ||
* @see {@link https://unbelievaboat.com/api/docs#get-guild|Get Guild} | ||
*/ | ||
getGuild(guildId) { | ||
if (!guildId) throw new Error('guildId must be specified'); | ||
if (typeof guildId !== 'string') throw new TypeError('guildId must be a string'); | ||
return this._request('GET', `guilds/${guildId}`) | ||
@@ -113,9 +165,15 @@ .then(data => new Guild(data)); | ||
/** | ||
* Retrieve the permissions for the application | ||
* This can only be used with an application API token | ||
* @param {string} guildId - Guild ID | ||
* @returns {Promise<{Permission}>} | ||
* Retrieve the permissions for the application. | ||
* This can only be used with an application API token. | ||
* | ||
* @public | ||
* @param {string} guildId - The guild id to get the permissions for the application from. | ||
* @throws {TypeError} | ||
* @returns {Promise<Permission>} The permissions object for the application. | ||
* | ||
* @see {@link https://unbelievaboat.com/api/docs#get-application-permissions|Get Application Permissions} | ||
*/ | ||
getApplicationPermission(guildId) { | ||
if (!guildId) throw new Error('guildId must be specified'); | ||
if (typeof guildId !== 'string') throw new TypeError('guildId must be a string'); | ||
return this._request('GET', `applications/@me/guilds/${guildId}`) | ||
@@ -126,11 +184,12 @@ .then(data => new Permission(data.permissions)); | ||
/** | ||
* Send a request | ||
* @param method - HTTP method (GET, PUT, PATCH etc.) | ||
* @param endpoint - API endpoint | ||
* @param [data] - JSON data | ||
* @param [query] - Query string | ||
* @returns {Promise<Object>} | ||
* Send a request. | ||
* | ||
* @private | ||
* @param {string} method - The http method to use (GET, PUT, PATCH etc.). | ||
* @param {string} endpoint - The api endpoint to request. | ||
* @param {object} [data={}] - The data to provide provide to the server. | ||
* @param {object} [query={}] - The query string options to use in the url. | ||
* @returns {Promise<any>} The raw request data. | ||
*/ | ||
_request(method, endpoint, data, query) { | ||
_request(method, endpoint, data = {}, query = {}) { | ||
return this.requestHandler.request(method, endpoint, data, query); | ||
@@ -137,0 +196,0 @@ } |
class APIError extends Error { | ||
constructor(message, status, errors) { | ||
constructor(response) { | ||
super(); | ||
this.name = this.constructor.name; | ||
this.message = message || 'Unknown error'; | ||
this.status = status; | ||
this.errors = errors || null; | ||
this.message = response.data.error || response.data.message || 'Unknown error'; | ||
this.status = response.status; | ||
this.errors = response.data.errors || null; | ||
Object.defineProperty(this, 'response', { | ||
enumerable: true, | ||
value: response | ||
}); | ||
} | ||
@@ -9,0 +14,0 @@ } |
class HTTPError extends Error { | ||
constructor(message, status) { | ||
constructor(response) { | ||
super(); | ||
this.name = this.constructor.name; | ||
this.message = message || 'Unknown error'; | ||
this.status = status; | ||
this.message = response.statusText || 'Unknown error'; | ||
this.status = response.status; | ||
Object.defineProperty(this, 'response', { | ||
enumerable: true, | ||
value: response | ||
}); | ||
} | ||
@@ -8,0 +13,0 @@ } |
@@ -1,2 +0,2 @@ | ||
const request = require('request'); | ||
const axios = require('axios'); | ||
const Bucket = require('./structures/Bucket'); | ||
@@ -9,3 +9,3 @@ const { APIError, HTTPError } = require('./errors'); | ||
this._client = client; | ||
this.ratelimits = new Map(); | ||
this.ratelimits = {}; | ||
} | ||
@@ -22,16 +22,16 @@ | ||
const options = { | ||
validateStatus: null, | ||
headers: { | ||
Authorization: this._client.token, | ||
'Authorization': this._client.token, | ||
'Content-Type': 'application/json' | ||
}, | ||
uri: `${this._client.baseURL}/${this._client.version ? `${this._client.version}/` : ''}${endpoint}`, | ||
baseURL: this._client.baseURL, | ||
url: `/${this._client.version ? `${this._client.version}/` : ''}${endpoint}`, | ||
method: method, | ||
json: data, | ||
qs: query | ||
data: data, | ||
params: query | ||
}; | ||
request(options, (err, res, body) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
axios.request(options) | ||
.then(res => { | ||
// Increase the number of attempts | ||
@@ -45,6 +45,6 @@ ++_attempts; | ||
const rejectWithError = () => { | ||
if (body && body.error) { | ||
reject(new APIError(body.error || body.message, res.statusCode, body.errors)); | ||
if (res.data && res.data.error) { | ||
reject(new APIError(res)); | ||
} else { | ||
reject(new HTTPError(res.statusMessage, res.statusCode)); | ||
reject(new HTTPError(res)); | ||
} | ||
@@ -69,5 +69,5 @@ }; | ||
if (res.statusCode.toString().startsWith('2')) { | ||
resolve(body); | ||
} else if (res.statusCode === 429) { | ||
if (res.status >= 200 && res.status < 300) { | ||
resolve(res.data); | ||
} else if (res.status === 429) { | ||
// Check if too many retry attempts | ||
@@ -82,6 +82,5 @@ if (_attempts >= this._client.maxRetries) { | ||
} | ||
} | ||
callback(); | ||
}); | ||
callback(); | ||
}); | ||
}; | ||
@@ -88,0 +87,0 @@ |
26943
536
+ Addedaxios@^0.20.0
+ Addedaxios@0.20.0(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
- Removedrequest@^2.87.0
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)