Example #1
0
export default DS.RESTAdapter.extend(Ember.Evented, WithLoggerMixin, {
  /**
   * @inheritDoc
   */
  defaultSerializer: 'sails',

  /**
   * Whether to use CSRF
   * @since 0.0.1
   * @property useCSRF
   * @type Boolean
   */
  useCSRF: false,

  /**
   * Path where to GET the CSRF
   * @since 0.0.15
   * @property csrfTokenPath
   * @type String
   */
  csrfTokenPath: '/csrfToken',

  /**
   * The csrfToken
   * @since 0.0.1
   * @property csrfToken
   * @type String
   */
  csrfToken: null,

  /**
   * Are we loading CSRF token?
   * @since 0.0.7
   * @property isLoadingCSRF
   * @type Boolean
   */
  isLoadingCSRF: bool('_csrfTokenLoadingPromise'),

  /**
   * The promise responsible of the current CSRF token fetch
   * @since 0.0.15
   * @property _csrfTokenLoadingPromise
   * @type Promise
   * @private
   */
  _csrfTokenLoadingPromise: null,


  /**
   * @since 0.0.4
   * @method init
   * @inheritDoc
   */
  init: function () {
    this._super();
    this.set('csrfToken', null);
  },

  /**
   * Send a message using `_request` of extending class
   *
   * @since 0.0.11
   * @method ajax
   * @inheritDoc
   */
  ajax: function (url, method, options) {
    var processRequest, out = {};
    method = method.toUpperCase();
    if (!options) {
      options = {};
    }
    if (!options.data && method !== 'GET') {
      // so that we can add our CSRF token
      options.data = {};
    }
    processRequest = bind(this, function () {
      return this._request(out, url, method, options)
        .then(bind(this, function (response) {
          this.info(`${out.protocol} ${method} request on ${url}: SUCCESS`);
          this.debug('  → request:', options.data);
          this.debug('  ← response:', response);
          if (this.isErrorObject(response)) {
            if (response.errors) {
              return RSVP.reject(new DS.InvalidError(this.formatError(response)));
            }
            return RSVP.reject(response);
          }
          return response;
        }))
        .catch(bind(this, function (error) {
          this.warn(`${out.protocol} ${method} request on ${url}: ERROR`);
          this.info('  → request:', options.data);
          this.info('  ← error:', error);
          return RSVP.reject(error);
        }));
    });
    if (method !== 'GET') {
      return this.fetchCSRFToken()
        .then(bind(this, function () {
          this.checkCSRF(options.data);
          return processRequest();
        }));
    }
    else {
      return processRequest();
    }
  },

  /**
   * @since 0.0.1
   * @method ajaxError
   * @inheritDoc
   */
  ajaxError: function (jqXHR) {
    var error = this._super(jqXHR);
    var data;

    try {
      data = $.parseJSON(jqXHR.responseText);
    }
    catch (err) {
      data = jqXHR.responseText;
    }

    if (data.errors) {
      this.error('error returned from Sails', data);
      return new DS.InvalidError(this.formatError(data));
    }
    else if (data) {
      return new Error(data);
    }
    else {
      return error;
    }
  },

  /**
   * Fetches the CSRF token if needed
   *
   * @since 0.0.3
   * @method fetchCSRFToken
   * @param {Boolean} [force] If `true`, the token will be fetched even if it has already been fetched
   * @return {RSVP.Promise}
   */
  fetchCSRFToken: function (force) {
    var self = this, promise;
    if (this.get('useCSRF') && (force || !this.get('csrfToken'))) {
      if (!(promise = this.get('_csrfTokenLoadingPromise'))) {
        this.set('csrfToken', null);
        this.debug('fetching CSRF token...');
        promise = this._fetchCSRFToken()
          // handle success response
          .then(function (token) {
            if (!token) {
              self.error('Got an empty CSRF token from the server.');
              return RSVP.reject('Got an empty CSRF token from the server!');
            }
            self.info('got a new CSRF token:', token);
            self.set('csrfToken', token);
            schedule('actions', self, 'trigger', 'didLoadCSRF', token);
            return token;
          })
          // handle errors
          .catch(function (error) {
            self.error('error trying to get new CSRF token:', error);
            schedule('actions', self, 'trigger', 'didLoadCSRF', null, error);
            return error;
          })
          // reset the loading promise
          .finally(bind(this, 'set', '_csrfTokenLoadingPromise', null));
        this.set('_csrfTokenLoadingPromise', promise);
      }
      // return the loading promise
      return promise;
    }
    return RSVP.resolve(null);
  },

  /**
   * Format an error coming from Sails
   *
   * @since 0.0.1
   * @method formatError
   * @param {Object} error The error to format
   * @return {Object}
   */
  formatError: function (error) {
    return Object.keys(error.invalidAttributes).reduce(function (memo, property) {
      memo[property] = error.invalidAttributes[property].map(function (err) {
        return err.message;
      });
      return memo;
    }, {});
  },

  /**
   * @since 0.0.1
   * @method pathForType
   * @inheritDoc
   */
  pathForType: function (type) {
    return pluralize(camelize(type));
  },

  /**
   * Is the given result a Sails error object?
   *
   * @since 0.0.1
   * @method isErrorObject
   * @param {Object} data The object to test
   * @return {Boolean} Returns `true` if it's an error object, else `false`
   */
  isErrorObject: function (data) {
    return !!(data && data.error && data.model && data.summary && data.status);
  },

  /**
   * Check if we have a CSRF and include it in the given data to be sent
   *
   * @since 0.0.1
   * @method checkCSRF
   * @param {Object} [data] The data on which to attach the CSRF token
   * @return {Object} data The given data
   */
  checkCSRF: function (data) {
    if (!this.useCSRF) {
      return data;
    }
    this.info('adding CSRF token');
    if (!this.csrfToken) {
      this.error('CSRF not fetched yet');
      throw new Error("CSRF Token not fetched yet.");
    }
    data._csrf = this.csrfToken;
    return data;
  }
});
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

