2.0.0 • Published 1 year ago
@thisismissem/adonisjs-respond-with v2.0.0
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
Acceptheader content-negotiation - automatically responds with a
406 Unacceptableerror 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
}
}
}2.0.0-next.1
1 year ago
2.0.0
1 year ago
2.0.0-next.0
1 year ago
1.0.1
1 year ago
1.0.0
1 year ago
0.0.1
1 year ago