All files / src/sdk/models apps.ts

90.19% Statements 46/51
72.72% Branches 16/22
100% Functions 7/7
93.87% Lines 46/49

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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208          13x 13x 13x 13x       13x                                                               13x     28x   28x 28x                       1x         1x 1x           1x                               10x         10x 10x           9x 9x   1x                               2x         2x 2x 2x 2x 2x   2x 4x   2x   2x               4x 4x           4x   14x 14x   14x 8x 4x   4x     6x         2x                                             1x         1x 1x 1x 1x 1x            
import {
  AppInfoResponseDto,
  AppListResDTO,
  SingleAppInfoResDTO,
} from "../client";
import apiClient from "../client/client";
import { CEG } from "../utils/error";
import { TELEMETRY_LOGGER } from "../utils/telemetry";
import { TELEMETRY_EVENTS } from "../utils/telemetry/events";
 
import { Client } from "@hey-api/client-axios";
import { z } from "zod";
import {
  ZGetAppParams,
  ZGetRequiredParams,
  ZGetRequiredParamsForAuthScheme,
  ZRequiredParamsFullResponse,
  ZRequiredParamsResponse,
} from "../types/app";
import { AxiosBackendClient } from "./backendClient";
 
// schema types generated from zod
export type AppGetRequiredParams = z.infer<typeof ZGetRequiredParams>;
export type AppGetRequiredParamsForAuthSchemeParam = z.infer<
  typeof ZGetRequiredParamsForAuthScheme
> & {
  appName?: string;
  /**
   * @deprecated use appName instead
   */
  appId?: string;
};
export type AppRequiredParamsFullResponse = z.infer<
  typeof ZRequiredParamsFullResponse
>;
export type AppRequiredParamsResponse = z.infer<typeof ZRequiredParamsResponse>;
export type AppGetDataParams = z.infer<typeof ZGetAppParams>;
 
// types generated from backend client
export type AppItemResponse = SingleAppInfoResDTO;
export type AppListResponse = AppItemListResponse[];
export type AppListRes = AppListResDTO;
export type AppItemListResponse = AppInfoResponseDto;
 
export class Apps {
  private backendClient: AxiosBackendClient;
  private client: Client;
  private fileName: string = "js/src/sdk/models/apps.ts";
  constructor(backendClient: AxiosBackendClient, client: Client) {
    this.backendClient = backendClient;
    this.client = client;
  }
 
  /**
   * Retrieves a list of all available apps in the Composio platform.
   *
   * This method allows clients to explore and discover the supported apps. It returns an array of app objects, each containing essential details such as the app's key, name, description, logo, categories, and unique identifier.
   *
   * @returns {Promise<AppItemListResponse[]>} A promise that resolves to the list of all apps.
   * @throws {ComposioError} If the request fails.
   */
  async list(): Promise<AppListResponse> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "list",
      file: this.fileName,
      params: {},
    });
    try {
      const { data } = await apiClient.apps.getApps({
        client: this.client,
        query: {
          additionalFields: "auth_schemes",
        },
      });
      return data?.items || [];
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
 
  /**
   * Retrieves details of a specific app in the Composio platform.
   *
   * This method allows clients to fetch detailed information about a specific app by providing its unique key. The response includes the app's name, key, status, description, logo, categories, authentication schemes, and other metadata.
   *
   * @param {AppGetDataParams} data The data for the request, including the app's unique key.
   * @returns {Promise<AppItemResponse>} A promise that resolves to the details of the app.
   * @throws {ComposioError} If the request fails.
   */
  async get(data: AppGetDataParams): Promise<AppItemResponse> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "get",
      file: this.fileName,
      params: { data },
    });
    try {
      const { data: response } = await apiClient.apps.getApp({
        client: this.client,
        path: {
          appName: data.appKey,
        },
      });
      Iif (!response) throw new Error("App not found");
      return response;
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
 
  /**
   * Retrieves the required parameters for a specific app in the Composio platform.
   *
   * This method allows clients to fetch the necessary parameters for a specific app by providing its unique key. The response includes the app's name, key, status, description, logo, categories, authentication schemes, and other metadata.
   *
   * @param {string} appId The unique key of the app.
   * @returns {Promise<AppRequiredParamsFullResponse>} A promise that resolves to the required parameters for the app.
   * @throws {ComposioError} If the request fails.
   */
  async getRequiredParams(
    appId: string
  ): Promise<AppRequiredParamsFullResponse> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "getRequiredParams",
      file: this.fileName,
      params: { appId },
    });
    try {
      ZGetRequiredParams.parse({ appId });
      const appData = await this.get({ appKey: appId });
      Iif (!appData) throw new Error("App not found");
      const authSchemes = appData.auth_schemes;
      const availableAuthSchemes = (
        authSchemes as Array<{ mode: string }>
      )?.map((scheme) => scheme?.mode);
 
      const authSchemesObject: Record<string, AppRequiredParamsResponse> = {};
 
      for (const scheme of authSchemes as Array<{
        mode: string;
        fields: Array<{
          name: string;
          required: boolean;
          expected_from_customer: boolean;
        }>;
      }>) {
        const name = scheme.mode;
        authSchemesObject[name] = {
          required_fields: [],
          optional_fields: [],
          expected_from_user: [],
        };
 
        scheme.fields.forEach((field) => {
          const isExpectedForIntegrationSetup =
            field.expected_from_customer === false;
          const isRequired = field.required;
 
          if (isExpectedForIntegrationSetup) {
            if (isRequired) {
              authSchemesObject[name].expected_from_user.push(field.name);
            } else {
              authSchemesObject[name].optional_fields.push(field.name);
            }
          } else {
            authSchemesObject[name].required_fields.push(field.name);
          }
        });
      }
 
      return {
        availableAuthSchemes,
        authSchemes: authSchemesObject,
      };
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
 
  /**
   * Retrieves the required parameters for a specific authentication scheme of an app in the Composio platform.
   *
   * This method allows clients to fetch the necessary parameters for a specific authentication scheme of an app by providing its unique key and the authentication scheme.
   *
   * @param {AppGetRequiredParamsForAuthSchemeParam} data The data for the request, including the app's unique key and the authentication scheme.
   * @returns {Promise<AppRequiredParamsResponse>} A promise that resolves to the required parameters for the authentication scheme.
   * @throws {ComposioError} If the request fails.
   */
  async getRequiredParamsForAuthScheme({
    appId,
    appName,
    authScheme,
  }: AppGetRequiredParamsForAuthSchemeParam): Promise<AppRequiredParamsResponse> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "getRequiredParamsForAuthScheme",
      file: this.fileName,
      params: { appId, authScheme },
    });
    try {
      const finalAppId = appName || appId;
      ZGetRequiredParamsForAuthScheme.parse({ appId: finalAppId, authScheme });
      const params = await this.getRequiredParams(finalAppId);
      return params.authSchemes[authScheme];
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
}