# All files / composer/utils API.js

0% Statements 0/9
0% Branches 0/4
0% Functions 0/1
0% Lines 0/9

    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

     /**
     * Thin wrapper around @bufferapp/buffer-js-api that automatically
     * attaches the CSRF token to requests, and enforces absolute urls.
     */

    import RPCClient from './RPCClient';

    import BufferAPI from '@bufferapp/buffer-js-api';
    import AppStore from '../stores/AppStore';
    import { bufferOrigins } from '../AppConstants';

    class API extends BufferAPI {
      static request(url, data = {}, settings, apiSettings = {}) {
        const { onNewPublish } = AppStore.getUserData();

        /**
         * The new publish dashboard uses an RPC client to
         * make authenticated requests to the Buffer API.
         * Since the composer is separate from Publish and depends
         * on the normal Buffer API we create another RPC client
         * to allow the composer to make proxied requests.
         */
        if (onNewPublish) {
          return RPCClient.call('composerApiProxy', {
            url: `/1/${url}`,
            args: data,
          });
        }

        const { environment } = AppStore.getMetaData();

        const defaultData = {
          csrf_token: AppStore.getCsrfToken(),
        };

        data = Object.assign(defaultData, data);

        const defaultSettings = {
          endpoint: `${bufferOrigins.get(environment)}/api/`,
        };

        apiSettings = Object.assign(defaultSettings, apiSettings);

        return BufferAPI.request(url, data, settings, apiSettings);
      }
    }

    export default API;