diff --git a/index.js b/index.js index ed0884e..6d56e35 100644 --- a/index.js +++ b/index.js @@ -701,6 +701,48 @@ class LLMWhispererClientV2 { const response = await axios(options); + if (response.status !== 201) { + const message = response.data; + message.statusCode = response.status; + throw new LLMWhispererClientException(message.message, response.status); + } else { + return { + status_code: response.status, + message: response.data, + }; + } + } + + /** + * @function + * @name updateWebhookDetails + * @description This function updates the details of a webhook. + * @async + * @param {string} webhookName - The name of the webhook. + * @param {string} webhookUrl - The URL of the webhook. + * @param {string} authToken - The authentication token for the webhook. + * @returns {Promise} Returns a promise that resolves with an object containing the response from the webhook details update. The object includes the status code and the response data. + * @throws {LLMWhispererClientException} Throws an LLMWhispererClientException if an error occurs during the operation. + * + */ + async updateWebhookDetails(webhookName, webhookUrl, authToken) { + const apiUrl = `${this.baseUrl}/whisper-manage-callback`; + const data = { + webhook_name: webhookName, + url: webhookUrl, + auth_token: authToken, + }; + const myHeaders = { ...this.headers, "Content-Type": "application/json" }; + const options = { + method: "put", + url: apiUrl, + headers: myHeaders, + timeout: this.apiTimeout * 1000, + data: data, + }; + + const response = await axios(options); + if (response.status !== 200) { const message = response.data; message.statusCode = response.status; @@ -748,6 +790,41 @@ class LLMWhispererClientV2 { } } + /** + * @function + * @name deleteWebhookDetails + * @description This function deletes the details of a webhook. + * @async + * @param {string} webhookName - The name of the webhook. + * @returns {Promise} Returns a promise that resolves with an object containing the response from the delete operation. The object includes the status code and the response data. + * @throws {LLMWhispererClientException} Throws an LLMWhispererClientException if an error occurs during the operation. + * + */ + async deleteWebhookDetails(webhookName) { + const apiUrl = `${this.baseUrl}/whisper-manage-callback`; + const params = { webhook_name: webhookName }; + const options = { + method: "delete", + url: apiUrl, + headers: this.headers, + params: params, + timeout: 200 * 1000, + }; + + const response = await axios(options); + + if (response.status !== 200) { + const message = response.data; + message.statusCode = response.status; + throw new LLMWhispererClientException(message.message, response.status); + } else { + return { + status_code: response.status, + message: response.data, + }; + } + } + /** * Retrieves the highlight information of the LLMWhisperer API. * diff --git a/package.json b/package.json index 08abac5..d71d03e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "llmwhisperer-client", - "version": "2.2.0", + "version": "2.3.0", "description": "LLMWhisper JS Client", "main": "index.js", "scripts": { @@ -14,7 +14,6 @@ "license": "MIT", "dependencies": { "axios": "~1.7.2", - "llmwhisperer-client": "^2.0.1", "string-similarity": "^4.0.4", "winston": "~3.13.0" }, diff --git a/test/test.js b/test/test.js index b2a8e4b..03a0a8e 100644 --- a/test/test.js +++ b/test/test.js @@ -106,5 +106,46 @@ describe("LLMWhispererClientV2", () => { expect(line2.page).toBe(0); expect(line2.page_height).toBe(3168); }, 20000); // 20-second timeout + + + test("webhook", async () => { + const url = "https://webhook.site/b76ecc5f-8320-4410-b24f-66525d2c92cb"; + const token = ""; + const webhookName = "llmwhisperer-js-client-test"; + const response = await client.registerWebhook(url, token, webhookName); + + expect(response).toEqual({ status_code: 201, message: { message: 'Webhook created successfully' } }); + + const getResponse = await client.getWebhookDetails(webhookName); + + expect(getResponse).toEqual({ + status_code: 200, message: { + auth_token: token, url: url, webhook_name: webhookName + } + }); + + const updateResponse = await client.updateWebhookDetails(webhookName, url, "new_token"); + expect(updateResponse).toEqual({ status_code: 200, message: { message: 'Webhook updated successfully' } }); + + const getUpdatedResponse = await client.getWebhookDetails(webhookName); + expect(getUpdatedResponse).toEqual({ + status_code: 200, message: { + auth_token: "new_token", url: url, webhook_name: webhookName + } + }); + + const deleteResponse = await client.deleteWebhookDetails(webhookName); + expect(deleteResponse).toEqual({ status_code: 200, message: { message: 'Webhook deleted successfully' } }); + + try { + await client.getWebhookDetails(webhookName); + + } catch (e) { + expect(e.response.status).toBe(404); + expect(e.response.data.message).toBe('Webhook details not found'); + } + + }, 15000); // 15-second timeout + });