-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from firstandthird/hapi17_plus
Hapi17 plus
- Loading branch information
Showing
27 changed files
with
305 additions
and
2,190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,5 @@ language: node_js | |
|
||
node_js: | ||
- 8 | ||
- 6 | ||
|
||
script: npm run test-travis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,34 @@ | ||
'use strict'; | ||
const async = require('async'); | ||
const str2fn = require('str2fn'); | ||
const Boom = require('boom'); | ||
const boom = require('boom'); | ||
const aug = require('aug'); | ||
const pprops = require('p-props'); | ||
const varson = require('varson'); | ||
|
||
module.exports = (viewConfig) => (request, reply) => { | ||
async.autoInject({ | ||
preProcess: done => { | ||
if (viewConfig.preProcess) { | ||
return str2fn(request.server.methods, viewConfig.preProcess)(request, viewConfig.options, reply, done); | ||
} | ||
return done(); | ||
}, | ||
locals: (preProcess, done) => { | ||
if (preProcess) { | ||
return done(null, false); | ||
} | ||
return request.server.methods.views.fetch(request, viewConfig, done); | ||
}, | ||
globals: (preProcess, done) => { | ||
if (preProcess) { | ||
return done(null, false); | ||
} | ||
if (!viewConfig.options.globals) { | ||
return done(null, {}); | ||
} | ||
return request.server.methods.views.fetch(request, Object.assign({ options: viewConfig.options }, viewConfig.options.globals), done); | ||
module.exports = (viewConfig) => { | ||
return async (request, h) => { | ||
if (viewConfig.preProcess) { | ||
viewConfig.preProcess(request, viewConfig.options, h); | ||
} | ||
}, (err, data) => { | ||
if (err) { | ||
if (typeof viewConfig.options.onError === 'function') { | ||
return viewConfig.options.onError(err, reply); | ||
} | ||
// aug with globals: | ||
const obj = aug({}, viewConfig.data, viewConfig.options.globals); | ||
try { | ||
const data = varson(obj, { request, methods: request.server.methods }); | ||
const result = await pprops(data); | ||
if (viewConfig.preResponse) { | ||
viewConfig.preResponse(request, viewConfig.options, result); | ||
} | ||
return h.view(viewConfig.view, result); | ||
} catch (err) { | ||
if (typeof viewConfig.onError === 'function') { | ||
return viewConfig.onError(err, reply); | ||
} | ||
if (typeof viewConfig.options.onError === 'string') { | ||
return str2fn(request.server.methods, viewConfig.options.onError)(err, reply); | ||
return viewConfig.onError(err); | ||
} | ||
if (typeof viewConfig.onError === 'string') { | ||
return str2fn(request.server.methods, viewConfig.onError)(err, reply); | ||
if (typeof viewConfig.options.onError === 'function') { | ||
return viewConfig.options.onError(err); | ||
} | ||
// boom errs can be handed back directly: | ||
if (err.isBoom) { | ||
return reply(err); | ||
throw err; | ||
} | ||
return reply(Boom.boomify(err)); | ||
} | ||
// Returned early in preProcess | ||
if (!data.globals && !data.locals) { | ||
return; | ||
} | ||
|
||
const combinedData = aug({}, data.globals, data.locals); | ||
if (viewConfig.options.debug) { | ||
request.server.log(['hapi-views', 'debug'], { | ||
data: combinedData, | ||
path: request.url.path | ||
}); | ||
} | ||
if (viewConfig.options.allowDebugQuery && (request.query.json === '1' || request.query.debug === '1')) { | ||
return reply(combinedData).type('application/json'); | ||
} | ||
|
||
if (viewConfig.preResponse) { | ||
return str2fn(request.server.methods, viewConfig.preResponse)(request, viewConfig.options, combinedData, reply, () => { | ||
reply.view(viewConfig.view, combinedData); | ||
}); | ||
throw boom.boomify(err); | ||
} | ||
const viewName = typeof viewConfig.view === 'function' ? viewConfig.view(combinedData) : viewConfig.view; | ||
return reply.view(viewName, combinedData); | ||
}); | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.