fastify-request-language
Advanced tools
Comparing version 1.0.0 to 2.0.0
61
index.js
@@ -1,27 +0,42 @@ | ||
'use strict'; | ||
const fp = require('fastify-plugin'); | ||
const acceptLanguage = require('accept-language'); | ||
const bcp47 = require('bcp47'); | ||
module.exports = function(props) { | ||
'use strict'; | ||
// Check that the language tag is set and that it is an array | ||
if(typeof props.languages === 'undefined' || Object.prototype.toString.call(props.languages) !== '[object Array]') { | ||
throw new TypeError('You must define your languages in an array of strings.'); | ||
} | ||
props.languages.forEach(function(languageTag) { | ||
var language = bcp47.parse(languageTag); | ||
if(language === null) { | ||
throw new TypeError('Your language tag \'' + languageTag + '\' is not BCP47 compliant. For more info https://tools.ietf.org/html/bcp47.') | ||
} | ||
}); | ||
acceptLanguage.languages(props.languages); | ||
module.exports = fp( | ||
(fastify, options, next) => { | ||
// Check that the language tag is set and that it is an array | ||
if (!options || !Array.isArray(options.languages)) { | ||
next(new TypeError( | ||
'You must define your languages in an array of strings.' | ||
)); | ||
return; | ||
} | ||
return function fastifyRequestLanguage(req, res, next) { | ||
req.language = acceptLanguage.get(req.headers['accept-language']); | ||
next(); | ||
} | ||
} | ||
for (const languageTag of options.languages) { | ||
const language = bcp47.parse(languageTag); | ||
if (language === null) { | ||
next(new TypeError( | ||
'Your language tag \'' + | ||
languageTag + | ||
'\' is not BCP47 compliant. For more info https://tools.ietf.org/html/bcp47.' | ||
)); | ||
return; | ||
} | ||
} | ||
acceptLanguage.languages(options.languages); | ||
fastify.decorateRequest('language', ''); | ||
fastify.addHook('onRequest', (request, reply, done) => { | ||
request.language = acceptLanguage.get(request.headers['accept-language']); | ||
done(); | ||
}); | ||
next(); | ||
}, | ||
{ | ||
fastify: '3.x', | ||
name: 'fastify-request-language' | ||
} | ||
); |
{ | ||
"name": "fastify-request-language", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A middleware to attach language to request by parsing Accept-Language header", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "xo && tap test.js" | ||
}, | ||
@@ -27,4 +27,10 @@ "repository": { | ||
"accept-language": "^3.0.18", | ||
"bcp47": "^1.1.2" | ||
"bcp47": "^1.1.2", | ||
"fastify-plugin": "^2.3.3" | ||
}, | ||
"devDependencies": { | ||
"fastify": "^3.3.0", | ||
"tap": "^14.10.8", | ||
"xo": "^0.33.1" | ||
} | ||
} |
# fastify-request-language | ||
A middleware to attach language to request by parsing Accept-Language header | ||
![Build Status](https://img.shields.io/github/workflow/status/gradeup/fastify-request-language/Continuous%20Integration?style=flat-square) | ||
![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg?style=flat-square) | ||
Adds language to [the Fastify `request` object](https://www.fastify.io/docs/master/Request/) by parsing `Accept-Language` HTTP Header. | ||
## Requirements | ||
- Node >= 12 | ||
- fastify >= 3 | ||
## Install | ||
```npm install fastify-request-language``` | ||
## Usage | ||
```js | ||
const fastify = require('fastify')({ | ||
logger: true, | ||
trustProxy: true, | ||
}); | ||
fastify.register(require('fastify-request-language'), { | ||
languages: ['en', 'hi'], | ||
}); | ||
// Access language in request.language | ||
fastify.get('/', (request, reply) => { | ||
reply.send({ language: request.language }); | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
5622
7
82
1
31
3
3
+ Addedfastify-plugin@^2.3.3
+ Addedfastify-plugin@2.3.4(transitive)
+ Addedsemver@7.6.3(transitive)