pointless-fetch
Version:
Functional point-free utilities for fetch
39 lines (35 loc) • 1.01 kB
text/typescript
/**
* Encodes an object into the plain URL as url-query-string
*
* ```js
* query('/list', {
* amount: 5,
* filters: ['price', 'date']
* })
* ```
*
* returns
* ```js
* '/list?amount=5&filters=price,date'
* ```
*
* @param url a url to encode params into
* @param queryParams query params in object form
* @returns url with encoded params
*/
export function query(url: string, queryParams: object) {
const query = Object.keys(queryParams)
.filter(k => k && queryParams[k] !== undefined)
.map((k): [string, string] => {
if (Array.isArray(queryParams[k])) {
return [queryParams[k].join(','), k];
} else if (typeof queryParams[k] === 'object') {
return [JSON.stringify(queryParams[k]), k];
}
return [queryParams[k], k];
})
.map(([value, key]) => `${encodeURIComponent(key)}=${encodeURI(value)}`)
.join('&');
const prefix = (String(url).indexOf('?') > -1 ? '&' : '?');
return url + (query.length > 0 ? prefix + query : '');
}