github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/adapters/application.js (about)

     1  import { inject as service } from '@ember/service';
     2  import { computed } from '@ember/object';
     3  import RESTAdapter from '@ember-data/adapter/rest';
     4  import codesForError from '../utils/codes-for-error';
     5  import removeRecord from '../utils/remove-record';
     6  import { default as NoLeaderError, NO_LEADER } from '../utils/no-leader-error';
     7  import classic from 'ember-classic-decorator';
     8  
     9  export const namespace = 'v1';
    10  
    11  @classic
    12  export default class ApplicationAdapter extends RESTAdapter {
    13    namespace = namespace;
    14  
    15    @service system;
    16    @service token;
    17  
    18    @computed('token.secret')
    19    get headers() {
    20      const token = this.get('token.secret');
    21      if (token) {
    22        return {
    23          'X-Nomad-Token': token,
    24        };
    25      }
    26  
    27      return undefined;
    28    }
    29  
    30    handleResponse(status, headers, payload) {
    31      if (status === 500 && payload === NO_LEADER) {
    32        return new NoLeaderError();
    33      }
    34      return super.handleResponse(...arguments);
    35    }
    36  
    37    findAll() {
    38      return super.findAll(...arguments).catch(error => {
    39        const errorCodes = codesForError(error);
    40  
    41        const isNotImplemented = errorCodes.includes('501');
    42  
    43        if (isNotImplemented) {
    44          return [];
    45        }
    46  
    47        // Rethrow to be handled downstream
    48        throw error;
    49      });
    50    }
    51  
    52    ajaxOptions(url, verb, options = {}) {
    53      options.data || (options.data = {});
    54      if (this.get('system.shouldIncludeRegion')) {
    55        // Region should only ever be a query param. The default ajaxOptions
    56        // behavior is to include data attributes in the requestBody for PUT
    57        // and POST requests. This works around that.
    58        const region = this.get('system.activeRegion');
    59        if (region) {
    60          url = associateRegion(url, region);
    61        }
    62      }
    63      return super.ajaxOptions(url, verb, options);
    64    }
    65  
    66    // In order to remove stale records from the store, findHasMany has to unload
    67    // all records related to the request in question.
    68    findHasMany(store, snapshot, link, relationship) {
    69      return super.findHasMany(...arguments).then(payload => {
    70        const relationshipType = relationship.type;
    71        const inverse = snapshot.record.inverseFor(relationship.key);
    72        if (inverse) {
    73          store
    74            .peekAll(relationshipType)
    75            .filter(record => record.get(`${inverse.name}.id`) === snapshot.id)
    76            .forEach(record => {
    77              removeRecord(store, record);
    78            });
    79        }
    80        return payload;
    81      });
    82    }
    83  
    84    // Single record requests deviate from REST practice by using
    85    // the singular form of the resource name.
    86    //
    87    // REST:  /some-resources/:id
    88    // Nomad: /some-resource/:id
    89    //
    90    // This is the original implementation of _buildURL
    91    // without the pluralization of modelName
    92    urlForFindRecord(id, modelName) {
    93      let path;
    94      let url = [];
    95      let host = this.host;
    96      let prefix = this.urlPrefix();
    97  
    98      if (modelName) {
    99        path = modelName.camelize();
   100        if (path) {
   101          url.push(path);
   102        }
   103      }
   104  
   105      if (id) {
   106        url.push(encodeURIComponent(id));
   107      }
   108  
   109      if (prefix) {
   110        url.unshift(prefix);
   111      }
   112  
   113      url = url.join('/');
   114      if (!host && url && url.charAt(0) !== '/') {
   115        url = '/' + url;
   116      }
   117  
   118      return url;
   119    }
   120  
   121    urlForUpdateRecord() {
   122      return this.urlForFindRecord(...arguments);
   123    }
   124  }
   125  
   126  function associateRegion(url, region) {
   127    return url.indexOf('?') !== -1 ? `${url}&region=${region}` : `${url}?region=${region}`;
   128  }