-
Notifications
You must be signed in to change notification settings - Fork 210
/
base.service.js
105 lines (92 loc) · 3.13 KB
/
base.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import Http from './http.init'
import { ResponseWrapper, ErrorWrapper } from './util'
export default class BaseService {
constructor () {
if (!this.entity) {
throw new Error('Child service class not provide entity')
}
}
/**
* ------------------------------
* @HELPERS
* ------------------------------
*/
request (status = {auth: false}) {
return new Http(status)
}
responseWrapper (...rest) {
return new ResponseWrapper(...rest)
}
errorWrapper (...rest) {
return new ErrorWrapper(...rest)
}
/**
* ------------------------------
* @API_CALLS_PUBLIC
* ------------------------------
*/
getListPublic (parameters = {}) {
const params = {
...parameters
}
return new Promise((resolve, reject) => {
return this.request().get(`${this.entity}`, { params })
.then(response => {
let data = {
content: response.data.data.results,
total: response.data.data.total
}
resolve(this.responseWrapper(response, data))
}).catch(error => {
let message = error.response.data ? error.response.data.error : error.response.statusText
reject(this.errorWrapper(error, message))
})
})
}
getByIdPublic (id = window.required()) {
return new Promise((resolve, reject) => {
return this.request().get(`${this.entity}/${id}`)
.then(response => resolve(this.responseWrapper(response, response.data.data)))
.catch(error => {
let message = error.response.data ? error.response.data.error : error.response.statusText
reject(this.errorWrapper(error, message))
})
})
}
/**
* ------------------------------
* @API_CALLS_PRIVATE
* ------------------------------
*/
getById (id = window.required()) {
return new Promise((resolve, reject) => {
return this.request({auth: true}).get(`${this.entity}/${id}`)
.then(response => resolve(this.responseWrapper(response, response.data.data)))
.catch(error => {
let message = error.response.data ? error.response.data.error : error.response.statusText
reject(this.errorWrapper(error, message))
})
})
}
create (data = window.required()) {
return new Promise((resolve, reject) => {
return this.request({auth: true}).post(`${this.entity}`, data)
.then(response => resolve(this.responseWrapper(response, response.data.data)))
.catch(error => reject(this.errorWrapper(error)))
})
}
update (id = window.required(), data = window.required()) {
return new Promise((resolve, reject) => {
return this.request({auth: true}).patch(`${this.entity}/${id}`, data)
.then(response => resolve(this.responseWrapper(response, response.data.data)))
.catch(error => reject(this.errorWrapper(error)))
})
}
remove (id = window.required()) {
return new Promise((resolve, reject) => {
return this.request({auth: true}).delete(`${this.entity}/${id}`)
.then(response => resolve(this.responseWrapper(response, response.data.data)))
.catch(error => reject(this.errorWrapper(error)))
})
}
}