api-service.js 1000 B

123456789101112131415161718192021222324252627282930
  1. const app = require('./app')
  2. const appApiService = require('./app-api-service')
  3. app.service('api', function($http) {
  4. let opts = {
  5. headers: {},
  6. withCredentials: true
  7. }
  8. const api = (req) => req.then(res => {
  9. // Enter post-processing here
  10. return res.data
  11. })
  12. this.login = data => api($http.post('/api/auth/login', data, opts))
  13. this.setToken = token => {
  14. this.token = token
  15. opts.headers.authentication = `Bearer ${token}`
  16. }
  17. this.crud = (apiPrefix) => ({
  18. list: () => api($http.get(`${apiPrefix}`, opts)),
  19. create: data => api($http.post(`${apiPrefix}`, data, opts)),
  20. read: id => api($http.get(`${apiPrefix}/${id}`, opts)),
  21. update: (id, data) => api($http.patch(`${apiPrefix}/${id}`, data, opts)),
  22. delete: (id) => api($http.delete(`${apiPrefix}/${id}`, opts)),
  23. trash: () => api($http.get(`${apiPrefix}/trash`, opts)),
  24. undelete: (id) => api($http.delete(`${apiPrefix}/trash/${id}`, opts))
  25. })
  26. appApiService.apply(this, [api, $http])
  27. })