-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrepository.js
158 lines (145 loc) · 3.74 KB
/
repository.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const RepositoryInterface = {
URI: 'uri',
CREATE: 'create',
LIST: 'list',
DETAIL: 'detail',
UPDATE: 'update',
DELETE: 'delete',
}
/**
* 作为 service 层 API 的基类
* 基于 RESTful 定义对统一资源的 CRUD 接口
* @export
* @class Repository
*/
export class Repository {
/**
*Creates an instance of Repository.
* @param {*} resource
* @memberof Repository
*/
constructor(resource) {
this.resource = resource
this.$axios = null
this.methods = null
}
/**
* @public
* @override
* @param {@nuxtjs/axios} $axios
* @returns {String} API Uri
* @memberof Repository
*/
[RepositoryInterface.URI]() {
return this.resource
}
/**
*Create a resource
* @public
* @override
* @param {@nuxtjs/axios} $axios
* @returns function
* @memberof Repository
*/
[RepositoryInterface.CREATE]($axios) {
return (payload, ...args) => $axios.$post(this.uri(), payload, ...args)
}
/**
*Get a resource by id
* @public
* @override
* @param {@nuxtjs/axios} $axios
* @returns function
* @memberof Repository
*/
[RepositoryInterface.DETAIL]($axios) {
return (id, ...args) => $axios.$get(`${this.uri()}/${id}`, ...args)
}
/**
*Get the list of kind of resource
* @public
* @override
* @param {@nuxtjs/axios} $axios
* @returns function
* @memberof Repository
*/
[RepositoryInterface.LIST]($axios) {
return (...args) => $axios.$get(this.uri(), ...args)
}
/**
*Update a resource
* @public
* @override
* @param {@nuxtjs/axios} $axios
* @returns function
* @memberof Repository
*/
[RepositoryInterface.UPDATE]($axios) {
return (id, payload, ...args) =>
$axios.$put(`${this.uri()}/${id}`, payload, ...args)
}
/**
*Delete a resource by id
* @public
* @override
* @param {@nuxtjs/axios} $axios
* @returns {Function}
* @memberof Repository
*/
[RepositoryInterface.DELETE]($axios) {
return (id, ...args) => $axios.$delete(`${this.uri()}/${id}`, ...args)
}
/**
*Init such a interface for developer
* 不建议外部覆盖此方法,为了保证调用者,可以使用统一的 API 调用方法。
* @private
* @param {@nuxtjs/axios} $axios
* @returns {Object}
* @memberof Repository
*/
init($axios) {
// 确保多次 init 且传入相同的 axios 实例时返回相同的引用
if (!this.methods && this.$axios !== $axios) {
// bind this 是为了继承 Repository 的属性方法,方便扩展
this.methods = {
[RepositoryInterface.URI]: this[RepositoryInterface.URI].bind(this),
[RepositoryInterface.CREATE]: this[RepositoryInterface.CREATE](
$axios,
).bind(this),
[RepositoryInterface.DETAIL]: this[RepositoryInterface.DETAIL](
$axios,
).bind(this),
[RepositoryInterface.LIST]: this[RepositoryInterface.LIST]($axios).bind(
this,
),
[RepositoryInterface.DELETE]: this[RepositoryInterface.DELETE](
$axios,
).bind(this),
[RepositoryInterface.UPDATE]: this[RepositoryInterface.UPDATE](
$axios,
).bind(this),
}
}
return this.methods
}
}
/**
*可以通过继承 Repository 来实现不满足 RESTful 最佳实践的接口
*这是一个例子, 接口的 query 上必须带 appId,接口的设计包含多个主从 id 等
* @export
* @class ExampleRepository
* @extends {Repository}
*/
export class ExampleRepository extends Repository {
constructor(resource, id) {
super(resource)
this.id = id
}
uri(appId) {
return `${this.resource}/status/${this.id}?appId=${appId}`
}
update($axios) {
return (appId, payload, ...args) =>
$axios.$post(`${this.uri(appId)}`, payload, ...args)
}
}