From 4a4ff3e0fa0f2aa159129e136e602c39e1e67b43 Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Thu, 21 Nov 2024 03:05:00 +0300 Subject: [PATCH 01/12] update clients Signed-off-by: wambuipixel --- config.toml | 2 +- examples/clients.ts | 285 +++++++++++ examples/health.ts | 6 +- examples/things.ts | 176 ------- examples/users.ts | 4 +- src/bootstrap.ts | 6 +- src/clients.ts | 1029 +++++++++++++++++++++++++++++++++++++++ src/defs.ts | 21 +- src/groups.ts | 16 +- src/health.ts | 14 +- src/roles.ts | 3 + src/sdk.ts | 24 +- src/things.ts | 768 ----------------------------- src/users.ts | 36 +- tests/bootstrap.test.ts | 2 +- tests/clients.test.ts | 155 ++++++ tests/health.test.ts | 23 +- tests/things.test.ts | 218 --------- 18 files changed, 1552 insertions(+), 1236 deletions(-) create mode 100644 examples/clients.ts delete mode 100644 examples/things.ts create mode 100644 src/clients.ts delete mode 100644 src/things.ts create mode 100644 tests/clients.test.ts delete mode 100644 tests/things.test.ts diff --git a/config.toml b/config.toml index 118ecaec..00d3e8ff 100644 --- a/config.toml +++ b/config.toml @@ -11,7 +11,7 @@ user_token = "" certs_url = "http://localhost:9019" http_adapter_url = "http://localhost/http:9016" reader_url = "http://localhost:9011" - things_url = "http://localhost:9000" + things_url = "http://localhost:9006" tls_verification = false users_url = "http://localhost:9002" channels_url = "http://localhost:9005" diff --git a/examples/clients.ts b/examples/clients.ts new file mode 100644 index 00000000..7551f52c --- /dev/null +++ b/examples/clients.ts @@ -0,0 +1,285 @@ +// Import the SDK class from the mainflux-sdk package +import SDK from "../src/sdk"; + +const defaultUrl = "http://localhost"; + +const mySdk = new SDK({ + clientsUrl: `${defaultUrl}:9006`, + usersUrl: `${defaultUrl}:9002`, +}); + +// clients.ts examples. + +const domainId = "f86fe07c-90dd-4bfc-95b7-6311e2d908ba"; +const token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzIxNDg3ODIsImlhdCI6MTczMjE0NTE4MiwiaXNzIjoibWFnaXN0cmFsYS5hdXRoIiwidHlwZSI6MCwidXNlciI6IjcyYjcyMTZiLWNmODktNDdlNS04ZDlhLWJlNGYwMzkyNTJmZiJ9.baSQ_lRLUsrP6jqF1AZHk_ryxxe7org3txWg90_sSG2_SrybaYxFxU4Ygp19lNXs52NgNuL6ln_fLkvazEOR6A"; + +mySdk.clients + .CreateClient({ name: "" }, domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .Disable("", domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .Enable("", domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .UpdateClient({ id: "", name: "Client 1" }, domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .UpdateClientSecret({ id: "", credentials: { secret: "newSecret" } }, domainId, token) + .then((response: any) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .UpdateClientTags( + { id: "", tags: ["", ""] }, + domainId, + token, + ) + .then((response: any) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .Clients({ offset: 0, limit: 10 }, domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .Client("", domainId, token) + .then((response: any) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .DeleteClient("", domainId, token) + .then((response: any) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .ListUserClients( + "", + { offset: 0, limit: 10 }, + domainId, + token, + ) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.log(error); + }); + +mySdk.clients.ClientParents(domainId, "", "", token) + .then((response: any) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients.DeleteClientParents(domainId, "", token) + .then((response: any) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .CreateClients([{ name: "" }, { name: "" }], domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .CreateClientRole("", "", domainId, token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .ListClientRoles("", domainId, { offset: 0, limit: 10 }, token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .ViewClientRole("", domainId, "", token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .UpdateClientRole( + "", + domainId, + "", + { name: "" }, + token + ) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .DeleteClientRole("2bf128e4-a1f6-401b-95ce-556d436ecebc", domainId, "", token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .AddClientRoleActions( + "", + domainId, + "", + ["", ""], + token + ) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .ListClientRoleActions("", domainId, "", token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .DeleteClientRoleActions( + "", + domainId, + "", + ["", ""], + token + ) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .DeleteAllClientRoleActions("", domainId, "", token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .AddClientRoleMembers("", domainId, "", ["", ""], token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .ListClientRoleMembers( + "", + domainId, + "", + { offset: 0, limit: 10 }, + token + ) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .DeleteClientRoleMembers( + "", + domainId, + "", + ["", ""], + token + ) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .DeleteAllClientRoleMembers("", domainId, "", token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/health.ts b/examples/health.ts index 35cac9b2..5af017af 100644 --- a/examples/health.ts +++ b/examples/health.ts @@ -4,7 +4,7 @@ const defaultUrl = "http://localhost"; const mySdk = new SDK({ usersUrl: `${defaultUrl}:9002`, - thingsUrl: `${defaultUrl}:9000`, + clientsUrl: `${defaultUrl}:9006`, channelsUrl: `${defaultUrl}:9005`, invitationsUrl: `${defaultUrl}:9020`, journalUrl: `${defaultUrl}:9021`, @@ -15,8 +15,8 @@ const mySdk = new SDK({ bootstrapUrl: `${defaultUrl}:9013`, }); -// Things service Health -mySdk.Health.Health("things") +// Clients service Health +mySdk.Health.Health("clients") .then((response: any) => { console.log("response: ", response); }) diff --git a/examples/things.ts b/examples/things.ts deleted file mode 100644 index 9505ab1d..00000000 --- a/examples/things.ts +++ /dev/null @@ -1,176 +0,0 @@ -// Import the SDK class from the mainflux-sdk package -import SDK from "../src/sdk"; - -const defaultUrl = "http://localhost"; - -const mySdk = new SDK({ - thingsUrl: `${defaultUrl}:9000`, - usersUrl: `${defaultUrl}:9002`, -}); - -// Things.ts examples. - -const domainId = ""; -const token = ""; - -mySdk.things - .Create({ name: "" }, domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .Disable("", domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.error(error); - }); - -mySdk.things - .Enable("", domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.error(error); - }); - -mySdk.things - .UpdateThing({ id: "", name: "" }, domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .UpdateThingSecret({ id: "", credentials: { secret: "newSecret" } }, domainId, token) - .then((response: any) => { - console.log(response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .UpdateThingTags( - { id: "", tags: ["", ""] }, - domainId, - token, - ) - .then((response: any) => { - console.log(response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .ThingsByChannel("", { offset: 0, limit: 5 }, domainId, token) - .then((response: any) => { - console.log(response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .Things({ offset: 0, limit: 10 }, domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .Thing("", domainId, token) - .then((response: any) => { - console.log("response: ", response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .ThingsPermissions("", domainId, token) - .then((response: any) => { - console.log("response: ", response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .ShareThing( - "", - - "administrator", - [ - "", "", - ], - domainId, - token, - ) - .then((response: any) => { - console.log("response: ", response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .UnShareThing( - "", - "", - - [ - "", "", - ], - domainId, - token, - ) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .ListThingUsers( - "", - { offset: 0, limit: 10 }, - domainId, - token, - ) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .DeleteThing("", domainId, token) - .then((response: any) => { - console.log("response: ", response); - }) - .catch((error) => { - console.log(error); - }); - -mySdk.things - .CreateThings([{ name: "" }, { name: "" }], domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.log(error); - }); diff --git a/examples/users.ts b/examples/users.ts index d659b495..37415461 100644 --- a/examples/users.ts +++ b/examples/users.ts @@ -5,7 +5,7 @@ const defaultUrl = "http://localhost"; const mySdk = new SDK({ usersUrl: `${defaultUrl}:9002`, - thingsUrl: `${defaultUrl}:9000`, + clientsUrl: `${defaultUrl}:9006`, }); const token = ""; @@ -191,7 +191,7 @@ mySdk.users.ListUserChannels( console.log(error); }); -mySdk.users.ListUserThings( +mySdk.users.ListUserClients( "", "", { offset: 0, limit: 10 }, diff --git a/src/bootstrap.ts b/src/bootstrap.ts index 33305707..fc858aa5 100644 --- a/src/bootstrap.ts +++ b/src/bootstrap.ts @@ -128,7 +128,7 @@ export default class Bootstrap { try { const response = await fetch( new URL( - `${domainId}/${this.whitelistEndpoint}/${bootstrap.thing_id}`, + `${domainId}/${this.whitelistEndpoint}/${bootstrap.client_id}`, this.bootstrapUrl ).toString(), options @@ -178,7 +178,7 @@ export default class Bootstrap { try { const response = await fetch( new URL( - `${domainId}/${this.configsEndpoint}/${bootstrap.thing_id}`, + `${domainId}/${this.configsEndpoint}/${bootstrap.client_id}`, this.bootstrapUrl ).toString(), options @@ -265,7 +265,7 @@ export default class Bootstrap { try { const response = await fetch( new URL( - `${domainId}/${this.bootstrapCertsEndpoint}/${configs.thing_id}`, + `${domainId}/${this.bootstrapCertsEndpoint}/${configs.client_id}`, this.bootstrapUrl ).toString(), options diff --git a/src/clients.ts b/src/clients.ts new file mode 100644 index 00000000..e27732cd --- /dev/null +++ b/src/clients.ts @@ -0,0 +1,1029 @@ +import Errors from "./errors"; +import Roles from "./roles"; +import type { + Client, + ClientsPage, + Response, + PageMetadata, + Role, + RolePage, + BasicPageMeta, +} from "./defs"; + +export default class Clients { + // Clients service client. + /** + @class Clients + private Clients_url: URL; + content_type: string; + ClientsEndpoint: string; + // + //Clients API is used for creating and managing Clients. + //It is used for creating, updating, deleting and retrieving Clients. + //@param {string} clients_url - Clients service URL. + //@returns {Object} - Clients service client. + */ + private readonly clientsUrl: URL; + + private readonly usersUrl?: URL; + + private readonly contentType: string; + + private readonly clientsEndpoint: string; + + private readonly clientRoles: Roles; + + 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(); + } + + /** + * @method CreateClient + * Creates a new client. + * @param {Object} Client - Client object containing details like name and metadata. + * @param {string} domainId - The unique ID of the domain. + * @param {stringtring} token - Authorization token. + * @returns {Promise} - The created client object. + * @throws {Error} If the client cannot be created. + */ + public async CreateClient( + client: Client, + domainId: string, + token: string + ): Promise { + const options: RequestInit = { + method: "POST", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify(client), + }; + + try { + const response = await fetch( + new URL( + `${domainId}/${this.clientsEndpoint}`, + this.clientsUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const clientData: Client = await response.json(); + return clientData; + } catch (error) { + throw error; + } + } + + /** + * @method CreateClients + * Creates multiple new clients. + * @param {Client[]} clients - An array of client 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} - A page of clients. + * @throws {Error} If the clients cannot be created. + */ + public async CreateClients( + clients: Client[], + domainId: string, + token: string + ): Promise { + const options: RequestInit = { + method: "POST", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify(Clients), + }; + try { + const response = await fetch( + new URL( + `${domainId}/${this.clientsEndpoint}/bulk`, + this.clientsUrl + ).toString(), + options + ); + console.log("url", response.url); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const ClientData: ClientsPage = await response.json(); + return ClientData; + } catch (error) { + throw error; + } + } + + public async Enable( + clientId: string, + domainId: string, + token: string + ): Promise { + // Enables a Client. + /** + * @method Enable - Enables a previously disabled Client when provided with a valid token and Client ID. + * @param {string} clientID - client ID. + * @param {string} domainId - The Domain ID. + * @param {string} token - User token. + * @returns {Object} - Returns updated Client. + */ + + const options: RequestInit = { + method: "POST", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}`, + }, + }; + + try { + const response = await fetch( + new URL( + `${domainId}/${this.clientsEndpoint}/${clientId}/enable`, + this.clientsUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const enabledClient: Client = await response.json(); + return enabledClient; + } catch (error) { + throw error; + } + } + + public async Disable( + ClientId: string, + domainId: string, + token: string + ): Promise { + // Disables Client. + /** + * @method Disable - Disables a Client when provided with a valid token, domain ID and Client ID. + * @param {string} ClientId - Client ID. + * @param {string} domainId - The Domain ID. + * @param {string} token - User token. + * @returns {Object} - Returns disabled Client + */ + + const options: RequestInit = { + method: "POST", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}`, + }, + }; + try { + const response = await fetch( + new URL( + `${domainId}/${this.clientsEndpoint}/${ClientId}/disable`, + this.clientsUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const disabledClient: Client = await response.json(); + return disabledClient; + } catch (error) { + throw error; + } + } + + public async UpdateClient( + Client: Client, + domainId: string, + token: string + ): Promise { + // Updates Client. + /** + * @method Update - Updates Client when provided with a valid token, + * domain ID and Client object. + * @param {Object} Client - Client object. + * @param {string} domainId - The Domain ID. + * @param {string} token - User token. + * @returns {Object} - Client object. + * @example + * const Client = { + * "name": "Client3", + * "tags": [ + * "tag1" + * ], + * "credentials": { + * "identity": "Clientidentity", + * "secret":"12345678" + * }, + * "owner": "bb7edb32-2eac-4aad-aebe-ed96fe073879", + * "id": "bb7edb32-2eac-4aad-aebe-ed96fe073879", + * } + */ + + const options: RequestInit = { + method: "PATCH", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify(Client), + }; + try { + const response = await fetch( + new URL( + `${domainId}/${this.clientsEndpoint}/${Client.id}`, + this.clientsUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const ClientData: Client = await response.json(); + return ClientData; + } catch (error) { + throw error; + } + } + + public async UpdateClientSecret( + Client: Client, + domainId: string, + token: string + ): Promise { + // Updates Client secret. + /** + * @method UpdateClientSecret - Updates Client secret when provided with a valid token and domain ID, + * domain ID and Client object. + * @param {string} Client_id - Client ID. + * @param {Object} Client - Client object. + * @param {string} token - User token. + * @returns {Object} - Client object. + * @example + * const Client = { + * "name": "Client3", + * "tags": [ + * "tag1" + * ], + * "credentials": { + * "identity": "Clientidentity", + * "secret":"56788912" + * }, + * "owner": "bb7edb32-2eac-4aad-aebe-ed96fe073879", + * "id": "bb7edb32-2eac-4aad-aebe-ed96fe073879", + * } + */ + + const options: RequestInit = { + method: "PATCH", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ secret: Client.credentials?.secret }), + }; + try { + const response = await fetch( + new URL( + `${domainId}/${this.clientsEndpoint}/${Client.id}/secret`, + this.clientsUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const ClientData: Client = await response.json(); + return ClientData; + } catch (error) { + throw error; + } + } + + public async UpdateClientTags( + Client: Client, + domainId: string, + token: string + ): Promise { + // Updates Client tags. + /** + * @method UpdateClientTags - Updates Client tags when provided with a valid token, + * domain ID and Client object. + * + * @param {Object} Client - Client object. + * @param {string} domainId - The Domain ID. + * @param {string} token - User token. + * @returns {Object} - Client object. + * @example + * const Client = { + * "name": "Client3", + * "tags": [ + * "tag1" + * ], + * "credentials": { + * "identity": "Clientidentity", + * "secret":"56788912" + * }, + * "owner": "bb7edb32-2eac-4aad-aebe-ed96fe073879", + * "id": "bb7edb32-2eac-4aad-aebe-ed96fe073879", + * } + */ + + const options: RequestInit = { + method: "PATCH", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify(Client), + }; + + try { + const response = await fetch( + new URL( + `${domainId}/${this.clientsEndpoint}/${Client.id}/tags`, + this.clientsUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const ClientData: Client = await response.json(); + return ClientData; + } catch (error) { + throw error; + } + } + + public async Client( + clientId: string, + domainId: string, + token: string + ): Promise { + // Gets a Client + /** + * Provides information about the Client with provided ID. The Client is + * retrieved using authorization token. + * @method Client - Gets a Client. + * @param {String} ClientId - Client ID. + * @param {string} domainId - The Domain ID. + * @param {String} token - Access token. + * @returns {Object} - Client object. + * @example + * const ClientId = "886b4266-77d1-4258-abae-2931fb4f16de" + * + */ + + const options: RequestInit = { + method: "GET", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}`, + }, + }; + + try { + const response = await fetch( + new URL( + `${domainId}/${this.clientsEndpoint}/${clientId}`, + this.clientsUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const ClientData: Client = await response.json(); + return ClientData; + } catch (error) { + throw error; + } + } + + /** + * @method Clients + * Retrieves all clients 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} - A page of clients. + * @throws {Error} If the clients cannot be fetched. + */ + public async Clients( + 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}/${this.clientsEndpoint}?${new URLSearchParams( + stringParams + ).toString()}`, + this.clientsUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const ClientsData: ClientsPage = await response.json(); + return ClientsData; + } catch (error) { + throw error; + } + } + + public async ListUserClients( + userId: 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}/users/${userId}/clients?${new URLSearchParams(stringParams).toString()}`, + this.usersUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const clientsData: ClientsPage = await response.json(); + return clientsData; + } catch (error) { + throw error; + } + } + + /** + * @method ClientParents + * Sets parent to a channel. + * @param {string} domainId - The unique ID of the domain. + * @param {string} clientlId - 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} - 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 ClientParents(domainId: string, clientlId: string, parentGroupId: string, token: string) : Promise { + const options: RequestInit = { + method: "POST", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}` + }, + body: JSON.stringify({ parent_group_id: parentGroupId }) + }; + try { + const response = await fetch( + new URL(`${domainId}/${this.clientsEndpoint}/${clientlId}/parent`, this.clientsUrl).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const addClientParentsResponse: Response = { status: response.status, message: "Client Group Parent added successfully" }; + return addClientParentsResponse; + } catch (error) { + throw error; + } + } + + /** + * @method DeleteClientParents + * Removes the parent group from a specified client. + * @param {string} domainId - The unique ID of the domain. + * @param {string} clientId - The unique ID of the client. + * @param {string} token - Authorization token. + * @returns {Promise} - 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 DeleteClientParents(domainId: string, clientId: string, token: string) : Promise { + const options: RequestInit = { + method: "DELETE", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}` + }, + }; + try { + const response = await fetch( + new URL( + `${domainId}/${this.clientsEndpoint}/${clientId}/parent`, + this.clientsUrl + ).toString(), + options + ); + console.log("url", response.url); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const deleteChannelParentsResponse: Response = { status: response.status, message: "Channel Group Parent deleted successfully" }; + return deleteChannelParentsResponse; + } catch (error) { + throw error; + } + } + + public async DeleteClient( + ClientId: string, + domainId: string, + token: string + ): Promise { + // Deletes a Client. + /** + * @method DeleteClient - Deletes a Client. + * @param {string} ClientId - Client ID. + * @param {string} domainId - The Domain ID. + * @param {string} token - User token. + * @returns {Object} - NoClient + * */ + const options: RequestInit = { + method: "DELETE", + headers: { + "Content-Type": this.contentType, + Authorization: `Bearer ${token}`, + }, + }; + + try { + const response = await fetch( + new URL( + `${domainId}/${this.clientsEndpoint}/${ClientId}`, + this.clientsUrl + ).toString(), + options + ); + if (!response.ok) { + const errorRes = await response.json(); + throw Errors.HandleError(errorRes.message, response.status); + } + const deleteResponse: Response = { + status: response.status, + message: "Client deleted successfully", + }; + return deleteResponse; + } catch (error) { + throw error; + } + } + + /** + * @method CreateClientRole + * Creates a new role within a specific client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The name of the role to create. + * @param {string} token - Authorization token. + * @param {string[]} optionalActions - Optional actions assigned to the role. + * @param {string[]} optionalMembers - Optional members assigned to the role. + * @returns {Promise} A promise that resolves with the role created. + * @throws {Error} If the role cannot be created or already exists. + */ + public async CreateClientRole( + clientId: string, + roleName: string, + domainId: string, + token: string, + optionalActions?: string[], + optionalMembers?: string[], + ): Promise { + try { + const role: Role = await this.clientRoles.CreateRole( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + token, + optionalActions, + optionalMembers, + ); + return role; + } catch (error) { + throw error; + } + } + + /** + * @method ListClientRoles + * Lists all roles within a specific client. + * @param {string} clientId - The unique identifier of the client. + * @param {PageMetadata} queryParams - Metadata for pagination or filters. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves with a page of roles in the domain. + * @throws {Error} If the client is invalid or roles cannot be fetched. + */ + public async ListClientRoles( + clientId: string, + domainId: string, + queryParams: PageMetadata, + token: string, + ): Promise { + try { + const rolesPage: RolePage = await this.clientRoles.ListRoles( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + queryParams, + token, + ); + return rolesPage; + } catch (error) { + throw error; + } + } + + /** + * @method ViewClientRole + * Retrieves details about a specific role in a client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves with the role details. + * @throws {Error} If the role does not exist or cannot be retrieved. + */ + public async ViewClientRole( + clientId: string, + domainId: string, + roleName: string, + token: string, + ): Promise { + try { + const role = await this.clientRoles.ViewRole( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + token, + ); + return role; + } catch (error) { + throw error; + } + } + + /** + * @method UpdateClientRole + * Updates the details of a specific role in a client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {Role} role - The role to be updated. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves with the updated role. + * @throws {Error} If the role cannot be updated. + */ + public async UpdateClientRole( + clientId: string, + domainId: string, + roleName: string, + role: Role, + token: string, + ): Promise { + try { + const updatedRole = await this.clientRoles.UpdateRole( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + role, + token, + ); + return updatedRole; + } catch (error) { + throw error; + } + } + + /** + * Deletes a specific role from a client. + * + * @function DeleteClientRole + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves when the role is deleted. + * @throws {Error} If the role cannot be deleted. + */ + public async DeleteClientRole( + clientId: string, + domainId: string, + roleName: string, + token: string, + ): Promise { + try { + const response = await this.clientRoles.DeleteRole( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + token, + ); + return response; + } catch (error) { + throw error; + } + } + + /** + * @method AddClientRoleActions + * Adds actions to a specific role in a client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {string} token - Authorization token. + * @param {string[]} actions - The actions to add to the role. + * @returns {Promise} A promise that resolves with an array of actions. + * @throws {Error} If the actions cannot be added. + */ + public async AddClientRoleActions( + clientId: string, + domainId: string, + roleName: string, + actions: string[], + token: string, + ) { + try { + const response = await this.clientRoles.AddRoleActions( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + actions, + token, + ); + return response; + } catch (error) { + throw error; + } + } + + /** + * @method ListClientRoleActions + * Lists all actions associated with a specific role in a client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves with an array of actions. + * @throws {Error} If actions cannot be retrieved. + */ + public async ListClientRoleActions( + clientId: string, + domainId: string, + roleName: string, + token: string, + ): Promise { + try { + const updatedRole = await this.clientRoles.ListRoleActions( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + token, + ); + return updatedRole; + } catch (error) { + throw error; + } + } + + /** + * @method DeleteClientRoleActions + * Deletes specific actions from a role in a client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {string[]} actions - The actions to delete from the role. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves when actions are deleted. + * @throws {Error} If the actions cannot be deleted. + */ + public async DeleteClientRoleActions( + clientId: string, + domainId: string, + roleName: string, + actions: string[], + token: string, + ): Promise { + try { + const response = await this.clientRoles.DeleteRoleActions( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + actions, + token, + ); + return response; + } catch (error) { + throw error; + } + } + + /** + * @method DeleteAllClientRoleActions + * Deletes all actions associated with a specific role in a client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves when all actions are deleted. + * @throws {Error} If the actions cannot be deleted. + */ + public async DeleteAllClientRoleActions( + clientId: string, + domainId: string, + roleName: string, + token: string, + ): Promise { + try { + const response = await this.clientRoles.DeleteAllRoleActions( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + token, + ); + return response; + } catch (error) { + throw error; + } + } + + /** + * @method AddClientRoleMembers + * Adds members to a specific role in a client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {string[]} members - The IDs of the members to add. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves with an array of member ids. + * @throws {Error} If the members cannot be added. + */ + public async AddClientRoleMembers( + clientId: string, + domainId: string, + roleName: string, + members: string[], + token: string, + ): Promise { + try { + const response = await this.clientRoles.AddRoleMembers( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + members, + token, + ); + return response; + } catch (error) { + throw error; + } + } + + /** + * @method ListClientRoleMembers + * Lists all members associated with a specific role in a client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves with an array of member ids. + * @throws {Error} If members cannot be retrieved. + */ + public async ListClientRoleMembers( + clientId: string, + domainId: string, + roleName: string, + queryParams: BasicPageMeta, + token: string, + ): Promise { + try { + const updatedRole = await this.clientRoles.ListRoleMembers( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + queryParams, + token, + ); + return updatedRole; + } catch (error) { + throw error; + } + } + + /** + * @method DeleteClientRoleMembers + * Deletes specific members from a role in a client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {string[]} members - The IDs of the members to delete. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves when members are deleted. + * @throws {Error} If the members cannot be deleted. + */ + public async DeleteClientRoleMembers( + clientId: string, + domainId: string, + roleName: string, + members: string[], + token: string, + ): Promise { + try { + const response = await this.clientRoles.DeleteRoleMembers( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + members, + token, + ); + return response; + } catch (error) { + throw error; + } + } + + /** + * @method DeleteAllClientRoleMembers + * Deletes all members associated with a specific role in a client. + * @param {string} clientId - The unique identifier of the client. + * @param {string} roleName - The unique identifier of the role. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves when all members are deleted. + * @throws {Error} If the members cannot be deleted. + */ + public async DeleteAllClientRoleMembers( + clientId: string, + domainId: string, + roleName: string, + token: string, + ): Promise { + try { + const response = await this.clientRoles.DeleteAllRoleMembers( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + clientId, + roleName, + token, + ); + return response; + } catch (error) { + throw error; + } + } +} diff --git a/src/defs.ts b/src/defs.ts index 76898c96..511ab162 100644 --- a/src/defs.ts +++ b/src/defs.ts @@ -30,19 +30,19 @@ export interface Credentials { secret?: string; } -export interface ThingCredentials { +export interface ClientCredentials { identity?: string; secret?: string; } -export interface ThingBasicInfo { +export interface ClientBasicInfo { id?: string; name?: string; - credentials?: ThingCredentials; + credentials?: ClientCredentials; status?: Status; } -export interface Thing extends ThingBasicInfo { +export interface Client extends ClientBasicInfo { tags?: string[]; domain_id?: string | DomainBasicInfo; metadata?: Record; @@ -52,8 +52,8 @@ export interface Thing extends ThingBasicInfo { permissions?: string[]; } -export interface ThingsPage { - things: Thing[]; +export interface ClientsPage { + clients: Client[]; total: number; offset: number; limit: number; @@ -245,7 +245,7 @@ export interface MessagesPage { export interface MessagesPageMetadata extends PageMetadata { subtopic?: string; - publisher?: string | ThingBasicInfo; + publisher?: string | ClientBasicInfo; protocol?: string; comparator?: string; vb?: boolean; @@ -259,7 +259,7 @@ export interface MessagesPageMetadata extends PageMetadata { export interface SenMLMessage { channel?: string | ChannelBasicInfo; subtopic?: string; - publisher?: string | ThingBasicInfo; + publisher?: string | ClientBasicInfo; protocol?: string; name?: string; unit?: string; @@ -273,7 +273,7 @@ export interface SenMLMessage { } export interface Cert { - thing_id?: string; + Client_id?: string; cert_serial?: string; client_key?: string; client_cert?: string; @@ -291,8 +291,7 @@ export interface BootstrapConfig { channels?: string[]; external_id?: string; external_key?: string; - thing_id?: string; - thing_key?: string; + client_id?: string; name?: string; client_cert?: string; client_key?: string; diff --git a/src/groups.ts b/src/groups.ts index d85c4442..bc9f0fcc 100644 --- a/src/groups.ts +++ b/src/groups.ts @@ -17,14 +17,14 @@ export default class Groups { * Groups API client is used for managing groups. It is used for * creating, updating, deleting, and retrieving groups. * @param {string} groupsUrl - The URL of the Groups service. - * @param {string} thingsUrl- Things service URL. + * @param {string} clientsUrl- Clients service URL. * @param {string} contentType - The content type of the request. * @param {string} groupsEndpoint - The endpoint of the Groups service. * @returns {Groups} - Returns a Groups object. */ private readonly usersUrl: URL; - private readonly thingsUrl?: URL; + private readonly clientsUrl?: URL; private readonly contentType: string; @@ -32,16 +32,16 @@ export default class Groups { public constructor({ usersUrl, - thingsUrl, + clientsUrl, }: { usersUrl: string; - thingsUrl?: string; + clientsUrl?: string; }) { this.usersUrl = new URL(usersUrl); - if (thingsUrl !== undefined) { - this.thingsUrl = new URL(thingsUrl); + if (clientsUrl !== undefined) { + this.clientsUrl = new URL(clientsUrl); } else { - this.thingsUrl = new URL(""); + this.clientsUrl = new URL(""); } this.contentType = "application/json"; this.groupsEndpoint = "groups"; @@ -577,7 +577,7 @@ export default class Groups { }/${groupId}/channels?${new URLSearchParams( stringParams ).toString()}`, - this.thingsUrl + this.clientsUrl ).toString(), options ); diff --git a/src/health.ts b/src/health.ts index 06b0c1b1..c31408da 100644 --- a/src/health.ts +++ b/src/health.ts @@ -4,7 +4,7 @@ import Errors from "./errors"; export default class Health { private readonly usersUrl?: URL; - private readonly thingsUrl?: URL; + private readonly clientsUrl?: URL; private readonly channelsUrl?: URL; @@ -24,7 +24,7 @@ export default class Health { public constructor({ usersUrl, - thingsUrl, + clientsUrl, channelsUrl, bootstrapUrl, certsUrl, @@ -34,7 +34,7 @@ export default class Health { invitationsUrl, }: { usersUrl?: string; - thingsUrl?: string; + clientsUrl?: string; channelsUrl?: string; bootstrapUrl?: string; certsUrl?: string; @@ -46,8 +46,8 @@ export default class Health { if (usersUrl !== undefined) { this.usersUrl = new URL(usersUrl); } - if (thingsUrl !== undefined) { - this.thingsUrl = new URL(thingsUrl); + if (clientsUrl !== undefined) { + this.clientsUrl = new URL(clientsUrl); } if (channelsUrl !== undefined) { this.channelsUrl = new URL(channelsUrl); @@ -76,8 +76,8 @@ export default class Health { public async Health(service: string): Promise { let url: URL | undefined; switch (service) { - case "things": { - url = this.thingsUrl; + case "clients": { + url = this.clientsUrl; break; } case "users": { diff --git a/src/roles.ts b/src/roles.ts index 664178ed..a0760c09 100644 --- a/src/roles.ts +++ b/src/roles.ts @@ -65,6 +65,7 @@ export default class Roles { new URL(`${endpoint}/${entityId}/roles`, url).toString(), options, ); + console.log("url", response.url); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); @@ -237,6 +238,7 @@ export default class Roles { ).toString(), options, ); + console.log("url", response.url); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); @@ -271,6 +273,7 @@ export default class Roles { ).toString(), options, ); + console.log("url", response.url); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); diff --git a/src/sdk.ts b/src/sdk.ts index f71289c6..56eb6314 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -1,6 +1,5 @@ import Users from "./users"; import Domains from "./domains"; -import Things from "./things"; import Certs from "./certs"; import Groups from "./groups"; import Invitations from "./invitations"; @@ -9,13 +8,14 @@ import Messages from "./messages"; import Bootstrap from "./bootstrap"; import Journal from "./journal"; import Health from "./health"; +import Clients from "./clients"; export type { User, UsersPage, - ThingBasicInfo, - Thing, - ThingsPage, + ClientBasicInfo, + Client, + ClientsPage, GroupBasicInfo, Group, GroupsPage, @@ -36,7 +36,7 @@ export type { Relation, GroupRelation, Credentials, - ThingCredentials, + ClientCredentials, UserBasicInfo, DomainBasicInfo, Permissions, @@ -63,7 +63,7 @@ export interface SDKConfig { usersUrl?: string; channelsUrl?: string; domainsUrl?: string; - thingsUrl?: string; + clientsUrl?: string; certsUrl?: string; readersUrl?: string; httpAdapterUrl?: string; @@ -77,7 +77,7 @@ class SDK { domains: Domains; - things: Things; + clients: Clients; certs: Certs; @@ -99,7 +99,7 @@ class SDK { usersUrl = defaultUrl, channelsUrl = defaultUrl, domainsUrl = defaultUrl, - thingsUrl = defaultUrl, + clientsUrl = defaultUrl, certsUrl = defaultUrl, readersUrl = defaultUrl, httpAdapterUrl = defaultUrl, @@ -107,11 +107,11 @@ class SDK { bootstrapUrl = defaultUrl, journalUrl = defaultUrl, }: SDKConfig = {}) { - this.users = new Users({ usersUrl, thingsUrl }); + this.users = new Users({ usersUrl, clientsUrl }); this.domains = new Domains({ domainsUrl, usersUrl }); - this.things = new Things({ thingsUrl, usersUrl }); + this.clients = new Clients({ clientsUrl, usersUrl }); this.certs = new Certs(certsUrl); - this.groups = new Groups({ usersUrl, thingsUrl }); + this.groups = new Groups({ usersUrl, clientsUrl }); this.channels = new Channels({ channelsUrl }); this.messages = new Messages({ readersUrl, httpAdapterUrl }); this.invitations = new Invitations(invitationsUrl); @@ -119,7 +119,7 @@ class SDK { this.Journal = new Journal(journalUrl); this.Health = new Health({ usersUrl, - thingsUrl, + clientsUrl, channelsUrl, bootstrapUrl, certsUrl, diff --git a/src/things.ts b/src/things.ts deleted file mode 100644 index d54f90fc..00000000 --- a/src/things.ts +++ /dev/null @@ -1,768 +0,0 @@ -import Errors from "./errors"; - -import type { - Thing, - ThingsPage, - Response, - PageMetadata, - UsersPage, - Permissions, - Relation, -} from "./defs"; - -export default class Things { - // Things service client. - /** - @class Things - private things_url: URL; - content_type: string; - thingsEndpoint: string; - // - //Things API is used for creating and managing things. - //It is used for creating, updating, deleting and retrieving things. - //@param {string} things_url - Things service URL. - //@returns {Object} - Things service client. - */ - private readonly thingsUrl: URL; - - private readonly usersUrl?: URL; - - private readonly contentType: string; - - private readonly thingsEndpoint: string; - - public constructor({ - thingsUrl, - usersUrl, - }: { - thingsUrl: string; - usersUrl?: string; - }) { - this.thingsUrl = new URL(thingsUrl); - if (usersUrl !== undefined) { - this.usersUrl = new URL(usersUrl); - } else { - this.usersUrl = new URL(""); - } - this.contentType = "application/json"; - this.thingsEndpoint = "things"; - } - - public async Create( - thing: Thing, - domainId: string, - token: string - ): Promise { - // Creates a new thing - /** - * @method Create - Creates a new thing. - * @param {Object} thing - Thing object. - * @param {string} domainId - The Domain ID. - * @param {String} token - Access token. - * @returns {Object} - Thing object. - * @example - * const thing = { - * "name":"thing1", - * } - */ - const options: RequestInit = { - method: "POST", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify(thing), - }; - - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const thingData: Thing = await response.json(); - return thingData; - } catch (error) { - throw error; - } - } - - public async CreateThings( - things: Thing[], - domainId: string, - token: string - ): Promise { - // Creates multiple things. - /** - * @method CreateThings - Creates multiple things. - * @param {Object} things - Array of things. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Thing object. - * @example - * const things = [ - * { - * "name": "thing3", - * "tags": [ - * "tag1" - * ], - * } - * ] - * */ - const options: RequestInit = { - method: "POST", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify(things), - }; - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/bulk`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const thingData: ThingsPage = await response.json(); - return thingData; - } catch (error) { - throw error; - } - } - - public async ThingsByChannel( - channelID: string, - queryParams: PageMetadata, - domainId: string, - token: string - ): Promise { - // Retrieves list of things connected to specified channel with pagination metadata. - /** - * @method ThingsByChannel - Retrieves list of things connected to specified channel - * with pagination metadata. - * @param {string} channelID - channel ID. - * @param {Object} queryParams - Query parameters such as offset and limit. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Channels list. - */ - - 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}/${ - this.thingsEndpoint - }?${new URLSearchParams(stringParams).toString()}`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const ThingsData: ThingsPage = await response.json(); - return ThingsData; - } catch (error) { - throw error; - } - } - - public async Enable( - thingId: string, - domainId: string, - token: string - ): Promise { - // Enables a thing. - /** - * @method Enable - Enables a previously disabled thing when provided with a valid token and thing ID. - * @param {string} thingID - Thing ID. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Returns updated thing. - */ - - const options: RequestInit = { - method: "POST", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - }; - - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/${thingId}/enable`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const enabledThing: Thing = await response.json(); - return enabledThing; - } catch (error) { - throw error; - } - } - - public async Disable( - thingId: string, - domainId: string, - token: string - ): Promise { - // Disables thing. - /** - * @method Disable - Disables a thing when provided with a valid token, domain ID and thing ID. - * @param {string} thingId - Thing ID. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Returns disabled thing - */ - - const options: RequestInit = { - method: "POST", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - }; - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/${thingId}/disable`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const disabledThing: Thing = await response.json(); - return disabledThing; - } catch (error) { - throw error; - } - } - - public async UpdateThing( - thing: Thing, - domainId: string, - token: string - ): Promise { - // Updates thing. - /** - * @method Update - Updates thing when provided with a valid token, - * domain ID and thing object. - * @param {Object} thing - Thing object. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Thing object. - * @example - * const thing = { - * "name": "thing3", - * "tags": [ - * "tag1" - * ], - * "credentials": { - * "identity": "thingidentity", - * "secret":"12345678" - * }, - * "owner": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * "id": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * } - */ - - const options: RequestInit = { - method: "PATCH", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify(thing), - }; - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/${thing.id}`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const thingData: Thing = await response.json(); - return thingData; - } catch (error) { - throw error; - } - } - - public async UpdateThingSecret( - thing: Thing, - domainId: string, - token: string - ): Promise { - // Updates thing secret. - /** - * @method UpdateThingSecret - Updates thing secret when provided with a valid token and domain ID, - * domain ID and thing object. - * @param {string} thing_id - Thing ID. - * @param {Object} thing - Thing object. - * @param {string} token - User token. - * @returns {Object} - Thing object. - * @example - * const thing = { - * "name": "thing3", - * "tags": [ - * "tag1" - * ], - * "credentials": { - * "identity": "thingidentity", - * "secret":"56788912" - * }, - * "owner": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * "id": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * } - */ - - const options: RequestInit = { - method: "PATCH", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify({ secret: thing.credentials?.secret }), - }; - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/${thing.id}/secret`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const thingData: Thing = await response.json(); - return thingData; - } catch (error) { - throw error; - } - } - - public async UpdateThingTags( - thing: Thing, - domainId: string, - token: string - ): Promise { - // Updates thing tags. - /** - * @method UpdateThingTags - Updates thing tags when provided with a valid token, - * domain ID and thing object. - * - * @param {Object} thing - Thing object. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Thing object. - * @example - * const thing = { - * "name": "thing3", - * "tags": [ - * "tag1" - * ], - * "credentials": { - * "identity": "thingidentity", - * "secret":"56788912" - * }, - * "owner": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * "id": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * } - */ - - const options: RequestInit = { - method: "PATCH", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify(thing), - }; - - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/${thing.id}/tags`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const thingData: Thing = await response.json(); - return thingData; - } catch (error) { - throw error; - } - } - - public async Thing( - thingId: string, - domainId: string, - token: string - ): Promise { - // Gets a thing - /** - * Provides information about the thing with provided ID. The thing is - * retrieved using authorization token. - * @method Thing - Gets a Thing. - * @param {String} thingId - Thing ID. - * @param {string} domainId - The Domain ID. - * @param {String} token - Access token. - * @returns {Object} - Thing object. - * @example - * const thingId = "886b4266-77d1-4258-abae-2931fb4f16de" - * - */ - - const options: RequestInit = { - method: "GET", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - }; - - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/${thingId}`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const thingData: Thing = await response.json(); - return thingData; - } catch (error) { - throw error; - } - } - - public async ThingsPermissions( - thingId: string, - domainId: string, - token: string - ): Promise { - // Retrieves thing permissions. - /** - * @method Permissions - Retrieves thing permissions when provided with a valid token - * and thing ID. - * @param {string} thingId - Thing ID. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Thing permissions. - * @example - * const thing_id = "bb7edb32-2eac-4aad-aebe-ed96fe073879" - * const token - * const permissions = Things.Permissions(thing_id, token) - * console.log(permissions) - * */ - - const options: RequestInit = { - method: "GET", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - }; - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/${thingId}/permissions`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const thingData: Permissions = await response.json(); - return thingData; - } catch (error) { - throw error; - } - } - - public async Things( - queryParams: PageMetadata, - domainId: string, - token: string - ): Promise { - // Gets all things with pagination. - /** - * Provides information about all users. The users are retrieved using - * authorization user_token. - * - * @method Things - Gets all things with pagination. - * @param {Object} queryParams - Query parameters. - * @param {string} domainId - The Domain ID. - * @param {String} token - Access token. - * @returns {Object} - Thing object. - * @example - * const queryParams = { - * "offset": 0, - * "limit": 10 - * } - * - */ - - 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}/${this.thingsEndpoint}?${new URLSearchParams( - stringParams - ).toString()}`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const thingsData: ThingsPage = await response.json(); - return thingsData; - } catch (error) { - throw error; - } - } - - public async ListThingUsers( - thingId: 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}/${ - this.thingsEndpoint - }/${thingId}/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 userData: UsersPage = await response.json(); - return userData; - } catch (error) { - throw error; - } - } - - public async ShareThing( - thingId: string, - Relation: Relation, - userIDs: string[], - domainId: string, - token: string - ): Promise { - // Shares a thing with a user. - /** - * @method ShareThing - Shares a thing with a user. - * @param {string} thingId - Thing ID. - * @param {Relation} Relation - User relation to the thing. - * @param {string} domainId - The Domain ID. - * @param {string []} userIDs - Array of user ID's. - * @param {string} token - User token. - * @returns {Object} - Nothing - * - * */ - const req = { relation: Relation, user_ids: userIDs }; - const options: RequestInit = { - method: "POST", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify(req), - }; - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/${thingId}/share`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const shareResponse: Response = { - status: response.status, - message: "Thing shared successfully", - }; - return shareResponse; - } catch (error) { - throw error; - } - } - - public async UnShareThing( - thingId: string, - Relation: string, - userIDs: string[], - domainId: string, - token: string - ): Promise { - // Shares a thing with a user. - /** - * @method UnShareThing - UnShares a thing with a user. - * @param {string} thingId - Thing ID. - * @param {string []} userIDs - Array of user ID's. - * @param {string} Relation - User relation to the thing. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Nothing - * - * */ - const req = { relation: Relation, user_ids: userIDs }; - const options: RequestInit = { - method: "POST", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify(req), - }; - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/${thingId}/unshare`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const unshareResponse: Response = { - status: response.status, - message: "Thing unShared successfully", - }; - return unshareResponse; - } catch (error) { - throw error; - } - } - - public async DeleteThing( - thingId: string, - domainId: string, - token: string - ): Promise { - // Deletes a thing. - /** - * @method DeleteThing - Deletes a thing. - * @param {string} thingId - Thing ID. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Nothing - * */ - const options: RequestInit = { - method: "DELETE", - headers: { - "Content-Type": this.contentType, - Authorization: `Bearer ${token}`, - }, - }; - - try { - const response = await fetch( - new URL( - `${domainId}/${this.thingsEndpoint}/${thingId}`, - this.thingsUrl - ).toString(), - options - ); - if (!response.ok) { - const errorRes = await response.json(); - throw Errors.HandleError(errorRes.message, response.status); - } - const deleteResponse: Response = { - status: response.status, - message: "Thing deleted successfully", - }; - return deleteResponse; - } catch (error) { - throw error; - } - } -} diff --git a/src/users.ts b/src/users.ts index d70ff90a..39cbc2af 100644 --- a/src/users.ts +++ b/src/users.ts @@ -2,7 +2,7 @@ import Errors from "./errors"; import type { User, UsersPage, - ThingsPage, + ClientsPage, GroupsPage, Login, PageMetadata, @@ -24,7 +24,7 @@ export default class Users { */ private readonly usersUrl: URL; - private readonly thingsUrl?: URL; + private readonly clientsUrl?: URL; private readonly contentType: string; @@ -34,16 +34,16 @@ export default class Users { public constructor({ usersUrl, - thingsUrl, + clientsUrl, }: { usersUrl: string; - thingsUrl?: string; + clientsUrl?: string; }) { this.usersUrl = new URL(usersUrl); - if (thingsUrl !== undefined) { - this.thingsUrl = new URL(thingsUrl); + if (clientsUrl !== undefined) { + this.clientsUrl = new URL(clientsUrl); } else { - this.thingsUrl = new URL(""); + this.clientsUrl = new URL(""); } this.contentType = "application/json"; this.usersEndpoint = "users"; @@ -745,21 +745,21 @@ export default class Users { } } - public async ListUserThings( + public async ListUserClients( userId: string, domainId: string, queryParams: PageMetadata, token: string - ): Promise { - // Get things of a user. + ): Promise { + // Get clients of a user. /** - * Gets the various things a user owns. - * @method ListUserThings - Get memberships of a user. + * Gets the various clients a user owns. + * @method ListUserClients - Get memberships of a user. * @param {String} userId - Member ID. * @param {String} domainId - Domain ID. * @param {Object} queryParams - Query parameters for example offset and limit. * @param {String} token - Access token. - * @returns {Object} - Things object. + * @returns {Object} - Clients object. */ const stringParams: Record = Object.fromEntries( Object.entries(queryParams).map(([key, value]) => [key, String(value)]) @@ -777,8 +777,8 @@ export default class Users { new URL( `${domainId}/${ this.usersEndpoint - }/${userId}/things?${new URLSearchParams(stringParams).toString()}`, - this.thingsUrl + }/${userId}/clients?${new URLSearchParams(stringParams).toString()}`, + this.clientsUrl ).toString(), options ); @@ -786,8 +786,8 @@ export default class Users { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const thingsData: ThingsPage = await response.json(); - return thingsData; + const clientsData: ClientsPage = await response.json(); + return clientsData; } catch (error) { throw error; } @@ -827,7 +827,7 @@ export default class Users { `${domainId}/${ this.usersEndpoint }/${userId}/channels?${new URLSearchParams(stringParams).toString()}`, - this.thingsUrl + this.clientsUrl ).toString(), options ); diff --git a/tests/bootstrap.test.ts b/tests/bootstrap.test.ts index 1ca68e36..e4e82bdc 100644 --- a/tests/bootstrap.test.ts +++ b/tests/bootstrap.test.ts @@ -12,7 +12,7 @@ describe("Bootstraps", () => { const bootstrap: BootstrapConfig = { external_id: "012", external_key: "aabbcc", - thing_id: "77cbb344-7c41-47f3-a53a-a3d435b67207", + client_id: "77cbb344-7c41-47f3-a53a-a3d435b67207", name: "percius", }; const queryParams: PageMetadata = { diff --git a/tests/clients.test.ts b/tests/clients.test.ts new file mode 100644 index 00000000..d684299a --- /dev/null +++ b/tests/clients.test.ts @@ -0,0 +1,155 @@ +import fetchMock, { enableFetchMocks } from "jest-fetch-mock"; + +import SDK from "../src/sdk"; +import type { + Client, ClientsPage, UsersPage, User, +} from "../src/sdk"; + +enableFetchMocks(); + +const clientsUrl = "http://localhost"; +const usersUrl = "http://localhost"; +const sdk = new SDK({ clientsUrl, usersUrl }); + +describe("Clients", () => { + const client: Client = { + id: "bb7edb32-2eac-4aad-aebe-ed96fe073879", + name: "clientName", + tags: ["tag1", "tag2"], + credentials: { + identity: "clientIdentity", + secret: "bb7edb32-2eac-4aad-aebe-ed96fe073879", + }, + metadata: { + domain: "example.com", + }, + status: "enabled", + }; + const user: User = { + id: "886b4266-77d1-4258-abae-2931fb4f16de", + first_name: "tahliah", + last_name: "barnett", + tags: ["holy", "terrain"], + email: "fkatwigs@email.com", + credentials: { + username: "fkatwigs@email.com", + secret: "12345678", + }, + role: "administrator", + status: "enabled", + }; + const clients = [ + { name: "client1", id: "bb7edb32-2eac-4aad-aebe-ed96fe073879" }, + { name: "client2", id: "bb7edb32-2eac-4aad-aebe-ed96fe073879" }, + ]; + const queryParams = { + offset: 0, + limit: 10, + }; + const usersPage: UsersPage = { + users: [user], + total: 2, + offset: 0, + limit: 10, + }; + const clientsPage: ClientsPage = { + clients: [client], + total: 2, + offset: 10, + limit: 0, + }; + const clientId = "bb7edb32-2eac-4aad-aebe-ed96fe073879"; + const userId = "bb7edb32-2eac-4aad-aebe-ed96fe073879"; + const domainId = "886b4266-77d1-4258-abae-2931fb4f16de"; + const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjU3OTMwNjksImlhdCI6"; + + beforeEach(() => { + fetchMock.resetMocks(); + }); + + test("Create should create a client and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(client)); + + const response = await sdk.clients.CreateClient(client, domainId, token); + expect(response).toEqual(client); + }); + + test("CreateClients should create multiple clients and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(clients)); + + const response = await sdk.clients.CreateClients(clients, domainId, token); + expect(response).toEqual(clients); + }); + + test("Disable should disable a client and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(client)); + + const response = await sdk.clients.Disable(clientId, domainId, token); + expect(response).toEqual(client); + }); + + test("Enable should enable a client and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(client)); + + const response = await sdk.clients.Enable(clientId, domainId, token); + expect(response).toEqual(client); + }); + + test("UpdateClientSecret should update a client secret and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(client)); + + const response = await sdk.clients.UpdateClientSecret(client, domainId, token); + expect(response).toEqual(client); + }); + + test("UpdateClientTags should update a client tags and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(client)); + + const response = await sdk.clients.UpdateClientTags(client, domainId, token); + expect(response).toEqual(client); + }); + + test("Client should retrieve a client and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(client)); + + const response = await sdk.clients.Client(clientId, domainId, token); + expect(response).toEqual(client); + }); + + test("UpdateClient should update a client and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(client)); + + const response = await sdk.clients.UpdateClient(client, domainId, token); + expect(response).toEqual(client); + }); + + test("Clients should get a list of all clients and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(clientsPage)); + + const response = await sdk.clients.Clients(queryParams, domainId, token); + expect(response).toEqual(clientsPage); + }); + + test("ListUserClients should list users linked to a client and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(usersPage)); + + const response = await sdk.clients.ListUserClients( + userId, + queryParams, + domainId, + token, + ); + expect(response).toEqual(usersPage); + }); + + test("DeleteClient should delete a client and return success", async () => { + const deleteResponse = { + status: 200, + message: "Client deleted successfully", + }; + fetchMock.mockResponseOnce(JSON.stringify(deleteResponse)); + + const response = await sdk.clients.DeleteClient(clientId, domainId, token); + expect(response).toEqual(deleteResponse); + }); +}); diff --git a/tests/health.test.ts b/tests/health.test.ts index 8bd005d0..9e67fee7 100644 --- a/tests/health.test.ts +++ b/tests/health.test.ts @@ -6,7 +6,7 @@ import type { HealthInfo } from "../src/sdk"; enableFetchMocks(); const usersUrl = "http://localhost"; -const thingsUrl = "http://localhost"; +const clientsUrl = "http://localhost"; const channelsUrl = "http://localhost"; const certsUrl = "http://localhost"; const readersUrl = "http://localhost"; @@ -17,7 +17,7 @@ const journalUrl = "http://localhost"; const sdk = new SDK({ usersUrl, - thingsUrl, + clientsUrl, channelsUrl, certsUrl, readersUrl, @@ -46,11 +46,11 @@ describe("Health", () => { instance_id: "aafd1f94-5d6b-4aa5-9b02-22f4e9d21423", }; - const thingsServiceHealthInfo: HealthInfo = { + const clientsServiceHealthInfo: HealthInfo = { status: "pass", version: "v0.14.0", commit: "c3e7159cb762396f064d43d55c30d90011c9357f", - description: "things service", + description: "clients service", build_time: "2024-07-25_14:20:35", instance_id: "aafd1f94-5d6b-4aa5-9b02-22f4e9d21423", }; @@ -120,11 +120,18 @@ describe("Health", () => { expect(response).toEqual(usersServiceHealthInfo); }); - test("fetch things service health information", async () => { - fetchMock.mockResponseOnce(JSON.stringify(thingsServiceHealthInfo)); + test("fetch clients service health information", async () => { + fetchMock.mockResponseOnce(JSON.stringify(clientsServiceHealthInfo)); - const response = await sdk.Health.Health("users"); - expect(response).toEqual(thingsServiceHealthInfo); + const response = await sdk.Health.Health("clients"); + expect(response).toEqual(clientsServiceHealthInfo); + }); + + test("fetch channels service health information", async () => { + fetchMock.mockResponseOnce(JSON.stringify(channelsServiceHealthInfo)); + + const response = await sdk.Health.Health("channels"); + expect(response).toEqual(channelsServiceHealthInfo); }); test("fetch channels service health information", async () => { diff --git a/tests/things.test.ts b/tests/things.test.ts deleted file mode 100644 index 028e1e7d..00000000 --- a/tests/things.test.ts +++ /dev/null @@ -1,218 +0,0 @@ -import fetchMock, { enableFetchMocks } from "jest-fetch-mock"; - -import SDK from "../src/sdk"; -import type { - Thing, ThingsPage, UsersPage, User, -} from "../src/sdk"; - -enableFetchMocks(); - -const thingsUrl = "http://localhost"; -const usersUrl = "http://localhost"; -const sdk = new SDK({ thingsUrl, usersUrl }); - -describe("Things", () => { - const thing: Thing = { - id: "bb7edb32-2eac-4aad-aebe-ed96fe073879", - name: "thingName", - tags: ["tag1", "tag2"], - credentials: { - identity: "thingidentity", - secret: "bb7edb32-2eac-4aad-aebe-ed96fe073879", - }, - metadata: { - domain: "example.com", - }, - status: "enabled", - }; - const user: User = { - id: "886b4266-77d1-4258-abae-2931fb4f16de", - first_name: "tahliah", - last_name: "barnett", - tags: ["holy", "terrain"], - email: "fkatwigs@email.com", - credentials: { - username: "fkatwigs@email.com", - secret: "12345678", - }, - role: "administrator", - status: "enabled", - }; - const things = [ - { name: "thing1", id: "bb7edb32-2eac-4aad-aebe-ed96fe073879" }, - { name: "thing2", id: "bb7edb32-2eac-4aad-aebe-ed96fe073879" }, - ]; - const queryParams = { - offset: 0, - limit: 10, - }; - const usersPage: UsersPage = { - users: [user], - total: 2, - offset: 0, - limit: 10, - }; - const thingsPage: ThingsPage = { - things: [thing], - total: 2, - offset: 10, - limit: 0, - }; - const thingId = "bb7edb32-2eac-4aad-aebe-ed96fe073879"; - const channelId = "bb7edb32-2eac-4aad-aebe-ed96fe073879"; - const permissions = "admin"; - const relation = "administrator"; - const userIds = [ - "b9921574-f562-4048-a6bf-295c0036fc2a", - "ce42d80e-9773-49b2-a8c2-6aa748597a92", - ]; - const domainId = "886b4266-77d1-4258-abae-2931fb4f16de"; - const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjU3OTMwNjksImlhdCI6"; - - beforeEach(() => { - fetchMock.resetMocks(); - }); - - test("Create should create a thing and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(thing)); - - const response = await sdk.things.Create(thing, domainId, token); - expect(response).toEqual(thing); - }); - - test("CreateThings should create multiple things and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(things)); - - const response = await sdk.things.CreateThings(things, domainId, token); - expect(response).toEqual(things); - }); - - test("Disable should update a thing and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(thing)); - - const response = await sdk.things.Disable(thingId, domainId, token); - expect(response).toEqual(thing); - }); - - test("Enable should update a thing and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(thing)); - - const response = await sdk.things.Enable(thingId, domainId, token); - expect(response).toEqual(thing); - }); - - test("UpdateThingSecret should update a thing secret and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(thing)); - - const response = await sdk.things.UpdateThingSecret(thing, domainId, token); - expect(response).toEqual(thing); - }); - - test("UpdateThingTags should update a thing tags and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(thing)); - - const response = await sdk.things.UpdateThingTags(thing, domainId, token); - expect(response).toEqual(thing); - }); - - test("Thing should retrieve a thing and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(thing)); - - const response = await sdk.things.Thing(thingId, domainId, token); - expect(response).toEqual(thing); - }); - - test("UpdateThing should update a thing and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(thing)); - - const response = await sdk.things.UpdateThing(thing, domainId, token); - expect(response).toEqual(thing); - }); - - test("ThingsByChannel should return a list of channels connected to a specific thing and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(thing)); - - const response = await sdk.things.ThingsByChannel( - channelId, - queryParams, - domainId, - token, - ); - expect(response).toEqual(thing); - }); - - test("ThingsPermissions should update a thing secret and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(permissions)); - - const response = await sdk.things.ThingsPermissions( - thingId, - domainId, - token, - ); - expect(response).toEqual(permissions); - }); - - test("Things should get a list of all things and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(thingsPage)); - - const response = await sdk.things.Things(queryParams, domainId, token); - expect(response).toEqual(thingsPage); - }); - - test("ListThingUsers should list users linked to a thing and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(usersPage)); - - const response = await sdk.things.ListThingUsers( - thingId, - queryParams, - domainId, - token, - ); - expect(response).toEqual(usersPage); - }); - - test("ShareThing should share a thing and return success", async () => { - const shareResponse = { - status: 200, - message: "Thing shared successfully", - }; - fetchMock.mockResponseOnce(JSON.stringify(shareResponse)); - - const response = await sdk.things.ShareThing( - thingId, - relation, - userIds, - domainId, - token, - ); - expect(response).toEqual(shareResponse); - }); - - test("UnShareThing should unshare a thing and return success", async () => { - const unshareResponse = { - status: 200, - message: "Thing unShared successfully", - }; - fetchMock.mockResponseOnce(JSON.stringify(unshareResponse)); - - const response = await sdk.things.UnShareThing( - thingId, - relation, - userIds, - domainId, - token, - ); - expect(response).toEqual(unshareResponse); - }); - - test("DeleteThing should delete a thing and return success", async () => { - const deleteResponse = { - status: 200, - message: "Thing deleted successfully", - }; - fetchMock.mockResponseOnce(JSON.stringify(deleteResponse)); - - const response = await sdk.things.DeleteThing(thingId, domainId, token); - expect(response).toEqual(deleteResponse); - }); -}); From f32044c4e3f794e793cd68d184e265a10387e543 Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Thu, 21 Nov 2024 15:52:05 +0300 Subject: [PATCH 02/12] add more functions Signed-off-by: wambuipixel --- examples/clients.ts | 468 ++++++++++++++++++++++---------------------- src/clients.ts | 74 ++++--- 2 files changed, 269 insertions(+), 273 deletions(-) diff --git a/examples/clients.ts b/examples/clients.ts index 7551f52c..d2c33fa6 100644 --- a/examples/clients.ts +++ b/examples/clients.ts @@ -11,96 +11,96 @@ const mySdk = new SDK({ // clients.ts examples. const domainId = "f86fe07c-90dd-4bfc-95b7-6311e2d908ba"; -const token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzIxNDg3ODIsImlhdCI6MTczMjE0NTE4MiwiaXNzIjoibWFnaXN0cmFsYS5hdXRoIiwidHlwZSI6MCwidXNlciI6IjcyYjcyMTZiLWNmODktNDdlNS04ZDlhLWJlNGYwMzkyNTJmZiJ9.baSQ_lRLUsrP6jqF1AZHk_ryxxe7org3txWg90_sSG2_SrybaYxFxU4Ygp19lNXs52NgNuL6ln_fLkvazEOR6A"; +const token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzIxOTM5NTAsImlhdCI6MTczMjE5MDM1MCwiaXNzIjoibWFnaXN0cmFsYS5hdXRoIiwidHlwZSI6MCwidXNlciI6IjcyYjcyMTZiLWNmODktNDdlNS04ZDlhLWJlNGYwMzkyNTJmZiJ9.uVCIfSvZJsz-EAp2mIBrIZChGyDa8gcrpcwT8Ionq0LZd3OSlqkWM1cT7CLPXuzaMwNmSToKNvePP340yM4mOA"; -mySdk.clients - .CreateClient({ name: "" }, domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .CreateClient({ name: "Arya" }, domainId, token) +// .then((response: any) => { +// console.log("response:", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .Disable("", domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .Disable("", domainId, token) +// .then((response: any) => { +// console.log("response:", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .Enable("", domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .Enable("", domainId, token) +// .then((response: any) => { +// console.log("response:", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .UpdateClient({ id: "", name: "Client 1" }, domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .UpdateClient({ id: "", name: "Client 1" }, domainId, token) +// .then((response: any) => { +// console.log("response:", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .UpdateClientSecret({ id: "", credentials: { secret: "newSecret" } }, domainId, token) - .then((response: any) => { - console.log(response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .UpdateClientSecret({ id: "", credentials: { secret: "newSecret" } }, domainId, token) +// .then((response: any) => { +// console.log(response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .UpdateClientTags( - { id: "", tags: ["", ""] }, - domainId, - token, - ) - .then((response: any) => { - console.log(response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .UpdateClientTags( +// { id: "", tags: ["", ""] }, +// domainId, +// token, +// ) +// .then((response: any) => { +// console.log(response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .Clients({ offset: 0, limit: 10 }, domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .Clients({ offset: 0, limit: 10 }, domainId, token) +// .then((response: any) => { +// console.log("response:", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .Client("", domainId, token) - .then((response: any) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .Client("", domainId, token) +// .then((response: any) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .DeleteClient("", domainId, token) - .then((response: any) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .DeleteClient("", domainId, token) +// .then((response: any) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); mySdk.clients .ListUserClients( - "", + "8bd6c0e0-f4e7-4fb5-87f0-3c292415f717", { offset: 0, limit: 10 }, domainId, token, @@ -112,174 +112,174 @@ mySdk.clients console.log(error); }); -mySdk.clients.ClientParents(domainId, "", "", token) - .then((response: any) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients.ClientParents(domainId, "", "", token) +// .then((response: any) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients.DeleteClientParents(domainId, "", token) - .then((response: any) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients.DeleteClientParents(domainId, "", token) +// .then((response: any) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .CreateClients([{ name: "" }, { name: "" }], domainId, token) - .then((response: any) => { - console.log("response:", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .CreateClients([{ name: "" }, { name: "" }], domainId, token) +// .then((response: any) => { +// console.log("response:", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .CreateClientRole("", "", domainId, token) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .CreateClientRole("ef388650-d06a-4d37-b2e5-b25e98d88abb", "Essos", domainId, token) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .ListClientRoles("", domainId, { offset: 0, limit: 10 }, token) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .ListClientRoles("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, { offset: 0, limit: 10 }, token) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .ViewClientRole("", domainId, "", token) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .ViewClientRole("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Wicked", token) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .UpdateClientRole( - "", - domainId, - "", - { name: "" }, - token - ) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .UpdateClientRole( +// "ef388650-d06a-4d37-b2e5-b25e98d88abb", +// domainId, +// "Wicked", +// { name: "Westeros" }, +// token +// ) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .DeleteClientRole("2bf128e4-a1f6-401b-95ce-556d436ecebc", domainId, "", token) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .DeleteClientRole("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Essos", token) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .AddClientRoleActions( - "", - domainId, - "", - ["", ""], - token - ) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .AddClientRoleActions( +// "ef388650-d06a-4d37-b2e5-b25e98d88abb", +// domainId, +// "Westeros", +// ["read", "update"], +// token +// ) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .ListClientRoleActions("", domainId, "", token) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .ListClientRoleActions("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Westeros", token) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .DeleteClientRoleActions( - "", - domainId, - "", - ["", ""], - token - ) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .DeleteClientRoleActions( +// "ef388650-d06a-4d37-b2e5-b25e98d88abb", +// domainId, +// "Westeros", +// ["read"], +// token +// ) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .DeleteAllClientRoleActions("", domainId, "", token) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .DeleteAllClientRoleActions("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Westeros", token) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .AddClientRoleMembers("", domainId, "", ["", ""], token) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .AddClientRoleMembers("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Westeros", ["2144a485-e52e-4b78-9aca-9e9868aa5948"], token) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .ListClientRoleMembers( - "", - domainId, - "", - { offset: 0, limit: 10 }, - token - ) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .ListClientRoleMembers( +// "ef388650-d06a-4d37-b2e5-b25e98d88abb", +// domainId, +// "Westeros", +// { offset: 0, limit: 10 }, +// token +// ) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .DeleteClientRoleMembers( - "", - domainId, - "", - ["", ""], - token - ) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .DeleteClientRoleMembers( +// "ef388650-d06a-4d37-b2e5-b25e98d88abb", +// domainId, +// "Westeros", +// ["2144a485-e52e-4b78-9aca-9e9868aa5948"], +// token +// ) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); -mySdk.clients - .DeleteAllClientRoleMembers("", domainId, "", token) - .then((response) => { - console.log("response: ", response); - }) - .catch((error) => { - console.error(error); - }); +// mySdk.clients +// .DeleteAllClientRoleMembers("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Westeros", token) +// .then((response) => { +// console.log("response: ", response); +// }) +// .catch((error) => { +// console.error(error); +// }); diff --git a/src/clients.ts b/src/clients.ts index e27732cd..d83c381f 100644 --- a/src/clients.ts +++ b/src/clients.ts @@ -10,19 +10,11 @@ import type { BasicPageMeta, } from "./defs"; +/** + * @class Clients - + * Handles interactions with the clients API, including creating, updating, and managing clients, roles, and permissions. + */ export default class Clients { - // Clients service client. - /** - @class Clients - private Clients_url: URL; - content_type: string; - ClientsEndpoint: string; - // - //Clients API is used for creating and managing Clients. - //It is used for creating, updating, deleting and retrieving Clients. - //@param {string} clients_url - Clients service URL. - //@returns {Object} - Clients service client. - */ private readonly clientsUrl: URL; private readonly usersUrl?: URL; @@ -33,6 +25,13 @@ export default class Clients { private readonly clientRoles: Roles; + /** + * @constructor + * 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, usersUrl, @@ -52,8 +51,7 @@ export default class Clients { } /** - * @method CreateClient - * Creates a new client. + * @method CreateClient - Creates a new client. * @param {Object} Client - Client object containing details like name and metadata. * @param {string} domainId - The unique ID of the domain. * @param {stringtring} token - Authorization token. @@ -94,8 +92,7 @@ export default class Clients { } /** - * @method CreateClients - * Creates multiple new clients. + * @method CreateClients - Creates multiple new clients. * @param {Client[]} clients - An array of client objects, each containing details like name, metadata, and tags. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. @@ -113,7 +110,7 @@ export default class Clients { "Content-Type": this.contentType, Authorization: `Bearer ${token}`, }, - body: JSON.stringify(Clients), + body: JSON.stringify(clients), }; try { const response = await fetch( @@ -135,20 +132,19 @@ export default class Clients { } } + /** + * @method Enable - Enables a previously disabled client by its ID. + * @param {string} clientID - The unique ID of the client. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} - The updated client object with enabled status. + * @throws {Error} If the client cannot be enabled. + */ public async Enable( clientId: string, domainId: string, token: string ): Promise { - // Enables a Client. - /** - * @method Enable - Enables a previously disabled Client when provided with a valid token and Client ID. - * @param {string} clientID - client ID. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Returns updated Client. - */ - const options: RequestInit = { method: "POST", headers: { @@ -176,20 +172,19 @@ export default class Clients { } } + /** + * @method Disable - Disables an enabled client by its ID.. + * @param {string} ClientId - The unique ID of the client. + * @param {string} domainId -The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Object} - The updated client object with disabled status. + * @throws {Error} If the group cannot be disabled. + */ public async Disable( ClientId: string, domainId: string, token: string ): Promise { - // Disables Client. - /** - * @method Disable - Disables a Client when provided with a valid token, domain ID and Client ID. - * @param {string} ClientId - Client ID. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Returns disabled Client - */ - const options: RequestInit = { method: "POST", headers: { @@ -482,9 +477,9 @@ export default class Clients { domainId: string, token: string ): Promise { - const stringParams: Record = Object.fromEntries( - Object.entries(queryParams).map(([key, value]) => [key, String(value)]) - ); + // const stringParams: Record = Object.fromEntries( + // Object.entries(queryParams).map(([key, value]) => [key, String(value)]) + // ); const options: RequestInit = { method: "GET", headers: { @@ -495,11 +490,12 @@ export default class Clients { try { const response = await fetch( new URL( - `${domainId}/users/${userId}/clients?${new URLSearchParams(stringParams).toString()}`, + `${domainId}/users/${userId}/clients`, this.usersUrl ).toString(), options ); + console.log("url", response.url); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); From 3409a780f0f0fdc01bee94ad08df9b43bd34808a Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Thu, 21 Nov 2024 17:39:23 +0300 Subject: [PATCH 03/12] fix tests Signed-off-by: wambuipixel --- src/clients.ts | 237 +++++++++++++++--------------------------- tests/clients.test.ts | 192 ++++++++++++++++++++++++++++++++++ tests/users.test.ts | 18 ++-- 3 files changed, 285 insertions(+), 162 deletions(-) diff --git a/src/clients.ts b/src/clients.ts index d83c381f..79c7b91a 100644 --- a/src/clients.ts +++ b/src/clients.ts @@ -177,11 +177,11 @@ export default class Clients { * @param {string} ClientId - The unique ID of the client. * @param {string} domainId -The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Object} - The updated client object with disabled status. + * @returns {Promise} - The updated client object with disabled status. * @throws {Error} If the group cannot be disabled. */ public async Disable( - ClientId: string, + clientId: string, domainId: string, token: string ): Promise { @@ -195,7 +195,7 @@ export default class Clients { try { const response = await fetch( new URL( - `${domainId}/${this.clientsEndpoint}/${ClientId}/disable`, + `${domainId}/${this.clientsEndpoint}/${clientId}/disable`, this.clientsUrl ).toString(), options @@ -211,46 +211,31 @@ export default class Clients { } } + /** + * @method UpdateClient - Updates the information of an existing client. + * @param {Client} client- The client object. + * @param {string} domainId - The unique identifier of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} - The updated channel object. + * @throws {Error} If the channel cannot be updated. + */ public async UpdateClient( - Client: Client, + client: Client, domainId: string, token: string ): Promise { - // Updates Client. - /** - * @method Update - Updates Client when provided with a valid token, - * domain ID and Client object. - * @param {Object} Client - Client object. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Client object. - * @example - * const Client = { - * "name": "Client3", - * "tags": [ - * "tag1" - * ], - * "credentials": { - * "identity": "Clientidentity", - * "secret":"12345678" - * }, - * "owner": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * "id": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * } - */ - const options: RequestInit = { method: "PATCH", headers: { "Content-Type": this.contentType, Authorization: `Bearer ${token}`, }, - body: JSON.stringify(Client), + body: JSON.stringify(client), }; try { const response = await fetch( new URL( - `${domainId}/${this.clientsEndpoint}/${Client.id}`, + `${domainId}/${this.clientsEndpoint}/${client.id}`, this.clientsUrl ).toString(), options @@ -259,53 +244,38 @@ export default class Clients { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const ClientData: Client = await response.json(); - return ClientData; + const clientData: Client = await response.json(); + return clientData; } catch (error) { throw error; } } + /** + * @method UpdateClientSecret - Updates an existing client's secret. + * @param {string} domainId - The unique ID of the domain.. + * @param {Client} client - Client object with updated properties. + * @param {string} token - Authorization token.. + * @returns {Promise } - The updated client object. + * @throws {Error} If the client secret cannot be updated. + */ public async UpdateClientSecret( - Client: Client, + client: Client, domainId: string, token: string ): Promise { - // Updates Client secret. - /** - * @method UpdateClientSecret - Updates Client secret when provided with a valid token and domain ID, - * domain ID and Client object. - * @param {string} Client_id - Client ID. - * @param {Object} Client - Client object. - * @param {string} token - User token. - * @returns {Object} - Client object. - * @example - * const Client = { - * "name": "Client3", - * "tags": [ - * "tag1" - * ], - * "credentials": { - * "identity": "Clientidentity", - * "secret":"56788912" - * }, - * "owner": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * "id": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * } - */ - const options: RequestInit = { method: "PATCH", headers: { "Content-Type": this.contentType, Authorization: `Bearer ${token}`, }, - body: JSON.stringify({ secret: Client.credentials?.secret }), + body: JSON.stringify({ secret: client.credentials?.secret }), }; try { const response = await fetch( new URL( - `${domainId}/${this.clientsEndpoint}/${Client.id}/secret`, + `${domainId}/${this.clientsEndpoint}/${client.id}/secret`, this.clientsUrl ).toString(), options @@ -314,55 +284,39 @@ export default class Clients { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const ClientData: Client = await response.json(); - return ClientData; + const clientData: Client = await response.json(); + return clientData; } catch (error) { throw error; } } + /** + * @method UpdateClientTags - Updates an existing client's tags. + * @param {client} Client - Client object with updated properties. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} - The updated client object. + * @throws {Error} If the client tags cannot be updated. + */ public async UpdateClientTags( - Client: Client, + client: Client, domainId: string, token: string ): Promise { - // Updates Client tags. - /** - * @method UpdateClientTags - Updates Client tags when provided with a valid token, - * domain ID and Client object. - * - * @param {Object} Client - Client object. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - Client object. - * @example - * const Client = { - * "name": "Client3", - * "tags": [ - * "tag1" - * ], - * "credentials": { - * "identity": "Clientidentity", - * "secret":"56788912" - * }, - * "owner": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * "id": "bb7edb32-2eac-4aad-aebe-ed96fe073879", - * } - */ - const options: RequestInit = { method: "PATCH", headers: { "Content-Type": this.contentType, Authorization: `Bearer ${token}`, }, - body: JSON.stringify(Client), + body: JSON.stringify(client), }; try { const response = await fetch( new URL( - `${domainId}/${this.clientsEndpoint}/${Client.id}/tags`, + `${domainId}/${this.clientsEndpoint}/${client.id}/tags`, this.clientsUrl ).toString(), options @@ -371,32 +325,26 @@ export default class Clients { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const ClientData: Client = await response.json(); - return ClientData; + const clientData: Client = await response.json(); + return clientData; } catch (error) { throw error; } } + /** + * @method Client - Retrieves a client by its id. + * @param {string} clientId - The unique ID of the client. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} - The requested client object. + * @throws {Error} If the client cannot be fetched. + */ public async Client( clientId: string, domainId: string, token: string ): Promise { - // Gets a Client - /** - * Provides information about the Client with provided ID. The Client is - * retrieved using authorization token. - * @method Client - Gets a Client. - * @param {String} ClientId - Client ID. - * @param {string} domainId - The Domain ID. - * @param {String} token - Access token. - * @returns {Object} - Client object. - * @example - * const ClientId = "886b4266-77d1-4258-abae-2931fb4f16de" - * - */ - const options: RequestInit = { method: "GET", headers: { @@ -425,13 +373,12 @@ export default class Clients { } /** - * @method Clients - * Retrieves all clients 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} - A page of clients. - * @throws {Error} If the clients cannot be fetched. + * @method Clients - Retrieves all clients 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} - A page of clients. + * @throws {Error} If the clients cannot be fetched. */ public async Clients( queryParams: PageMetadata, @@ -508,14 +455,13 @@ export default class Clients { } /** - * @method ClientParents - * Sets parent to a channel. + * @method ClientParents - Sets parent to a client. * @param {string} domainId - The unique ID of the domain. - * @param {string} clientlId - The unique ID of the channel to be updated. + * @param {string} clientlId - The unique ID of the client 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} - 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. + * @returns {Promise} - A promise that resolves when the parent group is successfully set for the specified client. + * @throws {Error} If the parent group cannot be set for the client. */ public async ClientParents(domainId: string, clientlId: string, parentGroupId: string, token: string) : Promise { const options: RequestInit = { @@ -543,13 +489,12 @@ export default class Clients { } /** - * @method DeleteClientParents - * Removes the parent group from a specified client. + * @method DeleteClientParents - Removes the parent group from a specified client. * @param {string} domainId - The unique ID of the domain. * @param {string} clientId - The unique ID of the client. * @param {string} token - Authorization token. - * @returns {Promise} - 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. + * @returns {Promise} - A promise that resolves when the parent group is successfully removed from the specified client. + * @throws {Error} If the parent group cannot removed from the client. */ public async DeleteClientParents(domainId: string, clientId: string, token: string) : Promise { const options: RequestInit = { @@ -579,19 +524,19 @@ export default class Clients { } } + /** + * @method DeleteClient - Deletes a client with specified id.. + * @param {string} clientId - The unique ID of the client. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} - A promise that resolves when the client is deleted. + * @throws {Error} If the client cannot be deleted. + */ public async DeleteClient( ClientId: string, domainId: string, token: string ): Promise { - // Deletes a Client. - /** - * @method DeleteClient - Deletes a Client. - * @param {string} ClientId - Client ID. - * @param {string} domainId - The Domain ID. - * @param {string} token - User token. - * @returns {Object} - NoClient - * */ const options: RequestInit = { method: "DELETE", headers: { @@ -623,8 +568,7 @@ export default class Clients { } /** - * @method CreateClientRole - * Creates a new role within a specific client. + * @method CreateClientRole - Creates a new role within a specific client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The name of the role to create. * @param {string} token - Authorization token. @@ -658,8 +602,7 @@ export default class Clients { } /** - * @method ListClientRoles - * Lists all roles within a specific client. + * @method ListClientRoles - Lists all roles within a specific client. * @param {string} clientId - The unique identifier of the client. * @param {PageMetadata} queryParams - Metadata for pagination or filters. * @param {string} token - Authorization token. @@ -687,8 +630,7 @@ export default class Clients { } /** - * @method ViewClientRole - * Retrieves details about a specific role in a client. + * @method ViewClientRole - Retrieves details about a specific role in a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. @@ -716,8 +658,7 @@ export default class Clients { } /** - * @method UpdateClientRole - * Updates the details of a specific role in a client. + * @method UpdateClientRole - Updates the details of a specific role in a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {Role} role - The role to be updated. @@ -748,9 +689,7 @@ export default class Clients { } /** - * Deletes a specific role from a client. - * - * @function DeleteClientRole + * @method DeleteClientRole - Deletes a specific role from a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. @@ -778,8 +717,7 @@ export default class Clients { } /** - * @method AddClientRoleActions - * Adds actions to a specific role in a client. + * @method AddClientRoleActions - Adds actions to a specific role in a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. @@ -810,8 +748,7 @@ export default class Clients { } /** - * @method ListClientRoleActions - * Lists all actions associated with a specific role in a client. + * @method ListClientRoleActions - Lists all actions associated with a specific role in a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. @@ -839,8 +776,7 @@ export default class Clients { } /** - * @method DeleteClientRoleActions - * Deletes specific actions from a role in a client. + * @method DeleteClientRoleActions - Deletes specific actions from a role in a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {string[]} actions - The actions to delete from the role. @@ -871,8 +807,7 @@ export default class Clients { } /** - * @method DeleteAllClientRoleActions - * Deletes all actions associated with a specific role in a client. + * @method DeleteAllClientRoleActions - Deletes all actions associated with a specific role in a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. @@ -900,8 +835,7 @@ export default class Clients { } /** - * @method AddClientRoleMembers - * Adds members to a specific role in a client. + * @method AddClientRoleMembers - Adds members to a specific role in a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {string[]} members - The IDs of the members to add. @@ -932,8 +866,7 @@ export default class Clients { } /** - * @method ListClientRoleMembers - * Lists all members associated with a specific role in a client. + * @method ListClientRoleMembers - Lists all members associated with a specific role in a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. @@ -963,8 +896,7 @@ export default class Clients { } /** - * @method DeleteClientRoleMembers - * Deletes specific members from a role in a client. + * @method DeleteClientRoleMembers - Deletes specific members from a role in a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {string[]} members - The IDs of the members to delete. @@ -995,8 +927,7 @@ export default class Clients { } /** - * @method DeleteAllClientRoleMembers - * Deletes all members associated with a specific role in a client. + * @method DeleteAllClientRoleMembers - Deletes all members associated with a specific role in a client. * @param {string} clientId - The unique identifier of the client. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. diff --git a/tests/clients.test.ts b/tests/clients.test.ts index d684299a..f3ddec16 100644 --- a/tests/clients.test.ts +++ b/tests/clients.test.ts @@ -62,6 +62,10 @@ describe("Clients", () => { const userId = "bb7edb32-2eac-4aad-aebe-ed96fe073879"; const domainId = "886b4266-77d1-4258-abae-2931fb4f16de"; const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjU3OTMwNjksImlhdCI6"; + const roleName = "editor"; + const actions = ["read", "write"]; + const members = ["user1", "user2"]; + const role = { name: roleName, actions, members }; beforeEach(() => { fetchMock.resetMocks(); @@ -152,4 +156,192 @@ describe("Clients", () => { const response = await sdk.clients.DeleteClient(clientId, domainId, token); expect(response).toEqual(deleteResponse); }); + + test("CreateClientRole should create a new role and return it", async () => { + fetchMock.mockResponseOnce(JSON.stringify(role)); + + const response = await sdk.clients.CreateClientRole( + clientId, + roleName, + domainId, + token, + actions, + members + ); + expect(response).toEqual(role); + }); + + test("ListClientRoles should return a page of roles", async () => { + const rolesPage = { roles: [role], total: 1, offset: 0, limit: 10 }; + fetchMock.mockResponseOnce(JSON.stringify(rolesPage)); + + const response = await sdk.clients.ListClientRoles( + clientId, + domainId, + { offset: 0, limit: 10 }, + token + ); + expect(response).toEqual(rolesPage); + }); + + test("ViewClientRole should return details of a specific role", async () => { + fetchMock.mockResponseOnce(JSON.stringify(role)); + + const response = await sdk.clients.ViewClientRole( + clientId, + domainId, + roleName, + token + ); + expect(response).toEqual(role); + }); + + test("UpdateClientRole should update a role and return the updated role", async () => { + const updatedRole = { ...role, actions: [...role.actions, "execute"] }; + fetchMock.mockResponseOnce(JSON.stringify(updatedRole)); + + const response = await sdk.clients.UpdateClientRole( + clientId, + domainId, + roleName, + updatedRole, + token + ); + expect(response).toEqual(updatedRole); + }); + + test("DeleteClientRole should delete a role and return success response", async () => { + const successResponse = { + status: 200, + message: "Role deleted successfully", + }; + fetchMock.mockResponseOnce(JSON.stringify(successResponse)); + + const response = await sdk.clients.DeleteClientRole( + clientId, + domainId, + roleName, + token + ); + expect(response).toEqual(successResponse); + }); + + test("AddClientRoleActions should add actions to a role and return updated actions", async () => { + const updatedActions = [...actions, "execute"]; + fetchMock.mockResponseOnce(JSON.stringify(updatedActions)); + + const response = await sdk.clients.AddClientRoleActions( + clientId, + domainId, + roleName, + ["execute"], + token + ); + expect(response).toEqual(updatedActions); + }); + + test("ListClientRoleActions should return actions of a specific role", async () => { + fetchMock.mockResponseOnce(JSON.stringify(actions)); + + const response = await sdk.clients.ListClientRoleActions( + clientId, + domainId, + roleName, + token + ); + expect(response).toEqual(actions); + }); + + test("DeleteClientRoleActions should remove actions from a role and return success response", async () => { + const successResponse = { + status: 200, + message: "Role actions deleted successfully", + }; + fetchMock.mockResponseOnce(JSON.stringify(successResponse)); + + const response = await sdk.clients.DeleteClientRoleActions( + clientId, + domainId, + roleName, + ["write"], + token + ); + expect(response).toEqual(successResponse); + }); + + test("DeleteAllClientRoleActions should remove all actions from a role and return success response", async () => { + const successResponse = { + status: 200, + message: "Role actions deleted successfully", + }; + fetchMock.mockResponseOnce(JSON.stringify(successResponse)); + + const response = await sdk.clients.DeleteAllClientRoleActions( + clientId, + domainId, + roleName, + token + ); + expect(response).toEqual(successResponse); + }); + + test("AddClientRoleMembers should add members to a role and return updated members", async () => { + const updatedMembers = [...members, "user3"]; + fetchMock.mockResponseOnce(JSON.stringify(updatedMembers)); + + const response = await sdk.clients.AddClientRoleMembers( + clientId, + domainId, + roleName, + ["user3"], + token + ); + expect(response).toEqual(updatedMembers); + }); + + test("ListClientRoleMembers should return members of a specific role", async () => { + fetchMock.mockResponseOnce(JSON.stringify(members)); + + const response = await sdk.clients.ListClientRoleMembers( + clientId, + domainId, + roleName, + { offset: 0, limit: 10 }, + token + ); + expect(response).toEqual(members); + }); + + test("DeleteClientRoleMembers should remove members from a role and return success response", async () => { + const successResponse = { + status: 200, + message: "Role members deleted successfully", + }; + fetchMock.mockResponseOnce(JSON.stringify(successResponse)); + + const response = await sdk.clients.DeleteClientRoleMembers( + clientId, + domainId, + roleName, + ["user1"], + token + ); + expect(response).toEqual(successResponse); + }); + + test("DeleteAllClientRoleMembers should remove all members from a role and return success response", async () => { + const successResponse = { + status: 200, + message: "Role members deleted successfully", + }; + fetchMock.mockResponseOnce(JSON.stringify(successResponse)); + + const response = await sdk.clients.DeleteAllClientRoleMembers( + clientId, + domainId, + roleName, + token + ); + expect(response).toEqual(successResponse); + }); }); diff --git a/tests/users.test.ts b/tests/users.test.ts index 0034a07a..fb3d015b 100644 --- a/tests/users.test.ts +++ b/tests/users.test.ts @@ -7,8 +7,8 @@ import type { Login, Group, GroupsPage, - Thing, - ThingsPage, + Client, + ClientsPage, Channel, Token, } from "../src/sdk"; @@ -80,14 +80,14 @@ describe("Users", () => { limit: 10, }; - const thing: Thing = { + const client: Client = { id: "886b4266-77d1-4258-abae-2931fb4f16de", name: "fkatwigs", domain_id: "886b4266-77d1-4258-abae-2931fb4f16de", }; - const ThingsPage: ThingsPage = { - things: [thing], + const clientsPage: ClientsPage = { + clients: [client], total: 1, offset: 0, limit: 10, @@ -238,16 +238,16 @@ describe("Users", () => { expect(response).toEqual(GroupsPage); }); - test("list user things should return a list of things associated with a user and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(ThingsPage)); + test("list user clients should return a list of clients associated with a user and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(clientsPage)); - const response = await sdk.users.ListUserThings( + const response = await sdk.users.ListUserClients( domainId, userId, queryParams, token, ); - expect(response).toEqual(ThingsPage); + expect(response).toEqual(clientsPage); }); test("list user channels should return a list of channels associated with a user and return success", async () => { From 7a6ccf64d4ec6278a5f49c7e74dbfd5d990842b0 Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Thu, 21 Nov 2024 19:16:38 +0300 Subject: [PATCH 04/12] remove unused file Signed-off-by: wambuipixel --- examples/client/listThings.html | 35 --- examples/clients.ts | 482 ++++++++++++++++---------------- src/clients.ts | 82 +++--- src/sdk.ts | 2 +- tests/clients.test.ts | 35 +-- 5 files changed, 308 insertions(+), 328 deletions(-) delete mode 100644 examples/client/listThings.html diff --git a/examples/client/listThings.html b/examples/client/listThings.html deleted file mode 100644 index c00431dc..00000000 --- a/examples/client/listThings.html +++ /dev/null @@ -1,35 +0,0 @@ - - - - List Things Console - - -

