github.com/emate/nomad@v0.8.2-wo-binpacking/ui/app/adapters/application.js (about)

     1  import { inject as service } from '@ember/service';
     2  import { computed, get } from '@ember/object';
     3  import RESTAdapter from 'ember-data/adapters/rest';
     4  import codesForError from '../utils/codes-for-error';
     5  import removeRecord from '../utils/remove-record';
     6  
     7  export const namespace = 'v1';
     8  
     9  export default RESTAdapter.extend({
    10    namespace,
    11  
    12    token: service(),
    13  
    14    headers: computed('token.secret', function() {
    15      const token = this.get('token.secret');
    16      if (token) {
    17        return {
    18          'X-Nomad-Token': token,
    19        };
    20      }
    21    }),
    22  
    23    findAll() {
    24      return this._super(...arguments).catch(error => {
    25        const errorCodes = codesForError(error);
    26  
    27        const isNotImplemented = errorCodes.includes('501');
    28  
    29        if (isNotImplemented) {
    30          return [];
    31        }
    32  
    33        // Rethrow to be handled downstream
    34        throw error;
    35      });
    36    },
    37  
    38    // In order to remove stale records from the store, findHasMany has to unload
    39    // all records related to the request in question.
    40    findHasMany(store, snapshot, link, relationship) {
    41      return this._super(...arguments).then(payload => {
    42        const relationshipType = relationship.type;
    43        const inverse = snapshot.record.inverseFor(relationship.key);
    44        if (inverse) {
    45          store
    46            .peekAll(relationshipType)
    47            .filter(record => record.get(`${inverse.name}.id`) === snapshot.id)
    48            .forEach(record => {
    49              removeRecord(store, record);
    50            });
    51        }
    52        return payload;
    53      });
    54    },
    55  
    56    // Single record requests deviate from REST practice by using
    57    // the singular form of the resource name.
    58    //
    59    // REST:  /some-resources/:id
    60    // Nomad: /some-resource/:id
    61    //
    62    // This is the original implementation of _buildURL
    63    // without the pluralization of modelName
    64    urlForFindRecord(id, modelName) {
    65      let path;
    66      let url = [];
    67      let host = get(this, 'host');
    68      let prefix = this.urlPrefix();
    69  
    70      if (modelName) {
    71        path = modelName.camelize();
    72        if (path) {
    73          url.push(path);
    74        }
    75      }
    76  
    77      if (id) {
    78        url.push(encodeURIComponent(id));
    79      }
    80  
    81      if (prefix) {
    82        url.unshift(prefix);
    83      }
    84  
    85      url = url.join('/');
    86      if (!host && url && url.charAt(0) !== '/') {
    87        url = '/' + url;
    88      }
    89  
    90      return url;
    91    },
    92  });