Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hapi v17 #124

Merged
merged 7 commits into from Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 2 additions & 7 deletions .travis.yml
@@ -1,19 +1,14 @@
language: node_js

node_js:
- "4"
- "6"
- "7"
- "8"
- "node"

sudo: false

env:
- HAPI_VERSION="13"
- HAPI_VERSION="14"
- HAPI_VERSION="15"
- HAPI_VERSION="17"

install:
- npm install
- npm install hapi@$HAPI_VERSION

2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2012-2016, Project contributors.
Copyright (c) 2012-2017, Project contributors.
Copyright (c) 2012-2015, Walmart.
All rights reserved.

Expand Down
2 changes: 1 addition & 1 deletion examples/mustache/partials.js 100644 → 100755
Expand Up @@ -27,7 +27,7 @@ internals.main = function () {
server.register(Vision, (err) => {

if (err) {
throw error;
throw err;
}

const partials = {};
Expand Down
28 changes: 11 additions & 17 deletions lib/index.js
@@ -1,11 +1,13 @@
'use strict';

// Load modules

const Hoek = require('hoek');
const Joi = require('joi');
const Manager = require('./manager');
// Additional helper modules required in constructor

const Manager = require('./manager');


// Declare internals

Expand All @@ -22,7 +24,7 @@ internals.schema = Joi.alternatives([
]);


exports.register = function (server, pluginOptions, next) {
exports.register = function (server, pluginOptions) {

server.decorate('server', 'views', function (options) {

Expand All @@ -47,35 +49,27 @@ exports.register = function (server, pluginOptions, next) {

server.handler('view', internals.handler);

server.decorate('reply', 'view', function (template, context, options) {
server.decorate('toolkit', 'view', function (template, context, options) {

const realm = (this.realm.plugins.vision || this.request.server.realm.plugins.vision || {});
Hoek.assert(realm.manager, 'Cannot render view without a views manager configured');
return this.response(realm.manager._response(template, context, options, this.request));
return this.wrap(realm.manager._response(template, context, options, this.request));
});

return next();
};

exports.register.attributes = {
connections: false,
once: true,
pkg: require('../package.json')
};


internals.render = function (template, context, options, callback) {

if (!callback && typeof options === 'function') {
callback = options;
options = {};
}
internals.render = async function (template, context, options = {}) {

const isServer = (typeof this.route === 'function');
const server = (isServer ? this : this.server);
const vision = ((!isServer ? this.route.realm.plugins.vision : null) || server.realm.plugins.vision || server.root.realm.plugins.vision || {});
const vision = ((!isServer ? this.route.realm.plugins.vision : null) || server.realm.plugins.vision || server.root.plugins.vision || {});
Hoek.assert(vision.manager, 'Missing views manager');
return vision.manager.render(template, context, options, callback);
return await vision.manager.render(template, context, options);
};


Expand All @@ -93,7 +87,7 @@ internals.handler = function (route, options) {
options: options.options
};

return function (request, reply) {
return function (request, responder) {

const context = {
params: request.params,
Expand All @@ -110,6 +104,6 @@ internals.handler = function (route, options) {
}
}

reply.view(settings.template, context, settings.options);
return responder.view(settings.template, context, settings.options);
};
};
48 changes: 28 additions & 20 deletions lib/manager.js
Expand Up @@ -3,6 +3,7 @@

const Fs = require('fs');
const Path = require('path');

const Boom = require('boom');
const Hoek = require('hoek');
const Joi = require('joi');
Expand Down Expand Up @@ -454,8 +455,7 @@ internals.Manager.prototype._path = function (template, settings, isLayout, next

return nextFile();
});
},
() => {
}, () => {

return next(Boom.badImplementation('View file not found: `' + template + '`. Locations searched: [' + paths.join(',') + ']'));
});
Expand Down Expand Up @@ -570,43 +570,51 @@ internals.Manager.prototype._response = function (template, context, options, re
};


internals.marshal = function (response, callback) {
internals.marshal = function (response) {

const manager = response.source.manager;

manager._render(response.source.compiled, response.source.context, response.request, (err, rendered) => {
return new Promise((resolve, reject) => {

if (err) {
return callback(err);
}
manager._render(response.source.compiled, response.source.context, response.request, (err, rendered) => {

if (err) {
return reject(err);
}

const config = response.source.compiled.settings;
const config = response.source.compiled.settings;

if (!response.headers['content-type']) {
response.type(config.contentType);
}
if (!response.headers['content-type']) {
response.type(config.contentType);
}

response.encoding(config.encoding);
response.encoding(config.encoding);

return callback(null, rendered);
return resolve(rendered);
});
});
};

internals.prepare = function (response, callback) {

internals.prepare = function (response) {

const manager = response.source.manager;

manager._prepare(response.source.template, response.source.options, (err, compiled) => {
return new Promise((resolve, reject) => {

if (err) {
return callback(err);
}
manager._prepare(response.source.template, response.source.options, (err, compiled) => {

response.source.compiled = compiled;
return callback(response);
if (err) {
return reject(err);
}

response.source.compiled = compiled;
return resolve(response);
});
});
};


internals._wrapMethod = (bind, method, args) => {

return new Promise((resolve, reject) => {
Expand Down
19 changes: 11 additions & 8 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "vision",
"description": "Templates rendering plugin support for hapi.js",
"version": "4.1.1",
"version": "5.0.0-rc4",
"repository": "git://github.com/hapijs/vision",
"main": "lib/index.js",
"keywords": [
Expand All @@ -11,24 +11,27 @@
"hapi"
],
"engines": {
"node": ">=4.0.0"
"node": ">=8.0.0"
},
"dependencies": {
"boom": "4.x.x",
"hoek": "4.x.x",
"boom": "6.x.x",
"hoek": "5.x.x",
"items": "2.x.x",
"joi": "10.x.x"
"joi": "11.x.x"
},
"peerDependencies": {
"hapi": ">=17.0.0-0"
},
"devDependencies": {
"babel-core": "6.x.x",
"babel-plugin-transform-react-jsx": "6.x.x",
"code": "4.x.x",
"code": "5.x.x",
"ejs": "2.x.x",
"handlebars": "4.x.x",
"hapi": "15.x.x",
"hapi": "^17.0.0-rc4",
"hapi-react-views": "6.x.x",
"pug": ">=2.0.0-beta6",
"lab": "11.x.x",
"lab": "14.x.x",
"marko": "3.x.x",
"mustache": "2.x.x",
"nunjucks": "2.x.0",
Expand Down