export default DS.RESTAdapter.extend(DataAdapterMixin, {
  authorizer: 'authorizer:token'
});
//   });
// } else {
  adapter = DS.RESTAdapter.extend({
    namespace: 'api',

    // @private
    // @override
    ajaxOptions: function (url, type, options) {
      var hash = this._super(url, type, options);

      if (type === 'GET' && !!hash.data && !!hash.data.where) {
        hash.url = hash.url + '?filter=' + encodeURIComponent(JSON.stringify(hash.data));
        delete hash.data.where;
      }

      return hash;
    },

    // @override
    ajaxError: function(jqXHR, responseText, errorThrown) {
      var error = this._super(jqXHR, responseText, errorThrown);

      if (jqXHR && jqXHR.status === 422) {
        return new DS.InvalidError(Ember.$.parseJSON(jqXHR.responseText));
      } else {
        return error;
      }
    },
  });
// }
import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  headers: {

 }
});
import DS from 'ember-data';
import ENV from 'address-book/config/environment';

export default DS.RESTAdapter.extend({
    host: ENV.APPLICATION_HOST
});
Example #6
0
import DS from "ember-data";
export default DS.RESTAdapter.extend({
  namespace: 'api/v1'
});
Example #7
0
import DS from "ember-data";

export default DS.RESTAdapter.extend({
  buildURL: function(modelName, id) {
    id = id ? '/' + id : '';

    return this.get('host') + '/artists' + id;
  }
});
Example #8
0
import DS from 'ember-data';
import config from '../config/environment';
/*
export default DS.FixtureAdapter.extend({
  latency: 500
});
*/
export default DS.RESTAdapter.extend({
  //host: "http://" + window.location.hostname
  host: config.APP.wsURL
  //host: "http://yuki.lunasys.fr"
});
import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  host: 'http://localhost:8080'
});
Example #10
0
import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  namespace: 'json/data'
});