Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kevin1193/adonis-kvn-response
https://github.com/kevin1193/adonis-kvn-response
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/kevin1193/adonis-kvn-response
- Owner: kevin1193
- Created: 2021-12-08T09:50:14.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-27T15:37:54.000Z (over 2 years ago)
- Last Synced: 2024-06-28T14:43:13.090Z (6 months ago)
- Language: TypeScript
- Size: 203 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-adonisjs - AdonisJS Kvn Response - Common REST API response helper to REST API adonisjs project (Packages)
README
# Adonis v5 Kvn Response
This addon adds the functionality to integrate common REST API responses to Response class of HttpContext.
## Installation
Make sure to install it using `npm` or `yarn`.
```bash
# npm
npm i adonis-kvn-response
node ace invoke adonis-kvn-response# yarn
yarn add adonis-kvn-response
node ace invoke adonis-kvn-response
```## Usage
Make sure to register the provider inside `.adonisrc.json` file.
```json
"providers": [
"...other packages",
"adonis-kvn-response"
]
```For TypeScript projects add to `tsconfig.json` file:
```json
"compilerOptions": {
"types": [
"...other packages",
"adonis-kvn-response/build/adonis-typings"
]
}
```## Example
#### response.ok()
```
public async someFunction({ response, auth }: HttpContextContract) {
return response.ok('Your messaged here.')
}
```
#### response.data()
```
public async someFunction({ response, auth }: HttpContextContract) {
return response.data({
email: email,
name: name
}, 'Your messaged here is optional')
}
```
#### response.resource()
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))
}
```
#### response.accepted()
Note: For creating record status code = 201
```
public async store({ response, auth, transform }: HttpContextContract) {
return response.accepted({
id: id,
name: name
})
}
```
#### response.error()#### response.unableToProcess()
#### response.notFound()
#### response.forbidden()
#### response.unauthorized()
#### response.badRequest()