如何使用babel为我的js服务模块实现已validation的es 7装饰器

我在装饰阶段使用babel:0在我的stream量通量js项目的支持,我想使用我的服务API模块validation装饰检查有效的用户会话。

谷歌search,似乎有几个职位,解释不同的变化,但无法find一个权威的文档或指导。

这是我到目前为止所尝试的,我知道我的authentication函数的参数是不正确的,不知道是否需要为我的模块实现一个类,而不是只使用exports对象。

我无法find文档的部分是如何实现装饰器本身 – 在这种情况下,装饰函数将接收并检查它的req参数。

// how do I change this method so that it can be implemented as a decorator function checkAuthenticated(req) { if (!req.session || !req.session.username) { throw new Error('unauthenticated'); } } module.exports = { @checkAuthenticated read: function(req, resource, params, serviceConfig, callback) { //@authenticated decorator should allow me to move this out of this here //checkAuthenticated(req); if (resource === 'product.search') { var keyword = params.text; if (!keyword || keyword.length === 0) { return callback('empty param', null); } else { searchProducts(keyword, callback); } } } }; 

 class Http{ @checkAuthenticated read(req, resource, params, serviceConfig, callback) { if (resource === 'product.search') { var keyword = params.text; if (!keyword || keyword.length === 0) { return callback('empty param', null); } else { this.searchProducts(keyword, callback); } } } searchProducts(keyword, callback) { callback(null, 'worked'); } } function checkAuthenticated(target, key, descriptor) { return { ...descriptor, value: function(){ console.log(arguments); const req = arguments[0]; if (!req.session || !req.session.username) { throw new Error('unauthenticated'); } return descriptor.value.apply(this, arguments); } }; } let h = new Http(); h.read( { session: { username: 'user' } }, 'product.search', { text: 'my keywords' }, null, function(err, result) { if (err) return alert(err); return alert(result); } ); 

查看jsbin http://jsbin.com/yebito/edit?js,console,output