Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-request-language

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-request-language - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

.editorconfig

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 });
});
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc