fastify-accepts-serializer
Advanced tools
Comparing version 3.1.0 to 3.2.0
14
index.js
@@ -23,4 +23,14 @@ 'use strict' | ||
reply.serializer.cache = serializerCache | ||
reply.serializer.serializerManager = SerializerManager.expand(reply.serializer, globalSerializerManager) | ||
if (reply.context.config.serializers) { | ||
// keep route level cache in config to prevent messing with global cache | ||
reply.context.config.serializers.cache = Object.assign({}, reply.context.config.serializers.cache) | ||
reply.serializer.serializerManager = SerializerManager.expand({ | ||
serializers: reply.context.config.serializers, | ||
cache: reply.context.config.serializers.cache | ||
}, globalSerializerManager) | ||
} else { | ||
// use global serializer | ||
reply.serializer.cache = serializerCache | ||
reply.serializer.serializerManager = SerializerManager.expand(reply.serializer, globalSerializerManager) | ||
} | ||
@@ -27,0 +37,0 @@ const serializerManager = reply.serializer.serializerManager |
{ | ||
"name": "fastify-accepts-serializer", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Serializer according to the accept header", | ||
@@ -24,5 +24,5 @@ "main": "index.js", | ||
"fastify": "^3.0.0", | ||
"msgpack5": "^4.2.1", | ||
"msgpack5": "^4.3.0", | ||
"pre-commit": "^1.2.2", | ||
"protobufjs": "^6.8.8", | ||
"protobufjs": "^6.10.2", | ||
"snazzy": "^9.0.0", | ||
@@ -29,0 +29,0 @@ "standard": "^16.0.2", |
@@ -45,10 +45,8 @@ # fastify-accepts-serializer | ||
const config = { | ||
serializer: { | ||
serializers: [ | ||
{ | ||
regex: /^application\/x-protobuf$/, | ||
serializer: body => AwesomeMessage.encode(AwesomeMessage.create(body)).finish() | ||
} | ||
] | ||
} | ||
serializers: [ | ||
{ | ||
regex: /^application\/x-protobuf$/, | ||
serializer: body => AwesomeMessage.encode(AwesomeMessage.create(body)).finish() | ||
} | ||
] | ||
} | ||
@@ -59,3 +57,2 @@ | ||
}) | ||
``` | ||
@@ -62,0 +59,0 @@ ## Behaviour |
@@ -318,2 +318,91 @@ 'use strict' | ||
test('serializer per route through route option', t => { | ||
t.plan(3) | ||
const fastify = Fastify() | ||
fastify.register(plugin, { | ||
serializers: [ | ||
{ | ||
regex: /^application\/yaml$/, | ||
serializer: body => YAML.stringify(body) | ||
} | ||
], | ||
default: 'application/yaml' | ||
}) | ||
const config = { | ||
serializers: [ | ||
{ | ||
regex: /^application\/x-protobuf$/, | ||
serializer: body => AwesomeMessage.encode(AwesomeMessage.create(body)).finish() | ||
} | ||
] | ||
} | ||
fastify.get('/request', { config }, function (req, reply) { | ||
reply.send({ pippo: 'pluto' }) | ||
}) | ||
fastify.get('/request2', function (req, reply) { | ||
reply.send('my-custom-string') | ||
}) | ||
fastify.get('/request3', function (req, reply) { | ||
reply | ||
.serializer(_ => 'foo-bar-baz') | ||
.send({ pippo: 'pluto' }) | ||
}) | ||
t.test('application/x-protobuf -> protobuf', t => { | ||
t.plan(3) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/request', | ||
payload: {}, | ||
headers: { | ||
accept: 'application/x-protobuf' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
t.strictDeepEqual(res.headers['content-type'], 'application/x-protobuf') | ||
t.strictDeepEqual(res.payload, AwesomeMessage.encode(AwesomeMessage.create({ pippo: 'pluto' })).finish().toString()) | ||
}) | ||
}) | ||
t.test('route level should not pullute global cache', t => { | ||
t.plan(3) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/request2', | ||
payload: {}, | ||
headers: { | ||
accept: 'application/yaml' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
t.strictDeepEqual(res.headers['content-type'], 'application/yaml') | ||
t.strictDeepEqual(res.payload, 'my-custom-string') | ||
}) | ||
}) | ||
t.test('overwrite by fastify reply', t => { | ||
t.plan(3) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/request3', | ||
payload: {}, | ||
headers: { | ||
accept: 'application/yaml' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
t.strictDeepEqual(res.headers['content-type'], 'application/yaml') | ||
t.strictDeepEqual(res.payload, 'foo-bar-baz') | ||
}) | ||
}) | ||
}) | ||
test('serializer without conf', t => { | ||
@@ -320,0 +409,0 @@ t.plan(2) |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
21448
537
0
12
64