| 38 | } |
| 39 | |
| 40 | export const getQueryString = (params: Record<string, unknown>): string => { |
| 41 | const qs: string[] = [] |
| 42 | |
| 43 | const append = (key: string, value: unknown) => { |
| 44 | qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`) |
| 45 | } |
| 46 | |
| 47 | const encodePair = (key: string, value: unknown) => { |
| 48 | if (value === undefined || value === null) { |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | if (value instanceof Date) { |
| 53 | append(key, value.toISOString()) |
| 54 | } else if (Array.isArray(value)) { |
| 55 | value.forEach((v) => encodePair(key, v)) |
| 56 | } else if (typeof value === 'object') { |
| 57 | Object.entries(value).forEach(([k, v]) => encodePair(`${key}[${k}]`, v)) |
| 58 | } else { |
| 59 | append(key, value) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | Object.entries(params).forEach(([key, value]) => encodePair(key, value)) |
| 64 | |
| 65 | return qs.length ? `?${qs.join('&')}` : '' |
| 66 | } |
| 67 | |
| 68 | const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => { |
| 69 | const encoder = config.ENCODE_PATH || encodeURI |