All files / src/sdk/models backendClient.ts

80.95% Statements 17/21
37.5% Branches 6/16
75% Functions 3/4
80.95% Lines 17/21

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 11517x 17x 17x 17x 17x         17x                                                 22x 22x 22x 22x                       22x 1x                       21x 1x                 20x                                             20x                               14x      
import { Client, createClient, createConfig } from "@hey-api/client-axios";
import apiClient from "../client/client";
import { CEG } from "../utils/error";
import { COMPOSIO_SDK_ERROR_CODES } from "../utils/errors/src/constants";
import { removeTrailingSlashIfExists } from "../utils/string";
 
/**
 * Class representing the details required to initialize and configure the API client.
 */
export class AxiosBackendClient {
  /**
   * The API key used for authenticating requests.
   */
  public apiKey: string;
 
  /**
   * The base URL of the API against which requests will be made.
   */
  public baseUrl: string;
 
  /**
   * The runtime environment where the client is being used.
   */
  public runtime: string;
  public instance: Client;
 
  /**
   * Creates an instance of apiClientDetails.
   * @param {string} apiKey - The API key for client initialization.
   * @param {string} baseUrl - The base URL for the API client.
   * @param {string} runtime - The runtime environment identifier.
   * @throws Will throw an error if the API key is not provided.
   */
  constructor(apiKey: string, baseUrl: string, runtime?: string) {
    this.runtime = runtime || "";
    this.apiKey = apiKey;
    this.baseUrl = removeTrailingSlashIfExists(baseUrl);
    this.instance = createClient(
      createConfig({
        baseURL: this.baseUrl,
        headers: {
          // common: {
          "X-API-KEY": `${this.apiKey}`,
          "X-SOURCE": "js_sdk",
          "X-RUNTIME": this.runtime,
          // }
        },
      })
    );
    if (!apiKey) {
      throw CEG.getCustomError(
        COMPOSIO_SDK_ERROR_CODES.COMMON.API_KEY_UNAVAILABLE,
        {
          message: "API key is not available",
          description:
            "The API key required for authentication is not provided. You can get the API key from the Composio dashboard.",
          possibleFix: "Please provide the API key in the constructor",
        }
      );
    }
 
    // Validate baseUrl
    if (!baseUrl.startsWith("http://") && !baseUrl.startsWith("https://")) {
      throw CEG.getCustomError(
        COMPOSIO_SDK_ERROR_CODES.COMMON.BASE_URL_NOT_REACHABLE,
        {
          message: `🔗 Base URL ${baseUrl} is not valid`,
          description: "The composio backend URL provided is not valid",
        }
      );
    }
 
    this.initializeApiClient();
  }
 
  /**
   * Retrieves the client ID from the user's information.
   * @returns {Promise<string>} A promise that resolves to the client ID.
   * @throws Will throw an error if the HTTP request fails.
   */
  public async getClientId(): Promise<string> {
    try {
      const { data } = await apiClient.clientAuth.getUserInfo({
        client: this.instance,
      });
      return data?.client?.id || "";
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
  /**
   * Initializes the API client with the provided configuration.
   * @private
   */
  private initializeApiClient() {
    this.instance.setConfig({
      baseURL: removeTrailingSlashIfExists(this.baseUrl),
      headers: {
        common: {
          "X-API-KEY": `${this.apiKey}`,
          "X-SOURCE": "js_sdk",
          "X-RUNTIME": this.runtime,
        },
      },
      throwOnError: true,
    });
 
    // setAxiosClientConfig(this.instance.instance);
  }
 
  getAxiosInstance() {
    return this.instance.instance;
  }
}