Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Allow passing options to qs.stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewtaylor authored and fabien0102 committed Mar 9, 2020
1 parent eed1135 commit 3a2700e
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/useGet.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Cancelable, DebounceSettings } from "lodash";
import debounce from "lodash/debounce";
import merge from "lodash/merge";
import qs from "qs";
import qs, { IStringifyOptions } from "qs";
import { useCallback, useContext, useEffect, useRef, useState } from "react";
import url from "url";

Expand All @@ -24,6 +24,10 @@ export interface UseGetProps<TData, TQueryParams> {
* Query parameters
*/
queryParams?: TQueryParams;
/**
* Query parameter stringify options
*/
queryParamStringifyOptions?: IStringifyOptions;
/**
* Don't send the error to the Provider
*/
Expand Down Expand Up @@ -56,13 +60,13 @@ export interface UseGetProps<TData, TQueryParams> {
| number;
}

export function resolvePath<TQueryParams>(base: string, path: string, queryParams: TQueryParams) {
export function resolvePath<TQueryParams>(base: string, path: string, queryParams: TQueryParams, queryParamOptions: IStringifyOptions) {
const appendedBase = base.endsWith("/") ? base : `${base}/`;
const trimmedPath = path.startsWith("/") ? path.slice(1) : path;

return url.resolve(
appendedBase,
Object.keys(queryParams).length ? `${trimmedPath}?${qs.stringify(queryParams)}` : trimmedPath,
Object.keys(queryParams).length ? `${trimmedPath}?${qs.stringify(queryParams, queryParamOptions)}` : trimmedPath,
);
}

Expand Down Expand Up @@ -93,7 +97,7 @@ async function _fetchData<TData, TError, TQueryParams>(
(typeof context.requestOptions === "function" ? await context.requestOptions() : context.requestOptions) || {};

const request = new Request(
resolvePath(base, path, { ...context.queryParams, ...queryParams }),
resolvePath(base, path, { ...context.queryParams, ...queryParams }, props.queryParamStringifyOptions || P{),
merge({}, contextRequestOptions, requestOptions, { signal }),
);

Expand Down Expand Up @@ -215,7 +219,7 @@ export function useGet<TData = any, TError = any, TQueryParams = { [key: string]
absolutePath: resolvePath(props.base || context.base, props.path, {
...context.queryParams,
...props.queryParams,
}),
}, props.queryParamStringifyOptions || {}),
cancel: () => {
setState({
...state,
Expand Down

0 comments on commit 3a2700e

Please sign in to comment.