adonis-resource-middleware
v1.1.0
Published
Restful resource middleware for AdonisJs
Downloads
67
Maintainers
Readme
adonis-resource-middleware
Restful resource middleware for AdonisJs
Usage
Install
npm i -S adonis-resource-middleware
Edit
/start/kernel.js
const namedMiddleware = { /* ... */ /* add a named middleware */ resource: 'adonis-resource-middleware', }
In your
/start/routes.js
:Route.get('/api/:resource', async ({ Model }) => { /** * `Model` is the Model class of the requested resource * It convert the `:resource` in url parameters to single and camelcase class name * so `/users` route means `use('App/Models/User')` */ return await Model.paginate(1, 10) }).middleware(['resource']) /* Specify the resource name */ Route.get('/api/users').middleware(['resource:users']) /* The 2nd param specify if throw exceptions automatically */ Route.get('/api/users').middleware(['resource:users,false']) /* Also you can ignore the 1st param */ Route.get('/api/:resource').middleware(['resource:,false']) Route.get('/api/:resource/:id', async ({ model }) => { /** * `model` is the model instace of the requested resource */ return model }).middleware(['resource']) /* also you can use `Route.resource` */ Route.resource('/api/:resource', 'ResourceController').middleware(['resource'])
Handle Exceptions
It throws exceptions by default, also you can handle it, please check The Official Docs of Handling Exceptions.
E_RESOURCE_MODEL_CLASS_NOT_FOUND
If the model class does not exists inApp/Models/
.E_RESOURCE_MODEL_NOT_FOUND
If the record does not exists in the database.