|
@@ -26,20 +26,20 @@ interface PRASResponse extends Response {
|
|
|
|
|
|
export class PRAS {
|
|
|
private getPRASResultObject(
|
|
|
- statusCode: number,
|
|
|
- status: string,
|
|
|
- message: string,
|
|
|
- data: any,
|
|
|
- code: string,
|
|
|
- extra: any
|
|
|
+ statusCode = 200,
|
|
|
+ status = "OK",
|
|
|
+ message = "",
|
|
|
+ data = null,
|
|
|
+ code = "UNKNOWN_CODE",
|
|
|
+ extra = null
|
|
|
) {
|
|
|
return {
|
|
|
- statusCode: statusCode || 200,
|
|
|
- status: status || "OK",
|
|
|
- message: message || "",
|
|
|
- data: data || null,
|
|
|
- code: code || "UNKNOWN_CODE",
|
|
|
- extra: extra || null,
|
|
|
+ statusCode,
|
|
|
+ status,
|
|
|
+ message,
|
|
|
+ data,
|
|
|
+ code,
|
|
|
+ extra,
|
|
|
};
|
|
|
}
|
|
|
|
|
@@ -103,30 +103,38 @@ export class PRAS {
|
|
|
},
|
|
|
};
|
|
|
|
|
|
- const originalSend = res.send;
|
|
|
- res.send = (body) => {
|
|
|
- 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(Number(prasBody.statusCode) || 200);
|
|
|
- delete prasBody.statusCode;
|
|
|
-
|
|
|
- return originalSend.call(res, JSON.stringify(prasBody));
|
|
|
- };
|
|
|
+ const rawHeaders = req.rawHeaders;
|
|
|
+ if (
|
|
|
+ rawHeaders.hasOwnProperty("Accept") &&
|
|
|
+ /application\/json/.test(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();
|
|
|
};
|