项目作者: wxs77577

项目描述 :
Restful api for AdonisJs
高级语言: JavaScript
项目地址: git://github.com/wxs77577/adonis-rest.git
创建时间: 2017-06-12T09:10:37Z
项目社区:https://github.com/wxs77577/adonis-rest

开源协议:

下载


AdonisJs Restful API

Not ready for production
Currently support AdonisJs v4+MongoDB only.
Also check REST-ADMIN - An awesome admin dashboard based on vue 2 and bootstrap v4

Setup

also you can use npm by ommit --yarn

  1. Install required packages

    1. adonis install @adonisjs/validator --yarn
    2. adonis install @adonisjs/antl --yarn
    3. adonis install @adonisjs/drive --yarn
    4. adonis install lucid-mongo --yarn
    5. # install adonis-rest
    6. adonis install adonis-rest --yarn
  2. Edit /start/app.js

    1. const providers = [
    2. '@adonisjs/validator/providers/ValidatorProvider',
    3. '@adonisjs/antl/providers/AntlProvider',
    4. '@adonisjs/drive/providers/DriveProvider',
    5. 'lucid-mongo/providers/LucidMongoProvider',
    6. 'adonis-rest/providers/RestProvider',
    7. ]
  3. Edit /start/routes.js

    1. `Route.rest('/rest/api', 'api')`
  4. Open http://localhost:3333/rest/api/users (or another port) should return paginated user list.

Documentation

Config

The config file of adonis-rest is /config/rest.js,you can define any number of modules of adonis-rest routes. e.g. For frontend api and backend api, we call them api and admin

  1. module.exports = {
  2. //route module name
  3. api: {
  4. //authenticator name
  5. auth: 'jwt',
  6. // which means there are only `index` and `show` routes
  7. isAdmin: false,
  8. //all of your resources config
  9. resources: {
  10. // for `/products`
  11. products: {
  12. // must access with a valid token
  13. auth: true,
  14. // all of your default query config for `/products`
  15. query: {
  16. // when list all products
  17. index: {
  18. // fetch appends, please refer to **Appends**
  19. append: ['is_buy'],
  20. // fetch related data
  21. with: ['categories'],
  22. // also you can define default sorting
  23. sort: { _id: -1 },
  24. },
  25. // when show a product
  26. show: {
  27. append: ['is_buy'],
  28. }
  29. }
  30. },
  31. }
  32. },
  33. admin: {
  34. //maybe `adminJwt`
  35. auth: 'jwt',
  36. //allow C(create)/U(update)D/(delete) routes
  37. isAdmin: true,
  38. //allow destroy all routes
  39. allowDestroyAll: true,
  40. resources: {
  41. // ...
  42. }
  43. }
  44. }

And then, you can add routes easily:
/start/routes.js

  1. /**
  2. * @param String base url
  3. * @param string key of route module in `/config/rest.js`
  4. **/
  5. Route.rest('/rest/api', 'api')
  6. Route.rest('/rest/admin', 'admin')

Now, You can check the followed links: (if your port of server is 3333)

Base Model

There is a more powerful base model Rest/Models/Model

You can define a Product model like this:

  1. const Model = use('Rest/Models/Model')
  2. module.exports = class Product extends Model {
  3. }