UNPKG

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