fastify-accepts-serializer
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -9,2 +9,5 @@ 'use strict' | ||
function acceptsSerializerPlugin (fastify, options, next) { | ||
const serializerCache = {} | ||
options.cache = serializerCache | ||
const globalSerializerManager = SerializerManager.build(options) | ||
@@ -21,6 +24,3 @@ | ||
if (!reply.serializer) { | ||
reply.serializer = {} | ||
} | ||
reply.serializer.cache = serializerCache | ||
reply.serializer.serializerManager = SerializerManager.expand(reply.serializer, globalSerializerManager) | ||
@@ -27,0 +27,0 @@ |
{ | ||
"name": "fastify-accepts-serializer", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Serializer according to the accept header", | ||
@@ -8,2 +8,3 @@ "main": "index.js", | ||
"lint": "standard --verbose | snazzy", | ||
"lint:fix": "standard --fix", | ||
"unit": "tap test/test.js", | ||
@@ -27,4 +28,4 @@ "test": "npm run lint && tap test/test.js" | ||
"protobufjs": "^6.8.8", | ||
"snazzy": "^8.0.0", | ||
"standard": "^14.0.0", | ||
"snazzy": "^9.0.0", | ||
"standard": "^16.0.2", | ||
"tap": "^14.10.7", | ||
@@ -35,4 +36,4 @@ "yamljs": "^0.3.0" | ||
"fastify-accepts": "^2.0.0", | ||
"fastify-plugin": "^2.0.0" | ||
"fastify-plugin": "^3.0.0" | ||
} | ||
} |
@@ -17,3 +17,3 @@ 'use strict' | ||
this.serializers = configuration.serializers | ||
this.cache = {} | ||
this.cache = configuration.cache | ||
} | ||
@@ -23,8 +23,9 @@ | ||
const cacheValue = this.cache[types] | ||
if (cacheValue) return cacheValue | ||
for (var i = 0; i < types.length; i++) { | ||
for (let i = 0; i < types.length; i++) { | ||
const type = types[i] | ||
for (var j = 0; j < this.serializers.length; j++) { | ||
for (let j = 0; j < this.serializers.length; j++) { | ||
const serializer = this.serializers[j] | ||
@@ -46,3 +47,2 @@ if (serializer.isAble(type)) { | ||
SerializerManager.build = function (options) { | ||
options = options || {} | ||
options.serializers = options.serializers || [] | ||
@@ -53,3 +53,2 @@ return SerializerManager.expand(options, { serializers: [] }) | ||
SerializerManager.expand = function (options, fallbackSerializer) { | ||
options = options || {} | ||
options.serializers = options.serializers || [] | ||
@@ -59,3 +58,4 @@ | ||
return new SerializerManager({ | ||
serializers: serializers.concat(fallbackSerializer.serializers) | ||
serializers: serializers.concat(fallbackSerializer.serializers), | ||
cache: options.cache | ||
}) | ||
@@ -62,0 +62,0 @@ } |
@@ -368,1 +368,52 @@ 'use strict' | ||
}) | ||
test('serializer cache', t => { | ||
t.plan(1) | ||
const fastify = Fastify() | ||
fastify.register(plugin, { | ||
serializers: [ | ||
{ | ||
regex: /^application\/cache$/, | ||
serializer: (body) => body | ||
} | ||
] | ||
}) | ||
t.test('it shoud populate cache', t => { | ||
t.plan(8) | ||
fastify.get('/request', function (req, reply) { | ||
t.strictDeepEqual(Object.keys(reply.serializer.cache), ['application/cache']) | ||
reply.send('cache') | ||
}) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/request', | ||
payload: {}, | ||
headers: { | ||
accept: 'application/cache' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
t.strictDeepEqual(res.headers['content-type'], 'application/cache') | ||
t.strictDeepEqual(res.payload, 'cache') | ||
}) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/request', | ||
payload: {}, | ||
headers: { | ||
accept: 'application/cache' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
t.strictDeepEqual(res.headers['content-type'], 'application/cache') | ||
t.strictDeepEqual(res.payload, 'cache') | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
19517
13
481
1
- Removedfastify-plugin@2.3.4(transitive)
- Removedsemver@7.7.1(transitive)
Updatedfastify-plugin@^3.0.0