Skip to main content
Version: 1.53.4

API modification

If you want to modify automatically generated API, you can use composer custom onRoute hook.

addComposerOnRouteHook(openApiPath, methods, handler)

  • openApiPath (string) - A route OpenAPI path that Platformatic Composer takes from the OpenAPI specification.
  • methods (string[]) - Route HTTP methods that Platformatic Composer takes from the OpenAPI specification.
  • handler (function) - fastify onRoute hook handler.

onComposerResponse

onComposerResponse hook is called after the response is received from a composed service. It might be useful if you want to modify the response before it is sent to the client. If you want to use it you need to add onComposerResponse property to the config object of the route options.

  • request (object) - fastify request object.
  • reply (object) - fastify reply object.
  • body (object) - undici response body object.

Example

app.platformatic.addComposerOnRouteHook('/users/{id}', ['GET'], routeOptions => {
routeOptions.schema.response[200] = {
type: 'object',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' }
}
}

async function onComposerResponse (request, reply, body) {
const payload = await body.json()
const newPayload = {
firstName: payload.first_name,
lastName: payload.last_name
}
reply.send(newPayload)
}
routeOptions.config.onComposerResponse = onComposerResponse
})

Issues

If you run into a bug or have a suggestion for improvement, please raise an issue on GitHub or join our Discord feedback channel.