Skip to content

Commit

Permalink
new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuoch committed May 30, 2022
1 parent 2ac8300 commit d238b23
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
61 changes: 61 additions & 0 deletions src/libs/API.js
Original file line number Diff line number Diff line change
@@ -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,
};
8 changes: 6 additions & 2 deletions src/libs/Middleware/Reauthentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ function Reauthentication(response, request, isFromSequentialQueue) {
return data;
}

request.resolve(data);
if (request.resolve) {
request.resolve(data);
}
return;
}

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/libs/Middleware/Retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
});
}

Expand Down
19 changes: 9 additions & 10 deletions src/libs/Middleware/SaveResponseInOnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit d238b23

Please sign in to comment.