Skip to content

Commit

Permalink
Merge pull request #832 from Regalijan/master
Browse files Browse the repository at this point in the history
Add searchUsers method
  • Loading branch information
Regalijan authored Oct 25, 2024
2 parents f4c7843 + b03ad55 commit bc7ba73
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ noblox.getIdFromUsername = require('./users/getIdFromUsername.js')
noblox.getPlayerInfo = require('./users/getPlayerInfo.js')
noblox.getUsernameFromId = require('./users/getUsernameFromId.js')
noblox.onBlurbChange = require('./users/onBlurbChange.js')
noblox.searchUsers = require('./users/searchUsers.js')
noblox.clearSession = require('./util/clearSession.js')
noblox.generalRequest = require('./util/generalRequest.js')
noblox.getAction = require('./util/getAction.js')
Expand Down
41 changes: 41 additions & 0 deletions lib/users/searchUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Includes
const getPageResults = require('../util/getPageResults.js').func

// Args
exports.required = ['keyword']
exports.optional = ['limit', 'cursor', 'jar']

// Docs
/**
* ✅ Gets a list of users matching the keyword
* @category User
* @alias searchUsers
* @param {string} keyword - The search term to use
* @param {10 | 25 | 50 | 100} limit - The maximum number of matching users to return
* @param {string} cursor - The cursor to use when fetching the next or previous page
* @returns {Promise<UserSearchResult[]>}
* @example const noblox = require("noblox.js")
* noblox.searchUsers("bob", 10, "cursorstring")
**/

// Define
function searchUsers (jar, keyword, limit, cursor) {
return getPageResults({
url: '//users.roblox.com/v1/users/search',
jar,
limit,
pageCursor: cursor,
query: {
keyword
}
})
}

exports.func = function (args) {
return searchUsers(
args.jar,
args.keyword,
args.limit ?? 10,
args.cursor
)
}
18 changes: 17 additions & 1 deletion test/users.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getBlurb, getIdFromUsername, getPlayerInfo, getUsernameFromId, setCookie } = require('../lib')
const { getBlurb, getIdFromUsername, getPlayerInfo, getUsernameFromId, searchUsers, setCookie } = require('../lib')

beforeAll(() => {
return new Promise(resolve => {
Expand Down Expand Up @@ -65,4 +65,20 @@ describe('Users Methods', () => {
return expect(res).toEqual(expect.any(String))
})
})

it('searchUsers() returns results for the specified keyword', () => {
return searchUsers('roblox').then((res) => {
return expect(res).toMatchObject(
expect.arrayContaining([
expect.objectContaining({
previousUsernames: expect.any(Array),
hasVerifiedBadge: expect.any(Boolean),
id: expect.any(Number),
name: expect.any(String),
displayName: expect.any(String)
})
])
)
})
})
})
13 changes: 13 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,14 @@ declare module "noblox.js" {
guilded?: string;
}

interface UserSearchResult {
previousUsernames: string[];
hasVerifiedBadge: boolean;
id: number;
name: string;
displayName: string;
}

/// Badges

interface BadgeAwarder {
Expand Down Expand Up @@ -2169,6 +2177,11 @@ declare module "noblox.js" {
*/
function getUsernameFromId(id: number): Promise<string>;

/**
* ✅ Gets user search results for a keyword.
*/
function searchUsers(keyword: string, limit: number, cursor: string): Promise<UserSearchResult[]>;

/// Utility

/**
Expand Down
8 changes: 8 additions & 0 deletions typings/jsDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,14 @@ type PromotionChannelsResponse = {
guilded?: string;
}

type UserSearchResult = {
previousUsernames: string[];
hasVerifiedBadge: boolean;
id: number;
name: string;
displayName: string;
}

/// Badges

/**
Expand Down

0 comments on commit bc7ba73

Please sign in to comment.