fastify / fastify-accepts-serializer

Serializer according to the accept header

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Per route configuration doen't work

Eomm opened this issue · comments

The documented feature that let configure the serializers per routes doesn't work.
It has been removed in #4 after a discussion so we should fix the readme

Ref:

I would agree with @climba03003 , but... the custom configuration per route is documented but not implemented!

This snippet doesn't work:

const fastify = require('fastify')()

fastify.register(require('./index'), {
  default: 'application/json'
})

fastify.get('/', {
  config: {
    serializer: {
      serializers: [{
        regex: /^application\/yaml$/,
        serializer: body => 'yaml-custom-no-global-one'
      }]
    }
  }
},
(req, reply) => { reply.send({ foo: 'bar' }) })

fastify.inject({
  url: '/',
  headers: {
    Accept: 'application/yaml'
  }
}, (_, res) => {
  console.log(res.statusCode)
  console.log(res.payload) // expected yaml-custom-no-global-one it is a json: {"foo":"bar"}
})

While, registering the plugin like this works as expected:

fastify.register(require('./index'), {
  serializers: [
    {
      regex: /^application\/yaml$/,
      serializer: body => 'yaml-global'
    }
  ],
  default: 'application/json'
})

The tests confirm this:

test('serializer per route', t => {

Opening a new issue about it

Originally posted by @Eomm in #31 (comment)