// const UuidEncoder = require('uuid-encoder'); import {Errors} from './tools.errors'; export interface IResponse { success: boolean; error: any; data: any; } export interface ILResponse { statusCode: number; headers: any; body: string; } export interface IWResponse { action: string; error: any; data: any; } const respErrors = new Errors(); export const response = (success: boolean, error: any = null, data: any = null): IResponse => { if (success === false) { if (typeof error === 'string') { error = typeof respErrors.getErrorByName(error) === 'object' ? respErrors.getErrorByName(error) : error; } if ( (typeof error === 'string' && !error.includes("info not found")) || typeof error !== 'string' ){ console.log('BLN System Error: ', error, 'Data: ', data); } } return {success, error, data}; }; export const wResponse = (action: string, error: any = null, data: any = null): IWResponse => { if (error !== null) { if (typeof error === 'string') { error = typeof respErrors.getErrorByName(error) === 'object' ? respErrors.getErrorByName(error) : error; } return {action: 'api:error-' + action, error, data}; } else { return {action: 'api:' + action, error, data}; } }; export const lResponse = (success: boolean, error: any = null, data: any = null): ILResponse => { const responseHeaders = { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true }; if (error) { return { statusCode: error.code || 500, headers: responseHeaders, body: JSON.stringify(error) }; } return { statusCode: success ? 200 : 400, headers: responseHeaders, body: JSON.stringify(data) }; }; export const handledPromise = (promise: Promise, res: any, returnData: boolean = true, executeFirst: any = undefined) => { try { if (returnData) { promise .then(_response => { res.json(response(true, null, _response.data)); }) .catch(_response => { if (!_response.success) { res.status(400).json(response(false, _response.error)); } else { res.status(404).json(response(false, _response.error)); } }); } else { if (executeFirst) { if (executeFirst.success) { promise .then(_response => { res.json(response(true, null, true)); }) .catch(_response => { if (!_response.success) { res.status(400).json(response(false, _response.error)); } else { res.status(404).json(response(false, _response.error)); } }); } else { res.status(400).json(response(false, executeFirst.error)); } } else if (executeFirst == undefined) { promise .then(_response => { res.json(response(true, null, true)); }) .catch(_response => { if (!_response.success) { res.status(400).json(response(false, _response.error)); } else { res.status(404).json(response(false, _response.error)); } }); } else { res.status(404).json(response(false, executeFirst.error)); } } } catch (error) { res.status(500).json(response(false, error)); } };