2015-10-28 12:36:45 +01:00
|
|
|
import Ember from 'ember';
|
2015-02-13 05:22:32 +01:00
|
|
|
import DS from 'ember-data';
|
2014-12-26 01:01:02 +01:00
|
|
|
import ghostPaths from 'ghost/utils/ghost-paths';
|
2016-01-18 16:37:14 +01:00
|
|
|
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
|
2015-11-04 16:20:11 +01:00
|
|
|
|
|
|
|
const {inject} = Ember;
|
2015-10-28 12:36:45 +01:00
|
|
|
const {RESTAdapter} = DS;
|
2014-12-26 01:01:02 +01:00
|
|
|
|
2016-01-18 16:37:14 +01:00
|
|
|
export default RESTAdapter.extend(DataAdapterMixin, {
|
|
|
|
authorizer: 'authorizer:oauth2',
|
|
|
|
|
2014-12-26 01:01:02 +01:00
|
|
|
host: window.location.origin,
|
|
|
|
namespace: ghostPaths().apiRoot.slice(1),
|
|
|
|
|
2015-11-04 16:20:11 +01:00
|
|
|
session: inject.service('session'),
|
|
|
|
|
2015-10-28 12:36:45 +01:00
|
|
|
shouldBackgroundReloadRecord() {
|
2015-10-19 17:35:17 +02:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-10-28 12:36:45 +01:00
|
|
|
query(store, type, query) {
|
|
|
|
let id;
|
2014-12-26 01:01:02 +01:00
|
|
|
|
|
|
|
if (query.id) {
|
|
|
|
id = query.id;
|
|
|
|
delete query.id;
|
|
|
|
}
|
|
|
|
|
2015-06-03 04:56:42 +02:00
|
|
|
return this.ajax(this.buildURL(type.modelName, id), 'GET', {data: query});
|
2014-12-26 01:01:02 +01:00
|
|
|
},
|
|
|
|
|
2016-01-13 15:47:10 +01:00
|
|
|
buildURL() {
|
2014-12-26 01:01:02 +01:00
|
|
|
// Ensure trailing slashes
|
2016-01-13 15:47:10 +01:00
|
|
|
let url = this._super(...arguments);
|
2014-12-26 01:01:02 +01:00
|
|
|
|
|
|
|
if (url.slice(-1) !== '/') {
|
|
|
|
url += '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Override deleteRecord to disregard the response body on 2xx responses.
|
|
|
|
// This is currently needed because the API is returning status 200 along
|
|
|
|
// with the JSON object for the deleted entity and Ember expects an empty
|
|
|
|
// response body for successful DELETEs.
|
|
|
|
// Non-2xx (failure) responses will still work correctly as Ember will turn
|
|
|
|
// them into rejected promises.
|
2015-10-28 12:36:45 +01:00
|
|
|
deleteRecord() {
|
|
|
|
let response = this._super(...arguments);
|
2014-12-26 01:01:02 +01:00
|
|
|
|
2015-10-28 12:36:45 +01:00
|
|
|
return response.then(() => {
|
2014-12-26 01:01:02 +01:00
|
|
|
return null;
|
|
|
|
});
|
2015-11-04 16:20:11 +01:00
|
|
|
},
|
|
|
|
|
2015-10-28 12:36:45 +01:00
|
|
|
handleResponse(status) {
|
2015-11-04 16:20:11 +01:00
|
|
|
if (status === 401) {
|
|
|
|
if (this.get('session.isAuthenticated')) {
|
|
|
|
this.get('session').invalidate();
|
2015-11-30 19:23:47 +01:00
|
|
|
return; // prevent error from bubbling because invalidate is async
|
2015-11-04 16:20:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._super(...arguments);
|
2014-12-26 01:01:02 +01:00
|
|
|
}
|
|
|
|
});
|