1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tools_errors_1 = require("./tools.errors");
|
4 | const respErrors = new tools_errors_1.Errors();
|
5 | exports.response = (success, error = null, data = null) => {
|
6 | if (success === false) {
|
7 | if (typeof error === 'string') {
|
8 | error = typeof respErrors.getErrorByName(error) === 'object' ? respErrors.getErrorByName(error) : error;
|
9 | }
|
10 | if ((typeof error === 'string' && !error.includes("info not found")) || typeof error !== 'string') {
|
11 | console.log('BLN System Error: ', error, 'Data: ', data);
|
12 | }
|
13 | }
|
14 | return { success, error, data };
|
15 | };
|
16 | exports.wResponse = (action, error = null, data = null) => {
|
17 | if (error !== null) {
|
18 | if (typeof error === 'string') {
|
19 | error = typeof respErrors.getErrorByName(error) === 'object' ? respErrors.getErrorByName(error) : error;
|
20 | }
|
21 | return { action: 'api:error-' + action, error, data };
|
22 | }
|
23 | else {
|
24 | return { action: 'api:' + action, error, data };
|
25 | }
|
26 | };
|
27 | exports.lResponse = (success, error = null, data = null) => {
|
28 | const responseHeaders = {
|
29 | 'Content-Type': 'application/json',
|
30 | 'Access-Control-Allow-Origin': '*',
|
31 | 'Access-Control-Allow-Credentials': true
|
32 | };
|
33 | if (error) {
|
34 | return {
|
35 | statusCode: error.code || 500,
|
36 | headers: responseHeaders,
|
37 | body: JSON.stringify(error)
|
38 | };
|
39 | }
|
40 | return {
|
41 | statusCode: success ? 200 : 400,
|
42 | headers: responseHeaders,
|
43 | body: JSON.stringify(data)
|
44 | };
|
45 | };
|
46 | exports.handledPromise = (promise, res, returnData = true, executeFirst = undefined) => {
|
47 | try {
|
48 | if (returnData) {
|
49 | promise
|
50 | .then(_response => {
|
51 | res.json(exports.response(true, null, _response.data));
|
52 | })
|
53 | .catch(_response => {
|
54 | if (!_response.success) {
|
55 | res.status(400).json(exports.response(false, _response.error));
|
56 | }
|
57 | else {
|
58 | res.status(404).json(exports.response(false, _response.error));
|
59 | }
|
60 | });
|
61 | }
|
62 | else {
|
63 | if (executeFirst) {
|
64 | if (executeFirst.success) {
|
65 | promise
|
66 | .then(_response => {
|
67 | res.json(exports.response(true, null, true));
|
68 | })
|
69 | .catch(_response => {
|
70 | if (!_response.success) {
|
71 | res.status(400).json(exports.response(false, _response.error));
|
72 | }
|
73 | else {
|
74 | res.status(404).json(exports.response(false, _response.error));
|
75 | }
|
76 | });
|
77 | }
|
78 | else {
|
79 | res.status(400).json(exports.response(false, executeFirst.error));
|
80 | }
|
81 | }
|
82 | else if (executeFirst == undefined) {
|
83 | promise
|
84 | .then(_response => {
|
85 | res.json(exports.response(true, null, true));
|
86 | })
|
87 | .catch(_response => {
|
88 | if (!_response.success) {
|
89 | res.status(400).json(exports.response(false, _response.error));
|
90 | }
|
91 | else {
|
92 | res.status(404).json(exports.response(false, _response.error));
|
93 | }
|
94 | });
|
95 | }
|
96 | else {
|
97 | res.status(404).json(exports.response(false, executeFirst.error));
|
98 | }
|
99 | }
|
100 | }
|
101 | catch (error) {
|
102 | res.status(500).json(exports.response(false, error));
|
103 | }
|
104 | };
|
105 |
|
\ | No newline at end of file |