plivo-node/lib/rest/utils.js
2019-03-22 17:00:05 +05:30

91 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import _camelCase from 'lodash/camelCase';
import _snakeCase from 'lodash/snakeCase';
import _mapKeys from 'lodash/mapKeys';
import _mapValues from 'lodash/mapValues';
import _map from 'lodash/map';
function recursivelyRenameObject(object, renameFunc) {
if (!(object instanceof Object)) {
return object;
}
return _mapValues(_mapKeys(object, renameFunc), function (value) {
if (Array.isArray(value)) return _map(value, function (value) {
return recursivelyRenameObject(value, renameFunc);
});
if (typeof value !== 'object') return value;
return recursivelyRenameObject(value, renameFunc);
});
}
export function camelCaseRequestWrapper(requestFunc) {
return (method, action, params) => {
params = recursivelyRenameObject(params, function (value, key) {
if (typeof key !== 'string') return key;
// Snake Case logic has issue, it replaces double underscores with single
// So dont run snake case logic for following params
let skipParamsFromSnakeCasing = [
'message_time__lt', 'message_time__lte',
'message_time__gt', 'message_time__gte',
]
if (skipParamsFromSnakeCasing.indexOf(key) >= 0) {
return key;
}
return _snakeCase(key)
.replace('_less_than', '__lt')
.replace('_greater_than', '__gt')
.replace('_greater_or_equal', '__gte')
.replace('_less_or_equal', '__lte')
.replace('_equal', '')
.replace('_equals', '');
});
return requestFunc(method, action, params).then(res => {
res.body = recursivelyRenameObject(res.body, function (value, key) {
if (typeof key !== 'string') return key;
return _camelCase(key);
});
return res;
});
}
}
export function validateVoiceForSsml(voice, language) {
var voiceParts = voice.split('.');
if (['MAN', 'WOMAN'].indexOf(voice) != -1) {
return { success: true };
} else if (voiceParts.length != 2 || voiceParts[0] != 'Polly') {
return {
success: false, msg: "Invalid voice " + voice + '.'
};
}
// If voice is polly, validate accent based on language specified
// List of valid Polly voices
var languageWiseVoices = {
'Australian English': ['Nicole', 'Russell'],
'Brazilian Portuguese': ['Vitória', 'Ricardo']
};
// Validate supported languages.
if (!languageWiseVoices[language]) {
return {
success: false, msg: "Invalid language."
};
}
// Validate voice for given language.
if (languageWiseVoices[language].indexOf(voiceParts[1]) == -1) {
return {
success: false, msg: "XML Validation Error: <Speak> voice " + voice + " is not valid. Refer <link> for list of supported voices."
};
}
}