-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathAPI.js
61 lines (51 loc) · 1.61 KB
/
API.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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,
};