All files / sdk/models connectedAccounts.ts

53.57% Statements 15/28
52.94% Branches 9/17
57.14% Functions 8/14
53.84% Lines 14/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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122    10x 10x     10x       16x       11x     11x                       7x   7x           1x     1x       10x                       1x 1x 1x                                                                                                                              
 
import { ConnectionsControllerGetConnectionsData, ConnectionsControllerGetConnectionData, ConnectionsControllerGetConnectionsResponse, GetConnectionsResponseDto } from "../client";
import client from "../client/client";
import apiClient from "../client/client"
import { BackendClient } from "./backendClient";
 
export class ConnectedAccounts {
    backendClient: BackendClient;
 
    constructor(backendClient: BackendClient) {
        this.backendClient = backendClient; 
    }
    
    list(data: Record<string, any> = {}): Promise<GetConnectionsResponseDto>{
        return apiClient.connections.getConnections({
            query: data
        }).then(res=>{
            return res.data!
        })
 
    }
 
     create(data: any = {}): any {
        return apiClient.connections.initiateConnection({
            body: data
        }).then(res=>res.data)
    }
 
    get(data: { connectedAccountId :string}) {
        return apiClient.connections.getConnection({
            path: data
        }).then(res => res.data)
    }
 
    async initiate(
        data: any
    ): Promise<ConnectionRequest> {
        const res =  await client.connections.initiateConnection({ body: data }).then(res => res.data)
 
        //@ts-ignore
        return new ConnectionRequest(res?.connectionStatus!, res?.connectedAccountId!, res?.redirectUrl!)
    }
}
 
export class ConnectionRequest {
    connectionStatus: string;
    connectedAccountId: string;
    redirectUrl: string | null;
 
    /**
     * Connection request model.
     * @param {string} connectionStatus The status of the connection.
     * @param {string} connectedAccountId The unique identifier of the connected account.
     * @param {string} [redirectUrl] The redirect URL for completing the connection flow.
     */
    constructor(connectionStatus: string, connectedAccountId: string, redirectUrl: string | null = null) {
        this.connectionStatus = connectionStatus;
        this.connectedAccountId = connectedAccountId;
        this.redirectUrl = redirectUrl;
    }
 
    /**
     * Save user access data.
     * @param {Composio} client The Composio client instance.
     * @param {Object} data The data to save.
     * @param {Object} data.fieldInputs The field inputs to save.
     * @param {string} [data.redirectUrl] The redirect URL for completing the connection flow.
     * @param {string} [data.entityId] The entity ID associated with the user.
     * @returns {Promise<Object>} The response from the server.
     */
    async saveUserAccessData(data: {
        fieldInputs: Record<string, string>;
        redirectUrl?: string;
        entityId?: string;
    }) {
        const connectedAccount = await apiClient.connections.getConnection({
            path:{
               connectedAccountId: this.connectedAccountId
            }
        });
        return apiClient.connections.initiateConnection({
            body: {
                // @ts-ignore
                integrationId: connectedAccount.integrationId,
                //@ts-ignore
                data: data.fieldInputs,
                redirectUri: data.redirectUrl,
                userUuid: data.entityId,
            } 
        });
    }
 
    /**
     * Wait until the connection becomes active.
     * @param {Composio} client The Composio client instance.
     * @param {number} [timeout=60] The timeout period in seconds.
     * @returns {Promise<ConnectedAccountModel>} The connected account model.
     * @throws {ComposioClientError} If the connection does not become active within the timeout period.
     */
    async waitUntilActive(timeout = 60) {
        const startTime = Date.now();
        while (Date.now() - startTime < timeout * 1000) {
            // @ts-ignore
            const connection = await apiClient.connections.getConnection({
                path: {
                    connectedAccountId: this.connectedAccountId
                }
            }).then(res=>res.data);
            //@ts-ignore
            Iif (connection.status === 'ACTIVE') {
                return connection;
            }
            await new Promise(resolve => setTimeout(resolve, 1000));
        }
 
        throw new Error(
            'Connection did not become active within the timeout period.'
        );
    }
}