-
Notifications
You must be signed in to change notification settings - Fork 53
/
actions.js
132 lines (104 loc) · 3.7 KB
/
actions.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict'
const messages = require('./messages')
const Resources = require('./resources')
class Actions extends Resources {
constructor (client) {
super(client)
this.resource = 'actions'
this.identifiers.push('actionName')
this.qs_options.invoke = ['blocking']
}
list (options) {
options = options || {}
options.qs = this.qs(options, ['skip', 'limit', 'count'])
return super.list(options)
}
get (options) {
options = this.parseOptions(options)
options.qs = this.qs(options, ['code'])
return this.operationWithId('GET', options)
}
invoke (options) {
options = options || {}
if (options.blocking && options.result) {
return super.invoke(options).then(result => {
if (result.response) {
return result.response.result
} else return Promise.reject(result)
})
}
return super.invoke(options)
}
create (options) {
options.qs = this.qs(options, ['overwrite'])
options.body = this.actionBody(options)
return super.create(options)
}
actionBodyWithCode (options) {
const body = { exec: { kind: options.kind || 'nodejs:default', code: options.action } }
// allow options to override the derived exec object
if (options.exec) {
body.exec = Object.assign(body.exec, options.exec)
}
if (options.action instanceof Buffer) {
body.exec.code = options.action.toString('base64')
}
return body
}
actionBodyWithSequence (options) {
if (!(options.sequence instanceof Array)) {
throw new Error(messages.INVALID_SEQ_PARAMETER)
}
if (options.sequence.length === 0) {
throw new Error(messages.INVALID_SEQ_PARAMETER_LENGTH)
}
const body = { exec: { kind: 'sequence', components: options.sequence } }
return body
}
actionBody (options) {
const isUpdate = options && options.overwrite === true
const isCodeAction = options.hasOwnProperty('action')
const isSequenceAction = options.hasOwnProperty('sequence')
if (!isCodeAction && !isSequenceAction && !isUpdate) {
throw new Error(messages.MISSING_ACTION_OR_SEQ_BODY_ERROR)
}
if (isCodeAction && isSequenceAction) {
throw new Error(messages.INVALID_ACTION_AND_SEQ_PARAMETERS)
}
// user can manually define & control exact action definition by passing in an object
if (isCodeAction && typeof options.action === 'object' &&
(!(options.action instanceof Buffer))) {
return options.action
}
const body = isCodeAction
? this.actionBodyWithCode(options)
: isSequenceAction ? this.actionBodyWithSequence(options) : {}
if (typeof options.params === 'object') {
body.parameters = Object.keys(options.params).map(key => ({ key, value: options.params[key] }))
}
if (options.version) {
body.version = options.version
}
if (options.limits) {
body.limits = options.limits
}
return body
}
}
module.exports = Actions