Skip to content

Commit 6fd6210

Browse files
committed
fix: query string types
1 parent a49088b commit 6fd6210

File tree

7 files changed

+20
-19
lines changed

7 files changed

+20
-19
lines changed

docs/src/pages/playground/loader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { useLoadingState, useQueryStringState } from "../../../../src/brouther/brouther";
2+
import { useLoadingState, useQueryStringState } from "../../../../src";
33
import { Form, jsonResponse, LoaderProps, useDataLoader } from "../../exports";
44

55
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -16,7 +16,7 @@ const debounce = (fn: Function, ms: number = 0) => {
1616
let timeoutId: NodeJS.Timeout;
1717
return function (...args: any[]) {
1818
clearTimeout(timeoutId);
19-
timeoutId = setTimeout(() => fn.apply(this, args), ms);
19+
timeoutId = setTimeout(() => fn(...args), ms);
2020
};
2121
};
2222

src/brouther/brouther-response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Primitive } from "ts-toolbelt/out/Misc/Primitive";
1+
import { type Primitive } from "ts-toolbelt/out/Misc/Primitive";
22

33
export type ParseSerializable<T> = {
44
[K in keyof T]: T[K] extends Primitive ? T[K] : T[K] extends Date | undefined ? string : ParseSerializable<T[K]>;

src/brouther/brouther.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createContext, Fragment, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
1+
import React, { createContext, Fragment, SetStateAction, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
22
import { jsonToURLSearchParams, urlSearchParamsToJson } from "../form/form-data-api";
33
import { RouterNavigator } from "../router/router-navigator";
44
import type { BroutherFlags, ConfiguredRoute, Location, PathFormat } from "../types";
@@ -360,22 +360,23 @@ export const useFormActions = <R extends object>(): ActionState<R> => useBrouthe
360360
*/
361361
export const useQueryStringState = <T extends {} | string>(
362362
_?: T
363-
): [qs: T extends string ? QueryString.Parse<T> : T, set: (newQuery: T | ((prev: T) => T)) => void] => {
363+
): T extends string ? [qs: QueryString.Parse<T>, set: (q: SetStateAction<QueryString.Parse<T>>) => void] : [qs: T, set: SetStateAction<T>] => {
364+
type Hold = T extends string ? QueryString.Parse<T> : T;
364365
const { href, page, navigation } = useBrouther();
365366
const urlSearchParams = useUrlSearchParams();
366-
const qs = useMemo(
367+
const qs: Hold = useMemo(
367368
() => (page === null ? ({} as any) : transformData(urlSearchParams, mapUrlToQueryStringRecord(page.originalPath, fromStringToValue))),
368369
[href, page, urlSearchParams]
369370
);
370371
const callback = useCallback(
371-
(query: T | ((prev: T) => T)) => {
372+
(query: SetStateAction<Hold>) => {
372373
const location = new URL(window.location.href);
373-
const current = urlSearchParamsToJson<T>(new URLSearchParams(window.location.search));
374-
const result = typeof query === "function" ? query(current) : { ...(current as any), ...(query as any) };
374+
const current = urlSearchParamsToJson<Hold>(new URLSearchParams(window.location.search));
375+
const result = typeof query === "function" ? (query as Function)(current) : { ...(current as any), ...(query as any) };
375376
location.search = jsonToURLSearchParams(result).toString();
376377
navigation.push(location.href);
377378
},
378379
[navigation]
379380
);
380-
return [qs, callback];
381+
return [qs as Hold, callback as (q: Hold | ((h: Hold) => Hold)) => void] as any;
381382
};

src/router/link.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React, { forwardRef } from "react";
22
import { useBasename, useFlags, useHref, useNavigation } from "../brouther/brouther";
3-
import { AnyJson } from "../types";
4-
import { Paths } from "../types/paths";
3+
import { type AnyJson } from "../types";
4+
import { type Paths } from "../types/paths";
55
import type { QueryString } from "../types/query-string";
6-
import { QueryStringMapper } from "../utils/mappers";
7-
import { TextFragment } from "../utils/text-fragment";
6+
import { type QueryStringMapper } from "../utils/mappers";
7+
import { type TextFragment } from "../utils/text-fragment";
88
import { fetchTarget, join, mergeUrlEntities } from "../utils/utils";
99

1010
const isLeftClick = (e: React.MouseEvent) => e.button === 0;

src/router/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { useMemo } from "react";
33
import type { Function } from "ts-toolbelt";
44
import { useBrouther, useUrlSearchParams } from "../brouther/brouther";
55
import type { Actions, AsRouter, CreateMappedRoute, FetchPaths, Loader, Options, PathFormat, Route, RouteData, Router } from "../types";
6-
import { BrowserHistory } from "../types/history";
6+
import type { BrowserHistory } from "../types/history";
77
import type { Paths } from "../types/paths";
88
import type { QueryString } from "../types/query-string";
99
import { fromStringToValue, parsePath } from "../utils/mappers";

src/types/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type React from "react";
22
import type { Function, Number, Object, Union } from "ts-toolbelt";
3-
import { CustomResponse } from "../brouther/brouther-response";
3+
import { type CustomResponse } from "../brouther/brouther-response";
44
import type { RouterNavigator } from "../router/router-navigator";
5-
import { TextFragment } from "../utils/text-fragment";
6-
import { BrowserHistory } from "./history";
5+
import { type TextFragment } from "../utils/text-fragment";
6+
import { type BrowserHistory } from "./history";
77
import type { Paths } from "./paths";
88
import type { QueryString } from "./query-string";
99
import { X } from "./x";

src/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { CreateHref, Parser, ParsersMap, PathFormat, Route } from "../types";
2-
import { fromValueToString, QueryStringMapper } from "./mappers";
32
import type { Paths } from "../types/paths";
43
import type { QueryString } from "../types/query-string";
54
import { X } from "../types/x";
5+
import { fromValueToString, QueryStringMapper } from "./mappers";
66
import { stringifyTextFragment, TEXT_FRAGMENT_ID, TextFragment } from "./text-fragment";
77

88
export const has = <T extends {}, K extends X.AnyString<keyof T>>(o: T, k: K): k is K => Object.prototype.hasOwnProperty.call(o, k as any);

0 commit comments

Comments
 (0)