From d238b2368357f8954f1fb290966975a9c3792572 Mon Sep 17 00:00:00 2001 From: Nathalie Kuoch Date: Mon, 30 May 2022 16:35:08 +0200 Subject: [PATCH] new API --- src/libs/API.js | 61 +++++++++++++++++++++++ src/libs/Middleware/Reauthentication.js | 8 ++- src/libs/Middleware/Retry.js | 4 +- src/libs/Middleware/SaveResponseInOnyx.js | 19 ++++--- 4 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 src/libs/API.js diff --git a/src/libs/API.js b/src/libs/API.js new file mode 100644 index 000000000000..7bda882e82bd --- /dev/null +++ b/src/libs/API.js @@ -0,0 +1,61 @@ +import _ from 'underscore'; +import Onyx from 'react-native-onyx'; +import * as Request from './Request'; +import * as SequentialQueue from './Network/SequentialQueue'; +import {version} from '../../package.json'; + +function write(command, apiCommandParameters = {}, onyxData = {}) { + // Optimistically update Onyx + if (onyxData.optimisticData) { + Onyx.update(onyxData.optimisticData); + } + + // Assemble the data we'll send to the API + const data = { + ...apiCommandParameters, + appversion: version, + }; + + // Assemble all the request data we'll be storing in the queue + const request = { + command, + data, + ..._.omit(onyxData, 'optimisticData'), + }; + + // Write commands can be saved and retried, so push it to the SequentialQueue + SequentialQueue.push(request); +} + +function makeRequestWithSideEffects(command, apiCommandParameters = {}, onyxData = {}) { + // Optimistically update Onyx + if (onyxData.optimisticData) { + Onyx.update(onyxData.optimisticData); + } + + // Assemble the data we'll send to the API + const data = { + ...apiCommandParameters, + appversion: version, + }; + + // Assemble all the request data we'll be storing + const request = { + command, + data, + ..._.omit(onyxData, 'optimisticData'), + }; + + // Return a promise containing the response from HTTPS + return Request.processWithMiddleware(request); +} + +function read(command, apiCommandParameters, onyxData) { + makeRequestWithSideEffects(command, apiCommandParameters, onyxData); +} + +export { + write, + makeRequestWithSideEffects, + read, +}; diff --git a/src/libs/Middleware/Reauthentication.js b/src/libs/Middleware/Reauthentication.js index 33182bbe85c1..7dc971191b87 100644 --- a/src/libs/Middleware/Reauthentication.js +++ b/src/libs/Middleware/Reauthentication.js @@ -39,7 +39,9 @@ function Reauthentication(response, request, isFromSequentialQueue) { return data; } - request.resolve(data); + if (request.resolve) { + request.resolve(data); + } return; } @@ -81,7 +83,9 @@ function Reauthentication(response, request, isFromSequentialQueue) { return data; } - request.resolve(data); + if (request.resolve) { + request.resolve(data); + } // Return response data so we can chain the response with the following middlewares. return data; diff --git a/src/libs/Middleware/Retry.js b/src/libs/Middleware/Retry.js index dd100efa681a..24e9b86b0dbf 100644 --- a/src/libs/Middleware/Retry.js +++ b/src/libs/Middleware/Retry.js @@ -32,7 +32,9 @@ function Retry(response, request, isFromSequentialQueue) { console.debug('[Network] There was an error in the Log API command, unable to log to server!', error); } - request.resolve({jsonCode: CONST.JSON_CODE.UNABLE_TO_RETRY}); + if (request.resolve) { + request.resolve({jsonCode: CONST.JSON_CODE.UNABLE_TO_RETRY}); + } }); } diff --git a/src/libs/Middleware/SaveResponseInOnyx.js b/src/libs/Middleware/SaveResponseInOnyx.js index 0acea5fb93d3..cf8444c771dd 100644 --- a/src/libs/Middleware/SaveResponseInOnyx.js +++ b/src/libs/Middleware/SaveResponseInOnyx.js @@ -11,17 +11,16 @@ function SaveResponseInOnyx(response, request) { .then((responseData) => { // We'll only save the onyxData, successData and failureData for the refactored commands if (_.has(responseData, 'onyxData')) { - let data; + const data = []; if (responseData.jsonCode === 200) { - data = [ - ...request.successData, - ...responseData.onyxData, - ]; - } else { - data = [ - ...request.failureData, - ...responseData.onyxData, - ]; + if (request.successData) { + data.push(...request.successData); + } + } else if (request.failureData) { + data.push(...request.failureData); + } + if (responseData.onyxData) { + data.push(...responseData.onyxData); } Onyx.update(data); }