index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import type { Request, Response } from "express";
  2. interface PRASResponse extends Response {
  3. pras: {
  4. ok: (message: string, data: any, code: string, extra: any) => void;
  5. created: (message: string, data: any, code: string, extra: any) => void;
  6. badRequest: (message: string, data: any, code: string, extra: any) => void;
  7. unauthorized: (
  8. message: string,
  9. data: any,
  10. code: string,
  11. extra: any
  12. ) => void;
  13. forbidden: (message: string, data: any, code: string, extra: any) => void;
  14. notFound: (message: string, data: any, code: string, extra: any) => void;
  15. serverError: (message: string, data: any, code: string, extra: any) => void;
  16. customMessage: (
  17. statusCode: number,
  18. message: string,
  19. data: any,
  20. code: string,
  21. extra: any
  22. ) => void;
  23. };
  24. }
  25. export class PRAS {
  26. private getPRASResultObject(
  27. statusCode = 200,
  28. status = "OK",
  29. message = "",
  30. data: any = null,
  31. code = "UNKNOWN_CODE",
  32. extra: any = null
  33. ): any {
  34. return {
  35. statusCode,
  36. status,
  37. message,
  38. data,
  39. code,
  40. extra,
  41. };
  42. }
  43. public responseMiddleware() {
  44. // eslint-disable-next-line @typescript-eslint/ban-types
  45. return (req: Request, res: PRASResponse, next: Function): void => {
  46. res.pras = {
  47. ok: (message, data, code, extra) => {
  48. res.json(
  49. this.getPRASResultObject(200, "OK", message, data, code, extra)
  50. );
  51. },
  52. created: (message, data, code, extra) => {
  53. res.json(
  54. this.getPRASResultObject(201, "OK", message, data, code, extra)
  55. );
  56. },
  57. badRequest: (message, data, code, extra) => {
  58. res.json(
  59. this.getPRASResultObject(400, "FAIL", message, data, code, extra)
  60. );
  61. },
  62. unauthorized: (message, data, code, extra) => {
  63. res.json(
  64. this.getPRASResultObject(401, "FAIL", message, data, code, extra)
  65. );
  66. },
  67. forbidden: (message, data, code, extra) => {
  68. res.json(
  69. this.getPRASResultObject(403, "FAIL", message, data, code, extra)
  70. );
  71. },
  72. notFound: (message, data, code, extra) => {
  73. res.json(
  74. this.getPRASResultObject(404, "FAIL", message, data, code, extra)
  75. );
  76. },
  77. serverError: (message, data, code, extra) => {
  78. res.json(
  79. this.getPRASResultObject(500, "ERROR", message, data, code, extra)
  80. );
  81. },
  82. customMessage: (statusCode, message, data, code, extra) => {
  83. let status;
  84. if (statusCode >= 500) {
  85. status = "ERROR";
  86. } else if (statusCode >= 400) {
  87. status = "FAIL";
  88. } else {
  89. status = "OK";
  90. }
  91. res.json(
  92. this.getPRASResultObject(
  93. statusCode,
  94. status,
  95. message,
  96. data,
  97. code,
  98. extra
  99. )
  100. );
  101. },
  102. };
  103. if (
  104. req.rawHeaders.hasOwnProperty("Accept") &&
  105. /application\/json/.test(req.rawHeaders["Accept"])
  106. ) {
  107. const send = res.send;
  108. res.send = (body) => {
  109. try {
  110. if (typeof body === "string") {
  111. body = JSON.parse(body);
  112. }
  113. if (typeof body === "string") {
  114. body = { data: body };
  115. } else if (!body.hasOwnProperty("data")) {
  116. body = { data: body };
  117. }
  118. const prasBody = this.getPRASResultObject(
  119. body.statusCode,
  120. body.status,
  121. body.message,
  122. body.data,
  123. body.code,
  124. body.extra
  125. );
  126. res.status(prasBody.statusCode || 200);
  127. delete prasBody.statusCode;
  128. return send.call(res, JSON.stringify(prasBody));
  129. } catch (err) {
  130. return send.call(res, body);
  131. }
  132. };
  133. }
  134. next();
  135. };
  136. }
  137. }
  138. export default new PRAS();