2017-12-15 21:39:18 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { computed, get } from '@ember/object';
|
2017-09-19 14:47:10 +00:00
|
|
|
import RESTAdapter from 'ember-data/adapters/rest';
|
2017-10-13 00:40:49 +00:00
|
|
|
import codesForError from '../utils/codes-for-error';
|
2018-04-07 01:07:57 +00:00
|
|
|
import removeRecord from '../utils/remove-record';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
export const namespace = 'v1';
|
|
|
|
|
|
|
|
export default RESTAdapter.extend({
|
|
|
|
namespace,
|
|
|
|
|
2018-08-04 01:14:06 +00:00
|
|
|
system: service(),
|
2017-12-15 21:39:18 +00:00
|
|
|
token: service(),
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
headers: computed('token.secret', function() {
|
|
|
|
const token = this.get('token.secret');
|
2017-10-19 17:10:01 +00:00
|
|
|
if (token) {
|
|
|
|
return {
|
2017-09-19 14:47:10 +00:00
|
|
|
'X-Nomad-Token': token,
|
2017-10-19 17:10:01 +00:00
|
|
|
};
|
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
}),
|
|
|
|
|
2017-10-10 18:23:10 +00:00
|
|
|
findAll() {
|
|
|
|
return this._super(...arguments).catch(error => {
|
2017-10-13 00:40:49 +00:00
|
|
|
const errorCodes = codesForError(error);
|
|
|
|
|
|
|
|
const isNotImplemented = errorCodes.includes('501');
|
|
|
|
|
2017-10-24 23:06:10 +00:00
|
|
|
if (isNotImplemented) {
|
2017-10-10 18:23:10 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rethrow to be handled downstream
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-08-04 01:14:06 +00:00
|
|
|
ajaxOptions(url, type, options = {}) {
|
|
|
|
options.data || (options.data = {});
|
2018-08-09 02:39:32 +00:00
|
|
|
if (this.get('system.shouldIncludeRegion')) {
|
|
|
|
const region = this.get('system.activeRegion');
|
|
|
|
if (region) {
|
|
|
|
options.data.region = region;
|
|
|
|
}
|
2018-08-04 01:14:06 +00:00
|
|
|
}
|
2018-08-08 23:08:27 +00:00
|
|
|
return this._super(url, type, options);
|
2018-08-04 01:14:06 +00:00
|
|
|
},
|
|
|
|
|
2018-02-14 23:31:27 +00:00
|
|
|
// In order to remove stale records from the store, findHasMany has to unload
|
|
|
|
// all records related to the request in question.
|
|
|
|
findHasMany(store, snapshot, link, relationship) {
|
|
|
|
return this._super(...arguments).then(payload => {
|
|
|
|
const relationshipType = relationship.type;
|
2018-04-06 20:39:46 +00:00
|
|
|
const inverse = snapshot.record.inverseFor(relationship.key);
|
|
|
|
if (inverse) {
|
|
|
|
store
|
|
|
|
.peekAll(relationshipType)
|
|
|
|
.filter(record => record.get(`${inverse.name}.id`) === snapshot.id)
|
|
|
|
.forEach(record => {
|
2018-04-07 01:07:57 +00:00
|
|
|
removeRecord(store, record);
|
2018-04-06 20:39:46 +00:00
|
|
|
});
|
|
|
|
}
|
2018-02-14 23:31:27 +00:00
|
|
|
return payload;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
// Single record requests deviate from REST practice by using
|
|
|
|
// the singular form of the resource name.
|
|
|
|
//
|
|
|
|
// REST: /some-resources/:id
|
|
|
|
// Nomad: /some-resource/:id
|
|
|
|
//
|
|
|
|
// This is the original implementation of _buildURL
|
|
|
|
// without the pluralization of modelName
|
|
|
|
urlForFindRecord(id, modelName) {
|
|
|
|
let path;
|
|
|
|
let url = [];
|
|
|
|
let host = get(this, 'host');
|
|
|
|
let prefix = this.urlPrefix();
|
|
|
|
|
|
|
|
if (modelName) {
|
|
|
|
path = modelName.camelize();
|
|
|
|
if (path) {
|
|
|
|
url.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
url.push(encodeURIComponent(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefix) {
|
|
|
|
url.unshift(prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
url = url.join('/');
|
|
|
|
if (!host && url && url.charAt(0) !== '/') {
|
|
|
|
url = '/' + url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
});
|