Stats

 Latest version : 1.1.0

 Last updated on 2018-06-29T08:13:20.936Z

 Keywords : AdonisJs, middleware, restful, resource

 Downloads :

  • 23 in Last Month

 Links :

 Examples

¯\_(ツ)_/¯
No examples found for this package

 Readme

adonis-resource-middleware

Restful resource middleware for AdonisJs

Usage

  1. Install bash npm i -S adonis-resource-middleware

  2. Edit /start/kernel.js

    const namedMiddleware = {
      /* ... */
      /* add a named middleware */
      resource: 'adonis-resource-middleware',
    }
    
  3. 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.

  1. E_RESOURCE_MODEL_CLASS_NOT_FOUND If the model class does not exists in App/Models/.

  2. E_RESOURCE_MODEL_NOT_FOUND If the record does not exists in the database.

 Comments