import { Spinner } from 'clui'; import chalk from 'chalk'; import Config from '../config'; export const TryCatch = () => { return (_: Object, __?: string, descriptor?: PropertyDescriptor) => { const fn: Function = descriptor.value; descriptor.value = async (...args) => { try { const response = await fn.apply(this, args); return response; } catch (err) { const errors = err.response.errors; chalk.red('Request failed : \n'); console.log(errors); if (errors.find(x => x.message === 'Not authenticated')) { chalk.red(`Please run "${Config.PROJECT_NAME} login"`); } } } } } export const Loader = (text: string, style?: string[]) => { return (_: Object, __?: string, descriptor?: PropertyDescriptor) => { const fn: Function = descriptor.value; const spinner = new Spinner(text, style); descriptor.value = async (...args) => { try { spinner.start(); const response = await fn.apply(this, args); return response; } catch (err) { throw err; } finally { spinner.stop(); } } } }