@shaggytools/nhtsa-api-wrapper - v3.0.4 / Exports / utils/queryString
Module: utils/queryString
Table of contents
Type Aliases
Functions
Type Aliases
QueryStringParams
Ƭ QueryStringParams: Record
<string
, QueryStringTypes
>
Object to build the query string with
Defined in
QueryStringParamsEncoded
Ƭ QueryStringParamsEncoded<T
>:
Object returned by encodeQueryStringParams()
Type parameters
Name |
---|
T |
Defined in
QueryStringTypes
Ƭ QueryStringTypes: string
| number
| boolean
Valid URI component types
Defined in
Functions
createQueryString
▸ createQueryString<T
>(params?
, allowEmptyParams?
): 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 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 totrue
, the function will include keys with empty string values in the final query string, e.g. 'emptyKey='.
Type parameters
Name | Type |
---|---|
T | extends QueryStringParams |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
params | T | undefined | An object of search parameters to be converted to a query string. |
allowEmptyParams? | boolean | 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.
Defined in
encodeQueryStringParams
▸ encodeQueryStringParams<T
>(params
): QueryStringParamsEncoded
<T
>
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
, orboolean
. - 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.
Type parameters
Name | Type |
---|---|
T | extends QueryStringParams |
Parameters
Name | Type | Description |
---|---|---|
params | T | An object of search parameters to be encoded. |
Returns
- 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.