summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/actions/api.js
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-core/src/actions/api.js')
-rw-r--r--packages/gitbook-core/src/actions/api.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/actions/api.js b/packages/gitbook-core/src/actions/api.js
new file mode 100644
index 0000000..7a0910f
--- /dev/null
+++ b/packages/gitbook-core/src/actions/api.js
@@ -0,0 +1,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
+};