Skip to content

Commit

Permalink
Merge pull request #68 from agco/feature/multi-sse-route-must-be-regi…
Browse files Browse the repository at this point in the history
…stered-manually

Multi SSE route must be registered manually so that user can customiz…
  • Loading branch information
kristofsajdak committed Apr 8, 2016
2 parents 3b56cac + 9375cd5 commit 81cd5d9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
21 changes: 9 additions & 12 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,7 @@ exports.register = function (server, opts, next) {

Promise.all(_.map(adapters, (adapter)=>adapter.connect()))
.then(()=> {

if (adapterSSE) {
server.route({
method: 'get',
path: '/changes/streaming',
handler: sse({
context: server
})
})
}


const schemas = {};

const appendLinkedResources = includes(server, adapter, schemas).appendLinkedResources;
Expand Down Expand Up @@ -98,8 +88,15 @@ exports.register = function (server, opts, next) {
}

const getChangesStreaming = function (schema) {
onRouteRegister(schema)
Hoek.assert(adapterSSE, 'getChangesStreaming can not be invoked, adapterSSE is not set')
if (schema == null) {
return _.merge(routes.getChangesStreaming(schema), {
handler: sse({
context: server
})
})
}
onRouteRegister(schema)
return _.merge(routes.getChangesStreaming(schema), {
handler: sse({
context: server,
Expand Down
12 changes: 12 additions & 0 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ module.exports = function () {
}

const getChangesStreaming = function (schema) {
if (schema == null) {
return {
method: 'get',
path: '/changes/streaming',
config: {
tags: ['api'],
validate: {
query: schemaUtils.toJoiGetMultiSSEQueryValidation()
}
}
}
}
return {
method: 'GET',
path: `/${schema.type}/changes/streaming`,
Expand Down
8 changes: 7 additions & 1 deletion lib/utils/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,17 @@ module.exports = function () {
return {id}
}

const toJoiGetMultiSSEQueryValidation = function () {
const resources = Joi.string().required()
return {resources}
}

return {
toJoiPostValidatation,
toJoiPostRelationshipsValidatation,
toJoiGetQueryValidation,
toJoiGetByIdPathValidation,
toJoiPatchRelationshipsValidatation
toJoiPatchRelationshipsValidatation,
toJoiGetMultiSSEQueryValidation
}
}
2 changes: 1 addition & 1 deletion test/sse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ describe('SSE', function () {
url: '/changes/streaming'
}).then(function (res) {
expect(res).to.have.property('statusCode', 400)
const expectedMessage = 'You have not specified any resources, please do so by providing "resource?foo,bar" as query'
const expectedMessage = 'child "resources" fails because ["resources" is required]'
expect(res.result.errors[0]).to.have.property('message', expectedMessage)
})
})
Expand Down
1 change: 1 addition & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var utils = {
const routes = harvester.routes.all(schema)
_.forEach(routes, (route) => server.route(route))
});
server.route(harvester.routes.getChangesStreaming())
resolve({server, harvester})
})
})
Expand Down

0 comments on commit 81cd5d9

Please sign in to comment.