| 3 | const debug = Debug('request'); |
| 4 | |
| 5 | export function serialize(obj?: object): string { |
| 6 | if (!obj) { |
| 7 | return ''; |
| 8 | } |
| 9 | const rst: string[] = []; |
| 10 | Object.entries(obj || {}).forEach(([key, val]): void => { |
| 11 | if (val === null || val === undefined || val === '') return; |
| 12 | if (typeof val === 'object') rst.push(`${key}=${encodeURIComponent(JSON.stringify(val))}`); |
| 13 | else rst.push(`${key}=${encodeURIComponent(val)}`); |
| 14 | }); |
| 15 | return rst.join('&'); |
| 16 | } |
| 17 | |
| 18 | export function buildUrl(dataAPI: string, params?: object): string { |
| 19 | const paramStr = serialize(params); |