From 275279e7b9292035310ed9ea8f10baabf0f95f70 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Sun, 12 Mar 2023 03:37:18 +0100 Subject: [PATCH 1/4] use eslint-plugin-promise --- backend/.eslintrc.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/.eslintrc.js b/backend/.eslintrc.js index b33a3e480..e10be9228 100644 --- a/backend/.eslintrc.js +++ b/backend/.eslintrc.js @@ -5,7 +5,7 @@ module.exports = { node: true, }, parser: '@typescript-eslint/parser', - plugins: ['prettier', '@typescript-eslint', 'type-graphql', 'jest', 'import', 'n'], + plugins: ['prettier', '@typescript-eslint', 'type-graphql', 'jest', 'import', 'n', 'promise'], extends: [ 'standard', 'eslint:recommended', @@ -117,6 +117,21 @@ module.exports = { 'n/prefer-global/url-search-params': 'error', 'n/prefer-promises/dns': 'error', 'n/prefer-promises/fs': 'error', + // promise + 'promise/catch-or-return': 'error', + 'promise/no-return-wrap': 'error', + 'promise/param-names': 'error', + 'promise/always-return': 'error', + 'promise/no-native': 'off', + 'promise/no-nesting': 'warn', + 'promise/no-promise-in-callback': 'warn', + 'promise/no-callback-in-promise': 'warn', + 'promise/avoid-new': 'warn', + 'promise/no-new-statics': 'error', + 'promise/no-return-in-finally': 'warn', + 'promise/valid-params': 'warn', + 'promise/prefer-await-to-callbacks': 'error', + 'promise/no-multiple-resolved': 'error', }, overrides: [ // only for ts files From aa8b9d017d31b8eaf8e27aabba3723846ae9c2c0 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 6 Apr 2023 15:11:33 +0200 Subject: [PATCH 2/4] return value on then --- backend/src/emails/sendEmailTranslated.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/emails/sendEmailTranslated.ts b/backend/src/emails/sendEmailTranslated.ts index 6d89cc257..f7f2ea40d 100644 --- a/backend/src/emails/sendEmailTranslated.ts +++ b/backend/src/emails/sendEmailTranslated.ts @@ -75,6 +75,7 @@ export const sendEmailTranslated = async (params: { resultSend = result logger.info('Send email successfully !!!') logger.info('Result: ', result) + return result }) .catch((error: unknown) => { throw new LogError('Error sending notification email', error) From d89fca8d1d58d4268ccd99735b38fb3bc0195c9e Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 6 Apr 2023 15:12:49 +0200 Subject: [PATCH 3/4] restructure axios calls to use await --- backend/src/apis/HttpRequest.ts | 56 +++++++++++++++------------------ 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/backend/src/apis/HttpRequest.ts b/backend/src/apis/HttpRequest.ts index 278e3732c..78cb4510f 100644 --- a/backend/src/apis/HttpRequest.ts +++ b/backend/src/apis/HttpRequest.ts @@ -9,39 +9,35 @@ import { backendLogger as logger } from '@/server/logger' // eslint-disable-next-line @typescript-eslint/no-explicit-any export const apiPost = async (url: string, payload: unknown): Promise => { logger.trace('POST', url, payload) - return axios - .post(url, payload) - .then((result) => { - logger.trace('POST-Response', result) - if (result.status !== 200) { - throw new LogError('HTTP Status Error', result.status) - } - if (result.data.state !== 'success') { - throw new Error(result.data.msg) - } - return { success: true, data: result.data } - }) - .catch((error) => { - return { success: false, data: error.message } - }) + try { + const result = await axios.post(url, payload) + logger.trace('POST-Response', result) + if (result.status !== 200) { + throw new LogError('HTTP Status Error', result.status) + } + if (result.data.state !== 'success') { + throw new LogError(result.data.msg) + } + return { success: true, data: result.data } + } catch (error: unknown) { + return { success: false, data: error.message } + } } // eslint-disable-next-line @typescript-eslint/no-explicit-any export const apiGet = async (url: string): Promise => { logger.trace('GET: url=' + url) - return axios - .get(url) - .then((result) => { - logger.trace('GET-Response', result) - if (result.status !== 200) { - throw new LogError('HTTP Status Error', result.status) - } - if (!['success', 'warning'].includes(result.data.state)) { - throw new Error(result.data.msg) - } - return { success: true, data: result.data } - }) - .catch((error) => { - return { success: false, data: error.message } - }) + try { + const result = await axios.get(url) + logger.trace('GET-Response', result) + if (result.status !== 200) { + throw new LogError('HTTP Status Error', result.status) + } + if (!['success', 'warning'].includes(result.data.state)) { + throw new LogError(result.data.msg) + } + return { success: true, data: result.data } + } catch (error: unknown) { + return { success: false, data: error.message } + } } From 693338975e7a79fa2168efe4e0fe03259761bff7 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 6 Apr 2023 15:37:34 +0200 Subject: [PATCH 4/4] fix problems with compile --- backend/src/apis/HttpRequest.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/src/apis/HttpRequest.ts b/backend/src/apis/HttpRequest.ts index 152b48f21..f40d577bd 100644 --- a/backend/src/apis/HttpRequest.ts +++ b/backend/src/apis/HttpRequest.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-argument */ @@ -19,7 +20,7 @@ export const apiPost = async (url: string, payload: unknown): Promise => { throw new LogError(result.data.msg) } return { success: true, data: result.data } - } catch (error: unknown) { + } catch (error: any) { return { success: false, data: error.message } } } @@ -37,7 +38,7 @@ export const apiGet = async (url: string): Promise => { throw new LogError(result.data.msg) } return { success: true, data: result.data } - } catch (error: unknown) { + } catch (error: any) { return { success: false, data: error.message } } }