Skip to content

Commit

Permalink
Merge pull request #13 from lependu/update
Browse files Browse the repository at this point in the history
Update dependencies
  • Loading branch information
lependu committed May 2, 2020
2 parents 4906ac0 + 006b395 commit 3654ae9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 54 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ node_js:
- "13"
- "12"
- "10"
- "8"
- "6"

notifications:
email:
Expand Down
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Language parser plugin for fastify",
"main": "plugin.js",
"engines": {
"node": ">=6"
"node": ">=10"
},
"scripts": {
"lint": "standard --fix",
Expand All @@ -29,15 +29,10 @@
"devDependencies": {
"fastify": "^2.4.1",
"standard": "^14.0.0",
"tap": "^12.7.0"
"tap": "^14.10.7"
},
"dependencies": {
"accept-language-parser": "^1.5.0",
"fastify-plugin": "^1.6.0"
},
"greenkeeper": {
"ignore": [
"tap"
]
"fastify-plugin": "^1.6.1"
}
}
2 changes: 1 addition & 1 deletion parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function headerParser (decorator, key) {
function headerParserWithSupportedCheck (decorator, key, supportedLngs) {
return function (req, res, next) {
if (req[decorator] && req[decorator][key]) {
let found = require('accept-language-parser').pick(
const found = require('accept-language-parser').pick(
supportedLngs,
req[decorator][key]
)
Expand Down
4 changes: 2 additions & 2 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const fastifyLP = (fastify, opts, next) => {
const parsers = options.order

if (!Array.isArray(parsers)) {
return next(new Error(`options.order has to be an array`))
return next(new Error('options.order has to be an array'))
}

if (!parsers.length) {
return next(new Error(`options.order has to contain at least one parser.`))
return next(new Error('options.order has to contain at least one parser.'))
}

parsers.map(name => {
Expand Down
80 changes: 39 additions & 41 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

const { test } = require('tap')
const t = require('tap')
const Fastify = require('fastify')
const plugin = require('./plugin')
const { defaultOptions } = require('./default-options')

const PARSER_SUBTEST_COUNT = 4 * 5

test('Decorates request with detectedLng', t => {
t.test('Decorates request with detectedLng', t => {
t.plan(2)
const fastify = Fastify()

Expand All @@ -20,7 +20,7 @@ test('Decorates request with detectedLng', t => {
fastify.close()
})

test('Register errors', t => {
t.test('Register errors', t => {
t.plan(8)

testRegisterError(t, { order: 'not-array' },
Expand All @@ -40,7 +40,7 @@ test('Register errors', t => {
'throws if order option contains the same parser multiple times')
})

test('Parser', t => {
t.test('Parser', t => {
t.plan(7)

t.test('Default options | no supported check | no param', t => {
Expand Down Expand Up @@ -79,7 +79,7 @@ test('Parser', t => {

testParser(t, 'header', { order: ['header'] }, null, '/', '/',
{ 'accept-language': 'de;q=0.9,fr;q=0.8' },
JSON.stringify([{ 'code': 'de', 'script': null, 'quality': 0.9 }, { 'code': 'fr', 'script': null, 'quality': 0.8 }]),
JSON.stringify([{ code: 'de', script: null, quality: 0.9 }, { code: 'fr', script: null, quality: 0.8 }]),
'header | returns array of matched items sorted by q')
})

Expand Down Expand Up @@ -156,7 +156,7 @@ test('Parser', t => {
testParser(t, 'header', {
order: ['header'], headerDecorator: 'foo', headerKey: 'bar'
}, 'de;q=0.9,en;q=0.8', '/', '/', {},
JSON.stringify([{ 'code': 'de', 'script': null, 'quality': 0.9 }, { 'code': 'en', 'script': null, 'quality': 0.8 }]),
JSON.stringify([{ code: 'de', script: null, quality: 0.9 }, { code: 'en', script: null, quality: 0.8 }]),
'header | returns array of matched items')
})

Expand All @@ -181,14 +181,14 @@ test('Parser', t => {

testParserOrder(t, {
order: ['query', 'path', 'cookie', 'session', 'header']
}, '/prefix/it?lng=gr', JSON.stringify([{ 'code': 'pt', 'script': null, 'quality': 0.9 }, { 'code': 'sp', 'script': null, 'quality': 0.8 }]),
}, '/prefix/it?lng=gr', JSON.stringify([{ code: 'pt', script: null, quality: 0.9 }, { code: 'sp', script: null, quality: 0.8 }]),
'last one is header')
})
})

function testRegisterError (t, opts, check, msg) {
const fastify = Fastify()
t.tearDown(() => fastify.close.bind(fastify))
t.tearDown(() => fastify.close())

fastify
.register(plugin, opts)
Expand All @@ -200,7 +200,7 @@ function testRegisterError (t, opts, check, msg) {

function testParser (t, name, opts, ctx, route, url, headers, check, msg) {
const fastify = Fastify()
t.tearDown(() => fastify.close.bind(fastify))
t.tearDown(() => fastify.close())

const decorator =
opts[`${name}Decorator`] || defaultOptions[`${name}Decorator`]
Expand All @@ -212,44 +212,42 @@ function testParser (t, name, opts, ctx, route, url, headers, check, msg) {
.decorateRequest(decorator, { [key]: ctx })
}

fastify
.register(plugin, opts)
.get(route, (req, res) => res.send(req.detectedLng))
.ready(err => {
t.error(err)
fastify.register(plugin, opts)
fastify.get(route, (req, res) => res.send(req.detectedLng))
fastify.ready(err => {
t.error(err)

fastify.inject({
url,
method: 'GET',
headers
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, check, msg)
})
fastify.inject({
url,
method: 'GET',
headers
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, check, msg)
})
})
}

function testParserOrder (t, opts, url, check, msg) {
const fastify = Fastify()
t.tearDown(() => fastify.close.bind(fastify))

fastify
.decorateRequest('cookies', { fastifyLanguageParser: 'de' })
.decorateRequest('session', { fastifyLanguageParser: 'fr' })
.register(plugin, opts)
.get('/prefix/:lng', (req, res) => res.send(req.detectedLng))
.ready(err => {
t.tearDown(() => fastify.close())

fastify.decorateRequest('cookies', { fastifyLanguageParser: 'de' })
fastify.decorateRequest('session', { fastifyLanguageParser: 'fr' })
fastify.register(plugin, opts)
fastify.get('/prefix/:lng', (req, res) => res.send(req.detectedLng))
fastify.ready(err => {
t.error(err)

fastify.inject({
url,
method: 'GET',
headers: { 'accept-language': 'pt;q=0.9,sp;q=0.8' }
}, (err, res) => {
t.error(err)

fastify.inject({
url,
method: 'GET',
headers: { 'accept-language': 'pt;q=0.9,sp;q=0.8' }
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, check, msg)
})
t.equal(res.statusCode, 200)
t.equal(res.payload, check, msg)
})
})
}

0 comments on commit 3654ae9

Please sign in to comment.