Flowise
87 строк · 2.9 Кб
1import { Request, Response, NextFunction } from 'express'
2import credentialsService from '../../services/credentials'
3import { InternalFlowiseError } from '../../errors/internalFlowiseError'
4import { StatusCodes } from 'http-status-codes'
5
6const createCredential = async (req: Request, res: Response, next: NextFunction) => {
7try {
8if (!req.body) {
9throw new InternalFlowiseError(
10StatusCodes.PRECONDITION_FAILED,
11`Error: credentialsController.createCredential - body not provided!`
12)
13}
14const apiResponse = await credentialsService.createCredential(req.body)
15return res.json(apiResponse)
16} catch (error) {
17next(error)
18}
19}
20
21const deleteCredentials = async (req: Request, res: Response, next: NextFunction) => {
22try {
23if (typeof req.params === 'undefined' || !req.params.id) {
24throw new InternalFlowiseError(
25StatusCodes.PRECONDITION_FAILED,
26`Error: credentialsController.deleteCredentials - id not provided!`
27)
28}
29const apiResponse = await credentialsService.deleteCredentials(req.params.id)
30return res.json(apiResponse)
31} catch (error) {
32next(error)
33}
34}
35
36const getAllCredentials = async (req: Request, res: Response, next: NextFunction) => {
37try {
38const apiResponse = await credentialsService.getAllCredentials(req.query.credentialName)
39return res.json(apiResponse)
40} catch (error) {
41next(error)
42}
43}
44
45const getCredentialById = async (req: Request, res: Response, next: NextFunction) => {
46try {
47if (typeof req.params === 'undefined' || !req.params.id) {
48throw new InternalFlowiseError(
49StatusCodes.PRECONDITION_FAILED,
50`Error: credentialsController.getCredentialById - id not provided!`
51)
52}
53const apiResponse = await credentialsService.getCredentialById(req.params.id)
54return res.json(apiResponse)
55} catch (error) {
56next(error)
57}
58}
59
60const updateCredential = async (req: Request, res: Response, next: NextFunction) => {
61try {
62if (typeof req.params === 'undefined' || !req.params.id) {
63throw new InternalFlowiseError(
64StatusCodes.PRECONDITION_FAILED,
65`Error: credentialsController.updateCredential - id not provided!`
66)
67}
68if (!req.body) {
69throw new InternalFlowiseError(
70StatusCodes.PRECONDITION_FAILED,
71`Error: credentialsController.updateCredential - body not provided!`
72)
73}
74const apiResponse = await credentialsService.updateCredential(req.params.id, req.body)
75return res.json(apiResponse)
76} catch (error) {
77next(error)
78}
79}
80
81export default {
82createCredential,
83deleteCredentials,
84getAllCredentials,
85getCredentialById,
86updateCredential
87}
88