-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathutils.js
50 lines (44 loc) · 1.4 KB
/
utils.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
/**
* Utility functions for ari-client.
*
* @module utils
*
* @copyright 2014, Digium, Inc.
* @license Apache License, Version 2.0
* @author Samuel Fortier-Galarneau <[email protected]>
*/
'use strict';
var _ = require('lodash');
/**
* Modifies options to swagger as body params as appropriate using the given
* defined operation parameters.
*
* @memberof module:utils
* @method parseBodyParams
* @param {Object[]} params - defined operation parameters
* @param {Object} swaggerOptions - options that will be sent to a swagger
* operation
* @returns {Object} modified options
*/
function parseBodyParams(params, swaggerOptions) {
var options = _.clone(swaggerOptions);
var bodyParams = params.filter(function(param) {
return param.paramType === 'body';
});
bodyParams.forEach(function(bodyParam) {
var jsonBody = options[bodyParam.name];
if (jsonBody) {
// variables behaves differently in that it expects a variables key to
// wrap the key/value pairs
if (bodyParam.name === 'variables' && !options.variables.variables) {
jsonBody = {variables: jsonBody};
} else if (bodyParam.name === 'fields' && !options.fields.fields) {
jsonBody = {fields: jsonBody};
}
options.body = JSON.stringify(jsonBody);
delete options[bodyParam.name];
}
});
return options;
}
module.exports.parseBodyParams = parseBodyParams;