2016-01-18 18:37:14 +03:00
|
|
|
import Ember from 'ember';
|
|
|
|
import AjaxService from 'ember-ajax/services/ajax';
|
2016-02-26 16:25:47 +03:00
|
|
|
import {AjaxError} from 'ember-ajax/errors';
|
2016-05-24 15:06:59 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
2016-01-18 18:37:14 +03:00
|
|
|
|
|
|
|
const {inject, computed} = Ember;
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
export function RequestEntityTooLargeError(errors) {
|
|
|
|
AjaxError.call(this, errors, 'Request was rejected because it\'s larger than the maximum file size the server allows');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function UnsupportedMediaTypeError(errors) {
|
|
|
|
AjaxError.call(this, errors, 'Request was rejected because it contains an unknown or unsupported file type.');
|
|
|
|
}
|
|
|
|
|
2016-04-12 14:34:40 +03:00
|
|
|
// TODO: remove once upgraded to ember-ajax 2.0
|
|
|
|
export function NotFoundError(errors) {
|
|
|
|
AjaxError.call(this, errors, 'Resource was not found.');
|
|
|
|
}
|
|
|
|
|
|
|
|
NotFoundError.prototype = Object.create(AjaxError.prototype);
|
|
|
|
|
2016-01-18 18:37:14 +03:00
|
|
|
export default AjaxService.extend({
|
|
|
|
session: inject.service(),
|
|
|
|
|
|
|
|
headers: computed('session.isAuthenticated', function () {
|
|
|
|
let session = this.get('session');
|
2016-06-03 13:51:06 +03:00
|
|
|
let headers = {};
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-06-03 13:51:06 +03:00
|
|
|
headers['X-Ghost-Version'] = config.APP.version;
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-06-03 13:51:06 +03:00
|
|
|
if (session.get('isAuthenticated')) {
|
2016-01-18 18:37:14 +03:00
|
|
|
session.authorize('authorizer:oauth2', (headerName, headerValue) => {
|
|
|
|
headers[headerName] = headerValue;
|
|
|
|
});
|
|
|
|
}
|
2016-06-03 13:51:06 +03:00
|
|
|
|
|
|
|
return headers;
|
2016-01-18 18:37:14 +03:00
|
|
|
}),
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
handleResponse(status, headers, payload) {
|
|
|
|
if (this.isRequestEntityTooLarge(status, headers, payload)) {
|
|
|
|
return new RequestEntityTooLargeError(payload.errors);
|
|
|
|
} else if (this.isUnsupportedMediaType(status, headers, payload)) {
|
|
|
|
return new UnsupportedMediaTypeError(payload.errors);
|
2016-04-12 14:34:40 +03:00
|
|
|
} else if (this.isNotFoundError(status, headers, payload)) {
|
|
|
|
return new NotFoundError(payload.errors);
|
2016-02-26 16:25:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
2016-01-18 18:37:14 +03:00
|
|
|
normalizeErrorResponse(status, headers, payload) {
|
|
|
|
if (payload && typeof payload === 'object') {
|
2016-04-04 13:44:54 +03:00
|
|
|
payload.errors = payload.error || payload.errors || payload.message || undefined;
|
2016-01-18 18:37:14 +03:00
|
|
|
}
|
2016-04-04 13:44:54 +03:00
|
|
|
|
|
|
|
return this._super(status, headers, payload);
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
isRequestEntityTooLarge(status/*, headers, payload */) {
|
|
|
|
return status === 413;
|
|
|
|
},
|
|
|
|
|
|
|
|
isUnsupportedMediaType(status/*, headers, payload */) {
|
|
|
|
return status === 415;
|
2016-04-12 14:34:40 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
isNotFoundError(status) {
|
|
|
|
return status === 404;
|
2016-01-18 18:37:14 +03:00
|
|
|
}
|
|
|
|
});
|