12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.PRAS = void 0;
- class PRAS {
- getPRASResultObject(statusCode = 200, status = "OK", message = "", data = null, code = "UNKNOWN_CODE", extra = null) {
- return {
- statusCode,
- status,
- message,
- data,
- code,
- extra,
- };
- }
- responseMiddleware() {
- return (req, res, next) => {
- res.pras = {
- ok: (message, data, code, extra) => {
- res.json(this.getPRASResultObject(200, "OK", message, data, code, extra));
- },
- created: (message, data, code, extra) => {
- res.json(this.getPRASResultObject(201, "OK", message, data, code, extra));
- },
- badRequest: (message, data, code, extra) => {
- res.json(this.getPRASResultObject(400, "FAIL", message, data, code, extra));
- },
- unauthorized: (message, data, code, extra) => {
- res.json(this.getPRASResultObject(401, "FAIL", message, data, code, extra));
- },
- forbidden: (message, data, code, extra) => {
- res.json(this.getPRASResultObject(403, "FAIL", message, data, code, extra));
- },
- notFound: (message, data, code, extra) => {
- res.json(this.getPRASResultObject(404, "FAIL", message, data, code, extra));
- },
- serverError: (message, data, code, extra) => {
- res.json(this.getPRASResultObject(500, "ERROR", message, data, code, extra));
- },
- customMessage: (statusCode, message, data, code, extra) => {
- let status;
- if (statusCode >= 500) {
- status = "ERROR";
- }
- else if (statusCode >= 400) {
- status = "FAIL";
- }
- else {
- status = "OK";
- }
- res.json(this.getPRASResultObject(statusCode, status, message, data, code, extra));
- },
- };
- if (req.rawHeaders.hasOwnProperty("Accept") &&
- /application\/json/.test(req.rawHeaders["Accept"])) {
- const send = res.send;
- res.send = (body) => {
- try {
- if (typeof body === "string") {
- body = JSON.parse(body);
- }
- if (typeof body === "string") {
- body = { data: body };
- }
- else if (!body.hasOwnProperty("data")) {
- body = { data: body };
- }
- const prasBody = this.getPRASResultObject(body.statusCode, body.status, body.message, body.data, body.code, body.extra);
- res.status(prasBody.statusCode || 200);
- delete prasBody.statusCode;
- return send.call(res, JSON.stringify(prasBody));
- }
- catch (err) {
- return send.call(res, body);
- }
- };
- }
- next();
- };
- }
- }
- exports.PRAS = PRAS;
- exports.default = new PRAS();
|