github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/app/adapters/application.js (about) 1 import Ember from 'ember'; 2 import RESTAdapter from 'ember-data/adapters/rest'; 3 import codesForError from '../utils/codes-for-error'; 4 5 const { get, computed, inject } = Ember; 6 7 export const namespace = 'v1'; 8 9 export default RESTAdapter.extend({ 10 namespace, 11 12 token: inject.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 // Single record requests deviate from REST practice by using 39 // the singular form of the resource name. 40 // 41 // REST: /some-resources/:id 42 // Nomad: /some-resource/:id 43 // 44 // This is the original implementation of _buildURL 45 // without the pluralization of modelName 46 urlForFindRecord(id, modelName) { 47 let path; 48 let url = []; 49 let host = get(this, 'host'); 50 let prefix = this.urlPrefix(); 51 52 if (modelName) { 53 path = modelName.camelize(); 54 if (path) { 55 url.push(path); 56 } 57 } 58 59 if (id) { 60 url.push(encodeURIComponent(id)); 61 } 62 63 if (prefix) { 64 url.unshift(prefix); 65 } 66 67 url = url.join('/'); 68 if (!host && url && url.charAt(0) !== '/') { 69 url = '/' + url; 70 } 71 72 return url; 73 }, 74 });