open-nomad/ui/app/adapters/application.js

129 lines
3.3 KiB
JavaScript
Raw Normal View History

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';
import codesForError from '../utils/codes-for-error';
import removeRecord from '../utils/remove-record';
2018-07-31 21:32:17 +00:00
import { default as NoLeaderError, NO_LEADER } from '../utils/no-leader-error';
import classic from 'ember-classic-decorator';
2017-09-19 14:47:10 +00:00
export const namespace = 'v1';
@classic
export default class ApplicationAdapter extends RESTAdapter {
namespace = namespace;
2017-09-19 14:47:10 +00:00
@service system;
@service token;
2017-09-19 14:47:10 +00:00
@computed('token.secret')
get headers() {
2017-09-19 14:47:10 +00:00
const token = this.get('token.secret');
if (token) {
return {
2017-09-19 14:47:10 +00:00
'X-Nomad-Token': token,
};
}
return undefined;
}
2017-09-19 14:47:10 +00:00
2018-07-31 21:32:17 +00:00
handleResponse(status, headers, payload) {
if (status === 500 && payload === NO_LEADER) {
return new NoLeaderError();
}
return super.handleResponse(...arguments);
}
2018-07-31 21:32:17 +00:00
findAll() {
return super.findAll(...arguments).catch(error => {
const errorCodes = codesForError(error);
const isNotImplemented = errorCodes.includes('501');
if (isNotImplemented) {
return [];
}
// Rethrow to be handled downstream
throw error;
});
}
2020-07-21 04:05:07 +00:00
ajaxOptions(url, verb, options = {}) {
2018-08-04 01:14:06 +00:00
options.data || (options.data = {});
if (this.get('system.shouldIncludeRegion')) {
2020-07-21 04:05:07 +00:00
// Region should only ever be a query param. The default ajaxOptions
// behavior is to include data attributes in the requestBody for PUT
// and POST requests. This works around that.
const region = this.get('system.activeRegion');
if (region) {
2020-07-21 04:05:07 +00:00
url = associateRegion(url, region);
}
2018-08-04 01:14:06 +00:00
}
2020-07-21 04:05:07 +00:00
return super.ajaxOptions(url, verb, options);
}
2018-08-04 01:14:06 +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 super.findHasMany(...arguments).then(payload => {
const relationshipType = relationship.type;
const inverse = snapshot.record.inverseFor(relationship.key);
if (inverse) {
store
.peekAll(relationshipType)
.filter(record => record.get(`${inverse.name}.id`) === snapshot.id)
.forEach(record => {
removeRecord(store, record);
});
}
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);
}
}
2017-09-19 14:47:10 +00:00
if (id) {
url.push(encodeURIComponent(id));
}
2017-09-19 14:47:10 +00:00
if (prefix) {
url.unshift(prefix);
2017-09-19 14:47:10 +00:00
}
url = url.join('/');
if (!host && url && url.charAt(0) !== '/') {
url = '/' + url;
}
2017-09-19 14:47:10 +00:00
return url;
}
urlForUpdateRecord() {
return this.urlForFindRecord(...arguments);
}
}
2020-07-21 04:05:07 +00:00
function associateRegion(url, region) {
return url.indexOf('?') !== -1 ? `${url}&region=${region}` : `${url}?region=${region}`;
}