1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | class Plugin {
|
7 |
|
8 | modem: any
|
9 | id: any
|
10 |
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 | constructor (modem, id?) {
|
17 | this.modem = modem
|
18 | this.id = id
|
19 | }
|
20 |
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | list (opts) {
|
28 | const call = {
|
29 | path: '/plugins?',
|
30 | method: 'GET',
|
31 | options: opts,
|
32 | statusCodes: {
|
33 | 200: true,
|
34 | 500: 'server error'
|
35 | }
|
36 | }
|
37 |
|
38 | return new Promise((resolve, reject) => {
|
39 | this.modem.dial(call, (err, plugins) => {
|
40 | if (err) return reject(err)
|
41 | if (!plugins || !plugins.length) return resolve([])
|
42 | resolve(plugins.map((conf) => {
|
43 | const plugin = new Plugin(this.modem, conf.Id)
|
44 | return Object.assign(plugin, conf)
|
45 | }))
|
46 | })
|
47 | })
|
48 | }
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | upgrade (opts) {
|
57 | let id
|
58 | [ opts, id ] = this.__processArguments(opts)
|
59 |
|
60 | const call = {
|
61 | path: `/plugins/${id}/upgrade?`,
|
62 | method: 'POST',
|
63 | options: opts,
|
64 | statusCodes: {
|
65 | 204: true,
|
66 | 404: 'plugin not installed',
|
67 | 500: 'server error'
|
68 | }
|
69 | }
|
70 |
|
71 | return new Promise((resolve, reject) => {
|
72 | this.modem.dial(call, (err, conf) => {
|
73 | if (err) return reject(err)
|
74 | const plugin = new Plugin(this.modem, opts.name)
|
75 | resolve(plugin)
|
76 | })
|
77 | })
|
78 | }
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | create (opts) {
|
87 | const call = {
|
88 | path: '/plugins/create?',
|
89 | method: 'POST',
|
90 | options: opts,
|
91 | statusCodes: {
|
92 | 204: true,
|
93 | 500: 'server error'
|
94 | }
|
95 | }
|
96 |
|
97 | return new Promise((resolve, reject) => {
|
98 | this.modem.dial(call, (err, conf) => {
|
99 | if (err) return reject(err)
|
100 | const plugin = new Plugin(this.modem, opts.name)
|
101 | resolve(plugin)
|
102 | })
|
103 | })
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | install (opts) {
|
113 | const call = {
|
114 | path: '/plugins/pull?',
|
115 | method: 'POST',
|
116 | options: opts,
|
117 | statusCodes: {
|
118 | 200: true,
|
119 | 500: 'server error'
|
120 | }
|
121 | }
|
122 |
|
123 | return new Promise((resolve, reject) => {
|
124 | this.modem.dial(call, (err, conf) => {
|
125 | if (err) return reject(err)
|
126 | const plugin = new Plugin(this.modem, opts.name)
|
127 | resolve(plugin)
|
128 | })
|
129 | })
|
130 | }
|
131 |
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | status (opts, id) {
|
141 | [ opts, id ] = this.__processArguments(opts, id)
|
142 |
|
143 | const call = {
|
144 | path: `/plugins/${id}?`,
|
145 | method: 'GET',
|
146 | options: opts,
|
147 | statusCodes: {
|
148 | 200: true,
|
149 | 404: 'no such plugin',
|
150 | 500: 'server error'
|
151 | }
|
152 | }
|
153 |
|
154 | return new Promise((resolve, reject) => {
|
155 | this.modem.dial(call, (err, conf) => {
|
156 | if (err) return reject(err)
|
157 | const plugin = new Plugin(this.modem, id)
|
158 | resolve(Object.assign(plugin, conf))
|
159 | })
|
160 | })
|
161 | }
|
162 |
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | remove (opts, id) {
|
171 | [ opts, id ] = this.__processArguments(opts, id)
|
172 | const call = {
|
173 | path: `/plugins/${id}?`,
|
174 | method: 'DELETE',
|
175 | options: opts,
|
176 | statusCodes: {
|
177 | 200: true,
|
178 | 404: 'no such plugin',
|
179 | 500: 'server error'
|
180 | }
|
181 | }
|
182 |
|
183 | return new Promise((resolve, reject) => {
|
184 | this.modem.dial(call, (err, res) => {
|
185 | if (err) return reject(err)
|
186 | resolve(res)
|
187 | })
|
188 | })
|
189 | }
|
190 |
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | push (opts, id) {
|
199 | [ opts, id ] = this.__processArguments(opts, id)
|
200 |
|
201 | const call = {
|
202 | path: `/plugins/${id}/push?`,
|
203 | method: 'POST',
|
204 | options: opts,
|
205 | statusCodes: {
|
206 | 200: true,
|
207 | 404: 'plugin not found',
|
208 | 500: 'server error'
|
209 | }
|
210 | }
|
211 |
|
212 | return new Promise((resolve, reject) => {
|
213 | this.modem.dial(call, (err, conf) => {
|
214 | if (err) return reject(err)
|
215 | const plugin = new Plugin(this.modem, id)
|
216 | resolve(plugin)
|
217 | })
|
218 | })
|
219 | }
|
220 |
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | set (opts, id) {
|
229 | [ opts, id ] = this.__processArguments(opts, id)
|
230 |
|
231 | const call = {
|
232 | path: `/plugins/${id}/set?`,
|
233 | method: 'POST',
|
234 | options: opts,
|
235 | statusCodes: {
|
236 | 204: true,
|
237 | 404: 'plugin not found',
|
238 | 500: 'server error'
|
239 | }
|
240 | }
|
241 |
|
242 | return new Promise((resolve, reject) => {
|
243 | this.modem.dial(call, (err, conf) => {
|
244 | if (err) return reject(err)
|
245 | const plugin = new Plugin(this.modem, id)
|
246 | resolve(plugin)
|
247 | })
|
248 | })
|
249 | }
|
250 |
|
251 | |
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | enable (opts, id) {
|
259 | [ opts, id ] = this.__processArguments(opts, id)
|
260 |
|
261 | const call = {
|
262 | path: `/plugins/${id}/enable?`,
|
263 | method: 'POST',
|
264 | options: opts,
|
265 | statusCodes: {
|
266 | 200: true,
|
267 | 500: 'server error'
|
268 | }
|
269 | }
|
270 |
|
271 | return new Promise((resolve, reject) => {
|
272 | this.modem.dial(call, (err, conf) => {
|
273 | if (err) return reject(err)
|
274 | const plugin = new Plugin(this.modem, id)
|
275 | resolve(plugin)
|
276 | })
|
277 | })
|
278 | }
|
279 |
|
280 | |
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | disable (opts, id) {
|
288 | [ opts, id ] = this.__processArguments(opts, id)
|
289 |
|
290 | const call = {
|
291 | path: `/plugins/${id}/disable?`,
|
292 | method: 'POST',
|
293 | options: opts,
|
294 | statusCodes: {
|
295 | 200: true,
|
296 | 500: 'server error'
|
297 | }
|
298 | }
|
299 |
|
300 | return new Promise((resolve, reject) => {
|
301 | this.modem.dial(call, (err, conf) => {
|
302 | if (err) return reject(err)
|
303 | const plugin = new Plugin(this.modem, id)
|
304 | resolve(plugin)
|
305 | })
|
306 | })
|
307 | }
|
308 |
|
309 | __processArguments (opts, id?) {
|
310 | if (typeof opts === 'string' && !id) {
|
311 | id = opts
|
312 | }
|
313 | if (!id && this.id) {
|
314 | id = this.id
|
315 | }
|
316 | if (!opts) opts = {}
|
317 | return [ opts, id ]
|
318 | }
|
319 | }
|
320 |
|
321 | export default Plugin
|