Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB, might want to add a comment (or create an issue to clean up later) as this code (and other things in /Network referencing request.resolve) can be removed once we are fully migrated off of the DeprecatedAPI.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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