tipi-services
Version:
Node.js library to access wrapping REST API of tipi backend services
25 lines (22 loc) • 879 B
JavaScript
const _ = require('lodash')
const { Oops } = require('@gokiteam/oops')
const privates = {
convertObjectToQueryString (query, path = '') {
if(!_.isObject(query)) throw Oops.invalidArgument('query should be an object')
let queryParams = []
_.each(query, (value, key) => {
let currentPath = !path ? key : `${path}[${key}]`
if (_.isArray(value)) {
queryParams.push(`${currentPath}=${value.map(item => encodeURIComponent(item)).join(',')}`)
} else if (_.isObject(value)) {
queryParams = [...queryParams, ...privates.convertObjectToQueryString(value, currentPath)]
} else if (!_.isNil(value)) {
queryParams.push(`${currentPath}=${encodeURIComponent(value)}`)
}
})
return queryParams
}
}
module.exports = (query) => {
return privates.convertObjectToQueryString(query).join('&')
}