List things Console

- - - - - - diff --git a/examples/clients.ts b/examples/clients.ts index d2c33fa6..07d1b77b 100644 --- a/examples/clients.ts +++ b/examples/clients.ts @@ -5,102 +5,101 @@ const defaultUrl = "http://localhost"; const mySdk = new SDK({ clientsUrl: `${defaultUrl}:9006`, - usersUrl: `${defaultUrl}:9002`, }); -// clients.ts examples. +// Clients.ts examples. -const domainId = "f86fe07c-90dd-4bfc-95b7-6311e2d908ba"; -const token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzIxOTM5NTAsImlhdCI6MTczMjE5MDM1MCwiaXNzIjoibWFnaXN0cmFsYS5hdXRoIiwidHlwZSI6MCwidXNlciI6IjcyYjcyMTZiLWNmODktNDdlNS04ZDlhLWJlNGYwMzkyNTJmZiJ9.uVCIfSvZJsz-EAp2mIBrIZChGyDa8gcrpcwT8Ionq0LZd3OSlqkWM1cT7CLPXuzaMwNmSToKNvePP340yM4mOA"; +const token = ""; +const domainId = ""; -// mySdk.clients -// .CreateClient({ name: "Arya" }, domainId, token) -// .then((response: any) => { -// console.log("response:", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .CreateClient({ name: "" }, domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .Disable("", domainId, token) -// .then((response: any) => { -// console.log("response:", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .Disable("", domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .Enable("", domainId, token) -// .then((response: any) => { -// console.log("response:", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .Enable("", domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .UpdateClient({ id: "", name: "Client 1" }, domainId, token) -// .then((response: any) => { -// console.log("response:", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .UpdateClient({ id: "", name: "" }, domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .UpdateClientSecret({ id: "", credentials: { secret: "newSecret" } }, domainId, token) -// .then((response: any) => { -// console.log(response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .UpdateClientSecret({ id: "", credentials: { secret: "newSecret" } }, domainId, token) + .then((response: any) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .UpdateClientTags( -// { id: "", tags: ["", ""] }, -// domainId, -// token, -// ) -// .then((response: any) => { -// console.log(response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .UpdateClientTags( + { id: "", tags: ["", ""] }, + domainId, + token, + ) + .then((response: any) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .Clients({ offset: 0, limit: 10 }, domainId, token) -// .then((response: any) => { -// console.log("response:", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .Clients({ offset: 0, limit: 10 }, domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .Client("", domainId, token) -// .then((response: any) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .Client("", domainId, token) + .then((response: any) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .DeleteClient("", domainId, token) -// .then((response: any) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .DeleteClient("", domainId, token) + .then((response: any) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); mySdk.clients .ListUserClients( - "8bd6c0e0-f4e7-4fb5-87f0-3c292415f717", + "", { offset: 0, limit: 10 }, domainId, token, @@ -112,174 +111,183 @@ mySdk.clients console.log(error); }); -// mySdk.clients.ClientParents(domainId, "", "", token) -// .then((response: any) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients.ClientParents(domainId, "", "", token) + .then((response: any) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients.DeleteClientParents(domainId, "", token) -// .then((response: any) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients.DeleteClientParents(domainId, "", token) + .then((response: any) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); + +mySdk.clients + .CreateClients([{ name: "" }, { name: "" }], domainId, token) + .then((response: any) => { + console.log("response:", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .CreateClients([{ name: "" }, { name: "" }], domainId, token) -// .then((response: any) => { -// console.log("response:", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .ListClientActions(domainId, token) + .then((response: any) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .CreateClientRole("ef388650-d06a-4d37-b2e5-b25e98d88abb", "Essos", domainId, token) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .CreateClientRole("", "", domainId, token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .ListClientRoles("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, { offset: 0, limit: 10 }, token) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .ListClientRoles("", domainId, { offset: 0, limit: 10 }, token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .ViewClientRole("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Wicked", token) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .ViewClientRole("", domainId, "", token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .UpdateClientRole( -// "ef388650-d06a-4d37-b2e5-b25e98d88abb", -// domainId, -// "Wicked", -// { name: "Westeros" }, -// token -// ) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .UpdateClientRole( + "", + domainId, + "", + { name: "" }, + token + ) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .DeleteClientRole("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Essos", token) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .DeleteClientRole("", domainId, "", token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .AddClientRoleActions( -// "ef388650-d06a-4d37-b2e5-b25e98d88abb", -// domainId, -// "Westeros", -// ["read", "update"], -// token -// ) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .AddClientRoleActions( + "", + domainId, + "", + ["roleAction1", "roleAction2"], + token + ) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .ListClientRoleActions("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Westeros", token) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .ListClientRoleActions("", domainId, "", token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .DeleteClientRoleActions( -// "ef388650-d06a-4d37-b2e5-b25e98d88abb", -// domainId, -// "Westeros", -// ["read"], -// token -// ) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .DeleteClientRoleActions( + "", + domainId, + "", + ["roleAction1", "roleAction2"], + token + ) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .DeleteAllClientRoleActions("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Westeros", token) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .DeleteAllClientRoleActions("", domainId, "", token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .AddClientRoleMembers("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Westeros", ["2144a485-e52e-4b78-9aca-9e9868aa5948"], token) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .AddClientRoleMembers("", domainId, "", ["", ""], token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .ListClientRoleMembers( -// "ef388650-d06a-4d37-b2e5-b25e98d88abb", -// domainId, -// "Westeros", -// { offset: 0, limit: 10 }, -// token -// ) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .ListClientRoleMembers( + "", + domainId, + "", + { offset: 0, limit: 10 }, + token + ) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .DeleteClientRoleMembers( -// "ef388650-d06a-4d37-b2e5-b25e98d88abb", -// domainId, -// "Westeros", -// ["2144a485-e52e-4b78-9aca-9e9868aa5948"], -// token -// ) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .DeleteClientRoleMembers( + "", + domainId, + "", + ["", ""], + token + ) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); -// mySdk.clients -// .DeleteAllClientRoleMembers("ef388650-d06a-4d37-b2e5-b25e98d88abb", domainId, "Westeros", token) -// .then((response) => { -// console.log("response: ", response); -// }) -// .catch((error) => { -// console.error(error); -// }); +mySdk.clients + .DeleteAllClientRoleMembers("", domainId, "", token) + .then((response) => { + console.log("response: ", response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/src/clients.ts b/src/clients.ts index 79c7b91a..dbd1877a 100644 --- a/src/clients.ts +++ b/src/clients.ts @@ -17,8 +17,6 @@ import type { export default class Clients { private readonly clientsUrl: URL; - private readonly usersUrl?: URL; - private readonly contentType: string; private readonly clientsEndpoint: string; @@ -30,21 +28,13 @@ 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, - 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(); @@ -92,12 +82,12 @@ export default class Clients { } /** - * @method CreateClients - Creates multiple new clients. - * @param {Client[]} clients - An array of client 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} - A page of clients. - * @throws {Error} If the clients cannot be created. + * @method CreateClients - Creates multiple new clients. + * @param {Client[]} clients - An array of client 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} - A page of clients. + * @throws {Error} If the clients cannot be created. */ public async CreateClients( clients: Client[], @@ -120,7 +110,6 @@ export default class Clients { ).toString(), options ); - console.log("url", response.url); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); @@ -133,12 +122,12 @@ export default class Clients { } /** - * @method Enable - Enables a previously disabled client by its ID. - * @param {string} clientID - The unique ID of the client. - * @param {string} domainId - The unique ID of the domain. - * @param {string} token - Authorization token. - * @returns {Promise} - The updated client object with enabled status. - * @throws {Error} If the client cannot be enabled. + * @method Enable - Enables a previously disabled client by its ID. + * @param {string} clientID - The unique ID of the client. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} - The updated client object with enabled status. + * @throws {Error} If the client cannot be enabled. */ public async Enable( clientId: string, @@ -411,22 +400,31 @@ 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 clientsData: ClientsPage = await response.json(); + return clientsData; } catch (error) { throw error; } } + /** + * @method ListUserClients - Retrieves all clients that match the specified query parameters for the given userId. + * @param {string} userId - The unique ID of the user. + * @param {PageMetadata} queryParams - Query parameters for the request. + * @param {string} domainId - The unique ID of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} - A page of clients. + * @throws {Error} If the clients cannot be fetched. + */ public async ListUserClients( userId: string, queryParams: PageMetadata, domainId: string, token: string ): Promise { - // const stringParams: Record = Object.fromEntries( - // Object.entries(queryParams).map(([key, value]) => [key, String(value)]) - // ); + const stringParams: Record = Object.fromEntries( + Object.entries(queryParams).map(([key, value]) => [key, String(value)]) + ); const options: RequestInit = { method: "GET", headers: { @@ -437,12 +435,13 @@ export default class Clients { try { const response = await fetch( new URL( - `${domainId}/users/${userId}/clients`, - this.usersUrl + `${domainId}/users/${userId}/clients?${new URLSearchParams( + stringParams + ).toString()}`, + this.clientsUrl ).toString(), options ); - console.log("url", response.url); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); @@ -512,7 +511,6 @@ export default class Clients { ).toString(), options ); - console.log("url", response.url); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); @@ -567,6 +565,26 @@ export default class Clients { } } + /** + * @method ListClientActions - Lists all actions available to a specific client. + * @param {string} domainId - The unique identifier of the domain. + * @param {string} token - Authorization token. + * @returns {Promise} A promise that resolves with an array of actions. + * @throws {Error} If client actions cannot be fetched. + */ + public async ListClientActions(domainId: string, token: string): Promise { + try { + const actions: string[] = await this.clientRoles.ListAvailableActions( + this.clientsUrl, + `${domainId}/${this.clientsEndpoint}`, + token, + ); + return actions; + } catch (error) { + throw error; + } + } + /** * @method CreateClientRole - Creates a new role within a specific client. * @param {string} clientId - The unique identifier of the client. diff --git a/src/sdk.ts b/src/sdk.ts index 56eb6314..c2c374e6 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -109,7 +109,7 @@ class SDK { }: SDKConfig = {}) { this.users = new Users({ usersUrl, clientsUrl }); this.domains = new Domains({ domainsUrl, usersUrl }); - this.clients = new Clients({ clientsUrl, usersUrl }); + this.clients = new Clients({ clientsUrl }); this.certs = new Certs(certsUrl); this.groups = new Groups({ usersUrl, clientsUrl }); this.channels = new Channels({ channelsUrl }); diff --git a/tests/clients.test.ts b/tests/clients.test.ts index f3ddec16..164ad773 100644 --- a/tests/clients.test.ts +++ b/tests/clients.test.ts @@ -2,7 +2,7 @@ import fetchMock, { enableFetchMocks } from "jest-fetch-mock"; import SDK from "../src/sdk"; import type { - Client, ClientsPage, UsersPage, User, + Client, ClientsPage } from "../src/sdk"; enableFetchMocks(); @@ -25,19 +25,6 @@ describe("Clients", () => { }, status: "enabled", }; - const user: User = { - id: "886b4266-77d1-4258-abae-2931fb4f16de", - first_name: "tahliah", - last_name: "barnett", - tags: ["holy", "terrain"], - email: "fkatwigs@email.com", - credentials: { - username: "fkatwigs@email.com", - secret: "12345678", - }, - role: "administrator", - status: "enabled", - }; const clients = [ { name: "client1", id: "bb7edb32-2eac-4aad-aebe-ed96fe073879" }, { name: "client2", id: "bb7edb32-2eac-4aad-aebe-ed96fe073879" }, @@ -46,12 +33,6 @@ describe("Clients", () => { offset: 0, limit: 10, }; - const usersPage: UsersPage = { - users: [user], - total: 2, - offset: 0, - limit: 10, - }; const clientsPage: ClientsPage = { clients: [client], total: 2, @@ -134,8 +115,8 @@ describe("Clients", () => { expect(response).toEqual(clientsPage); }); - test("ListUserClients should list users linked to a client and return success", async () => { - fetchMock.mockResponseOnce(JSON.stringify(usersPage)); + test("ListUserClients should list clients linked to a client and return success", async () => { + fetchMock.mockResponseOnce(JSON.stringify(clientsPage)); const response = await sdk.clients.ListUserClients( userId, @@ -143,7 +124,7 @@ describe("Clients", () => { domainId, token, ); - expect(response).toEqual(usersPage); + expect(response).toEqual(clientsPage); }); test("DeleteClient should delete a client and return success", async () => { @@ -157,6 +138,14 @@ describe("Clients", () => { expect(response).toEqual(deleteResponse); }); + test("ListClientActions should return available actions", async () => { + const availableActions = ["read", "write", "delete"]; + fetchMock.mockResponseOnce(JSON.stringify(availableActions)); + + const response = await sdk.clients.ListClientActions(domainId, token); + expect(response).toEqual(availableActions); + }); + test("CreateClientRole should create a new role and return it", async () => { fetchMock.mockResponseOnce(JSON.stringify(role)); From 3797dec82ad22bc83373b041629978e5ce2f321e Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Thu, 21 Nov 2024 19:25:19 +0300 Subject: [PATCH 05/12] remove console log Signed-off-by: wambuipixel --- src/defs.ts | 2 +- src/roles.ts | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/defs.ts b/src/defs.ts index 511ab162..baa3090d 100644 --- a/src/defs.ts +++ b/src/defs.ts @@ -273,7 +273,7 @@ export interface SenMLMessage { } export interface Cert { - Client_id?: string; + client_id?: string; cert_serial?: string; client_key?: string; client_cert?: string; diff --git a/src/roles.ts b/src/roles.ts index a0760c09..664178ed 100644 --- a/src/roles.ts +++ b/src/roles.ts @@ -65,7 +65,6 @@ export default class Roles { new URL(`${endpoint}/${entityId}/roles`, url).toString(), options, ); - console.log("url", response.url); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); @@ -238,7 +237,6 @@ export default class Roles { ).toString(), options, ); - console.log("url", response.url); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); @@ -273,7 +271,6 @@ export default class Roles { ).toString(), options, ); - console.log("url", response.url); if (!response.ok) { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); From 64a36b9287ad3b1384490634bf11fadb68721628 Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Thu, 21 Nov 2024 19:28:21 +0300 Subject: [PATCH 06/12] replace things with clients in config toml file Signed-off-by: wambuipixel --- config.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.toml b/config.toml index 00d3e8ff..d3f4bbb6 100644 --- a/config.toml +++ b/config.toml @@ -11,7 +11,7 @@ user_token = "" certs_url = "http://localhost:9019" http_adapter_url = "http://localhost/http:9016" reader_url = "http://localhost:9011" - things_url = "http://localhost:9006" + clients_url = "http://localhost:9006" tls_verification = false users_url = "http://localhost:9002" channels_url = "http://localhost:9005" From 3c764da8d5b4dc74376469d60eb9304be3e2f787 Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Thu, 21 Nov 2024 19:30:24 +0300 Subject: [PATCH 07/12] add changeset Signed-off-by: wambuipixel --- .changeset/shy-goats-grin.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shy-goats-grin.md diff --git a/.changeset/shy-goats-grin.md b/.changeset/shy-goats-grin.md new file mode 100644 index 00000000..bb93e4f7 --- /dev/null +++ b/.changeset/shy-goats-grin.md @@ -0,0 +1,5 @@ +--- +"@absmach/magistrala-sdk": patch +--- + +Update clients service to match new auth From 0ad407dba87f8d7cccc257863c7473930f4d65b4 Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Fri, 22 Nov 2024 14:14:28 +0300 Subject: [PATCH 08/12] address requested changes Signed-off-by: wambuipixel --- examples/channels.ts | 3 - examples/clients.ts | 13 ++-- src/channels.ts | 60 ++++++++-------- src/clients.ts | 151 ++++++++++++++++++++++------------------- tests/channels.test.ts | 30 ++++---- tests/clients.test.ts | 50 +++++++------- 6 files changed, 157 insertions(+), 150 deletions(-) diff --git a/examples/channels.ts b/examples/channels.ts index ba6b0731..c38e4cd9 100644 --- a/examples/channels.ts +++ b/examples/channels.ts @@ -1,4 +1,3 @@ -// Import the SDK class from the mainflux-sdk package import SDK from "../src/sdk"; const defaultUrl = "http://localhost"; @@ -7,8 +6,6 @@ const mySdk = new SDK({ channelsUrl: `${defaultUrl}:9005`, }); -// Channels.ts examples. - const token = ""; const domainId = ""; diff --git a/examples/clients.ts b/examples/clients.ts index 07d1b77b..6acd8771 100644 --- a/examples/clients.ts +++ b/examples/clients.ts @@ -1,4 +1,3 @@ -// Import the SDK class from the mainflux-sdk package import SDK from "../src/sdk"; const defaultUrl = "http://localhost"; @@ -7,8 +6,6 @@ const mySdk = new SDK({ clientsUrl: `${defaultUrl}:9006`, }); -// Clients.ts examples. - const token = ""; const domainId = ""; @@ -201,7 +198,7 @@ mySdk.clients "", domainId, "", - ["roleAction1", "roleAction2"], + ["", ""], token ) .then((response) => { @@ -225,7 +222,7 @@ mySdk.clients "", domainId, "", - ["roleAction1", "roleAction2"], + ["", ""], token ) .then((response) => { @@ -245,7 +242,7 @@ mySdk.clients }); mySdk.clients - .AddClientRoleMembers("", domainId, "", ["", ""], token) + .AddClientRoleMembers("", domainId, "", ["", ""], token) .then((response) => { console.log("response: ", response); }) @@ -273,8 +270,8 @@ mySdk.clients "", domainId, "", - ["", ""], - token + ["", ""], + token, ) .then((response) => { console.log("response: ", response); diff --git a/src/channels.ts b/src/channels.ts index af972e3a..adc1b98d 100644 --- a/src/channels.ts +++ b/src/channels.ts @@ -39,8 +39,8 @@ export default class Channels { * @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} - The created channel object. - * @throws {Error} If the channel cannot be created. + * @returns {Promise} channel - The created channel object. + * @throws {Error} - If the channel cannot be created. */ public async CreateChannel( channel: Channel, @@ -79,8 +79,8 @@ export default class Channels { * @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} - The requested channel object. - * @throws {Error} If the channel cannot be fetched. + * @returns {Promise} channel - The requested channel object. + * @throws {Error} - If the channel cannot be fetched. */ public async Channel( channelId: string, @@ -118,8 +118,8 @@ export default class 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} - A page of channels. - * @throws {Error} If the channels cannot be created. + * @returns {Promise} channelsPage - A page of channels. + * @throws {Error} - If the channels cannot be created. */ public async CreateChannels( channels: Channel[], @@ -158,8 +158,8 @@ export default class Channels { * @param {PageMetadata} queryParams - Query parameters for the request. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - A page of channels. - * @throws {Error} If the channels cannot be fetched. + * @returns {Promise} channelsPage - A page of channels. + * @throws {Error} - If the channels cannot be fetched. */ public async Channels( queryParams: PageMetadata, @@ -202,8 +202,8 @@ export default class Channels { * @param {Channel} channel - Channel object with updated properties. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - The updated channel object. - * @throws {Error} If the channel cannot be updated. + * @returns {Promise} channel - The updated channel object. + * @throws {Error} - If the channel cannot be updated. */ public async UpdateChannelNameAndMetadata( channel: Channel, @@ -242,8 +242,8 @@ export default class Channels { * @param {Channel} channel - Channel object with updated properties. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - The updated channel object. - * @throws {Error} If the channel tags cannot be updated. + * @returns {Promise} channel - The updated channel object. + * @throws {Error} - If the channel tags cannot be updated. */ public async UpdateChannelTags( channel: Channel, @@ -282,8 +282,8 @@ export default class Channels { * @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} - The disabled channel object. - * @throws {Error} If the channel cannot be disabled. + * @returns {Promise} channel - The disabled channel object. + * @throws {Error} - If the channel cannot be disabled. */ public async DisableChannel( channelId: string, @@ -321,8 +321,8 @@ export default class Channels { * @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} - The enabled channel object. - * @throws {Error} If the channel cannot be enabled. + * @returns {Promise} channel - The enabled channel object. + * @throws {Error} - If the channel cannot be enabled. */ public async EnableChannel( channelId: string, @@ -360,8 +360,8 @@ export default class Channels { * @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} A promise that resolves when the channel is deleted. - * @throws {Error} If the channel cannot be deleted. + * @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, @@ -401,8 +401,8 @@ export default class Channels { * @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} A promise that resolves when the clients are connected to the channel. - * @throws {Error} If the clients cannot be connected to the channel. + * @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[], @@ -448,8 +448,8 @@ export default class Channels { * @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} A promise that resolves when the clients are connected to the channels. - * @throws {Error} If the clients cannot be connected to the channel. + * @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[], @@ -492,8 +492,8 @@ export default class Channels { * @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} - A promise that resolves when the clients are disconnected from the channels. - * @throws {Error} If the clients cannot be disconnected from the channels. + * @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[], @@ -536,8 +536,8 @@ export default class Channels { * @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} - A promise that resolves when the clients are disconnected from the channel. - * @throws {Error} If the clients cannot be disconnected from the channel. + * @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[], @@ -582,8 +582,8 @@ export default class Channels { * @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} - 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. + * @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 ChannelParents(domainId: string, channelId: string, parentGroupId: string, token: string) : Promise { const options: RequestInit = { @@ -615,8 +615,8 @@ export default class Channels { * @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} - 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. + * @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 DeleteChannelParents(domainId: string, channelId: string, token: string) : Promise { const options: RequestInit = { diff --git a/src/clients.ts b/src/clients.ts index dbd1877a..7461408b 100644 --- a/src/clients.ts +++ b/src/clients.ts @@ -42,11 +42,11 @@ export default class Clients { /** * @method CreateClient - Creates a new client. - * @param {Object} Client - Client object containing details like name and metadata. + * @param {Client} client - Client object containing details like name and metadata. * @param {string} domainId - The unique ID of the domain. * @param {stringtring} token - Authorization token. - * @returns {Promise} - The created client object. - * @throws {Error} If the client cannot be created. + * @returns {Promise} client - The created client object. + * @throws {Error} - If the client cannot be created. */ public async CreateClient( client: Client, @@ -86,8 +86,8 @@ export default class Clients { * @param {Client[]} clients - An array of client 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} - A page of clients. - * @throws {Error} If the clients cannot be created. + * @returns {Promise} clientsPage - A page of clients. + * @throws {Error} - If the clients cannot be created. */ public async CreateClients( clients: Client[], @@ -114,8 +114,8 @@ export default class Clients { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const ClientData: ClientsPage = await response.json(); - return ClientData; + const clientData: ClientsPage = await response.json(); + return clientData; } catch (error) { throw error; } @@ -123,11 +123,11 @@ export default class Clients { /** * @method Enable - Enables a previously disabled client by its ID. - * @param {string} clientID - The unique ID of the client. + * @param {string} clientId - The unique ID of the client. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - The updated client object with enabled status. - * @throws {Error} If the client cannot be enabled. + * @returns {Promise} client - The updated client object with enabled status. + * @throws {Error} - If the client cannot be enabled. */ public async Enable( clientId: string, @@ -163,11 +163,11 @@ export default class Clients { /** * @method Disable - Disables an enabled client by its ID.. - * @param {string} ClientId - The unique ID of the client. + * @param {string} clientId - The unique ID of the client. * @param {string} domainId -The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - The updated client object with disabled status. - * @throws {Error} If the group cannot be disabled. + * @returns {Promise} client - The updated client object with disabled status. + * @throws {Error} - If the group cannot be disabled. */ public async Disable( clientId: string, @@ -205,8 +205,8 @@ export default class Clients { * @param {Client} client- The client object. * @param {string} domainId - The unique identifier of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - The updated channel object. - * @throws {Error} If the channel cannot be updated. + * @returns {Promise} client - The updated client object. + * @throws {Error} - If the client cannot be updated. */ public async UpdateClient( client: Client, @@ -245,8 +245,8 @@ export default class Clients { * @param {string} domainId - The unique ID of the domain.. * @param {Client} client - Client object with updated properties. * @param {string} token - Authorization token.. - * @returns {Promise } - The updated client object. - * @throws {Error} If the client secret cannot be updated. + * @returns {Promise } client - The updated client object. + * @throws {Error} - If the client secret cannot be updated. */ public async UpdateClientSecret( client: Client, @@ -282,11 +282,11 @@ export default class Clients { /** * @method UpdateClientTags - Updates an existing client's tags. - * @param {client} Client - Client object with updated properties. + * @param {Client} client - Client object with updated properties. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - The updated client object. - * @throws {Error} If the client tags cannot be updated. + * @returns {Promise} client - The updated client object. + * @throws {Error} - If the client tags cannot be updated. */ public async UpdateClientTags( client: Client, @@ -326,8 +326,8 @@ export default class Clients { * @param {string} clientId - The unique ID of the client. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - The requested client object. - * @throws {Error} If the client cannot be fetched. + * @returns {Promise} client - The requested client object. + * @throws {Error} - If the client cannot be fetched. */ public async Client( clientId: string, @@ -354,8 +354,8 @@ export default class Clients { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const ClientData: Client = await response.json(); - return ClientData; + const clientData: Client = await response.json(); + return clientData; } catch (error) { throw error; } @@ -366,8 +366,8 @@ export default class Clients { * @param {PageMetadata} queryParams - Query parameters for the request. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - A page of clients. - * @throws {Error} If the clients cannot be fetched. + * @returns {Promise} clientsPage - A page of clients. + * @throws {Error} - If the clients cannot be fetched. */ public async Clients( queryParams: PageMetadata, @@ -413,8 +413,8 @@ export default class Clients { * @param {PageMetadata} queryParams - Query parameters for the request. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - A page of clients. - * @throws {Error} If the clients cannot be fetched. + * @returns {Promise} clientsPage - A page of clients. + * @throws {Error} - If the clients cannot be fetched. */ public async ListUserClients( userId: string, @@ -456,13 +456,13 @@ export default class Clients { /** * @method ClientParents - Sets parent to a client. * @param {string} domainId - The unique ID of the domain. - * @param {string} clientlId - The unique ID of the client to be updated. + * @param {string} clientId - The unique ID of the client 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} - A promise that resolves when the parent group is successfully set for the specified client. - * @throws {Error} If the parent group cannot be set for the client. + * @returns {Promise} response - A promise that resolves when the parent group is successfully set for the specified client. + * @throws {Error} - If the parent group cannot be set for the client. */ - public async ClientParents(domainId: string, clientlId: string, parentGroupId: string, token: string) : Promise { + public async ClientParents(domainId: string, clientId: string, parentGroupId: string, token: string) : Promise { const options: RequestInit = { method: "POST", headers: { @@ -473,7 +473,7 @@ export default class Clients { }; try { const response = await fetch( - new URL(`${domainId}/${this.clientsEndpoint}/${clientlId}/parent`, this.clientsUrl).toString(), + new URL(`${domainId}/${this.clientsEndpoint}/${clientId}/parent`, this.clientsUrl).toString(), options ); if (!response.ok) { @@ -492,8 +492,8 @@ export default class Clients { * @param {string} domainId - The unique ID of the domain. * @param {string} clientId - The unique ID of the client. * @param {string} token - Authorization token. - * @returns {Promise} - A promise that resolves when the parent group is successfully removed from the specified client. - * @throws {Error} If the parent group cannot removed from the client. + * @returns {Promise} response - A promise that resolves when the parent group is successfully removed from the specified client. + * @throws {Error} - If the parent group cannot removed from the client. */ public async DeleteClientParents(domainId: string, clientId: string, token: string) : Promise { const options: RequestInit = { @@ -527,11 +527,11 @@ export default class Clients { * @param {string} clientId - The unique ID of the client. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. - * @returns {Promise} - A promise that resolves when the client is deleted. - * @throws {Error} If the client cannot be deleted. + * @returns {Promise} response - A promise that resolves when the client is deleted. + * @throws {Error} - If the client cannot be deleted. */ public async DeleteClient( - ClientId: string, + clientId: string, domainId: string, token: string ): Promise { @@ -546,7 +546,7 @@ export default class Clients { try { const response = await fetch( new URL( - `${domainId}/${this.clientsEndpoint}/${ClientId}`, + `${domainId}/${this.clientsEndpoint}/${clientId}`, this.clientsUrl ).toString(), options @@ -569,8 +569,8 @@ export default class Clients { * @method ListClientActions - Lists all actions available to a specific client. * @param {string} domainId - The unique identifier of the domain. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves with an array of actions. - * @throws {Error} If client actions cannot be fetched. + * @returns {Promise} actions - A promise that resolves with an array of actions. + * @throws {Error} - If client actions cannot be fetched. */ public async ListClientActions(domainId: string, token: string): Promise { try { @@ -588,12 +588,13 @@ export default class Clients { /** * @method CreateClientRole - Creates a new role within a specific client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The name of the role to create. * @param {string} token - Authorization token. * @param {string[]} optionalActions - Optional actions assigned to the role. * @param {string[]} optionalMembers - Optional members assigned to the role. - * @returns {Promise} A promise that resolves with the role created. - * @throws {Error} If the role cannot be created or already exists. + * @returns {Promise} role - A promise that resolves with the role created. + * @throws {Error} - If the role cannot be created or already exists. */ public async CreateClientRole( clientId: string, @@ -622,10 +623,11 @@ export default class Clients { /** * @method ListClientRoles - Lists all roles within a specific client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {PageMetadata} queryParams - Metadata for pagination or filters. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves with a page of roles in the domain. - * @throws {Error} If the client is invalid or roles cannot be fetched. + * @returns {Promise} rolePage - A promise that resolves with a page of roles in the domain. + * @throws {Error} - If the client is invalid or roles cannot be fetched. */ public async ListClientRoles( clientId: string, @@ -650,10 +652,11 @@ export default class Clients { /** * @method ViewClientRole - Retrieves details about a specific role in a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves with the role details. - * @throws {Error} If the role does not exist or cannot be retrieved. + * @returns {Promise} role - A promise that resolves with the role details. + * @throws {Error} - If the role does not exist or cannot be retrieved. */ public async ViewClientRole( clientId: string, @@ -678,11 +681,12 @@ export default class Clients { /** * @method UpdateClientRole - Updates the details of a specific role in a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. * @param {Role} role - The role to be updated. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves with the updated role. - * @throws {Error} If the role cannot be updated. + * @returns {Promise} role - A promise that resolves with the updated role. + * @throws {Error} - If the role cannot be updated. */ public async UpdateClientRole( clientId: string, @@ -709,10 +713,11 @@ export default class Clients { /** * @method DeleteClientRole - Deletes a specific role from a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves when the role is deleted. - * @throws {Error} If the role cannot be deleted. + * @returns {Promise} response - A promise that resolves when the role is deleted. + * @throws {Error} - If the role cannot be deleted. */ public async DeleteClientRole( clientId: string, @@ -737,11 +742,12 @@ export default class Clients { /** * @method AddClientRoleActions - Adds actions to a specific role in a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. * @param {string[]} actions - The actions to add to the role. - * @returns {Promise} A promise that resolves with an array of actions. - * @throws {Error} If the actions cannot be added. + * @returns {Promise} role actions- A promise that resolves with an array of actions. + * @throws {Error} - If the actions cannot be added. */ public async AddClientRoleActions( clientId: string, @@ -749,7 +755,7 @@ export default class Clients { roleName: string, actions: string[], token: string, - ) { + ): Promise { try { const response = await this.clientRoles.AddRoleActions( this.clientsUrl, @@ -768,10 +774,11 @@ export default class Clients { /** * @method ListClientRoleActions - Lists all actions associated with a specific role in a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves with an array of actions. - * @throws {Error} If actions cannot be retrieved. + * @returns {Promise} role actions - A promise that resolves with an array of actions. + * @throws {Error} - If actions cannot be retrieved. */ public async ListClientRoleActions( clientId: string, @@ -796,11 +803,12 @@ export default class Clients { /** * @method DeleteClientRoleActions - Deletes specific actions from a role in a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. * @param {string[]} actions - The actions to delete from the role. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves when actions are deleted. - * @throws {Error} If the actions cannot be deleted. + * @returns {Promise} response - A promise that resolves when actions are deleted. + * @throws {Error} - If the actions cannot be deleted. */ public async DeleteClientRoleActions( clientId: string, @@ -827,10 +835,11 @@ export default class Clients { /** * @method DeleteAllClientRoleActions - Deletes all actions associated with a specific role in a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves when all actions are deleted. - * @throws {Error} If the actions cannot be deleted. + * @returns {Promise} response - A promise that resolves when all actions are deleted. + * @throws {Error} - If the actions cannot be deleted. */ public async DeleteAllClientRoleActions( clientId: string, @@ -855,11 +864,12 @@ export default class Clients { /** * @method AddClientRoleMembers - Adds members to a specific role in a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. * @param {string[]} members - The IDs of the members to add. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves with an array of member ids. - * @throws {Error} If the members cannot be added. + * @returns {Promise} members - A promise that resolves with an array of member ids. + * @throws {Error} - If the members cannot be added. */ public async AddClientRoleMembers( clientId: string, @@ -886,10 +896,11 @@ export default class Clients { /** * @method ListClientRoleMembers - Lists all members associated with a specific role in a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves with an array of member ids. - * @throws {Error} If members cannot be retrieved. + * @returns {Promise} members - A promise that resolves with an array of member ids. + * @throws {Error} - If members cannot be retrieved. */ public async ListClientRoleMembers( clientId: string, @@ -916,11 +927,12 @@ export default class Clients { /** * @method DeleteClientRoleMembers - Deletes specific members from a role in a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. * @param {string[]} members - The IDs of the members to delete. * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves when members are deleted. - * @throws {Error} If the members cannot be deleted. + * @returns {Promise} response - A promise that resolves when members are deleted. + * @throws {Error} - If the members cannot be deleted. */ public async DeleteClientRoleMembers( clientId: string, @@ -947,10 +959,11 @@ export default class Clients { /** * @method DeleteAllClientRoleMembers - Deletes all members associated with a specific role in a client. * @param {string} clientId - The unique identifier of the client. + * @param {string} domainId - The unique ID of the domain. * @param {string} roleName - The unique identifier of the role. - * @param {string} token - Authorization token. - * @returns {Promise} A promise that resolves when all members are deleted. - * @throws {Error} If the members cannot be deleted. + * @param {string} token - Authorization token. + * @returns {Promise} response - A promise that resolves when all members are deleted. + * @throws {Error} - If the members cannot be deleted. */ public async DeleteAllClientRoleMembers( clientId: string, diff --git a/tests/channels.test.ts b/tests/channels.test.ts index cb00a5ec..b00acaef 100644 --- a/tests/channels.test.ts +++ b/tests/channels.test.ts @@ -51,63 +51,63 @@ describe("Channels", () => { fetchMock.resetMocks(); }); - test("Channel should retrieve a channel and return success", async () => { + test("Channel should retrieve a channel", async () => { fetchMock.mockResponseOnce(JSON.stringify(channel)); const response = await sdk.channels.Channel(channelId, domainId, token); expect(response).toEqual(channel); }); - test("CreateChannel should create a channel and return success", async () => { + test("Create channel should create a channel", async () => { fetchMock.mockResponseOnce(JSON.stringify(channel)); const response = await sdk.channels.CreateChannel(channel, domainId, token); expect(response).toEqual(channel); }); - test("CreateChannels should create multiple channels and return success", async () => { + test("Create channels should create multiple channels", async () => { fetchMock.mockResponseOnce(JSON.stringify(channels)); const response = await sdk.channels.CreateChannels(channels, domainId, token); expect(response).toEqual(channels); }); - test("Channels should return a list of channels and return success", async () => { + test("Channels should return a list of channels", async () => { fetchMock.mockResponseOnce(JSON.stringify(channelsPage)); const response = await sdk.channels.Channels(queryParams, domainId, token); expect(response).toEqual(channelsPage); }); - test("UpdateChannelNameAndMetadata should update channel name and metadata and return success", async () => { + test("Update channel name and metadata should update a channel's name and metadata", async () => { fetchMock.mockResponseOnce(JSON.stringify(channel)); const response = await sdk.channels.UpdateChannelNameAndMetadata(channel, domainId, token); expect(response).toEqual(channel); }); - test("UpdateChannelTags should update channel tags and return success", async () => { + test("Update channel tags should update channel's tags", async () => { fetchMock.mockResponseOnce(JSON.stringify(channel)); const response = await sdk.channels.UpdateChannelTags(channel, domainId, token); expect(response).toEqual(channel); }); - test("EnableChannel should enable a channel and return success", async () => { + test("Enable channel should enable a channel", async () => { fetchMock.mockResponseOnce(JSON.stringify(channel)); const response = await sdk.channels.EnableChannel(channelId, domainId, token); expect(response).toEqual(channel); }); - test("DisableChannel should disable a channel and return success", async () => { + test("Disable channel should disable a channel", async () => { fetchMock.mockResponseOnce(JSON.stringify(channel)); const response = await sdk.channels.DisableChannel(channelId, domainId, token); expect(response).toEqual(channel); }); - test("Delete should delete a channel and return success", async () => { + test("Delete should delete a channel", async () => { const deleteResponse = { status: 200, message: "Channel deleted successfully", @@ -122,7 +122,7 @@ describe("Channels", () => { expect(response).toEqual(deleteResponse); }); - test("ConnectClient should connect clients to a channel and return success", async () => { + test("Connect client should connect clients to a channel", async () => { const connectClientResponse = { status: 200, message: "Clients connected successfully", @@ -139,7 +139,7 @@ describe("Channels", () => { expect(response).toEqual(connectClientResponse); }); - test("DisconnectClient should disconnect clients from a channel and return success", async () => { + test("Disconnect client should disconnect clients from a channel", async () => { const DisconnectClientResponse = { status: 200, message: "Clients disconnected successfully", @@ -156,7 +156,7 @@ describe("Channels", () => { expect(response).toEqual(DisconnectClientResponse); }); - test("Connect should connect clients to channels and return success", async () => { + test("Connect should connect clients to channels", async () => { const connectResponse = { status: 200, message: "Clients connected successfully", @@ -173,7 +173,7 @@ describe("Channels", () => { expect(response).toEqual(connectResponse); }); - test("Disconnect should disconnect clients to channels and return success", async () => { + test("Disconnect should disconnect clients from channels", async () => { const DisconnectResponse = { status: 200, message: "Clients disconnected successfully", @@ -190,7 +190,7 @@ describe("Channels", () => { expect(response).toEqual(DisconnectResponse); }); - test("ChannelParents should set a group parent to a channel and return success", async () => { + test("Channel parents should set a group parent to a channel", async () => { const ChannelParentsResponse = { status: 200, message: "Channel Group Parent added successfully", @@ -206,7 +206,7 @@ describe("Channels", () => { expect(response).toEqual(ChannelParentsResponse); }); - test("DeleteChannelParents should delete a group parent from a channel and return success", async () => { + test("Delete channel parents should delete a group parent from a channel", async () => { const ChannelParentsResponse = { status: 200, message: "Channel Group Parent deleted successfully", diff --git a/tests/clients.test.ts b/tests/clients.test.ts index 164ad773..de7800f7 100644 --- a/tests/clients.test.ts +++ b/tests/clients.test.ts @@ -52,70 +52,70 @@ describe("Clients", () => { fetchMock.resetMocks(); }); - test("Create should create a client and return success", async () => { + test("Create should create a client", async () => { fetchMock.mockResponseOnce(JSON.stringify(client)); const response = await sdk.clients.CreateClient(client, domainId, token); expect(response).toEqual(client); }); - test("CreateClients should create multiple clients and return success", async () => { + test("Create clients should create multiple clients", async () => { fetchMock.mockResponseOnce(JSON.stringify(clients)); const response = await sdk.clients.CreateClients(clients, domainId, token); expect(response).toEqual(clients); }); - test("Disable should disable a client and return success", async () => { + test("Disable should disable a client", async () => { fetchMock.mockResponseOnce(JSON.stringify(client)); const response = await sdk.clients.Disable(clientId, domainId, token); expect(response).toEqual(client); }); - test("Enable should enable a client and return success", async () => { + test("Enable should enable a client", async () => { fetchMock.mockResponseOnce(JSON.stringify(client)); const response = await sdk.clients.Enable(clientId, domainId, token); expect(response).toEqual(client); }); - test("UpdateClientSecret should update a client secret and return success", async () => { + test("Update client secret should update a client's secret", async () => { fetchMock.mockResponseOnce(JSON.stringify(client)); const response = await sdk.clients.UpdateClientSecret(client, domainId, token); expect(response).toEqual(client); }); - test("UpdateClientTags should update a client tags and return success", async () => { + test("Update client tags should update a client's tags", async () => { fetchMock.mockResponseOnce(JSON.stringify(client)); const response = await sdk.clients.UpdateClientTags(client, domainId, token); expect(response).toEqual(client); }); - test("Client should retrieve a client and return success", async () => { + test("Client should retrieve a client", async () => { fetchMock.mockResponseOnce(JSON.stringify(client)); const response = await sdk.clients.Client(clientId, domainId, token); expect(response).toEqual(client); }); - test("UpdateClient should update a client and return success", async () => { + test("Update client should update a client", async () => { fetchMock.mockResponseOnce(JSON.stringify(client)); const response = await sdk.clients.UpdateClient(client, domainId, token); expect(response).toEqual(client); }); - test("Clients should get a list of all clients and return success", async () => { + test("Clients should get a list of all clients", async () => { fetchMock.mockResponseOnce(JSON.stringify(clientsPage)); const response = await sdk.clients.Clients(queryParams, domainId, token); expect(response).toEqual(clientsPage); }); - test("ListUserClients should list clients linked to a client and return success", async () => { + test("List user clients should list clients linked to a user", async () => { fetchMock.mockResponseOnce(JSON.stringify(clientsPage)); const response = await sdk.clients.ListUserClients( @@ -127,7 +127,7 @@ describe("Clients", () => { expect(response).toEqual(clientsPage); }); - test("DeleteClient should delete a client and return success", async () => { + test("Delete client should delete a client", async () => { const deleteResponse = { status: 200, message: "Client deleted successfully", @@ -138,7 +138,7 @@ describe("Clients", () => { expect(response).toEqual(deleteResponse); }); - test("ListClientActions should return available actions", async () => { + test("List client actions should return available actions", async () => { const availableActions = ["read", "write", "delete"]; fetchMock.mockResponseOnce(JSON.stringify(availableActions)); @@ -146,7 +146,7 @@ describe("Clients", () => { expect(response).toEqual(availableActions); }); - test("CreateClientRole should create a new role and return it", async () => { + test("Create client role should create a new role and return it", async () => { fetchMock.mockResponseOnce(JSON.stringify(role)); const response = await sdk.clients.CreateClientRole( @@ -160,7 +160,7 @@ describe("Clients", () => { expect(response).toEqual(role); }); - test("ListClientRoles should return a page of roles", async () => { + test("List client roles should return a page of roles", async () => { const rolesPage = { roles: [role], total: 1, offset: 0, limit: 10 }; fetchMock.mockResponseOnce(JSON.stringify(rolesPage)); @@ -173,7 +173,7 @@ describe("Clients", () => { expect(response).toEqual(rolesPage); }); - test("ViewClientRole should return details of a specific role", async () => { + test("View client role should return details of a specific role", async () => { fetchMock.mockResponseOnce(JSON.stringify(role)); const response = await sdk.clients.ViewClientRole( @@ -185,7 +185,7 @@ describe("Clients", () => { expect(response).toEqual(role); }); - test("UpdateClientRole should update a role and return the updated role", async () => { + test("Update client role should update a role and return the updated role", async () => { const updatedRole = { ...role, actions: [...role.actions, "execute"] }; fetchMock.mockResponseOnce(JSON.stringify(updatedRole)); @@ -199,7 +199,7 @@ describe("Clients", () => { expect(response).toEqual(updatedRole); }); - test("DeleteClientRole should delete a role and return success response", async () => { + test("Delete client role should delete a role", async () => { const successResponse = { status: 200, message: "Role deleted successfully", @@ -215,7 +215,7 @@ describe("Clients", () => { expect(response).toEqual(successResponse); }); - test("AddClientRoleActions should add actions to a role and return updated actions", async () => { + test("Add client role actions should add actions to a role and return updated actions", async () => { const updatedActions = [...actions, "execute"]; fetchMock.mockResponseOnce(JSON.stringify(updatedActions)); @@ -229,7 +229,7 @@ describe("Clients", () => { expect(response).toEqual(updatedActions); }); - test("ListClientRoleActions should return actions of a specific role", async () => { + test("List client role actions should return actions of a specific role", async () => { fetchMock.mockResponseOnce(JSON.stringify(actions)); const response = await sdk.clients.ListClientRoleActions( @@ -241,7 +241,7 @@ describe("Clients", () => { expect(response).toEqual(actions); }); - test("DeleteClientRoleActions should remove actions from a role and return success response", async () => { + test("Delete client role actions should remove actions from a role", async () => { const successResponse = { status: 200, message: "Role actions deleted successfully", @@ -258,7 +258,7 @@ describe("Clients", () => { expect(response).toEqual(successResponse); }); - test("DeleteAllClientRoleActions should remove all actions from a role and return success response", async () => { + test("Delete all client role actions should remove all actions from a role", async () => { const successResponse = { status: 200, message: "Role actions deleted successfully", @@ -274,7 +274,7 @@ describe("Clients", () => { expect(response).toEqual(successResponse); }); - test("AddClientRoleMembers should add members to a role and return updated members", async () => { + test("Add client role members should add members to a role and return updated members", async () => { const updatedMembers = [...members, "user3"]; fetchMock.mockResponseOnce(JSON.stringify(updatedMembers)); @@ -288,7 +288,7 @@ describe("Clients", () => { expect(response).toEqual(updatedMembers); }); - test("ListClientRoleMembers should return members of a specific role", async () => { + test("List client role members should return members of a specific role", async () => { fetchMock.mockResponseOnce(JSON.stringify(members)); const response = await sdk.clients.ListClientRoleMembers( @@ -301,7 +301,7 @@ describe("Clients", () => { expect(response).toEqual(members); }); - test("DeleteClientRoleMembers should remove members from a role and return success response", async () => { + test("Delete client role members should remove members from a role response", async () => { const successResponse = { status: 200, message: "Role members deleted successfully", @@ -318,7 +318,7 @@ describe("Clients", () => { expect(response).toEqual(successResponse); }); - test("DeleteAllClientRoleMembers should remove all members from a role and return success response", async () => { + test("Delete all client role members should remove all members from a role response", async () => { const successResponse = { status: 200, message: "Role members deleted successfully", From c0cca35b8c4d69a2ca43143633617d99e0d61e2d Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Fri, 22 Nov 2024 18:39:46 +0300 Subject: [PATCH 09/12] update client and channel structs Signed-off-by: wambuipixel --- src/defs.ts | 7 +++---- tests/channels.test.ts | 1 - 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/defs.ts b/src/defs.ts index baa3090d..4303fb87 100644 --- a/src/defs.ts +++ b/src/defs.ts @@ -45,11 +45,13 @@ export interface ClientBasicInfo { export interface Client extends ClientBasicInfo { tags?: string[]; domain_id?: string | DomainBasicInfo; + parent_group_id?: string; metadata?: Record; created_at?: Date; updated_at?: Date; updated_by?: string | UserBasicInfo; permissions?: string[]; + identity?: string; } export interface ClientsPage { @@ -91,16 +93,13 @@ export interface ChannelBasicInfo { id?: string; name?: string; status?: Status; - description?: string; } export interface Channel extends ChannelBasicInfo { domain_id?: string | DomainBasicInfo; metadata?: Record; tags?: string[]; - ParentGroup?: string; - level?: number; - path?: string; + parent_group_id?: string; created_at?: Date; updated_at?: Date; updated_by?: string; diff --git a/tests/channels.test.ts b/tests/channels.test.ts index b00acaef..f88bf99b 100644 --- a/tests/channels.test.ts +++ b/tests/channels.test.ts @@ -16,7 +16,6 @@ describe("Channels", () => { id: "290b0f49-7a57-4b8c-9e4e-fbf17c6ab7d9", name: "channelName", domain_id: "bb7edb32-2eac-4aad-aebe-ed96fe073879", - description: "", tags: ["tag1", "tag2"], }; const channels = [ From c39b9c2590b24adb3484c53216eca0301c1b4c5c Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Mon, 25 Nov 2024 10:31:11 +0300 Subject: [PATCH 10/12] address requested change Signed-off-by: wambuipixel --- src/clients.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/clients.ts b/src/clients.ts index 7461408b..b625a07c 100644 --- a/src/clients.ts +++ b/src/clients.ts @@ -242,9 +242,9 @@ export default class Clients { /** * @method UpdateClientSecret - Updates an existing client's secret. - * @param {string} domainId - The unique ID of the domain.. - * @param {Client} client - Client object with updated properties. - * @param {string} token - Authorization token.. + * @param {string} domainId - The unique ID of the domain. + * @param {Client} client - Client object with updated secret. + * @param {string} token - Authorization token. * @returns {Promise } client - The updated client object. * @throws {Error} - If the client secret cannot be updated. */ @@ -282,7 +282,7 @@ export default class Clients { /** * @method UpdateClientTags - Updates an existing client's tags. - * @param {Client} client - Client object with updated properties. + * @param {Client} client - Client object with updated tags. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. * @returns {Promise} client - The updated client object. @@ -480,7 +480,7 @@ export default class Clients { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const addClientParentsResponse: Response = { status: response.status, message: "Client Group Parent added successfully" }; + const addClientParentsResponse: Response = { status: response.status, message: "Client group parent added successfully" }; return addClientParentsResponse; } catch (error) { throw error; @@ -515,8 +515,8 @@ export default class Clients { const errorRes = await response.json(); throw Errors.HandleError(errorRes.message, response.status); } - const deleteChannelParentsResponse: Response = { status: response.status, message: "Channel Group Parent deleted successfully" }; - return deleteChannelParentsResponse; + const deleteClientParentsResponse: Response = { status: response.status, message: "Client group parent deleted successfully" }; + return deleteClientParentsResponse; } catch (error) { throw error; } From 31c3572c648ccc69d7682306ea62b889165a1fe3 Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Mon, 25 Nov 2024 11:30:41 +0300 Subject: [PATCH 11/12] add requested changes Signed-off-by: wambuipixel --- examples/channels.ts | 4 ++-- examples/clients.ts | 4 ++-- src/channels.ts | 12 ++++++------ src/clients.ts | 12 ++++++------ src/defs.ts | 5 +++-- src/sdk.ts | 2 +- tests/channels.test.ts | 12 ++++++------ tests/clients.test.ts | 35 ++++++++++++++++++++++++++++++++++- 8 files changed, 60 insertions(+), 26 deletions(-) diff --git a/examples/channels.ts b/examples/channels.ts index c38e4cd9..e21efd29 100644 --- a/examples/channels.ts +++ b/examples/channels.ts @@ -149,7 +149,7 @@ mySdk.channels console.error(error); }); -mySdk.channels.ChannelParents(domainId, "", "", token) +mySdk.channels.SetChannelParents(domainId, "", "", token) .then((response: any) => { console.log("response: ", response); }) @@ -157,7 +157,7 @@ mySdk.channels.ChannelParents(domainId, "", "", token) console.error(error); }); -mySdk.channels.DeleteChannelParents(domainId, "", token) +mySdk.channels.DeleteChannelParentGroup(domainId, "", token) .then((response: any) => { console.log("response: ", response); }) diff --git a/examples/clients.ts b/examples/clients.ts index 6acd8771..24c4e16d 100644 --- a/examples/clients.ts +++ b/examples/clients.ts @@ -108,7 +108,7 @@ mySdk.clients console.log(error); }); -mySdk.clients.ClientParents(domainId, "", "", token) +mySdk.clients.SetClientParents(domainId, "", "", token) .then((response: any) => { console.log("response: ", response); }) @@ -116,7 +116,7 @@ mySdk.clients.ClientParents(domainId, "", "", token) console.error(error); }); -mySdk.clients.DeleteClientParents(domainId, "", token) +mySdk.clients.DeleteClientParentGroup(domainId, "", token) .then((response: any) => { console.log("response: ", response); }) diff --git a/src/channels.ts b/src/channels.ts index adc1b98d..808c88a1 100644 --- a/src/channels.ts +++ b/src/channels.ts @@ -577,7 +577,7 @@ export default class Channels { } /** - * @method ChannelParents - Sets parent to a channel. + * @method SetChannelParents - 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. @@ -585,7 +585,7 @@ export default class Channels { * @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 ChannelParents(domainId: string, channelId: string, parentGroupId: string, token: string) : Promise { + public async SetChannelParents(domainId: string, channelId: string, parentGroupId: string, token: string) : Promise { const options: RequestInit = { method: "POST", headers: { @@ -603,7 +603,7 @@ export default class Channels { 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,14 +611,14 @@ export default class Channels { } /** - * @method DeleteChannelParents - Removes the parent group from a specified channel. + * @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 DeleteChannelParents(domainId: string, channelId: string, token: string) : Promise { + public async DeleteChannelParentGroup(domainId: string, channelId: string, token: string) : Promise { const options: RequestInit = { method: "DELETE", headers: { @@ -638,7 +638,7 @@ 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; diff --git a/src/clients.ts b/src/clients.ts index b625a07c..b1313c4d 100644 --- a/src/clients.ts +++ b/src/clients.ts @@ -162,7 +162,7 @@ export default class Clients { } /** - * @method Disable - Disables an enabled client by its ID.. + * @method Disable - Disables an enabled client by its ID. * @param {string} clientId - The unique ID of the client. * @param {string} domainId -The unique ID of the domain. * @param {string} token - Authorization token. @@ -454,7 +454,7 @@ export default class Clients { } /** - * @method ClientParents - Sets parent to a client. + * @method SetClientParents - Sets parent to a client. * @param {string} domainId - The unique ID of the domain. * @param {string} clientId - The unique ID of the client to be updated. * @param {string} parentGroupId - The unique ID of the group to be set as the parent. @@ -462,7 +462,7 @@ export default class Clients { * @returns {Promise} response - A promise that resolves when the parent group is successfully set for the specified client. * @throws {Error} - If the parent group cannot be set for the client. */ - public async ClientParents(domainId: string, clientId: string, parentGroupId: string, token: string) : Promise { + public async SetClientParents(domainId: string, clientId: string, parentGroupId: string, token: string) : Promise { const options: RequestInit = { method: "POST", headers: { @@ -488,14 +488,14 @@ export default class Clients { } /** - * @method DeleteClientParents - Removes the parent group from a specified client. + * @method DeleteClientParentGroup - Removes the parent group from a specified client. * @param {string} domainId - The unique ID of the domain. * @param {string} clientId - The unique ID of the client. * @param {string} token - Authorization token. * @returns {Promise} response - A promise that resolves when the parent group is successfully removed from the specified client. * @throws {Error} - If the parent group cannot removed from the client. */ - public async DeleteClientParents(domainId: string, clientId: string, token: string) : Promise { + public async DeleteClientParentGroup(domainId: string, clientId: string, token: string) : Promise { const options: RequestInit = { method: "DELETE", headers: { @@ -523,7 +523,7 @@ export default class Clients { } /** - * @method DeleteClient - Deletes a client with specified id.. + * @method DeleteClient - Deletes a client with specified id. * @param {string} clientId - The unique ID of the client. * @param {string} domainId - The unique ID of the domain. * @param {string} token - Authorization token. diff --git a/src/defs.ts b/src/defs.ts index 4303fb87..48dd6fdc 100644 --- a/src/defs.ts +++ b/src/defs.ts @@ -3,7 +3,7 @@ export interface UserBasicInfo { first_name?: string; last_name?: string; email?: string; - credentials?: Credentials; + credentials?: UserCredentials; status?: Status; profile_picture?: string; } @@ -25,7 +25,7 @@ export interface UsersPage { limit: number; } -export interface Credentials { +export interface UserCredentials { username?: string; secret?: string; } @@ -291,6 +291,7 @@ export interface BootstrapConfig { external_id?: string; external_key?: string; client_id?: string; + client_secret?: string; name?: string; client_cert?: string; client_key?: string; diff --git a/src/sdk.ts b/src/sdk.ts index c2c374e6..585d193d 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -35,7 +35,7 @@ export type { InvitationsPage, Relation, GroupRelation, - Credentials, + UserCredentials, ClientCredentials, UserBasicInfo, DomainBasicInfo, diff --git a/tests/channels.test.ts b/tests/channels.test.ts index f88bf99b..4e375e15 100644 --- a/tests/channels.test.ts +++ b/tests/channels.test.ts @@ -189,14 +189,14 @@ describe("Channels", () => { expect(response).toEqual(DisconnectResponse); }); - test("Channel parents should set a group parent to a channel", async () => { + test("Set channel parents should set a group parent to a channel", async () => { const ChannelParentsResponse = { status: 200, - message: "Channel Group Parent added successfully", + message: "Channel group parent added successfully", }; fetchMock.mockResponseOnce(JSON.stringify(ChannelParentsResponse)); - const response = await sdk.channels.ChannelParents( + const response = await sdk.channels.SetChannelParents( domainId, channelId, groupId, @@ -205,14 +205,14 @@ describe("Channels", () => { expect(response).toEqual(ChannelParentsResponse); }); - test("Delete channel parents should delete a group parent from a channel", async () => { + test("Delete channel parent group should delete a group parent from a channel", async () => { const ChannelParentsResponse = { status: 200, - message: "Channel Group Parent deleted successfully", + message: "Channel group parent deleted successfully", }; fetchMock.mockResponseOnce(JSON.stringify(ChannelParentsResponse)); - const response = await sdk.channels.DeleteChannelParents( + const response = await sdk.channels.DeleteChannelParentGroup( domainId, channelId, token, diff --git a/tests/clients.test.ts b/tests/clients.test.ts index de7800f7..77459b54 100644 --- a/tests/clients.test.ts +++ b/tests/clients.test.ts @@ -41,6 +41,7 @@ describe("Clients", () => { }; const clientId = "bb7edb32-2eac-4aad-aebe-ed96fe073879"; const userId = "bb7edb32-2eac-4aad-aebe-ed96fe073879"; + const groupId = "1be56995-aa42-4940-88e3-1fb1e82065fa"; const domainId = "886b4266-77d1-4258-abae-2931fb4f16de"; const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjU3OTMwNjksImlhdCI6"; const roleName = "editor"; @@ -90,7 +91,8 @@ describe("Clients", () => { test("Update client tags should update a client's tags", async () => { fetchMock.mockResponseOnce(JSON.stringify(client)); - const response = await sdk.clients.UpdateClientTags(client, domainId, token); + const response = await sdk.clients + .UpdateClientTags(client, domainId, token); expect(response).toEqual(client); }); @@ -127,6 +129,37 @@ describe("Clients", () => { expect(response).toEqual(clientsPage); }); + test("Set client parents should set a group parent to a Client", async () => { + const ClientParentsResponse = { + status: 200, + message: "Client group parent added successfully", + }; + fetchMock.mockResponseOnce(JSON.stringify(ClientParentsResponse)); + + const response = await sdk.clients.SetClientParents( + domainId, + clientId, + groupId, + token, + ); + expect(response).toEqual(ClientParentsResponse); + }); + + test("Delete client parent group should delete a group parent from a Client", async () => { + const ClientParentsResponse = { + status: 200, + message: "Client group parent deleted successfully", + }; + fetchMock.mockResponseOnce(JSON.stringify(ClientParentsResponse)); + + const response = await sdk.clients.DeleteClientParentGroup( + domainId, + clientId, + token, + ); + expect(response).toEqual(ClientParentsResponse); + }); + test("Delete client should delete a client", async () => { const deleteResponse = { status: 200, From 0b67ad75d8faf97bbc65c631e0337691530c91f4 Mon Sep 17 00:00:00 2001 From: wambuipixel Date: Mon, 25 Nov 2024 12:06:28 +0300 Subject: [PATCH 12/12] fix: rename group id variable Signed-off-by: wambuipixel --- examples/channels.ts | 2 +- examples/clients.ts | 2 +- src/channels.ts | 4 ++-- src/clients.ts | 4 ++-- tests/channels.test.ts | 4 ++-- tests/clients.test.ts | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/channels.ts b/examples/channels.ts index e21efd29..e180a88e 100644 --- a/examples/channels.ts +++ b/examples/channels.ts @@ -149,7 +149,7 @@ mySdk.channels console.error(error); }); -mySdk.channels.SetChannelParents(domainId, "", "", token) +mySdk.channels.SetChannelParentGroup(domainId, "", "", token) .then((response: any) => { console.log("response: ", response); }) diff --git a/examples/clients.ts b/examples/clients.ts index 24c4e16d..811cd4e8 100644 --- a/examples/clients.ts +++ b/examples/clients.ts @@ -108,7 +108,7 @@ mySdk.clients console.log(error); }); -mySdk.clients.SetClientParents(domainId, "", "", token) +mySdk.clients.setClientParentGroup(domainId, "", "", token) .then((response: any) => { console.log("response: ", response); }) diff --git a/src/channels.ts b/src/channels.ts index 808c88a1..9248f712 100644 --- a/src/channels.ts +++ b/src/channels.ts @@ -577,7 +577,7 @@ export default class Channels { } /** - * @method SetChannelParents - Sets parent to a channel. + * @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. @@ -585,7 +585,7 @@ export default class Channels { * @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 SetChannelParents(domainId: string, channelId: string, parentGroupId: string, token: string) : Promise { + public async SetChannelParentGroup(domainId: string, channelId: string, parentGroupId: string, token: string) : Promise { const options: RequestInit = { method: "POST", headers: { diff --git a/src/clients.ts b/src/clients.ts index b1313c4d..05567f5b 100644 --- a/src/clients.ts +++ b/src/clients.ts @@ -454,7 +454,7 @@ export default class Clients { } /** - * @method SetClientParents - Sets parent to a client. + * @method setClientParentGroup - Sets parent to a client. * @param {string} domainId - The unique ID of the domain. * @param {string} clientId - The unique ID of the client to be updated. * @param {string} parentGroupId - The unique ID of the group to be set as the parent. @@ -462,7 +462,7 @@ export default class Clients { * @returns {Promise} response - A promise that resolves when the parent group is successfully set for the specified client. * @throws {Error} - If the parent group cannot be set for the client. */ - public async SetClientParents(domainId: string, clientId: string, parentGroupId: string, token: string) : Promise { + public async setClientParentGroup(domainId: string, clientId: string, parentGroupId: string, token: string) : Promise { const options: RequestInit = { method: "POST", headers: { diff --git a/tests/channels.test.ts b/tests/channels.test.ts index 4e375e15..02991ecb 100644 --- a/tests/channels.test.ts +++ b/tests/channels.test.ts @@ -189,14 +189,14 @@ describe("Channels", () => { expect(response).toEqual(DisconnectResponse); }); - test("Set channel parents should set a group parent to a channel", async () => { + test("Set channel parent group should set a group parent to a channel", async () => { const ChannelParentsResponse = { status: 200, message: "Channel group parent added successfully", }; fetchMock.mockResponseOnce(JSON.stringify(ChannelParentsResponse)); - const response = await sdk.channels.SetChannelParents( + const response = await sdk.channels.SetChannelParentGroup( domainId, channelId, groupId, diff --git a/tests/clients.test.ts b/tests/clients.test.ts index 77459b54..0cbe5547 100644 --- a/tests/clients.test.ts +++ b/tests/clients.test.ts @@ -129,14 +129,14 @@ describe("Clients", () => { expect(response).toEqual(clientsPage); }); - test("Set client parents should set a group parent to a Client", async () => { + test("Set client parent group should set a group parent to a Client", async () => { const ClientParentsResponse = { status: 200, message: "Client group parent added successfully", }; fetchMock.mockResponseOnce(JSON.stringify(ClientParentsResponse)); - const response = await sdk.clients.SetClientParents( + const response = await sdk.clients.setClientParentGroup( domainId, clientId, groupId,