Support default parameters when updating or creating actions.
Fixes #25
diff --git a/README.md b/README.md
index dd68a5f..a8fc466 100644
--- a/README.md
+++ b/README.md
@@ -325,6 +325,7 @@
The following optional parameters are supported:
- `namespace` - set custom namespace for endpoint
+- `params` - object containing default parameters for the action (default: `{}`)
If you pass in an array for the first parameter, the `create` call will be executed for each array item. The function returns a Promise which resolves with the results when all operations have finished.
diff --git a/lib/actions.js b/lib/actions.js
index abd2456..0348b70 100644
--- a/lib/actions.js
+++ b/lib/actions.js
@@ -39,13 +39,19 @@
throw new Error(messages.MISSING_ACTION_BODY_ERROR)
}
+ const body = { exec: { kind: 'nodejs:default', code: options.action } }
+
if (options.action instanceof Buffer) {
- options.action = options.action.toString('base64')
+ body.exec.code = options.action.toString('base64')
} else if (typeof options.action === 'object') {
return options.action
}
- return { exec: { kind: 'nodejs:default', code: options.action } }
+ if (typeof options.params === 'object') {
+ body.parameters = Object.keys(options.params).map(key => ({ key, value: options.params[key] }))
+ }
+
+ return body
}
}
diff --git a/test/integration/actions.test.js b/test/integration/actions.test.js
index 1f6b4fe..c335cec 100644
--- a/test/integration/actions.test.js
+++ b/test/integration/actions.test.js
@@ -98,6 +98,31 @@
}).catch(errors)
})
+test('create, get and delete with parameters an action', t => {
+ const params = {api: API_URL, api_key: API_KEY, namespace: NAMESPACE}
+
+ const errors = err => {
+ console.log(err)
+ t.fail()
+ }
+
+ const actions = new Actions(new Client(params))
+ return actions.create({name: 'random_action_params_test', params: { hello: 'world' }, action: 'function main() {return {payload:"testing"}}'}).then(result => {
+ t.is(result.name, 'random_action_params_test')
+ t.is(result.namespace, NAMESPACE)
+ t.deepEqual(result.parameters, [{key: 'hello', value: 'world'}])
+ t.is(result.exec.kind, 'nodejs:6')
+ t.is(result.exec.code, 'function main() {return {payload:"testing"}}')
+ return actions.update({actionName: 'random_action_params_test', params: { foo: 'bar' }, action: 'update test'}).then(update_result => {
+ t.is(update_result.name, 'random_action_params_test')
+ t.is(update_result.namespace, NAMESPACE)
+ t.deepEqual(update_result.parameters, [{key: 'foo', value: 'bar'}])
+ t.pass()
+ return actions.delete({name: 'random_action_params_test'}).catch(errors)
+ }).catch(errors)
+ }).catch(errors)
+})
+
test('invoke action with fully-qualified name', t => {
const params = {api: API_URL, api_key: API_KEY, namespace: NAMESPACE}
diff --git a/test/unit/actions.test.js b/test/unit/actions.test.js
index b3d7961..8f3c274 100644
--- a/test/unit/actions.test.js
+++ b/test/unit/actions.test.js
@@ -284,6 +284,28 @@
return actions.create({name: '12345', action})
})
+test('create a new action with default parameters', t => {
+ t.plan(4)
+ const ns = '_'
+ const client = {}
+ const action = 'function main() { // main function body};'
+ const params = {
+ foo: 'bar'
+ }
+ const actions = new Actions(client)
+
+ client.request = (method, path, options) => {
+ t.is(method, 'PUT')
+ t.is(path, `namespaces/${ns}/actions/12345`)
+ t.deepEqual(options.qs, {})
+ t.deepEqual(options.body, {exec: {kind: 'nodejs:default', code: action}, parameters: [
+ { key: 'foo', value: 'bar' }
+ ]})
+ }
+
+ return actions.create({name: '12345', action, params})
+})
+
test('create an action without providing an action body', t => {
const actions = new Actions()
t.throws(() => actions.create({name: '12345'}), /Missing mandatory action/)