Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add searchUsers method #832

Merged
merged 9 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Regalijan marked this conversation as resolved.
Show resolved Hide resolved
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
Loading