Source: components/UserComponent.js

  1. 'use strict';
  2. const Component = require('./Component')
  3. , ApiConstants = require('../Constants').API
  4. , User = require('../struct/User')
  5. , UserScore = require('../struct/UserScore');
  6. /**
  7. * User-related API component
  8. * @memberof module:Components
  9. * @example
  10. * api.user
  11. * .get('4222959')
  12. * .then(console.log);
  13. */
  14. class UserComponent extends Component {
  15. /**
  16. * Gets general user information.
  17. * @param {String} user The username or id to lookup.
  18. * @param {Mode} [mode] The gamemode.
  19. * @param {Number} [eventDays] The max number of event days.
  20. * @param {LookupType} [lookupType] The lookup type, id/string to lookup the user.
  21. * @return {Promise<Object|User>} The object from the API, or User object if parsing is enabled.
  22. */
  23. get(user, mode, eventDays, lookupType) {
  24. let options = { u: user };
  25. if (mode !== undefined)
  26. options.m = mode;
  27. if (eventDays !== undefined)
  28. options.event_days = eventDays;
  29. if (lookupType !== undefined)
  30. options.type = lookupType
  31. return this.api.requester.get(ApiConstants.USER_GET, options, true, true, User)
  32. .then(d => d[0]); // there can only be one user
  33. }
  34. /**
  35. * Gets the top scores for the user specified.
  36. * @param {String} user The username or id to lookup.
  37. * @param {Mode} [mode] The gamemode.
  38. * @param {Number} [limit] Amount of results to limit to.
  39. * @param {LookupType} [lookupType] The lookup type, id/string to lookup the user.
  40. * @return {Promise<Object[]|UserScore[]>} The object array from the API, or UserScore object array if parsing is enabled.
  41. */
  42. getBest(user, mode, limit, lookupType) {
  43. let options = { u: user };
  44. if (mode !== undefined)
  45. options.m = mode;
  46. if (limit !== undefined)
  47. options.limit = limit;
  48. if (lookupType !== undefined)
  49. options.type = lookupType;
  50. return this.api.requester.get(ApiConstants.USER_GET_BEST, options, true, true, UserScore);
  51. }
  52. /**
  53. * Gets the recent plays for the user specified.
  54. * @param {String} user The username or id to lookup.
  55. * @param {Mode} [mode] The gamemode.
  56. * @param {Number} [limit] Amount of results to limit to.
  57. * @param {LookupType} [lookupType] The lookup type, id/string to lookup the user.
  58. * @return {Promise<Object[]|UserScore[]>} The object array from the API, or UserScore object array if parsing is enabled.
  59. */
  60. getRecent(user, mode, limit, lookupType) {
  61. let options = { u: user };
  62. if (mode !== undefined)
  63. options.m = mode;
  64. if (limit !== undefined)
  65. options.limit = limit;
  66. if (lookupType !== undefined)
  67. options.type = lookupType;
  68. return this.api.requester.get(ApiConstants.USER_GET_RECENT, options, true, true, UserScore);
  69. }
  70. }
  71. module.exports = UserComponent;