Flowise

Форк
0
68 строк · 2.4 Кб
1
import { StatusCodes } from 'http-status-codes'
2
import { addAPIKey, deleteAPIKey, getAPIKeys, updateAPIKey } from '../../utils/apiKey'
3
import { addChatflowsCount } from '../../utils/addChatflowsCount'
4
import { getApiKey } from '../../utils/apiKey'
5
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
6
import { getErrorMessage } from '../../errors/utils'
7

8
const getAllApiKeys = async () => {
9
    try {
10
        const keys = await getAPIKeys()
11
        const dbResponse = await addChatflowsCount(keys)
12
        return dbResponse
13
    } catch (error) {
14
        throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.getAllApiKeys - ${getErrorMessage(error)}`)
15
    }
16
}
17

18
const createApiKey = async (keyName: string) => {
19
    try {
20
        const keys = await addAPIKey(keyName)
21
        const dbResponse = await addChatflowsCount(keys)
22
        return dbResponse
23
    } catch (error) {
24
        throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.createApiKey - ${getErrorMessage(error)}`)
25
    }
26
}
27

28
// Update api key
29
const updateApiKey = async (id: string, keyName: string) => {
30
    try {
31
        const keys = await updateAPIKey(id, keyName)
32
        const dbResponse = await addChatflowsCount(keys)
33
        return dbResponse
34
    } catch (error) {
35
        throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.updateApiKey - ${getErrorMessage(error)}`)
36
    }
37
}
38

39
const deleteApiKey = async (id: string) => {
40
    try {
41
        const keys = await deleteAPIKey(id)
42
        const dbResponse = await addChatflowsCount(keys)
43
        return dbResponse
44
    } catch (error) {
45
        throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.deleteApiKey - ${getErrorMessage(error)}`)
46
    }
47
}
48

49
const verifyApiKey = async (paramApiKey: string): Promise<any> => {
50
    try {
51
        const apiKey = await getApiKey(paramApiKey)
52
        if (!apiKey) {
53
            throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, `Unauthorized`)
54
        }
55
        const dbResponse = 'OK'
56
        return dbResponse
57
    } catch (error) {
58
        throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.verifyApiKey - ${getErrorMessage(error)}`)
59
    }
60
}
61

62
export default {
63
    createApiKey,
64
    deleteApiKey,
65
    getAllApiKeys,
66
    updateApiKey,
67
    verifyApiKey
68
}
69

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

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

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

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