-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
31 lines (27 loc) · 934 Bytes
/
server.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
import type { RequestHandler } from "express";
import type { PayTRClient } from "./client";
interface CallbackOptions {
errorHandler: "RespondBadRequest" | { redirectTo: string } | RequestHandler;
}
export function callback(
client: PayTRClient,
options: CallbackOptions = { errorHandler: "RespondBadRequest" }
): RequestHandler {
let onError: RequestHandler;
if (options.errorHandler === "RespondBadRequest") {
onError = (_, res) => res.status(400);
} else if (typeof options.errorHandler === "object") {
onError = (_, res) => res.redirect((<any>options.errorHandler).redirectTo);
} else if (typeof options.errorHandler === "function") {
onError = options.errorHandler;
} else {
throw Error("Options error handler value is not supported.");
}
return function (req, res, next) {
if (client.validateCallback(req.body)) {
next();
} else {
onError(req, res, next);
}
};
}