@visionfi/server-sdk
Version:
Server-side SDK for VisionFI API access using Google Service Account authentication
178 lines (177 loc) • 6.47 kB
JavaScript
import { VisionFiError } from '../../types/index.js';
/**
* Class for managing DocuSign Connect webhook configurations.
* Provides methods to get, create, update, and delete webhook configurations.
*/
export class DocuSignConnectManager {
apiClient;
/**
* Creates a new DocuSignConnectManager instance.
*
* @param apiClient - The Axios instance used for API requests
*/
constructor(apiClient) {
this.apiClient = apiClient;
}
/**
* Retrieves all webhook configurations for the client's DocuSign integration.
*
* @returns A promise that resolves to the list of webhook configurations
*
* @example
* ```typescript
* const webhooksResponse = await connectManager.getWebhooks();
*
* if (webhooksResponse.success) {
* console.log('DocuSign webhooks:', webhooksResponse.data);
* } else {
* console.error('Failed to get webhooks:', webhooksResponse.message);
* }
* ```
*/
async getWebhooks() {
try {
const response = await this.apiClient.get('/admin/docusign/connect');
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to retrieve DocuSign Connect webhooks');
}
}
/**
* Retrieves a specific webhook configuration by its GUID.
*
* @param webhookGuid - The unique identifier for the webhook configuration
* @returns A promise that resolves to the specific webhook configuration
*
* @example
* ```typescript
* const webhookGuid = 'd0381ee4-5b83-4d6b-9f37-177c2a6a79f2';
* const webhookResponse = await connectManager.getWebhook(webhookGuid);
*
* if (webhookResponse.success) {
* console.log('DocuSign webhook:', webhookResponse.data);
* } else {
* console.error('Failed to get webhook:', webhookResponse.message);
* }
* ```
*/
async getWebhook(webhookGuid) {
try {
const response = await this.apiClient.get(`/admin/docusign/connect/${webhookGuid}`);
return response.data;
}
catch (error) {
throw this.handleError(error, `Failed to retrieve DocuSign Connect webhook with GUID ${webhookGuid}`);
}
}
/**
* Creates a new DocuSign Connect webhook configuration.
*
* @param webhook - The webhook configuration to create
* @returns A promise that resolves to the created webhook configuration
*
* @example
* ```typescript
* const newWebhook = {
* webhookGuid: 'd0381ee4-5b83-4d6b-9f37-177c2a6a79f2',
* hmacSecret: '89c3444a-f4c2-4fda-98b8-59dc2fe3ea29',
* workflowId: 'electronic-signatures-workflow'
* };
*
* const webhookResponse = await connectManager.addWebhook(newWebhook);
*
* if (webhookResponse.success) {
* console.log('DocuSign webhook created:', webhookResponse.data);
* } else {
* console.error('Failed to create webhook:', webhookResponse.message);
* }
* ```
*/
async addWebhook(webhook) {
try {
const response = await this.apiClient.post('/admin/docusign/connect', webhook);
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to create DocuSign Connect webhook');
}
}
/**
* Updates an existing DocuSign Connect webhook configuration.
*
* @param webhookGuid - The unique identifier for the webhook configuration
* @param webhook - The partial webhook configuration to update
* @returns A promise that resolves to the updated webhook configuration
*
* @example
* ```typescript
* const webhookGuid = 'd0381ee4-5b83-4d6b-9f37-177c2a6a79f2';
* const webhookUpdate = {
* hmacSecret: 'updated-hmac-secret-value',
* workflowId: 'updated-workflow-id'
* };
*
* const webhookResponse = await connectManager.updateWebhook(webhookGuid, webhookUpdate);
*
* if (webhookResponse.success) {
* console.log('DocuSign webhook updated:', webhookResponse.data);
* } else {
* console.error('Failed to update webhook:', webhookResponse.message);
* }
* ```
*/
async updateWebhook(webhookGuid, webhook) {
try {
const response = await this.apiClient.put(`/admin/docusign/connect/${webhookGuid}`, webhook);
return response.data;
}
catch (error) {
throw this.handleError(error, `Failed to update DocuSign Connect webhook with GUID ${webhookGuid}`);
}
}
/**
* Deletes a DocuSign Connect webhook configuration.
*
* @param webhookGuid - The unique identifier for the webhook configuration
* @returns A promise that resolves to the deletion response
*
* @example
* ```typescript
* const webhookGuid = 'd0381ee4-5b83-4d6b-9f37-177c2a6a79f2';
* const deleteResponse = await connectManager.deleteWebhook(webhookGuid);
*
* if (deleteResponse.success) {
* console.log('DocuSign webhook deleted:', deleteResponse.message);
* } else {
* console.error('Failed to delete webhook:', deleteResponse.message);
* }
* ```
*/
async deleteWebhook(webhookGuid) {
try {
const response = await this.apiClient.delete(`/admin/docusign/connect/${webhookGuid}`);
return response.data;
}
catch (error) {
throw this.handleError(error, `Failed to delete DocuSign Connect webhook with GUID ${webhookGuid}`);
}
}
/**
* Handles errors from API calls and formats them for the client.
*
* @param error - The error object from the failed API call
* @param defaultMessage - A default message to use if no specific message is available
* @returns A VisionFiError with the appropriate message and status code
*/
handleError(error, defaultMessage) {
if (error instanceof VisionFiError) {
return error;
}
// Use the error message from the response if available, otherwise use default
const message = error.response?.data?.message || error.message || defaultMessage;
const statusCode = error.response?.status;
const code = error.response?.data?.code || 'docusign_connect_error';
return new VisionFiError(message, statusCode, code);
}
}