Ghost/ghost/admin/app/services/ajax.js

73 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-01-18 18:37:14 +03:00
import Ember from 'ember';
import AjaxService from 'ember-ajax/services/ajax';
import {AjaxError} from 'ember-ajax/errors';
import config from 'ghost-admin/config/environment';
2016-01-18 18:37:14 +03:00
const {inject, computed} = Ember;
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.');
}
// 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');
let headers = {};
2016-01-18 18:37:14 +03:00
headers['X-Ghost-Version'] = config.APP.version;
2016-01-18 18:37:14 +03:00
if (session.get('isAuthenticated')) {
2016-01-18 18:37:14 +03:00
session.authorize('authorizer:oauth2', (headerName, headerValue) => {
headers[headerName] = headerValue;
});
}
return headers;
2016-01-18 18:37:14 +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);
} else if (this.isNotFoundError(status, headers, payload)) {
return new NotFoundError(payload.errors);
}
return this._super(...arguments);
},
2016-01-18 18:37:14 +03:00
normalizeErrorResponse(status, headers, payload) {
if (payload && typeof payload === 'object') {
payload.errors = payload.error || payload.errors || payload.message || undefined;
2016-01-18 18:37:14 +03:00
}
return this._super(status, headers, payload);
},
isRequestEntityTooLarge(status/*, headers, payload */) {
return status === 413;
},
isUnsupportedMediaType(status/*, headers, payload */) {
return status === 415;
},
isNotFoundError(status) {
return status === 404;
2016-01-18 18:37:14 +03:00
}
});