client/src/APIClient.js
author duong tam kien <tk@deveha.com>
Tue, 20 Jun 2017 12:11:57 +0200
changeset 61 7586b4a11c32
parent 56 96543c395baa
permissions -rw-r--r--
README
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     1
class APIClient {
56
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
     2
  constructor(baseURL, store) {
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     3
    this.baseURL = baseURL;
56
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
     4
    this.store = store;
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     5
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     6
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     7
  createRequest = (method, uri, data, headers) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     8
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     9
    headers = headers || new Headers();
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    10
    headers.append("Content-Type", "application/json");
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    11
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    12
    var options = {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
      method: method,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
      headers: headers,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
    };
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
    if (data) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    18
      options.body = JSON.stringify(data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    19
    }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    20
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    21
    // TODO : use URL-module to build URL
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    22
    return new Request(this.baseURL + uri, options);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    23
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    24
56
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    25
  getToken = () => {
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    26
    const state = this.store.getState();
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    27
    return state.get('token');
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    28
  }
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    29
49
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    30
  hasToken = () => {
56
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    31
    //const token = localStorage.getItem('token');
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    32
    const token = this.getToken();
49
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    33
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    34
    return token !== null;
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    35
  }
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    36
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    37
  createAuthorizedRequest = (method, uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    38
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    39
    var headers = new Headers(),
56
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    40
        //token = localStorage.getItem('token') || '';
96543c395baa Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    41
        token = this.getToken() || '';
53
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 51
diff changeset
    42
    headers.append("Authorization", "JWT " + token);
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    43
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    44
    return this.createRequest(method, uri, data, headers);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    45
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    46
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    47
  request = (method, uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    48
    console.log(method + ' ' + uri);
49
39f69dc1c2d2 Use token to identify requests.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    49
    var req = this.hasToken() ? this.createAuthorizedRequest(method, uri, data) : this.createRequest(method, uri, data);
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    50
    return this.fetch(req, { credentials: 'include' });
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    51
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    52
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    53
  get = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    54
    return this.request('GET', uri, data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    55
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    56
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    57
  post = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    58
    return this.request('POST', uri, data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    59
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    60
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    61
  put = (uri, data) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    62
    return this.request('PUT', uri, data);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    63
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    64
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    65
  fetch = (req) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    66
    return new Promise((resolve, reject) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    67
      fetch(req)
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    68
        .then((response) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    69
          if (response.ok) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    70
            return response.json().then((data) => resolve(data));
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    71
          } else {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    72
            return response.json().then((data) => reject(data));
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    73
          }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    74
        });
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    75
    });
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    76
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    77
}
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    78
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    79
export default APIClient