api-service.js 701 B

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