Skip to content

Commit

Permalink
NOISSUE - Add search endpoint for users (#65)
Browse files Browse the repository at this point in the history
* fix: add search endpoint for users

Signed-off-by: wambuipixel <[email protected]>

* add: add changset

Signed-off-by: wambuipixel <[email protected]>

---------

Signed-off-by: wambuipixel <[email protected]>
  • Loading branch information
wambui-pixel authored Jul 30, 2024
1 parent 9d4f2ee commit 9bd2134
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-grapes-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@absmach/magistrala-sdk": minor
---

Add search endpoint to users
11 changes: 11 additions & 0 deletions examples/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,14 @@ mySdk.users.DeleteUser(
.catch((error) => {
console.log(error)
})

mySdk.users.SearchUsers(
{ name: '<userName>', id: '<userId>' },
token
)
.then((response: any) => {
console.log('response: ', response)
})
.catch((error) => {
console.log(error)
})
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class Users {
private readonly hostUrl: URL
private readonly contentType: string
private readonly usersEndpoint: string
private readonly searchEndpoint: string
private readonly userError: Errors

public constructor ({ usersUrl, thingsUrl, hostUrl }: { usersUrl: string, thingsUrl?: string, hostUrl?: string }) {
Expand All @@ -43,6 +44,7 @@ export default class Users {
}
this.contentType = 'application/json'
this.usersEndpoint = 'users'
this.searchEndpoint = 'search'
this.userError = new Errors()
}

Expand Down Expand Up @@ -855,4 +857,50 @@ export default class Users {
throw error
}
}

public async SearchUsers (queryParams: PageMetadata, token: string): Promise<UsersPage> {
// Search for users
/**
* @method SearchUsers - Search for users.
* @param {Object} queryParams - Query parameters.
* @param {String} token - Access token.
* @example
* const queryParams = {
* "offset": 0,
* "limit": 10,
* "name": "John Doe"
* "id": "c52d-3b0d-43b9-8c3e-275c087d875af"
* }
* @returns {Object} - Users Page object.
**/

const stringParams: Record<string, string> = Object.fromEntries(
Object.entries(queryParams).map(([key, value]) => [key, String(value)])
)

const options: RequestInit = {
method: 'GET',
headers: {
'Content-Type': this.contentType,
Authorization: `Bearer ${token}`
}
}
try {
const response = await fetch(
new URL(
`${this.usersEndpoint}/${this.searchEndpoint}?${new URLSearchParams(stringParams).toString()}`,
this.usersUrl
).toString(),
options
)
if (!response.ok) {
const errorRes = await response.json()
throw this.userError.HandleError(errorRes.message, response.status)
}
const usersData: UsersPage = await response.json()
return usersData
} catch (error) {
throw error
}
}
}
7 changes: 7 additions & 0 deletions tests/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,11 @@ describe('Users', () => {
const response = await sdk.users.DeleteUser(userId, token)
expect(response).toEqual(deleteResponse)
})

test('search user should search for a user and return success', async () => {
fetchMock.mockResponseOnce(JSON.stringify(UsersPage))

const response = await sdk.users.SearchUsers(queryParams, token)
expect(response).toEqual(UsersPage)
})
})

0 comments on commit 9bd2134

Please sign in to comment.