| 1 |
import type { |
| 2 |
Api, |
| 3 |
BaseQueryFn, |
| 4 |
EndpointDefinitions, |
| 5 |
InfiniteQueryDefinition, |
| 6 |
Module, |
| 7 |
MutationDefinition, |
| 8 |
PrefetchOptions, |
| 9 |
QueryArgFrom, |
| 10 |
QueryDefinition, |
| 11 |
QueryKeys, |
| 12 |
} from '@reduxjs/toolkit/query' |
| 13 |
import { |
| 14 |
batch as rrBatch, |
| 15 |
useDispatch as rrUseDispatch, |
| 16 |
useSelector as rrUseSelector, |
| 17 |
useStore as rrUseStore, |
| 18 |
} from 'react-redux' |
| 19 |
import { createSelector as _createSelector } from 'reselect' |
| 20 |
import { |
| 21 |
isInfiniteQueryDefinition, |
| 22 |
isMutationDefinition, |
| 23 |
isQueryDefinition, |
| 24 |
} from '../endpointDefinitions' |
| 25 |
import { safeAssign } from '../tsHelpers' |
| 26 |
import { capitalize, countObjectKeys } from '../utils' |
| 27 |
import type { |
| 28 |
InfiniteQueryHooks, |
| 29 |
MutationHooks, |
| 30 |
QueryHooks, |
| 31 |
} from './buildHooks' |
| 32 |
import { buildHooks } from './buildHooks' |
| 33 |
import type { HooksWithUniqueNames } from './namedHooks' |
| 34 |
|
| 35 |
export const reactHooksModuleName = /* @__PURE__ */ Symbol() |
| 36 |
export type ReactHooksModule = typeof reactHooksModuleName |
| 37 |
|
| 38 |
declare module '@reduxjs/toolkit/query' { |
| 39 |
export interface ApiModules< |
| 40 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 41 |
BaseQuery extends BaseQueryFn, |
| 42 |
Definitions extends EndpointDefinitions, |
| 43 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 44 |
ReducerPath extends string, |
| 45 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 46 |
TagTypes extends string, |
| 47 |
> { |
| 48 |
[reactHooksModuleName]: { |
| 49 |
/** |
| 50 |
* Endpoints based on the input endpoints provided to `createApi`, containing `select`, `hooks` and `action matchers`. |
| 51 |
*/ |
| 52 |
endpoints: { |
| 53 |
[K in keyof Definitions]: Definitions[K] extends QueryDefinition< |
| 54 |
any, |
| 55 |
any, |
| 56 |
any, |
| 57 |
any, |
| 58 |
any |
| 59 |
> |
| 60 |
? QueryHooks<Definitions[K]> |
| 61 |
: Definitions[K] extends MutationDefinition<any, any, any, any, any> |
| 62 |
? MutationHooks<Definitions[K]> |
| 63 |
: Definitions[K] extends InfiniteQueryDefinition< |
| 64 |
any, |
| 65 |
any, |
| 66 |
any, |
| 67 |
any, |
| 68 |
any |
| 69 |
> |
| 70 |
? InfiniteQueryHooks<Definitions[K]> |
| 71 |
: never |
| 72 |
} |
| 73 |
/** |
| 74 |
* A hook that accepts a string endpoint name, and provides a callback that when called, pre-fetches the data for that endpoint. |
| 75 |
*/ |
| 76 |
usePrefetch<EndpointName extends QueryKeys<Definitions>>( |
| 77 |
endpointName: EndpointName, |
| 78 |
options?: PrefetchOptions, |
| 79 |
): ( |
| 80 |
arg: QueryArgFrom<Definitions[EndpointName]>, |
| 81 |
options?: PrefetchOptions, |
| 82 |
) => void |
| 83 |
} & HooksWithUniqueNames<Definitions> |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
type RR = typeof import('react-redux') |
| 88 |
|
| 89 |
export interface ReactHooksModuleOptions { |
| 90 |
/** |
| 91 |
* The hooks from React Redux to be used |
| 92 |
*/ |
| 93 |
hooks?: { |
| 94 |
/** |
| 95 |
* The version of the `useDispatch` hook to be used |
| 96 |
*/ |
| 97 |
useDispatch: RR['useDispatch'] |
| 98 |
/** |
| 99 |
* The version of the `useSelector` hook to be used |
| 100 |
*/ |
| 101 |
useSelector: RR['useSelector'] |
| 102 |
/** |
| 103 |
* The version of the `useStore` hook to be used |
| 104 |
*/ |
| 105 |
useStore: RR['useStore'] |
| 106 |
} |
| 107 |
/** |
| 108 |
* The version of the `batchedUpdates` function to be used |
| 109 |
*/ |
| 110 |
batch?: RR['batch'] |
| 111 |
/** |
| 112 |
* Enables performing asynchronous tasks immediately within a render. |
| 113 |
* |
| 114 |
* @example |
| 115 |
* |
| 116 |
* ```ts |
| 117 |
* import { |
| 118 |
* buildCreateApi, |
| 119 |
* coreModule, |
| 120 |
* reactHooksModule |
| 121 |
* } from '@reduxjs/toolkit/query/react' |
| 122 |
* |
| 123 |
* const createApi = buildCreateApi( |
| 124 |
* coreModule(), |
| 125 |
* reactHooksModule({ unstable__sideEffectsInRender: true }) |
| 126 |
* ) |
| 127 |
* ``` |
| 128 |
*/ |
| 129 |
unstable__sideEffectsInRender?: boolean |
| 130 |
/** |
| 131 |
* A selector creator (usually from `reselect`, or matching the same signature) |
| 132 |
*/ |
| 133 |
createSelector?: typeof _createSelector |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Creates a module that generates react hooks from endpoints, for use with `buildCreateApi`. |
| 138 |
* |
| 139 |
* @example |
| 140 |
* ```ts |
| 141 |
* const MyContext = React.createContext<ReactReduxContextValue | null>(null); |
| 142 |
* const customCreateApi = buildCreateApi( |
| 143 |
* coreModule(), |
| 144 |
* reactHooksModule({ |
| 145 |
* hooks: { |
| 146 |
* useDispatch: createDispatchHook(MyContext), |
| 147 |
* useSelector: createSelectorHook(MyContext), |
| 148 |
* useStore: createStoreHook(MyContext) |
| 149 |
* } |
| 150 |
* }) |
| 151 |
* ); |
| 152 |
* ``` |
| 153 |
* |
| 154 |
* @returns A module for use with `buildCreateApi` |
| 155 |
*/ |
| 156 |
export const reactHooksModule = ({ |
| 157 |
batch = rrBatch, |
| 158 |
hooks = { |
| 159 |
useDispatch: rrUseDispatch, |
| 160 |
useSelector: rrUseSelector, |
| 161 |
useStore: rrUseStore, |
| 162 |
}, |
| 163 |
createSelector = _createSelector, |
| 164 |
unstable__sideEffectsInRender = false, |
| 165 |
...rest |
| 166 |
}: ReactHooksModuleOptions = {}): Module<ReactHooksModule> => { |
| 167 |
if (process.env.NODE_ENV !== 'production') { |
| 168 |
const hookNames = ['useDispatch', 'useSelector', 'useStore'] as const |
| 169 |
let warned = false |
| 170 |
for (const hookName of hookNames) { |
| 171 |
// warn for old hook options |
| 172 |
if (countObjectKeys(rest) > 0) { |
| 173 |
if ((rest as Partial<typeof hooks>)[hookName]) { |
| 174 |
if (!warned) { |
| 175 |
console.warn( |
| 176 |
'As of RTK 2.0, the hooks now need to be specified as one object, provided under a `hooks` key:' + |
| 177 |
'\n`reactHooksModule({ hooks: { useDispatch, useSelector, useStore } })`', |
| 178 |
) |
| 179 |
warned = true |
| 180 |
} |
| 181 |
} |
| 182 |
// migrate |
| 183 |
// @ts-ignore |
| 184 |
hooks[hookName] = rest[hookName] |
| 185 |
} |
| 186 |
// then make sure we have them all |
| 187 |
if (typeof hooks[hookName] !== 'function') { |
| 188 |
throw new Error( |
| 189 |
`When using custom hooks for context, all ${ |
| 190 |
hookNames.length |
| 191 |
} hooks need to be provided: ${hookNames.join( |
| 192 |
', ', |
| 193 |
)}.\nHook ${hookName} was either not provided or not a function.`, |
| 194 |
) |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
return { |
| 200 |
name: reactHooksModuleName, |
| 201 |
init(api, { serializeQueryArgs }, context) { |
| 202 |
const anyApi = api as any as Api< |
| 203 |
any, |
| 204 |
Record<string, any>, |
| 205 |
any, |
| 206 |
any, |
| 207 |
ReactHooksModule |
| 208 |
> |
| 209 |
const { |
| 210 |
buildQueryHooks, |
| 211 |
buildInfiniteQueryHooks, |
| 212 |
buildMutationHook, |
| 213 |
usePrefetch, |
| 214 |
} = buildHooks({ |
| 215 |
api, |
| 216 |
moduleOptions: { |
| 217 |
batch, |
| 218 |
hooks, |
| 219 |
unstable__sideEffectsInRender, |
| 220 |
createSelector, |
| 221 |
}, |
| 222 |
serializeQueryArgs, |
| 223 |
context, |
| 224 |
}) |
| 225 |
safeAssign(anyApi, { usePrefetch }) |
| 226 |
safeAssign(context, { batch }) |
| 227 |
|
| 228 |
return { |
| 229 |
injectEndpoint(endpointName, definition) { |
| 230 |
if (isQueryDefinition(definition)) { |
| 231 |
const { |
| 232 |
useQuery, |
| 233 |
useLazyQuery, |
| 234 |
useLazyQuerySubscription, |
| 235 |
useQueryState, |
| 236 |
useQuerySubscription, |
| 237 |
} = buildQueryHooks(endpointName) |
| 238 |
safeAssign(anyApi.endpoints[endpointName], { |
| 239 |
useQuery, |
| 240 |
useLazyQuery, |
| 241 |
useLazyQuerySubscription, |
| 242 |
useQueryState, |
| 243 |
useQuerySubscription, |
| 244 |
}) |
| 245 |
;(api as any)[`use${capitalize(endpointName)}Query`] = useQuery |
| 246 |
;(api as any)[`useLazy${capitalize(endpointName)}Query`] = |
| 247 |
useLazyQuery |
| 248 |
} |
| 249 |
if (isMutationDefinition(definition)) { |
| 250 |
const useMutation = buildMutationHook(endpointName) |
| 251 |
safeAssign(anyApi.endpoints[endpointName], { |
| 252 |
useMutation, |
| 253 |
}) |
| 254 |
;(api as any)[`use${capitalize(endpointName)}Mutation`] = |
| 255 |
useMutation |
| 256 |
} else if (isInfiniteQueryDefinition(definition)) { |
| 257 |
const { |
| 258 |
useInfiniteQuery, |
| 259 |
useInfiniteQuerySubscription, |
| 260 |
useInfiniteQueryState, |
| 261 |
} = buildInfiniteQueryHooks(endpointName) |
| 262 |
safeAssign(anyApi.endpoints[endpointName], { |
| 263 |
useInfiniteQuery, |
| 264 |
useInfiniteQuerySubscription, |
| 265 |
useInfiniteQueryState, |
| 266 |
}) |
| 267 |
;(api as any)[`use${capitalize(endpointName)}InfiniteQuery`] = |
| 268 |
useInfiniteQuery |
| 269 |
} |
| 270 |
}, |
| 271 |
} |
| 272 |
}, |
| 273 |
} |
| 274 |
} |