Flowise

Форк
0
90 строк · 3.8 Кб
1
import { cloneDeep } from 'lodash'
2
import { StatusCodes } from 'http-status-codes'
3
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
4
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
5
import { getErrorMessage } from '../../errors/utils'
6

7
// Get all component credentials
8
const getAllComponentsCredentials = async (): Promise<any> => {
9
    try {
10
        const appServer = getRunningExpressApp()
11
        const dbResponse = []
12
        for (const credName in appServer.nodesPool.componentCredentials) {
13
            const clonedCred = cloneDeep(appServer.nodesPool.componentCredentials[credName])
14
            dbResponse.push(clonedCred)
15
        }
16
        return dbResponse
17
    } catch (error) {
18
        throw new InternalFlowiseError(
19
            StatusCodes.INTERNAL_SERVER_ERROR,
20
            `Error: componentsCredentialsService.getAllComponentsCredentials - ${getErrorMessage(error)}`
21
        )
22
    }
23
}
24

25
const getComponentByName = async (credentialName: string) => {
26
    try {
27
        const appServer = getRunningExpressApp()
28
        if (!credentialName.includes('&amp;')) {
29
            if (Object.prototype.hasOwnProperty.call(appServer.nodesPool.componentCredentials, credentialName)) {
30
                return appServer.nodesPool.componentCredentials[credentialName]
31
            } else {
32
                throw new InternalFlowiseError(
33
                    StatusCodes.NOT_FOUND,
34
                    `Error: componentsCredentialsService.getSingleComponentsCredential - Credential ${credentialName} not found`
35
                )
36
            }
37
        } else {
38
            const dbResponse = []
39
            for (const name of credentialName.split('&amp;')) {
40
                if (Object.prototype.hasOwnProperty.call(appServer.nodesPool.componentCredentials, name)) {
41
                    dbResponse.push(appServer.nodesPool.componentCredentials[name])
42
                } else {
43
                    throw new InternalFlowiseError(
44
                        StatusCodes.NOT_FOUND,
45
                        `Error: componentsCredentialsService.getSingleComponentsCredential - Credential ${name} not found`
46
                    )
47
                }
48
            }
49
            return dbResponse
50
        }
51
    } catch (error) {
52
        throw new InternalFlowiseError(
53
            StatusCodes.INTERNAL_SERVER_ERROR,
54
            `Error: componentsCredentialsService.getSingleComponentsCredential - ${getErrorMessage(error)}`
55
        )
56
    }
57
}
58

59
// Returns specific component credential icon via name
60
const getSingleComponentsCredentialIcon = async (credentialName: string) => {
61
    try {
62
        const appServer = getRunningExpressApp()
63
        if (Object.prototype.hasOwnProperty.call(appServer.nodesPool.componentCredentials, credentialName)) {
64
            const credInstance = appServer.nodesPool.componentCredentials[credentialName]
65
            if (credInstance.icon === undefined) {
66
                throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialName} icon not found`)
67
            }
68

69
            if (credInstance.icon.endsWith('.svg') || credInstance.icon.endsWith('.png') || credInstance.icon.endsWith('.jpg')) {
70
                const filepath = credInstance.icon
71
                return filepath
72
            } else {
73
                throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Credential ${credentialName} icon is missing icon`)
74
            }
75
        } else {
76
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialName} not found`)
77
        }
78
    } catch (error) {
79
        throw new InternalFlowiseError(
80
            StatusCodes.INTERNAL_SERVER_ERROR,
81
            `Error: componentsCredentialsService.getSingleComponentsCredentialIcon - ${getErrorMessage(error)}`
82
        )
83
    }
84
}
85

86
export default {
87
    getAllComponentsCredentials,
88
    getComponentByName,
89
    getSingleComponentsCredentialIcon
90
}
91

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.