2015-10-28 14:36:45 +03:00
|
|
|
import Ember from 'ember';
|
2016-01-12 22:23:01 +03:00
|
|
|
import RESTAdapter from 'ember-data/adapters/rest';
|
2014-12-26 03:01:02 +03:00
|
|
|
import ghostPaths from 'ghost/utils/ghost-paths';
|
2016-01-18 18:37:14 +03:00
|
|
|
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
|
2016-06-03 13:51:06 +03:00
|
|
|
import config from 'ghost/config/environment';
|
2015-11-04 18:20:11 +03:00
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
const {
|
|
|
|
inject: {service}
|
|
|
|
} = Ember;
|
2014-12-26 03:01:02 +03:00
|
|
|
|
2016-01-18 18:37:14 +03:00
|
|
|
export default RESTAdapter.extend(DataAdapterMixin, {
|
|
|
|
authorizer: 'authorizer:oauth2',
|
|
|
|
|
2014-12-26 03:01:02 +03:00
|
|
|
host: window.location.origin,
|
|
|
|
namespace: ghostPaths().apiRoot.slice(1),
|
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
session: service(),
|
2015-11-04 18:20:11 +03:00
|
|
|
|
2016-06-03 13:51:06 +03:00
|
|
|
headers: {
|
|
|
|
'X-Ghost-Version': config.APP.version
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
shouldBackgroundReloadRecord() {
|
2015-10-19 18:35:17 +03:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
query(store, type, query) {
|
|
|
|
let id;
|
2014-12-26 03:01:02 +03:00
|
|
|
|
|
|
|
if (query.id) {
|
|
|
|
id = query.id;
|
|
|
|
delete query.id;
|
|
|
|
}
|
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
return this.ajax(this.buildURL(type.modelName, id), 'GET', {data: query});
|
2014-12-26 03:01:02 +03:00
|
|
|
},
|
|
|
|
|
2016-01-13 17:47:10 +03:00
|
|
|
buildURL() {
|
2014-12-26 03:01:02 +03:00
|
|
|
// Ensure trailing slashes
|
2016-01-13 17:47:10 +03:00
|
|
|
let url = this._super(...arguments);
|
2014-12-26 03:01:02 +03:00
|
|
|
|
|
|
|
if (url.slice(-1) !== '/') {
|
|
|
|
url += '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
handleResponse(status) {
|
2015-11-04 18:20:11 +03:00
|
|
|
if (status === 401) {
|
|
|
|
if (this.get('session.isAuthenticated')) {
|
|
|
|
this.get('session').invalidate();
|
2015-11-30 21:23:47 +03:00
|
|
|
return; // prevent error from bubbling because invalidate is async
|
2015-11-04 18:20:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._super(...arguments);
|
2014-12-26 03:01:02 +03:00
|
|
|
}
|
|
|
|
});
|