玩具
霍比玩具胸口
主维护者 - 变形常春藤
安装
npm install @hapipal/toys
使用量
另见 API 参考
玩具的用法为:hopi v20+和nodejs v16+(ưμ㼯a) (中文(简体) ).
玩具是用来减少普通锅炉板 霍皮 v20+ (英语) 项目.
以下是一个实例 Toys.auth.strategy() 财务报告和审定财务报表 Toys.withRouteDefaults()编辑 API 参考 也充满实例.
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const Toys = require('@hapipal/toys');
(async () => {
const server = Hapi.server();
// Make a one-off auth strategy for testing
Toys.auth.strategy(server, 'name-from-param', (request, h) => {
// Yes, perhaps not the most secure
const { username } = request.params;
if (!username) {
throw Boom.unauthorized(null, 'Custom');
}
return h.authenticated({ credentials: { user: { name: username } } });
});
// Default all route methods to "get", unless otherwise specified
const defaultToGet = Toys.withRouteDefaults({ method: 'get' });
server.route(
defaultToGet([
{
method: 'post',
path: '/',
handler: (request) => {
return { posted: true };
}
},
{ // Look ma, my method is defaulting to "get"!
path: '/as/{username}',
options: {
auth: 'name-from-param', // Here's our simple auth strategy
handler: (request) => {
const username = request.auth.credentials?.user?.name;
return { username };
}
}
}
])
);
await server.start();
console.log(`Now, go forth and ${server.info.uri}/as/your-name`);
})();