Skip to content

Commit

Permalink
Add search method for users, role, and relations
Browse files Browse the repository at this point in the history
  • Loading branch information
md-y committed Sep 16, 2021
1 parent f3e6e06 commit 8cc2523
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/structure/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Util = require('../util.js');
const AuthUtil = require('../auth.js');
const Relationship = require('../internal/relationship.js');

/**
* Represents an user
Expand Down Expand Up @@ -33,6 +34,50 @@ class User {
* @type {String}
*/
this.username = context.data.attributes.username;

/**
* The roles of this user such as "ROLE_MD_AT_HOME" and "ROLE_ADMIN"
* @type {String[]}
*/
this.roles = context.data.attributes.roles;

/**
* Groups this user is a part of
* @type {Relationship<import('../index').Group>[]}
*/
this.groups = Relationship.convertType('scanlation_group', context.data.relationships, this);
}

/**
* @ignore
* @typedef {Object} UserParameterObject
* @property {String} [UserParameterObject.username]
* @property {String[]} [UserParameterObject.ids] Max of 100 per request
* @property {Number} [UserParameterObject.limit] Not limited by API limits (more than 100). Use Infinity for maximum results (use at your own risk)
* @property {Number} [UserParameterObject.offset]
* @property {Object} [UserParameterObject.order]
* @property {'asc'|'desc'} [UserParameterObject.order.username]
*/

/**
* Peforms a search and returns an array of users. Requires authorization
* https://api.mangadex.org/docs.html#operation/get-user
* @param {UserParameterObject|String} [searchParameters] An object of offical search parameters, or a string representing the username
* @returns {Promise<User[]>}
*/
static search(searchParameters = {}) {
if (typeof searchParameters === 'string') searchParameters = { username: searchParameters };
return Util.apiCastedRequest('/user', User, searchParameters);
// Currently (9/14/21) MD does not support includes[]=scanlation_group for any user endpoint
}

/**
* Gets multiple users
* @param {...String|Relationship<User>} ids
* @returns {Promise<User[]>}
*/
static getMultiple(...ids) {
return Util.getMultipleIds(User.search, ids);
}

/**
Expand Down

0 comments on commit 8cc2523

Please sign in to comment.