-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (24 loc) · 750 Bytes
/
index.js
File metadata and controls
32 lines (24 loc) · 750 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict'
const fp = require('fastify-plugin')
const Useragent = require('useragent')
// https://www.npmjs.com/package/useragent#adding-more-features-to-the-useragent
// require('useragent/features')
function fastifyUserAgent (fastify, options, next) {
const opts = Object.assign({
name: 'userAgent',
hook: 'onRequest'
}, options)
fastify.decorateRequest(opts.name, null)
fastify.addHook(opts.hook, (request, reply, done) => {
request[opts.name] = Useragent.lookup(request.headers['user-agent'])
done()
})
next()
}
const plugin = fp(fastifyUserAgent, {
fastify: '^5.x',
name: 'fastify-user-agent'
})
module.exports = plugin
module.exports.default = plugin
module.exports.fastifyUserAgent = fastifyUserAgent