All files / sdk/models apps.ts

87.17% Statements 34/39
70% Branches 14/20
100% Functions 7/7
91.66% Lines 33/36

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  13x 13x                                             13x     51x                         6x 6x 6x                               9x 9x         8x 8x   1x         2x 2x 2x 2x 4x   2x   2x               4x 4x           4x 14x 14x   14x 8x 4x   4x     6x         2x                   1x 1x 1x              
import {  AppListResDTO, SingleAppInfoResDTO } from "../client";
import apiClient from "../client/client"
import { CEG } from "../utils/error";
 
import { BackendClient } from "./backendClient";
 
export type GetAppData = {
    appKey: string;
};
 
export type GetAppResponse = SingleAppInfoResDTO;
 
export type ListAllAppsResponse = AppListResDTO;
 
export type RequiredParamsResponse = {
    required_fields: string[];
    expected_from_user: string[];
    optional_fields: string[];
};
 
export type RequiredParamsFullResponse = {
    availableAuthSchemes: string[];
    authSchemes: Record<string, RequiredParamsResponse>;
};
 
export class Apps {
    backendClient: BackendClient;
    constructor(backendClient: BackendClient) {
        this.backendClient = backendClient;
    }
 
 
    /**
     * 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<AppListResDTO>} A promise that resolves to the list of all apps.
     * @throws {ApiError} If the request fails.
     */
    async list(){
        try {
            const {data} = await apiClient.apps.getApps();
            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 {GetAppData} data The data for the request, including the app's unique key.
     * @returns {CancelablePromise<GetAppResponse>} A promise that resolves to the details of the app.
     * @throws {ApiError} If the request fails.
     */
    async get(data: GetAppData){  
        try {
            const {data:response} = await apiClient.apps.getApp({
                path: {
                    appName: data.appKey
                }
            });
            Iif(!response) throw new Error("App not found");
            return response;
        } catch (error) {
            throw CEG.handleAllError(error);
        }
    }
 
    async getRequiredParams(appId: string): Promise<RequiredParamsFullResponse> {
        try {
            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, RequiredParamsResponse> = {};
 
            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);
        }
    }
 
    async getRequiredParamsForAuthScheme(appId: string, authScheme: string): Promise<RequiredParamsResponse> {
        try {
            const params = await this.getRequiredParams(appId);
            return params.authSchemes[authScheme];
        } catch (error) {
            throw CEG.handleAllError(error);
        }
    }
}