All files / shared/utils/src/api-clients tavus-api-wrapper.ts

0% Statements 0/27
0% Branches 0/4
0% Functions 0/7
0% Lines 0/27

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135                                                                                                                                                                                                                                                                             
/**
 * Tavus API Wrapper - Ensures all API calls to tavus.io are logged
 * 
 * This wrapper provides a centralized way to make API calls to tavus.io
 * while ensuring they are all logged through the TavusClient logging system.
 * Use this instead of direct fetch calls to tavus.io APIs.
 */
 
import { getTavusLogger } from './tavus-logger-browser';
import { getTavusApiKey } from '@buzztrail/shared-config';
 
export interface TavusApiRequest {
  method: string;
  url: string;
  headers?: Record<string, string>;
  body?: unknown;
}
 
export interface TavusApiResponse {
  status: number;
  statusText: string;
  headers: Record<string, string>;
  data: unknown;
  ok: boolean;
}
 
/**
 * Make a logged API call to tavus.io
 * This ensures all API calls are logged through the TavusClient logging system
 */
export async function makeTavusApiCall(request: TavusApiRequest): Promise<TavusApiResponse> {
  const logger = getTavusLogger();
  const apiKey = getTavusApiKey();
  
  // Prepare headers
  const headers = {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
    ...request.headers
  };
  
  // Log the request
  const requestId = logger.logRequest(request.method, request.url, headers, request.body);
  
  try {
    const response = await fetch(request.url, {
      method: request.method,
      headers,
      body: request.body ? JSON.stringify(request.body) : undefined
    });
    
    const data = await response.json();
    
    // Convert headers to plain object
    const responseHeaders: Record<string, string> = {};
    response.headers.forEach((value, key) => {
      responseHeaders[key] = value;
    });
    
    // Log the response
    logger.logResponse(requestId, response.status, responseHeaders, data, response);
    
    return {
      status: response.status,
      statusText: response.statusText,
      headers: responseHeaders,
      data,
      ok: response.ok
    };
  } catch (error) {
    // Log error response
    logger.logResponse(requestId, 0, {}, { error: error instanceof Error ? error.message : 'Unknown error' });
    
    throw error;
  }
}
 
/**
 * Convenience function for GET requests
 */
export async function tavusGet(url: string, headers?: Record<string, string>): Promise<TavusApiResponse> {
  return makeTavusApiCall({
    method: 'GET',
    url,
    headers,
  });
}
 
/**
 * Convenience function for POST requests
 */
export async function tavusPost(url: string, body?: unknown, headers?: Record<string, string>): Promise<TavusApiResponse> {
  return makeTavusApiCall({
    method: 'POST',
    url,
    headers,
    body,
  });
}
 
/**
 * Convenience function for PUT requests
 */
export async function tavusPut(url: string, body?: unknown, headers?: Record<string, string>): Promise<TavusApiResponse> {
  return makeTavusApiCall({
    method: 'PUT',
    url,
    headers,
    body,
  });
}
 
/**
 * Convenience function for DELETE requests
 */
export async function tavusDelete(url: string, headers?: Record<string, string>): Promise<TavusApiResponse> {
  return makeTavusApiCall({
    method: 'DELETE',
    url,
    headers,
  });
}
 
/**
 * Convenience function for PATCH requests
 */
export async function tavusPatch(url: string, body?: unknown, headers?: Record<string, string>): Promise<TavusApiResponse> {
  return makeTavusApiCall({
    method: 'PATCH',
    url,
    headers,
    body,
  });
}