All files / src/api AuditLog.ts

100% Statements 26/26
90.47% Branches 19/21
100% Functions 3/3
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1022x 2x 2x   2x                   8x 6x 6x     6x 1x       5x 1x       4x 2x         4x                                 2x 5x 5x   5x 5x         3x                                               2x     7x 7x   7x 7x           5x    
import axios from "axios";
import { makeApiRequest } from "../request";
import { AUDIT_LOG_API_PATH, GET, PUT } from "../Constants";
import { AuditLogRequest, AuditLogResponse } from "../interfaces/response/AuditLogResponse";
import { Client } from "../Client";
 
/**
 * Handles common error responses for audit log API calls.
 *
 * @param {unknown} error - The error to handle
 * @param {boolean} handle409 - Whether to handle 409 Conflict status (returns data instead of throwing)
 * @return {AuditLogResponse | never} Returns response data for 409, otherwise throws error
 */
function handleAuditLogError(error: unknown, handle409: boolean = false): AuditLogResponse | never {
  if (axios.isAxiosError(error) && error.response) {
    const status = error.response.status;
    const responseData = error.response.data;
    
    // Handle 409 Conflict - configuration is already set to the requested values
    if (status === 409 && handle409) {
      return responseData as AuditLogResponse;
    }
    
    // Handle 400 Bad Request - invalid payload
    if (status === 400) {
      throw new Error(responseData?.message);
    }
    
    // Handle 403 Forbidden - insufficient permissions
    if (status === 403) {
      throw new Error(responseData?.message);
    }
  }
  
  // Re-throw any other errors
  throw error;
}
 
/**
 * Retrieves the audit log configuration for the organization.
 *
 * Controls whether the organization's API requests and responses are stored for auditing.
 * Organizations can disable this if they don't need their API calls to be audited.
 *
 * @export
 * @return {Promise<AuditLogResponse>} Current audit log configuration
 * @throws {Error} Throws error for 403 (Forbidden) or other failures
 *
 * @example
 * const config = await getConfig();
 * // Output: { logRequest: true, logResponse: false }
 */
export async function getAuditConfig(): Promise<AuditLogResponse> {
  const client = Client.getInstance();
  const url = client.getDomain() + AUDIT_LOG_API_PATH;
 
  try {
    return await makeApiRequest<AuditLogResponse>({
      method: GET,
      url,
    });
  } catch (error) {
    return handleAuditLogError(error);
  }
}
 
/**
 * Updates the audit log configuration for the organization.
 *
 * Controls whether the organization's API requests and responses are stored for auditing.
 * Organizations can disable this if they don't need their API calls to be audited.
 *
 * Note: If the configuration is already set to the requested values, the API will return
 * a 409 Conflict status with a message indicating no change was made. This is handled
 * gracefully and the current configuration is returned.
 *
 * @export
 * @param {AuditLogRequest} payload - Audit log configuration
 * @return {Promise<AuditLogResponse>} Updated configuration or current configuration if no change
 * @throws {Error} Throws error for 400 (Bad Request), 403 (Forbidden), or other failures
 *
 * @example
 * const result = await update({ logRequest: false, logResponse: false });
 * // If already set to these values:
 * // { logRequest: false, logResponse: false, message: "No change in audit log configuration" }
 */
export async function updateAuditConfig(
  payload: AuditLogRequest
): Promise<AuditLogResponse> {
  const client = Client.getInstance();
  const url = client.getDomain() + AUDIT_LOG_API_PATH;
 
  try {
    return await makeApiRequest<AuditLogResponse>({
      method: PUT,
      url,
      data: payload,
    });
  } catch (error) {
    return handleAuditLogError(error, true);
  }
}