From 4955d5f0a41b40e9c8b048ffab9cc4b57b02769e Mon Sep 17 00:00:00 2001 From: ianmuchyri Date: Mon, 27 Jan 2025 12:24:28 +0300 Subject: [PATCH] add list entity users Signed-off-by: ianmuchyri --- .changeset/selfish-days-promise.md | 5 + examples/channels.ts | 30 ++- examples/clients.ts | 3 +- examples/groups.ts | 10 + package-lock.json | 4 +- src/channels.ts | 366 ++++++++++++++++++----------- src/clients.ts | 39 ++- src/groups.ts | 63 ++++- src/sdk.ts | 6 +- 9 files changed, 365 insertions(+), 161 deletions(-) create mode 100644 .changeset/selfish-days-promise.md diff --git a/.changeset/selfish-days-promise.md b/.changeset/selfish-days-promise.md new file mode 100644 index 00000000..1d6610a8 --- /dev/null +++ b/.changeset/selfish-days-promise.md @@ -0,0 +1,5 @@ +--- +"@absmach/magistrala-sdk": patch +--- + +add list entity users diff --git a/examples/channels.ts b/examples/channels.ts index 920e0e97..111dc0ea 100644 --- a/examples/channels.ts +++ b/examples/channels.ts @@ -4,6 +4,7 @@ const defaultUrl = "http://localhost"; const mySdk = new SDK({ channelsUrl: `${defaultUrl}:9005`, + usersUrl: `${defaultUrl}:9002`, }); const token = ""; @@ -31,7 +32,7 @@ mySdk.channels .CreateChannels( [{ name: "" }, { name: "" }], domainId, - token, + token ) .then((response: any) => { console.log("response:", response); @@ -53,7 +54,7 @@ mySdk.channels .UpdateChannelNameAndMetadata( { id: "", name: "", metadata: { key: "value" } }, domainId, - token, + token ) .then((response: any) => { console.log("response:", response); @@ -66,7 +67,7 @@ mySdk.channels .UpdateChannelTags( { id: "", tags: ["tag1", "tag2"] }, domainId, - token, + token ) .then((response: any) => { console.log("response:", response); @@ -99,7 +100,7 @@ mySdk.channels "", ["publish"], domainId, - token, + token ) .then((response: any) => { console.log("response: ", response); @@ -114,7 +115,7 @@ mySdk.channels "", ["publish"], domainId, - token, + token ) .then((response: any) => { console.log("response: ", response); @@ -129,7 +130,7 @@ mySdk.channels ["", ""], ["publish"], domainId, - token, + token ) .then((response: any) => { console.log("response:", response); @@ -144,7 +145,7 @@ mySdk.channels ["", ""], ["publish"], domainId, - token, + token ) .then((response: any) => { console.log("response: ", response); @@ -153,7 +154,8 @@ mySdk.channels console.error(error); }); -mySdk.channels.SetChannelParentGroup(domainId, "", "", token) +mySdk.channels + .SetChannelParentGroup(domainId, "", "", token) .then((response: any) => { console.log("response: ", response); }) @@ -161,7 +163,8 @@ mySdk.channels.SetChannelParentGroup(domainId, "", "", console.error(error); }); -mySdk.channels.DeleteChannelParentGroup(domainId, "", token) +mySdk.channels + .DeleteChannelParentGroup(domainId, "", token) .then((response: any) => { console.log("response: ", response); }) @@ -177,3 +180,12 @@ mySdk.channels .catch((error) => { console.error(error); }); + +mySdk.channels + .ListChannelUsers("", { offset: 0, limit: 10 }, domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.log(error); + }); diff --git a/examples/clients.ts b/examples/clients.ts index 776fbd21..1d7f38d5 100644 --- a/examples/clients.ts +++ b/examples/clients.ts @@ -4,6 +4,7 @@ const defaultUrl = "http://localhost"; const mySdk = new SDK({ clientsUrl: `${defaultUrl}:9006`, + usersUrl: `${defaultUrl}:9002`, }); const token = ""; @@ -99,7 +100,7 @@ mySdk.clients }); mySdk.clients - .ListUserClients("", { offset: 0, limit: 10 }, domainId, token) + .ListClientUsers("", { offset: 0, limit: 10 }, domainId, token) .then((response: any) => { console.log("response:", response); }) diff --git a/examples/groups.ts b/examples/groups.ts index 76caada7..b7e11376 100644 --- a/examples/groups.ts +++ b/examples/groups.ts @@ -4,6 +4,7 @@ const defaultUrl = "http://localhost"; const mySdk = new SDK({ groupsUrl: `${defaultUrl}:9004`, + usersUrl: `${defaultUrl}:9002`, }); const token = ""; @@ -153,6 +154,15 @@ mySdk.groups console.error(error); }); +mySdk.groups + .ListGroupUsers("", { offset: 0, limit: 10 }, domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.log(error); + }); + mySdk.groups .ListGroupActions(domainId, token) .then((response: any) => { diff --git a/package-lock.json b/package-lock.json index 81fc12bb..8600e5b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@absmach/magistrala-sdk", - "version": "0.9.4", + "version": "0.10.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@absmach/magistrala-sdk", - "version": "0.9.4", + "version": "0.10.3", "license": "Apache-2.0", "dependencies": { "@changesets/cli": "^2.27.6", diff --git a/src/channels.ts b/src/channels.ts index 23a88bda..88af7510 100644 --- a/src/channels.ts +++ b/src/channels.ts @@ -5,12 +5,13 @@ import type { PageMetadata, Response, ChannelsPage, + UsersPage, } from "./defs"; /** -* @class Channels -* Handles interactions with channels API, including creating, updating and managing channels. -*/ + * @class Channels + * Handles interactions with channels API, including creating, updating and managing channels. + */ export default class Channels { private readonly contentType: string; @@ -18,30 +19,40 @@ export default class Channels { private readonly channelsUrl: URL; + private readonly usersUrl?: URL; + /** * @constructor * Initializes the Channel API client. * @param {object} config - Configuration object. * @param {string} config.channelsUrl - Base URL for the channels API. + * @param {string} [config.usersUrl] - Optional URL for the users API. */ public constructor({ channelsUrl, + usersUrl, }: { - channelsUrl:string; + channelsUrl: string; + usersUrl?: string; }) { this.channelsUrl = new URL(channelsUrl); + if (usersUrl !== undefined) { + this.usersUrl = new URL(usersUrl); + } else { + this.usersUrl = new URL(""); + } this.contentType = "application/json"; this.channelsEndpoint = "channels"; } /** - * @method CreateChannel - Creates a new channel - * @param {Channel} channel - Channel object with a containing details like name, metadata and tags. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} channel - The created channel object. - * @throws {Error} - If the channel cannot be created. - */ + * @method CreateChannel - Creates a new channel + * @param {Channel} channel - Channel object with a containing details like name, metadata and tags. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} channel - The created channel object. + * @throws {Error} - If the channel cannot be created. + */ public async CreateChannel( channel: Channel, domainId: string, @@ -75,13 +86,13 @@ export default class Channels { } /** - * @method Channel - Retrieves a channel by its id. - * @param {string} channelId - The unique ID of the channel. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} channel - The requested channel object. - * @throws {Error} - If the channel cannot be fetched. - */ + * @method Channel - Retrieves a channel by its id. + * @param {string} channelId - The unique ID of the channel. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} channel - The requested channel object. + * @throws {Error} - If the channel cannot be fetched. + */ public async Channel( channelId: string, domainId: string, @@ -114,13 +125,13 @@ export default class Channels { } /** - * @method CreateChannels - Creates multiple new channels. - * @param {Channel[]} channels - An array of channel objects, each containing details like name, metadata, and tags. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} channelsPage - A page of channels. - * @throws {Error} - If the channels cannot be created. - */ + * @method CreateChannels - Creates multiple new channels. + * @param {Channel[]} channels - An array of channel objects, each containing details like name, metadata, and tags. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} channelsPage - A page of channels. + * @throws {Error} - If the channels cannot be created. + */ public async CreateChannels( channels: Channel[], domainId: string, @@ -154,13 +165,13 @@ export default class Channels { } /** - * @method Channels - Retrieves all channels matching the provided query parameters. - * @param {PageMetadata} queryParams - Query parameters for the request. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} channelsPage - A page of channels. - * @throws {Error} - If the channels cannot be fetched. - */ + * @method Channels - Retrieves all channels matching the provided query parameters. + * @param {PageMetadata} queryParams - Query parameters for the request. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} channelsPage - A page of channels. + * @throws {Error} - If the channels cannot be fetched. + */ public async Channels( queryParams: PageMetadata, domainId: string, @@ -198,13 +209,13 @@ export default class Channels { } /** - * @method UpdateChannelNameAndMetadata - Updates an existing channel's metadata and name. - * @param {Channel} channel - Channel object with updated properties. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} channel - The updated channel object. - * @throws {Error} - If the channel cannot be updated. - */ + * @method UpdateChannelNameAndMetadata - Updates an existing channel's metadata and name. + * @param {Channel} channel - Channel object with updated properties. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} channel - The updated channel object. + * @throws {Error} - If the channel cannot be updated. + */ public async UpdateChannelNameAndMetadata( channel: Channel, domainId: string, @@ -238,13 +249,13 @@ export default class Channels { } /** - * @method UpdateChannelTags - Updates an existing channel's tags. - * @param {Channel} channel - Channel object with updated properties. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} channel - The updated channel object. - * @throws {Error} - If the channel tags cannot be updated. - */ + * @method UpdateChannelTags - Updates an existing channel's tags. + * @param {Channel} channel - Channel object with updated properties. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} channel - The updated channel object. + * @throws {Error} - If the channel tags cannot be updated. + */ public async UpdateChannelTags( channel: Channel, domainId: string, @@ -278,13 +289,13 @@ export default class Channels { } /** - * @method DisableChannel - Disables a spcific channel. - * @param {string} channelId - The unique ID of the channel. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} channel - The disabled channel object. - * @throws {Error} - If the channel cannot be disabled. - */ + * @method DisableChannel - Disables a spcific channel. + * @param {string} channelId - The unique ID of the channel. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} channel - The disabled channel object. + * @throws {Error} - If the channel cannot be disabled. + */ public async DisableChannel( channelId: string, domainId: string, @@ -317,13 +328,13 @@ export default class Channels { } /** - * @method EnableChannel - Enables a previously disabled channel. - * @param {string} channelId - The unique ID of the channel. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} channel - The enabled channel object. - * @throws {Error} - If the channel cannot be enabled. - */ + * @method EnableChannel - Enables a previously disabled channel. + * @param {string} channelId - The unique ID of the channel. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} channel - The enabled channel object. + * @throws {Error} - If the channel cannot be enabled. + */ public async EnableChannel( channelId: string, domainId: string, @@ -356,13 +367,13 @@ export default class Channels { } /** - * @method DeleteChannel - Deletes channel with specified id. - * @param {string} channelId - The unique ID of the channel. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} response - A promise that resolves when the channel is deleted. - * @throws {Error} - If the channel cannot be deleted. - */ + * @method DeleteChannel - Deletes channel with specified id. + * @param {string} channelId - The unique ID of the channel. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} response - A promise that resolves when the channel is deleted. + * @throws {Error} - If the channel cannot be deleted. + */ public async DeleteChannel( channelId: string, domainId: string, @@ -377,7 +388,10 @@ export default class Channels { }; try { const response = await fetch( - new URL(`${domainId}/${this.channelsEndpoint}/${channelId}`, this.channelsUrl).toString(), + new URL( + `${domainId}/${this.channelsEndpoint}/${channelId}`, + this.channelsUrl + ).toString(), options ); if (!response.ok) { @@ -395,15 +409,15 @@ export default class Channels { } /** - * @method ConnectClient - Connects multiple clients to a channel. - * @param {string[]} clientIds - An array of unique clients IDs to be connected. - * @param {string} channelId - The unique ID of the channel to which the clients will connect. - * @param {string[]}connectionTypes - Connection types can be publish, subscribe or both publish and subscribe - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} response - A promise that resolves when the clients are connected to the channel. - * @throws {Error} - If the clients cannot be connected to the channel. - */ + * @method ConnectClient - Connects multiple clients to a channel. + * @param {string[]} clientIds - An array of unique clients IDs to be connected. + * @param {string} channelId - The unique ID of the channel to which the clients will connect. + * @param {string[]}connectionTypes - Connection types can be publish, subscribe or both publish and subscribe + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} response - A promise that resolves when the clients are connected to the channel. + * @throws {Error} - If the clients cannot be connected to the channel. + */ public async ConnectClient( clientIds: string[], channelId: string, @@ -417,7 +431,11 @@ export default class Channels { "Content-Type": this.contentType, Authorization: `Bearer ${token}`, }, - body: JSON.stringify({ client_ids: clientIds, channel_id: channelId, types: connectionTypes }), + body: JSON.stringify({ + client_ids: clientIds, + channel_id: channelId, + types: connectionTypes, + }), }; try { const response = await fetch( @@ -442,15 +460,15 @@ export default class Channels { } /** - * @method Connect - Connects multiple clients to multple channels. - * @param {string[]} clientIds - An array of unique clients IDs to be connected. - * @param {string[]} channelIds - An array of unique channels IDs to which the clients will connect. - * @param {string[]} connectionTypes - Connection types can be publish, subscribe or both publish and subscribe - * @param {string} domainId - The unique ID of the channel. - * @param {string} token - Authorization token. - * @returns {Promise} response - A promise that resolves when the clients are connected to the channels. - * @throws {Error} - If the clients cannot be connected to the channel. - */ + * @method Connect - Connects multiple clients to multple channels. + * @param {string[]} clientIds - An array of unique clients IDs to be connected. + * @param {string[]} channelIds - An array of unique channels IDs to which the clients will connect. + * @param {string[]} connectionTypes - Connection types can be publish, subscribe or both publish and subscribe + * @param {string} domainId - The unique ID of the channel. + * @param {string} token - Authorization token. + * @returns {Promise} response - A promise that resolves when the clients are connected to the channels. + * @throws {Error} - If the clients cannot be connected to the channel. + */ public async Connect( clientIds: string[], channelIds: string[], @@ -464,11 +482,18 @@ export default class Channels { "Content-Type": this.contentType, Authorization: `Bearer ${token}`, }, - body: JSON.stringify({ client_ids: clientIds, channel_ids: channelIds, types: connectionTypes }), + body: JSON.stringify({ + client_ids: clientIds, + channel_ids: channelIds, + types: connectionTypes, + }), }; try { const response = await fetch( - new URL(`${domainId}/${this.channelsEndpoint}/connect`, this.channelsUrl).toString(), + new URL( + `${domainId}/${this.channelsEndpoint}/connect`, + this.channelsUrl + ).toString(), options ); if (!response.ok) { @@ -486,15 +511,15 @@ export default class Channels { } /** - * @method Disconnect - Disconnects clients from channels. - * @param {string[]} clientIds - An array of unique clients IDs to be disconnected. - * @param {string[]} channelIds - An array of unique channels IDs to which the clients will disconnect. - * @param {string[]}connectionTypes - Connection types can be publish, subscribe or both publish and subscribe. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} response - A promise that resolves when the clients are disconnected from the channels. - * @throws {Error} - If the clients cannot be disconnected from the channels. - */ + * @method Disconnect - Disconnects clients from channels. + * @param {string[]} clientIds - An array of unique clients IDs to be disconnected. + * @param {string[]} channelIds - An array of unique channels IDs to which the clients will disconnect. + * @param {string[]}connectionTypes - Connection types can be publish, subscribe or both publish and subscribe. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} response - A promise that resolves when the clients are disconnected from the channels. + * @throws {Error} - If the clients cannot be disconnected from the channels. + */ public async Disconnect( clientIds: string[], channelIds: string[], @@ -508,11 +533,18 @@ export default class Channels { "Content-Type": this.contentType, Authorization: `Bearer ${token}`, }, - body: JSON.stringify({ client_ids: clientIds, channel_ids: channelIds, types: connectionTypes }), + body: JSON.stringify({ + client_ids: clientIds, + channel_ids: channelIds, + types: connectionTypes, + }), }; try { const response = await fetch( - new URL(`${domainId}/${this.channelsEndpoint}/disconnect`, this.channelsUrl).toString(), + new URL( + `${domainId}/${this.channelsEndpoint}/disconnect`, + this.channelsUrl + ).toString(), options ); if (!response.ok) { @@ -530,15 +562,15 @@ export default class Channels { } /** - * @method DisconnectClient - Disconnects clients from channel. - * @param {string[]} clientIds - An array of unique clients IDs to be disconnected. - * @param {string} channelId - The unique ID of the channel from which the clients will be disconnected. - * @param {string[]} connectionTypes - connection types can be publish, subscribe or both publish and subscribe. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} response - A promise that resolves when the clients are disconnected from the channel. - * @throws {Error} - If the clients cannot be disconnected from the channel. - */ + * @method DisconnectClient - Disconnects clients from channel. + * @param {string[]} clientIds - An array of unique clients IDs to be disconnected. + * @param {string} channelId - The unique ID of the channel from which the clients will be disconnected. + * @param {string[]} connectionTypes - connection types can be publish, subscribe or both publish and subscribe. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} response - A promise that resolves when the clients are disconnected from the channel. + * @throws {Error} - If the clients cannot be disconnected from the channel. + */ public async DisconnectClient( clientIds: string[], channelId: string, @@ -552,7 +584,11 @@ export default class Channels { "Content-Type": this.contentType, Authorization: `Bearer ${token}`, }, - body: JSON.stringify({ client_ids: clientIds, channel_id: channelId, types: connectionTypes }), + body: JSON.stringify({ + client_ids: clientIds, + channel_id: channelId, + types: connectionTypes, + }), }; try { const response = await fetch( @@ -577,33 +613,44 @@ export default class Channels { } /** - * @method SetChannelParentGroup - Sets parent to a channel. - * @param {string} domainId - The unique ID of the domain. - * @param {string} channelId - The unique ID of the channel to be updated. - * @param {string} parentGroupId - The unique ID of the group to be set as the parent. - * @param {string} token - Authorization token. - * @returns {Promise} response - A promise that resolves when the parent group is successfully set for the specified channel. - * @throws {Error} - If the parent group cannot be set for the channel. - */ - public async SetChannelParentGroup(domainId: string, channelId: string, parentGroupId: string, token: string) : Promise { + * @method SetChannelParentGroup - Sets parent to a channel. + * @param {string} domainId - The unique ID of the domain. + * @param {string} channelId - The unique ID of the channel to be updated. + * @param {string} parentGroupId - The unique ID of the group to be set as the parent. + * @param {string} token - Authorization token. + * @returns {Promise} response - A promise that resolves when the parent group is successfully set for the specified channel. + * @throws {Error} - If the parent group cannot be set for the channel. + */ + public async SetChannelParentGroup( + domainId: string, + channelId: string, + parentGroupId: string, + token: string + ): Promise { const options: RequestInit = { method: "POST", headers: { "Content-Type": this.contentType, - Authorization: `Bearer ${token}` + Authorization: `Bearer ${token}`, }, - body: JSON.stringify({ parent_group_id: parentGroupId }) + body: JSON.stringify({ parent_group_id: parentGroupId }), }; try { const response = await fetch( - new URL(`${domainId}/${this.channelsEndpoint}/${channelId}/parent`, this.channelsUrl).toString(), + new URL( + `${domainId}/${this.channelsEndpoint}/${channelId}/parent`, + this.channelsUrl + ).toString(), options ); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const addChannelParentsResponse: Response = { status: response.status, message: "Channel group parent added successfully" }; + const addChannelParentsResponse: Response = { + status: response.status, + message: "Channel group parent added successfully", + }; return addChannelParentsResponse; } catch (error) { throw error; @@ -611,19 +658,23 @@ export default class Channels { } /** - * @method DeleteChannelParentGroup - Removes the parent group from a specified channel. - * @param {string} domainId - The unique ID of the domain. - * @param {string} channelId - The unique ID of the channel. - * @param {string} token - Authorization token. - * @returns {Promise} response - A promise that resolves when the parent group is successfully removed from the specified channel. - * @throws {Error} - If the parent group cannot removed from the channel. - */ - public async DeleteChannelParentGroup(domainId: string, channelId: string, token: string) : Promise { + * @method DeleteChannelParentGroup - Removes the parent group from a specified channel. + * @param {string} domainId - The unique ID of the domain. + * @param {string} channelId - The unique ID of the channel. + * @param {string} token - Authorization token. + * @returns {Promise} response - A promise that resolves when the parent group is successfully removed from the specified channel. + * @throws {Error} - If the parent group cannot removed from the channel. + */ + public async DeleteChannelParentGroup( + domainId: string, + channelId: string, + token: string + ): Promise { const options: RequestInit = { method: "DELETE", headers: { "Content-Type": this.contentType, - Authorization: `Bearer ${token}` + Authorization: `Bearer ${token}`, }, }; try { @@ -638,10 +689,59 @@ export default class Channels { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const deleteChannelParentsResponse: Response = { status: response.status, message: "Channel group parent deleted successfully" }; + const deleteChannelParentsResponse: Response = { + status: response.status, + message: "Channel group parent deleted successfully", + }; return deleteChannelParentsResponse; } catch (error) { throw error; } } + + /** + * @method ListChannelUsers - Retrieves a list of users associated with a specific channel. + * @param {string} channelId - The unique ID of the channel. + * @param {PageMetadata} queryParams - Query parameters for the request. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} usersPage - A page of users. + * @throws {Error} - If the users of the channel cannot be fetched. + */ + public async ListChannelUsers( + channelId: string, + queryParams: PageMetadata, + domainId: string, + token: string + ): Promise { + 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( + `${domainId}/channels/${channelId}/users?${new URLSearchParams( + stringParams + ).toString()}`, + this.usersUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const usersData: UsersPage = await response.json(); + return usersData; + } catch (error) { + throw error; + } + } } diff --git a/src/clients.ts b/src/clients.ts index d1e5babd..1985597f 100644 --- a/src/clients.ts +++ b/src/clients.ts @@ -8,6 +8,7 @@ import type { Role, RolePage, BasicPageMeta, + UsersPage, } from "./defs"; /** @@ -17,6 +18,8 @@ import type { export default class Clients { private readonly clientsUrl: URL; + private readonly usersUrl?: URL; + private readonly contentType: string; private readonly clientsEndpoint: string; @@ -28,9 +31,21 @@ export default class Clients { * Initializes the Clients API client. * @param {object} config - Configuration object. * @param {string} config.clientsUrl - Base URL for the clients API. + * @param {string} [config.usersUrl] - Optional URL for the users API. */ - public constructor({ clientsUrl }: { clientsUrl: string }) { + public constructor({ + clientsUrl, + usersUrl, + }: { + clientsUrl: string; + usersUrl?: string; + }) { this.clientsUrl = new URL(clientsUrl); + if (usersUrl !== undefined) { + this.usersUrl = new URL(usersUrl); + } else { + this.usersUrl = new URL(""); + } this.contentType = "application/json"; this.clientsEndpoint = "clients"; this.clientRoles = new Roles(); @@ -404,20 +419,20 @@ export default class Clients { } /** - * @method ListUserClients - Retrieves all clients that match the specified query parameters for the given userId. - * @param {string} userId - The unique ID of the user. + * @method ListClientUsers - Retrieves a list of users associated with a specific client. + * @param {string} clientId - The unique ID of the client. * @param {PageMetadata} queryParams - Query parameters for the request. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} clientsPage - A page of clients. - * @throws {Error} - If the clients cannot be fetched. + * @returns {Promise} usersPage - A page of users. + * @throws {Error} - If the users of the client cannot be fetched. */ - public async ListUserClients( - userId: string, + public async ListClientUsers( + clientId: string, queryParams: PageMetadata, domainId: string, token: string - ): Promise { + ): Promise { const stringParams: Record = Object.fromEntries( Object.entries(queryParams).map(([key, value]) => [key, String(value)]) ); @@ -431,10 +446,10 @@ export default class Clients { try { const response = await fetch( new URL( - `${domainId}/users/${userId}/clients?${new URLSearchParams( + `${domainId}/clients/${clientId}/users?${new URLSearchParams( stringParams ).toString()}`, - this.clientsUrl + this.usersUrl ).toString(), options ); @@ -442,8 +457,8 @@ export default class Clients { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const clientsData: ClientsPage = await response.json(); - return clientsData; + const usersData: UsersPage = await response.json(); + return usersData; } catch (error) { throw error; } diff --git a/src/groups.ts b/src/groups.ts index bfd2155d..0b875541 100644 --- a/src/groups.ts +++ b/src/groups.ts @@ -9,6 +9,7 @@ import type { BasicPageMeta, HierarchyPageMeta, HierarchyPage, + UsersPage, } from "./defs"; import Roles from "./roles"; @@ -19,6 +20,8 @@ import Roles from "./roles"; export default class Groups { private readonly groupsUrl: URL; + private readonly usersUrl?: URL; + private readonly contentType: string; private readonly groupsEndpoint: string; @@ -30,9 +33,21 @@ export default class Groups { * Initializes the Groups API client. * @param {object} config - Configuration object. * @param {string} config.groupsUrl - Base URL for the groups API. + * @param {string} [config.usersUrl] - Optional URL for the users API. */ - public constructor({ groupsUrl }: { groupsUrl: string }) { + public constructor({ + groupsUrl, + usersUrl, + }: { + groupsUrl: string; + usersUrl?: string; + }) { this.groupsUrl = new URL(groupsUrl); + if (usersUrl !== undefined) { + this.usersUrl = new URL(usersUrl); + } else { + this.usersUrl = new URL(""); + } this.contentType = "application/json"; this.groupsEndpoint = "groups"; this.groupRoles = new Roles(); @@ -655,6 +670,52 @@ export default class Groups { } } + /** + * @method ListGroupUsers - Retrieves a list of users associated with a specific group. + * @param {string} groupId - The unique ID of the group. + * @param {PageMetadata} queryParams - Query parameters for the request. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} usersPage - A page of users. + * @throws {Error} - If the users of the group cannot be fetched. + */ + public async ListGroupUsers( + groupId: string, + queryParams: PageMetadata, + domainId: string, + token: string + ): Promise { + 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( + `${domainId}/groups/${groupId}/users?${new URLSearchParams( + stringParams + ).toString()}`, + this.usersUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const usersData: UsersPage = await response.json(); + return usersData; + } catch (error) { + throw error; + } + } + /** * @method listGroupActions - Lists all available actions for groups within a specified domain. * @param {string} domainId - The unique identifier of the domain. diff --git a/src/sdk.ts b/src/sdk.ts index cdf0f308..b6bc68f9 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -122,10 +122,10 @@ class SDK { }: SDKConfig = {}) { this.users = new Users({ usersUrl, clientsUrl }); this.domains = new Domains({ domainsUrl, usersUrl }); - this.clients = new Clients({ clientsUrl }); + this.clients = new Clients({ clientsUrl, usersUrl }); this.certs = new Certs(certsUrl); - this.groups = new Groups({ groupsUrl }); - this.channels = new Channels({ channelsUrl }); + this.groups = new Groups({ groupsUrl, usersUrl }); + this.channels = new Channels({ channelsUrl, usersUrl }); this.messages = new Messages({ readersUrl, httpAdapterUrl }); this.invitations = new Invitations(invitationsUrl); this.bootstrap = new Bootstrap(bootstrapUrl);