Skip to content

Adonisjs package for improved handling of responses for multiple content-types

License

Notifications You must be signed in to change notification settings

ThisIsMissEm/adonisjs-respond-with

Repository files navigation

AdonisJS Respond With

A small plugin for Adonis.js to make responding with different content-types easier.

API

This package extends the Request class to add a respondWith method, which takes a record of key-value pairs where the key is the accepted content-type, and the value is a callable function that handles the response.

Example Usage

export default class ExampleController {
  async show({ request, response, view }: HttpContext) {
    request.respondWith({
      html: () => view.render('pages/example'),
      json: () => response.json({
        example: true,
      }),
    })
  }
}

Or with short-hand using object literal concise method syntax:

export default class ExampleController {
  async show({ request, response, view }: HttpContext) {
    request.respondWith({
      html() { view.render('pages/example') },
      json() {
        response.json({
          example: true,
        })
      },
    })
  }
}

This package gives:

  • a cleaner API for handling the Accept header content-negotiation
  • automatically responds with a 406 Unacceptable error by default
  • allows for automatically responding with a default response type (the default is 'error', which gives the behavior above).

The alternative

If you didn't use this package, you'd need to write code like the following:

export default class ExampleController {
  async show({ request, response, view }: HttpContext) {
    switch (request.accepts(['json', 'html'])) {
      case 'json':
        return response.json({
          example: true,
        })
      case 'html':
        return view.render('pages/example')
      default:
        // decide yourself
    }
  }
}

About

Adonisjs package for improved handling of responses for multiple content-types

Topics

Resources

License

Stars

Watchers

Forks