Add version header to API requests

no issue
- modifies the version info included in `env.APP.version` to only include the `major.minor` version numbers
- update base adapter to include `X-Ghost-Version` header
- update `ajax` service to include `X-Ghost-Version` header
This commit is contained in:
Kevin Ansfield 2016-06-03 11:51:06 +01:00
parent a4d519d20c
commit 4cc4781b3e
6 changed files with 84 additions and 14 deletions

View File

@ -2,6 +2,7 @@ import Ember from 'ember';
import RESTAdapter from 'ember-data/adapters/rest';
import ghostPaths from 'ghost/utils/ghost-paths';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
import config from 'ghost/config/environment';
const {
inject: {service}
@ -15,6 +16,10 @@ export default RESTAdapter.extend(DataAdapterMixin, {
session: service(),
headers: {
'X-Ghost-Version': config.APP.version
},
shouldBackgroundReloadRecord() {
return false;
},

View File

@ -1,6 +1,7 @@
import Ember from 'ember';
import AjaxService from 'ember-ajax/services/ajax';
import {AjaxError} from 'ember-ajax/errors';
import config from 'ghost/config/environment';
const {inject, computed} = Ember;
@ -24,18 +25,17 @@ export default AjaxService.extend({
headers: computed('session.isAuthenticated', function () {
let session = this.get('session');
if (session.get('isAuthenticated')) {
let headers = {};
headers['X-Ghost-Version'] = config.APP.version;
if (session.get('isAuthenticated')) {
session.authorize('authorizer:oauth2', (headerName, headerValue) => {
headers[headerName] = headerValue;
});
}
return headers;
} else {
return [];
}
}),
handleResponse(status, headers, payload) {

View File

@ -17,6 +17,11 @@ module.exports = function (environment) {
APP: {
// Here you can pass flags/options to your application instance
// when it is created
// override the default version string which contains git info from
// https://github.com/cibernox/git-repo-version. Only include the
// `major.minor` version numbers
version: require('../package.json').version.replace(/\.\d+$/, '')
},
'ember-simple-auth': {

View File

@ -1,6 +1,6 @@
{
"name": "ghost",
"version": "0.0.0",
"version": "0.8.0",
"description": "Small description for ghost goes here",
"private": true,
"directories": {

View File

@ -6,8 +6,9 @@ import {
import Pretender from 'pretender';
import {AjaxError, UnauthorizedError} from 'ember-ajax/errors';
import {RequestEntityTooLargeError, UnsupportedMediaTypeError} from 'ghost/services/ajax';
import config from 'ghost/config/environment';
function stubAjaxEndpoint(server, response = {}, code = 500) {
function stubAjaxEndpoint(server, response = {}, code = 200) {
server.get('/test/', function () {
return [
code,
@ -34,9 +35,22 @@ describeModule(
server.shutdown();
});
it('adds Ghost version header to requests', function (done) {
let {version} = config.APP;
let ajax = this.subject();
stubAjaxEndpoint(server, {});
ajax.request('/test/').then(() => {
let [request] = server.handledRequests;
expect(request.requestHeaders['X-Ghost-Version']).to.equal(version);
done();
});
});
it('correctly parses single message response text', function (done) {
let error = {message: 'Test Error'};
stubAjaxEndpoint(server, error);
stubAjaxEndpoint(server, error, 500);
let ajax = this.subject();
@ -50,7 +64,7 @@ describeModule(
it('correctly parses single error response text', function (done) {
let error = {error: 'Test Error'};
stubAjaxEndpoint(server, error);
stubAjaxEndpoint(server, error, 500);
let ajax = this.subject();
@ -64,7 +78,7 @@ describeModule(
it('correctly parses multiple error messages', function (done) {
let error = {errors: ['First Error', 'Second Error']};
stubAjaxEndpoint(server, error);
stubAjaxEndpoint(server, error, 500);
let ajax = this.subject();
@ -77,7 +91,7 @@ describeModule(
});
it('returns default error object for non built-in error', function (done) {
stubAjaxEndpoint(server, {});
stubAjaxEndpoint(server, {}, 500);
let ajax = this.subject();

View File

@ -0,0 +1,46 @@
import { expect } from 'chai';
import {
describeModule,
it
} from 'ember-mocha';
import Pretender from 'pretender';
import config from 'ghost/config/environment';
describeModule(
'service:store',
'Integration: Service: store',
{
integration: true
},
function () {
let server;
beforeEach(function () {
server = new Pretender();
});
afterEach(function () {
server.shutdown();
});
it('adds Ghost version header to requests', function (done) {
let {version} = config.APP;
let store = this.subject();
server.get('/ghost/api/v0.1/posts/1/', function () {
return [
404,
{'Content-Type': 'application/json'},
JSON.stringify({})
];
});
store.find('post', 1).catch(() => {
let [request] = server.handledRequests;
console.log(request);
expect(request.requestHeaders['X-Ghost-Version']).to.equal(version);
done();
});
});
}
);