-
Notifications
You must be signed in to change notification settings - Fork 8
/
queryString.ts
128 lines (118 loc) · 4.83 KB
/
queryString.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* @module utils/queryString
* @category Utility Functions
*/
import { NHTSA_RESPONSE_FORMAT } from '@/constants'
import { validateArgument } from '@/utils'
/** Valid URI component types */
export type QueryStringTypes = string | number | boolean
/** Object to build the query string with */
export type QueryStringParams = Record<string, QueryStringTypes>
/** Object returned by encodeQueryStringParams() */
export type QueryStringParamsEncoded<T> = { [key in keyof T]: string }
/**
* This function is used internally by other package functions. As a consumer of this package, you
* should not need to use this function directly in most cases.
*
* Utility function to perform URI component encoding on all values in an object, for use in URL
* query strings.
*
* - Returns an object of valid URI encoded parameters with same keys as the original object.
* - Will silently filter out parameters with values that are not type `string`, `number`, or
* `boolean`.
* - It filters invalid key/values so that encodeURIComponent() does not throw an error.
*
* In it's current implementation, this function assumes that invalid types have already been
* filtered out, and that all values are valid. If you need to be sure that all keys are present
* in the returned object, you can use the `validateArgument()` function to check the types of all
* values are valid before calling this function.
*
* This function is not exported by the package, but is used internally by other
* functions. However, it _is_ exported by the package as part of the composable function
* `useQueryString`, and renamed to `encodeParams` for less verbose use.
*
* @param {QueryStringParams} params - An object of search parameters to be encoded.
* @returns {QueryStringParamsEncoded} - A new object of same keys as the original object with
* values converted to URI component strings. Any keys with values not a string, number, or
* boolean are filtered out of final object.
*/
export const encodeQueryStringParams = <T extends QueryStringParams>(
params: T
): QueryStringParamsEncoded<T> => {
/* Validate user provided params is an object */
validateArgument({
name: 'params',
value: params,
required: true,
types: ['object'],
})
const _params = Object.entries(params)
.filter(([key, value]) =>
validateArgument({
name: key,
types: ['string', 'number', 'boolean'],
value,
errorMode: 'boolean',
})
)
.reduce((acc, [key, value]) => {
/* can expect only strings, numbers, and booleans after filtering */
acc[key as keyof T] = encodeURIComponent(value)
return acc
}, {} as QueryStringParamsEncoded<T>)
return _params
}
/**
* This function is used internally by other package functions. As a consumer of this package, you
* should not need to use this function directly in most cases.
*
* Utility function to generate a query string conforming to URI component standards. Takes an an
* optional object of search parameters and returns an encoded query string.
*
* This function will always override `params.format` with `{ format: 'json' }`. This is hardcoded
* into the package and cannot be overridden, this package provides no support for CSV or XML
* formats at this time. This means the default query string will be `"?format=json"` even if no
* `params` are provided by user.
*
* - Ignores parameters that are not strings, numbers, or booleans, and also ignores empty strings
* by default.
*
* - If you don't provide an object as the first argument, an error will be thrown. Providing an
* empty object will not throw an error.
*
* - If the second argument, `allowEmptyParams`, is set to `true`, the function will include keys
* with empty string values in the final query string, e.g. 'emptyKey='.
*
* @param {QueryStringParams} params - An object of search parameters to be converted to a query
* string.
* @param {boolean} [allowEmptyParams=false] - Set to `true` to include keys with empty string
* values, e.g. 'emptyKey='.
* @returns {string} - A query string of search parameters for use in a final fetch URL.
*/
export const createQueryString = <T extends QueryStringParams>(
params = {} as T,
allowEmptyParams = false
): string => {
/* Validate user provided params is an object */
validateArgument({
name: 'params',
value: params,
types: ['object'],
})
/* Merge with valid user params but override with static params */
const _params = encodeQueryStringParams({
...params,
format: NHTSA_RESPONSE_FORMAT,
})
/* Create query string from params, default: ?format=json */
return (
'?' +
Object.entries(_params)
.map(([key, value], index, array) => {
return value.length || (allowEmptyParams && value === '')
? `${key}=${value}${index < array.length - 1 ? '&' : ''}`
: ''
})
.join('')
)
}