(
param: { name?: string; value?: StrictNullSearchParamsValueType | number },
/** allow empty names and values */
strict?: boolean,
/** extra options like strict handle null value */
options?: IQueryStringOptions,
)
| 55 | * Build a querystring parameter from a param object |
| 56 | */ |
| 57 | export const buildQueryParameter = ( |
| 58 | param: { name?: string; value?: StrictNullSearchParamsValueType | number }, |
| 59 | |
| 60 | /** allow empty names and values */ |
| 61 | strict?: boolean, |
| 62 | /** extra options like strict handle null value */ |
| 63 | options?: IQueryStringOptions, |
| 64 | ) => { |
| 65 | strict = strict === undefined ? true : strict; |
| 66 | const { strictNullHandling = false, encodeParams = true } = options || {}; |
| 67 | |
| 68 | // Skip non-name ones in strict mode |
| 69 | if (strict && !param.name) { |
| 70 | return ''; |
| 71 | } |
| 72 | |
| 73 | // Cast number values to strings |
| 74 | if (typeof param.value === 'number') { |
| 75 | param.value = String(param.value); |
| 76 | } |
| 77 | |
| 78 | // Keep equal sign if strictNullHandling and param value is empty string, see https://github.com/Kong/insomnia/issues/2111 |
| 79 | if (!strict || param.value || (strictNullHandling && param.value === '')) { |
| 80 | if (!encodeParams) { |
| 81 | return `${param.name}=${param.value}`; |
| 82 | } |
| 83 | // Don't encode ',' in values |
| 84 | const value = flexibleEncodeComponent(param.value || '').replace(/%2C/gi, ','); |
| 85 | const name = flexibleEncodeComponent(param.name || ''); |
| 86 | |
| 87 | return `${name}=${value}`; |
| 88 | } |
| 89 | return flexibleEncodeComponent(param.name); |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * Build a querystring from a list of name/value pairs |
no test coverage detected