-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
137 lines (100 loc) · 3.07 KB
/
app.ts
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
133
134
135
136
137
import express, { NextFunction, Response, Request, Router } from "express";
import { Logger } from "winston";
import bodyParser from "body-parser";
import cors from "cors";
import { Integration } from "./types";
type Dependencies = {
logger: Logger;
integration: Integration;
};
export const buildApp = (dependencies: Dependencies) => {
const { logger, integration } = dependencies;
const integrationRouter = Router();
integrationRouter.get("/manifest", (req, res) => {
res.json(integration.manifest);
});
integrationRouter.post("/authenticate", async (req, res) => {
const result = await integration.authenticate(req.body);
res.json(result);
});
integrationRouter.post("/listUsers", async (req, res) => {
if (integration.listUsers === undefined) {
res.status(404);
return;
}
const result = await integration.listUsers(req.body);
res.json(result);
});
integrationRouter.post("/actions/:slug/validate", async (req, res) => {
const { slug } = req.params;
if (integration.actions[slug] === undefined) {
res.status(404);
return;
}
const result = await integration.actions[slug].validate(req.body);
res.json(result);
});
integrationRouter.post("/actions/:slug/execute", async (req, res) => {
const { slug } = req.params;
if (integration.actions[slug] === undefined) {
res.status(404);
return;
}
const result = await integration.actions[slug].execute(req.body);
res.json(result);
});
integrationRouter.post("/model/fetch", async (req, res) => {
if (integration.model === undefined) {
res.status(404);
return;
}
const result = await integration.model.fetch(req.body);
res.json(result);
});
integrationRouter.post("/model/count", async (req, res) => {
if (
integration.model === undefined ||
integration.model.count === undefined
) {
res.status(404);
return;
}
const result = await integration.model.count(req.body);
res.json(result);
});
integrationRouter.post("/autocomplete", async (req, res) => {
if (integration.autocomplete === undefined) {
res.status(404);
return;
}
const result = await integration.autocomplete(req.body);
res.json(result);
});
integrationRouter.post("/getDynamicSchema", async (req, res) => {
if (integration.getDynamicSchema === undefined) {
res.status(404);
return;
}
const result = await integration.getDynamicSchema(req.body);
res.json(result);
});
integrationRouter.post("/completeOauth", async (req, res) => {
if (integration.completeOauth === undefined) {
res.status(404);
return;
}
const result = await integration.completeOauth(req.body);
res.json(result);
});
const app = express();
app.use(bodyParser.json());
app.use(cors());
app.use("/", integrationRouter);
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
logger.error(err);
res.status(500).json({
errorMessage: "Internal Server Error",
});
});
return app;
};