-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathnuqs.ts
More file actions
127 lines (115 loc) · 3.38 KB
/
Copy pathnuqs.ts
File metadata and controls
127 lines (115 loc) · 3.38 KB
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
// we're ensuring that keys are type-safe. there is no proper way to achieve it in `nuqs`.
// the `declare module` technique results in adding overloads, not overriding the existing functions.
// eslint-disable-next-line no-restricted-imports
import {
type Options,
type Parser,
useQueryState as useQueryStateOriginal,
type UseQueryStateOptions,
type UseQueryStateReturn,
useQueryStates as useQueryStatesOriginal,
type UseQueryStatesOptions,
type UseQueryStatesReturn,
} from "nuqs";
// eslint-disable-next-line no-restricted-imports
export * from "nuqs";
export type StateKey =
| "aggregations"
| "pageIndex"
| "pageSize"
| "pin"
| "streamAggregationRange"
| "streamFollow"
| "streamObserve"
| "streamRoutingKey"
| "stream"
| "table"
| "sort"
| "schema"
| "test"
| "filter"
| "view"
| "search"
| "searchScope";
// lifted from @prisma/client/runtime
type Exact<A, W> =
| (A extends unknown
? W extends A
? {
[K in keyof A]: Exact<A[K], W[K]>;
}
: W
: never)
| (A extends string | number | bigint | boolean | [] ? A : never);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function keyMap<const Map extends Partial<Record<StateKey, any>>>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
keyMap: Exact<Map, Partial<Record<StateKey, any>>>,
): BrandedKeyMap<Map> {
return keyMap as never;
}
export function urlKeys<const Map extends Partial<Record<StateKey, string>>>(
urlKeys: Exact<Map, Partial<Record<StateKey, string>>>,
): BrandedKeyMap<Map> {
return urlKeys as never;
}
const _BRAND_SYMBOL = Symbol("BRAND_SYMBOL");
type BrandedKeyMap<Map> = Map & {
[K in typeof _BRAND_SYMBOL]: never;
};
/**
* @see {@link useQueryStateOriginal}
*/
export function useQueryState<T>(
key: StateKey,
options: UseQueryStateOptions<T> & {
defaultValue: T;
},
): UseQueryStateReturn<
NonNullable<ReturnType<typeof options.parse>>,
typeof options.defaultValue
>;
export function useQueryState<T>(
key: StateKey,
options: UseQueryStateOptions<T>,
): UseQueryStateReturn<
NonNullable<ReturnType<typeof options.parse>>,
undefined
>;
export function useQueryState(
key: StateKey,
options: Options & {
defaultValue: string;
},
): UseQueryStateReturn<string, typeof options.defaultValue>;
export function useQueryState(
key: StateKey,
options: Pick<UseQueryStateOptions<string>, keyof Options>,
): UseQueryStateReturn<string, undefined>;
export function useQueryState(
key: StateKey,
): UseQueryStateReturn<string, undefined>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useQueryState(key: StateKey, options?: any): any {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return useQueryStateOriginal(key, options);
}
type UseQueryStatesKeysMap<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Map extends Partial<Record<StateKey, any>> = Partial<Record<StateKey, any>>,
> = {
[Key in keyof Map]: KeyMapValue<Map[Key]>;
};
type KeyMapValue<Type> = Parser<Type> &
Options & {
defaultValue?: Type;
};
/**
* @see {@link useQueryStatesOriginal}
*/
export function useQueryStates<KeyMap extends UseQueryStatesKeysMap>(
keyMap: BrandedKeyMap<KeyMap>,
options?: Partial<UseQueryStatesOptions<KeyMap>>,
): UseQueryStatesReturn<KeyMap> {
return useQueryStatesOriginal(keyMap as never, options);
}