This addon adds the functionality to integrate common REST API responses to Response class of HttpContext.
Make sure to install it using npm
or yarn
.
# npm
npm i adonis-kvn-response
node ace invoke adonis-kvn-response
# yarn
yarn add adonis-kvn-response
node ace invoke adonis-kvn-response
Make sure to register the provider inside .adonisrc.json
file.
"providers": [
"...other packages",
"adonis-kvn-response"
]
For TypeScript projects add to tsconfig.json
file:
"compilerOptions": {
"types": [
"...other packages",
"adonis-kvn-response/build/adonis-typings"
]
}
public async someFunction({ response, auth }: HttpContextContract) {
return response.ok('Your messaged here.')
}
public async someFunction({ response, auth }: HttpContextContract) {
return response.data({
email: email,
name: name
}, 'Your messaged here is optional')
}
Note: Need to install adonis-bumblebee-ts to be able to use this function for transforming data
public async listItems({ response, auth, transform }: HttpContextContract) {
return response.resource(await transform.collection(items, ItemTransformer))
}
public async showItem({ response, auth, transform }: HttpContextContract) {
return response.resource(await transform.item(item, ItemTransformer))
}
Note: For creating record status code = 201
public async store({ response, auth, transform }: HttpContextContract) {
return response.accepted({
id: id,
name: name
})
}