Skip to content

Commit ae60eb3

Browse files
committed
✨ Add possibility to add custom routes
1 parent 72a5efc commit ae60eb3

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/index.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
IntegrationEntityBridgeRequest,
1212
} from "./models";
1313
import { CustomRouter } from "./models/custom-router.model";
14+
import { CustomRoute } from "./models/custom-routes.model";
1415
import { getContactCache } from "./util/get-contact-cache";
16+
import { errorLogger, infoLogger } from "./util";
1517

1618
const PORT: number = Number(process.env.PORT) || 8080;
1719

@@ -32,7 +34,8 @@ let cache: ContactCache | null = null;
3234

3335
export function start(
3436
adapter: Adapter,
35-
customRouters: CustomRouter[] = []
37+
customRouters: CustomRouter[] = [],
38+
customRoutes: CustomRoute[] = []
3639
): Server {
3740
cache = getContactCache();
3841

@@ -93,6 +96,46 @@ export function start(
9396

9497
customRouters.forEach(({ path, router }) => app.use(path, router));
9598

99+
customRoutes.forEach(({ requestType, path, handler: customHandler }) => {
100+
infoLogger("start", `CustomRoute ${path} added.`, undefined);
101+
102+
const handler = (req: any, res: any, next: any) => {
103+
try {
104+
infoLogger(path, "START", undefined);
105+
106+
customHandler(req, res, next);
107+
108+
infoLogger(path, "END", undefined);
109+
} catch (error) {
110+
errorLogger(
111+
path,
112+
"Error while executing custom route",
113+
undefined,
114+
error ?? undefined
115+
);
116+
next(error);
117+
}
118+
};
119+
120+
switch (requestType) {
121+
case "get":
122+
app.get(path, handler);
123+
break;
124+
case "post":
125+
app.post(path, handler);
126+
break;
127+
case "put":
128+
app.put(path, handler);
129+
break;
130+
case "delete":
131+
app.delete(path, handler);
132+
break;
133+
134+
default:
135+
throw `CustomRoute requestType ${requestType} not implemented!`;
136+
}
137+
});
138+
96139
return app.listen(PORT, () => console.log(`Listening on port ${PORT}`));
97140
}
98141

src/models/custom-routes.model.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { RequestHandler } from "express";
2+
3+
export type CustomRoute = {
4+
requestType: "get" | "post" | "put" | "delete";
5+
path: string;
6+
handler: RequestHandler;
7+
};

0 commit comments

Comments
 (0)