diff --git a/.changeset/light-grapes-cheer.md b/.changeset/light-grapes-cheer.md new file mode 100644 index 0000000..96ad543 --- /dev/null +++ b/.changeset/light-grapes-cheer.md @@ -0,0 +1,5 @@ +--- +"@absmach/magistrala-sdk": minor +--- + +Add search endpoint to users diff --git a/examples/users.ts b/examples/users.ts index b8aa2c1..dd0d77b 100644 --- a/examples/users.ts +++ b/examples/users.ts @@ -222,3 +222,14 @@ mySdk.users.DeleteUser( .catch((error) => { console.log(error) }) + +mySdk.users.SearchUsers( + { name: '', id: '' }, + token +) + .then((response: any) => { + console.log('response: ', response) + }) + .catch((error) => { + console.log(error) + }) diff --git a/package-lock.json b/package-lock.json index b0d4aa6..372ba10 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@absmach/magistrala-sdk", - "version": "0.4.2", + "version": "0.4.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@absmach/magistrala-sdk", - "version": "0.4.2", + "version": "0.4.3", "license": "Apache-2.0", "dependencies": { "@changesets/cli": "^2.27.6", diff --git a/src/users.ts b/src/users.ts index a53b7ff..295e697 100644 --- a/src/users.ts +++ b/src/users.ts @@ -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 }) { @@ -43,6 +44,7 @@ export default class Users { } this.contentType = 'application/json' this.usersEndpoint = 'users' + this.searchEndpoint = 'search' this.userError = new Errors() } @@ -855,4 +857,50 @@ export default class Users { throw error } } + + public async SearchUsers (queryParams: PageMetadata, token: string): Promise { + // 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 = 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 + } + } } diff --git a/tests/users.test.ts b/tests/users.test.ts index a86f474..de6b99c 100644 --- a/tests/users.test.ts +++ b/tests/users.test.ts @@ -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) + }) })