-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bundling adjustments to create apis that can be referenced in rsc #4675
Open
phryneas
wants to merge
2
commits into
master
Choose a base branch
from
pr/rsc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// This must remain here so that the `mangleErrors.cjs` build script | ||
// does not have to import this into each source file it rewrites. | ||
import { formatProdErrorMessage } from '@reduxjs/toolkit' | ||
|
||
import { buildCreateApi, coreModule } from '@reduxjs/toolkit/query' | ||
import { unboundHooksModule, reactHooksModuleName } from './module' | ||
|
||
export * from '@reduxjs/toolkit/query' | ||
export { ApiProvider } from './ApiProvider' | ||
|
||
const throwFn = () => { | ||
throw new Error('Hooks can only be used in Client Components') | ||
} | ||
|
||
const reactHooksModule = unboundHooksModule({ | ||
hooks: { | ||
useDispatch: throwFn, | ||
useSelector: throwFn, | ||
useStore: throwFn, | ||
}, | ||
batch: throwFn, | ||
}) | ||
|
||
const createApi = /* @__PURE__ */ buildCreateApi( | ||
coreModule(), | ||
reactHooksModule() | ||
) | ||
|
||
export type { | ||
TypedUseMutationResult, | ||
TypedUseQueryHookResult, | ||
TypedUseQueryStateResult, | ||
TypedUseQuerySubscriptionResult, | ||
TypedLazyQueryTrigger, | ||
TypedUseLazyQuery, | ||
TypedUseMutation, | ||
TypedMutationTrigger, | ||
TypedQueryStateSelector, | ||
TypedUseQueryState, | ||
TypedUseQuery, | ||
TypedUseQuerySubscription, | ||
TypedUseLazyQuerySubscription, | ||
TypedUseQueryStateOptions, | ||
} from './buildHooks' | ||
export { UNINITIALIZED_VALUE } from './constants' | ||
export { createApi, reactHooksModule, reactHooksModuleName } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,12 +9,7 @@ import type { | |||||
QueryDefinition, | ||||||
QueryKeys, | ||||||
} from '@reduxjs/toolkit/query' | ||||||
import { | ||||||
batch as rrBatch, | ||||||
useDispatch as rrUseDispatch, | ||||||
useSelector as rrUseSelector, | ||||||
useStore as rrUseStore, | ||||||
} from 'react-redux' | ||||||
|
||||||
import { createSelector as _createSelector } from 'reselect' | ||||||
import { isMutationDefinition, isQueryDefinition } from '../endpointDefinitions' | ||||||
import { safeAssign } from '../tsHelpers' | ||||||
|
@@ -34,7 +29,7 @@ declare module '@reduxjs/toolkit/query' { | |||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||||||
ReducerPath extends string, | ||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||||||
TagTypes extends string, | ||||||
TagTypes extends string | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
> { | ||||||
[reactHooksModuleName]: { | ||||||
/** | ||||||
|
@@ -50,15 +45,15 @@ declare module '@reduxjs/toolkit/query' { | |||||
> | ||||||
? QueryHooks<Definitions[K]> | ||||||
: Definitions[K] extends MutationDefinition<any, any, any, any, any> | ||||||
? MutationHooks<Definitions[K]> | ||||||
: never | ||||||
? MutationHooks<Definitions[K]> | ||||||
: never | ||||||
} | ||||||
/** | ||||||
* A hook that accepts a string endpoint name, and provides a callback that when called, pre-fetches the data for that endpoint. | ||||||
*/ | ||||||
usePrefetch<EndpointName extends QueryKeys<Definitions>>( | ||||||
endpointName: EndpointName, | ||||||
options?: PrefetchOptions, | ||||||
options?: PrefetchOptions | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
): ( | ||||||
arg: QueryArgFrom<Definitions[EndpointName]>, | ||||||
options?: PrefetchOptions, | ||||||
|
@@ -136,103 +131,104 @@ export interface ReactHooksModuleOptions { | |||||
* | ||||||
* @returns A module for use with `buildCreateApi` | ||||||
*/ | ||||||
export const reactHooksModule = ({ | ||||||
batch = rrBatch, | ||||||
hooks = { | ||||||
useDispatch: rrUseDispatch, | ||||||
useSelector: rrUseSelector, | ||||||
useStore: rrUseStore, | ||||||
}, | ||||||
createSelector = _createSelector, | ||||||
unstable__sideEffectsInRender = false, | ||||||
...rest | ||||||
}: ReactHooksModuleOptions = {}): Module<ReactHooksModule> => { | ||||||
if (process.env.NODE_ENV !== 'production') { | ||||||
const hookNames = ['useDispatch', 'useSelector', 'useStore'] as const | ||||||
let warned = false | ||||||
for (const hookName of hookNames) { | ||||||
// warn for old hook options | ||||||
if (countObjectKeys(rest) > 0) { | ||||||
if ((rest as Partial<typeof hooks>)[hookName]) { | ||||||
if (!warned) { | ||||||
console.warn( | ||||||
'As of RTK 2.0, the hooks now need to be specified as one object, provided under a `hooks` key:' + | ||||||
'\n`reactHooksModule({ hooks: { useDispatch, useSelector, useStore } })`', | ||||||
) | ||||||
warned = true | ||||||
export const unboundHooksModule = | ||||||
({ | ||||||
batch: defaultBatch, | ||||||
hooks: defaultHooks, | ||||||
}: Required<Pick<ReactHooksModuleOptions, 'batch' | 'hooks'>>) => | ||||||
({ | ||||||
batch = defaultBatch, | ||||||
hooks = defaultHooks, | ||||||
createSelector = _createSelector, | ||||||
unstable__sideEffectsInRender = false, | ||||||
...rest | ||||||
}: ReactHooksModuleOptions = {}): Module<ReactHooksModule> => { | ||||||
if (process.env.NODE_ENV !== 'production') { | ||||||
const hookNames = ['useDispatch', 'useSelector', 'useStore'] as const | ||||||
let warned = false | ||||||
for (const hookName of hookNames) { | ||||||
// warn for old hook options | ||||||
if (countObjectKeys(rest) > 0) { | ||||||
if ((rest as Partial<typeof hooks>)[hookName]) { | ||||||
if (!warned) { | ||||||
console.warn( | ||||||
'As of RTK 2.0, the hooks now need to be specified as one object, provided under a `hooks` key:' + | ||||||
'\n`reactHooksModule({ hooks: { useDispatch, useSelector, useStore } })`' | ||||||
) | ||||||
warned = true | ||||||
} | ||||||
} | ||||||
// migrate | ||||||
// @ts-ignore | ||||||
hooks[hookName] = rest[hookName] | ||||||
} | ||||||
// then make sure we have them all | ||||||
if (typeof hooks[hookName] !== 'function') { | ||||||
throw new Error( | ||||||
`When using custom hooks for context, all ${ | ||||||
hookNames.length | ||||||
} hooks need to be provided: ${hookNames.join( | ||||||
', ' | ||||||
)}.\nHook ${hookName} was either not provided or not a function.` | ||||||
) | ||||||
} | ||||||
// migrate | ||||||
// @ts-ignore | ||||||
hooks[hookName] = rest[hookName] | ||||||
} | ||||||
// then make sure we have them all | ||||||
if (typeof hooks[hookName] !== 'function') { | ||||||
throw new Error( | ||||||
`When using custom hooks for context, all ${ | ||||||
hookNames.length | ||||||
} hooks need to be provided: ${hookNames.join( | ||||||
', ', | ||||||
)}.\nHook ${hookName} was either not provided or not a function.`, | ||||||
) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return { | ||||||
name: reactHooksModuleName, | ||||||
init(api, { serializeQueryArgs }, context) { | ||||||
const anyApi = api as any as Api< | ||||||
any, | ||||||
Record<string, any>, | ||||||
any, | ||||||
any, | ||||||
ReactHooksModule | ||||||
> | ||||||
const { buildQueryHooks, buildMutationHook, usePrefetch } = buildHooks({ | ||||||
api, | ||||||
moduleOptions: { | ||||||
batch, | ||||||
hooks, | ||||||
unstable__sideEffectsInRender, | ||||||
createSelector, | ||||||
}, | ||||||
serializeQueryArgs, | ||||||
context, | ||||||
}) | ||||||
safeAssign(anyApi, { usePrefetch }) | ||||||
safeAssign(context, { batch }) | ||||||
return { | ||||||
name: reactHooksModuleName, | ||||||
init(api, { serializeQueryArgs }, context) { | ||||||
const anyApi = api as any as Api< | ||||||
any, | ||||||
Record<string, any>, | ||||||
any, | ||||||
any, | ||||||
ReactHooksModule | ||||||
> | ||||||
const { buildQueryHooks, buildMutationHook, usePrefetch } = buildHooks({ | ||||||
api, | ||||||
moduleOptions: { | ||||||
batch, | ||||||
hooks, | ||||||
unstable__sideEffectsInRender, | ||||||
createSelector, | ||||||
}, | ||||||
serializeQueryArgs, | ||||||
context, | ||||||
}) | ||||||
safeAssign(anyApi, { usePrefetch }) | ||||||
safeAssign(context, { batch }) | ||||||
|
||||||
return { | ||||||
injectEndpoint(endpointName, definition) { | ||||||
if (isQueryDefinition(definition)) { | ||||||
const { | ||||||
useQuery, | ||||||
useLazyQuery, | ||||||
useLazyQuerySubscription, | ||||||
useQueryState, | ||||||
useQuerySubscription, | ||||||
} = buildQueryHooks(endpointName) | ||||||
safeAssign(anyApi.endpoints[endpointName], { | ||||||
useQuery, | ||||||
useLazyQuery, | ||||||
useLazyQuerySubscription, | ||||||
useQueryState, | ||||||
useQuerySubscription, | ||||||
}) | ||||||
;(api as any)[`use${capitalize(endpointName)}Query`] = useQuery | ||||||
;(api as any)[`useLazy${capitalize(endpointName)}Query`] = | ||||||
useLazyQuery | ||||||
} else if (isMutationDefinition(definition)) { | ||||||
const useMutation = buildMutationHook(endpointName) | ||||||
safeAssign(anyApi.endpoints[endpointName], { | ||||||
useMutation, | ||||||
}) | ||||||
;(api as any)[`use${capitalize(endpointName)}Mutation`] = | ||||||
useMutation | ||||||
} | ||||||
}, | ||||||
} | ||||||
}, | ||||||
return { | ||||||
injectEndpoint(endpointName, definition) { | ||||||
if (isQueryDefinition(definition)) { | ||||||
const { | ||||||
useQuery, | ||||||
useLazyQuery, | ||||||
useLazyQuerySubscription, | ||||||
useQueryState, | ||||||
useQuerySubscription, | ||||||
} = buildQueryHooks(endpointName) | ||||||
safeAssign(anyApi.endpoints[endpointName], { | ||||||
useQuery, | ||||||
useLazyQuery, | ||||||
useLazyQuerySubscription, | ||||||
useQueryState, | ||||||
useQuerySubscription, | ||||||
}) | ||||||
;(api as any)[`use${capitalize(endpointName)}Query`] = useQuery | ||||||
;(api as any)[`useLazy${capitalize(endpointName)}Query`] = | ||||||
useLazyQuery | ||||||
} else if (isMutationDefinition(definition)) { | ||||||
const useMutation = buildMutationHook(endpointName) | ||||||
safeAssign(anyApi.endpoints[endpointName], { | ||||||
useMutation, | ||||||
}) | ||||||
;(api as any)[`use${capitalize(endpointName)}Mutation`] = | ||||||
useMutation | ||||||
} | ||||||
}, | ||||||
} | ||||||
}, | ||||||
} | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.