From 748cacf9dd812120f44495ba7befddf2404209d7 Mon Sep 17 00:00:00 2001 From: payam naghdi Date: Sat, 2 Oct 2021 19:36:20 +0330 Subject: [PATCH 1/2] Feature: Add error parser and response parser --- src/index.js | 43 +++++++++++++++- test/api-manager.test.js | 103 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index d6c7dc6..732a5c9 100644 --- a/src/index.js +++ b/src/index.js @@ -12,6 +12,9 @@ const METHODS = { class VueAPIManager { constructor(config) { + this._responseParser = null; + this._httpErrorParser = null; + this._defaultErrorMessage = 'No message' this._host = config.hasOwnProperty('host') ? config.host : ""; this._apiURL = `${this._host}${config.hasOwnProperty('rootURL') ? config.rootURL : "/"}`; this._apis = config.hasOwnProperty('apis') ? config.apis : {}; @@ -47,6 +50,16 @@ class VueAPIManager { this._authorizationHeader = authHeaderGetter; } + setResponseParser(responseParser) { + this._responseParser = responseParser; + } + + setHttpErrorParser(httpErrorParser) { + this._httpErrorParser = httpErrorParser; + } + setDefaultErrorMessage(message) { + this._defaultErrorMessage = message; + } _getAuthorizationHeader() { if (!this._authorizationHeader) { throw new NullAuthorizationHeaderError() @@ -70,8 +83,34 @@ class VueAPIManager { if (requiresAuth) { requestHeaders['Authorization'] = `${this._authorizationHeaderPrefix} ${this._getAuthorizationHeader()}`; } - const requestParams ={...defaultParams, ...params} - return await getHTTPMethod(method)(url, requestParams, requestHeaders); + const requestParams = {...defaultParams, ...params} + try { + let response = await getHTTPMethod(method)(url, requestParams, requestHeaders); + if (this._responseParser) + response.parsedData = this._responseParser(response); + return response; + } catch (error) { + if (error.response) { + // The request was made and the server responded with a status code + // that falls out of the range of 2xx + let response = error.response; + if (this._httpErrorParser) + response.parsedError = this._httpErrorParser(response); + else + response.parsedError = error.response.status; + return response; + } else { + // Something happened in setting up the request that triggered an Error + if (error.message) + return { + parsedError: error.message + } + else + return { + parsedError: this._defaultErrorMessage + } + } + } } } diff --git a/test/api-manager.test.js b/test/api-manager.test.js index 135c48a..79a6931 100644 --- a/test/api-manager.test.js +++ b/test/api-manager.test.js @@ -330,4 +330,107 @@ describe('VueAPIManager', () => { expect(mockAxios.delete).toBeCalledTimes(1); expect(mockAxios.delete).toBeCalledWith("/deleteDataWithParams", {"headers": {}, data: {"id": 1}}) }) + + it('Parse response with 2xx status', async () => { + let apiManager = new VueAPIManager(API_MANAGER_CONFIG); + + mockAxios.get.mockImplementationOnce(() => + Promise.resolve({ + data: { + result: { + status: 'OK' + } + } + }) + ); + const dataWithoutParser = await apiManager.getAll(); + expect(dataWithoutParser.parsedData).toBe(undefined) + + mockAxios.get.mockImplementationOnce(() => + Promise.resolve({ + data: { + result: { + status: 'OK' + } + } + }) + ); + const responseParser = (response) => { + return response.data.result.status + } + apiManager.setResponseParser(responseParser) + const data = await apiManager.getAll(); + expect(data.parsedData).toBe('OK') + }) + + it('Parse response with status > 2xx', async () => { + + mockAxios.get.mockImplementationOnce(() => { + throw { + response: { + status: 400, + data: { + result: { + status: 'NOK' + } + } + } + } + } + ); + + let apiManager = new VueAPIManager(API_MANAGER_CONFIG); + + const dataWithoutParser = await apiManager.getAll(); + expect(dataWithoutParser.parsedError).toBe(400); + + mockAxios.get.mockImplementationOnce(() => { + throw { + response: { + status: 400, + data: { + result: { + status: 'NOK' + } + } + } + } + } + ); + const httpErrorParser = (response) => { + return response.data.result.status; + } + apiManager.setHttpErrorParser(httpErrorParser); + const data = await apiManager.getAll(); + expect(data.parsedError).toBe('NOK') + }) + it('Error without response with message', async () => { + mockAxios.get.mockImplementationOnce(() => { + throw { + message: 'Error from tests' + } + } + ); + let apiManager = new VueAPIManager(API_MANAGER_CONFIG); + const data = await apiManager.getAll(); + expect(data.parsedError).toBe('Error from tests'); + }) + it('Error without response and message', async () => { + mockAxios.get.mockImplementationOnce(() => { + throw {} + } + ); + let apiManager = new VueAPIManager(API_MANAGER_CONFIG); + const data = await apiManager.getAll(); + expect(data.parsedError).toBe('No message'); + + mockAxios.get.mockImplementationOnce(() => { + throw {} + } + ); + apiManager.setDefaultErrorMessage('New default message') + const dataNewDefault = await apiManager.getAll() + expect(dataNewDefault.parsedError).toBe('New default message'); + + }) }) From b6a3c2e982f1146d1dee21874a2ff2c7984a8573 Mon Sep 17 00:00:00 2001 From: payam naghdi Date: Sat, 2 Oct 2021 19:37:31 +0330 Subject: [PATCH 2/2] Version 7.0.0 - HTTP error parser - Response parser --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a499619..3ece392 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@payamnaghdy/vue-api-manager", - "version": "0.0.6", + "version": "0.0.7", "main": "index.js", "keywords": [ "vue",