summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/actions/api.js
blob: 7a0910f9b838878ed977b7b5fe1f40d3634c5de9 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
const GitBookAPI = require('gitbook-api');
const ACTION_TYPES = require('./TYPES');

const User = require('../models/User');

/**
 * Execute an HTTP API request.
 *
 * @param {String} method
 * @param {String} url
 * @param {Object} params
 * @param {Object} data
 * @return {Action}
 */
function request(method, url, params, data) {
    return (dispatch, getState) => {
        const client = new GitBookAPI({}, {
            host: '<todo>'
        });

        return client.request(url, {
            method,
            data,
            params
        });
    };
}

const get  = (...args) => request('GET', ...args);
const post = (...args) => request('POST', ...args);
const del  = (...args) => request('DELETE', ...args);

/**
 * Fetch infos about current user.
 * @return {Action}
 */
function fetchCurrentUser() {
    return (dispatch) => {
        return dispatch(get('reader'))
        .then((user) => {
            dispatch({
                type: ACTION_TYPES.API_USER_FETCHED,
                user: new User(user)
            });
        });
    };
}

/**
 * Activate the API:
 *  - Fetch the current user.
 * @return {Action}
 */
function activate() {
    return (dispatch) => {
        return dispatch(fetchCurrentUser());
    };
}

/**
 * Deactivate the AP
 * @return {Action}
 */
function deactivate() {
    return (dispatch) => {

    };
}

module.exports = {
    get, post, del,
    activate, deactivate
};