@growae/reactive-react 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +47 -0
  3. package/src/context.test.ts +52 -0
  4. package/src/context.ts +25 -0
  5. package/src/errors/base.ts +8 -0
  6. package/src/errors/context.ts +12 -0
  7. package/src/errors/index.ts +5 -0
  8. package/src/exports/index.ts +230 -0
  9. package/src/exports/query.ts +12 -0
  10. package/src/hooks/index.ts +69 -0
  11. package/src/hooks/useAccount.test.ts +12 -0
  12. package/src/hooks/useAccount.ts +43 -0
  13. package/src/hooks/useBalance.test.ts +72 -0
  14. package/src/hooks/useBalance.ts +42 -0
  15. package/src/hooks/useBlock.test.ts +12 -0
  16. package/src/hooks/useBlock.ts +42 -0
  17. package/src/hooks/useCallContract.test.ts +12 -0
  18. package/src/hooks/useCallContract.ts +54 -0
  19. package/src/hooks/useChannelDeposit.test.ts +12 -0
  20. package/src/hooks/useChannelDeposit.ts +53 -0
  21. package/src/hooks/useClaimName.test.ts +12 -0
  22. package/src/hooks/useClaimName.ts +53 -0
  23. package/src/hooks/useCloseChannel.test.ts +12 -0
  24. package/src/hooks/useCloseChannel.ts +53 -0
  25. package/src/hooks/useConfig.test.ts +53 -0
  26. package/src/hooks/useConfig.ts +20 -0
  27. package/src/hooks/useConnect.test.ts +67 -0
  28. package/src/hooks/useConnect.ts +69 -0
  29. package/src/hooks/useConnection.test.ts +12 -0
  30. package/src/hooks/useConnection.ts +26 -0
  31. package/src/hooks/useConnections.test.ts +12 -0
  32. package/src/hooks/useConnections.ts +26 -0
  33. package/src/hooks/useConnectorClient.test.ts +12 -0
  34. package/src/hooks/useConnectorClient.ts +44 -0
  35. package/src/hooks/useConnectors.test.ts +12 -0
  36. package/src/hooks/useConnectors.ts +26 -0
  37. package/src/hooks/useContractBytecode.test.ts +12 -0
  38. package/src/hooks/useContractBytecode.ts +41 -0
  39. package/src/hooks/useContractEvents.test.ts +12 -0
  40. package/src/hooks/useContractEvents.ts +42 -0
  41. package/src/hooks/useDeployContract.test.ts +12 -0
  42. package/src/hooks/useDeployContract.ts +54 -0
  43. package/src/hooks/useDisconnect.test.ts +12 -0
  44. package/src/hooks/useDisconnect.ts +54 -0
  45. package/src/hooks/useEstimateGas.test.ts +12 -0
  46. package/src/hooks/useEstimateGas.ts +42 -0
  47. package/src/hooks/useHeight.test.ts +12 -0
  48. package/src/hooks/useHeight.ts +39 -0
  49. package/src/hooks/useNetworkId.test.ts +54 -0
  50. package/src/hooks/useNetworkId.ts +26 -0
  51. package/src/hooks/useNetworks.test.ts +12 -0
  52. package/src/hooks/useNetworks.ts +19 -0
  53. package/src/hooks/useNodeClient.test.ts +12 -0
  54. package/src/hooks/useNodeClient.ts +30 -0
  55. package/src/hooks/useOpenChannel.test.ts +12 -0
  56. package/src/hooks/useOpenChannel.ts +53 -0
  57. package/src/hooks/useOracleQueries.test.ts +12 -0
  58. package/src/hooks/useOracleQueries.ts +40 -0
  59. package/src/hooks/useOracleState.test.ts +12 -0
  60. package/src/hooks/useOracleState.ts +40 -0
  61. package/src/hooks/usePayForTransaction.test.ts +12 -0
  62. package/src/hooks/usePayForTransaction.ts +54 -0
  63. package/src/hooks/usePreclaimName.test.ts +12 -0
  64. package/src/hooks/usePreclaimName.ts +53 -0
  65. package/src/hooks/useQueryOracle.test.ts +12 -0
  66. package/src/hooks/useQueryOracle.ts +53 -0
  67. package/src/hooks/useReadContract.test.ts +12 -0
  68. package/src/hooks/useReadContract.ts +43 -0
  69. package/src/hooks/useReadContracts.test.ts +12 -0
  70. package/src/hooks/useReadContracts.ts +38 -0
  71. package/src/hooks/useReconnect.test.ts +12 -0
  72. package/src/hooks/useReconnect.ts +54 -0
  73. package/src/hooks/useRegisterOracle.test.ts +12 -0
  74. package/src/hooks/useRegisterOracle.ts +53 -0
  75. package/src/hooks/useResolveName.test.ts +12 -0
  76. package/src/hooks/useResolveName.ts +41 -0
  77. package/src/hooks/useRespondToQuery.test.ts +12 -0
  78. package/src/hooks/useRespondToQuery.ts +53 -0
  79. package/src/hooks/useRevokeName.test.ts +12 -0
  80. package/src/hooks/useRevokeName.ts +53 -0
  81. package/src/hooks/useSendTransaction.test.ts +12 -0
  82. package/src/hooks/useSendTransaction.ts +54 -0
  83. package/src/hooks/useSignMessage.test.ts +12 -0
  84. package/src/hooks/useSignMessage.ts +54 -0
  85. package/src/hooks/useSignTransaction.test.ts +12 -0
  86. package/src/hooks/useSignTransaction.ts +54 -0
  87. package/src/hooks/useSignTypedData.test.ts +12 -0
  88. package/src/hooks/useSignTypedData.ts +54 -0
  89. package/src/hooks/useSimulateContract.test.ts +12 -0
  90. package/src/hooks/useSimulateContract.ts +43 -0
  91. package/src/hooks/useSpend.test.ts +12 -0
  92. package/src/hooks/useSpend.ts +53 -0
  93. package/src/hooks/useSwitchNetwork.test.ts +12 -0
  94. package/src/hooks/useSwitchNetwork.ts +57 -0
  95. package/src/hooks/useTransaction.test.ts +12 -0
  96. package/src/hooks/useTransaction.ts +41 -0
  97. package/src/hooks/useTransactionCount.test.ts +12 -0
  98. package/src/hooks/useTransactionCount.ts +41 -0
  99. package/src/hooks/useTransferName.test.ts +12 -0
  100. package/src/hooks/useTransferName.ts +53 -0
  101. package/src/hooks/useUpdateName.test.ts +12 -0
  102. package/src/hooks/useUpdateName.ts +53 -0
  103. package/src/hooks/useVerifyMessage.test.ts +12 -0
  104. package/src/hooks/useVerifyMessage.ts +39 -0
  105. package/src/hooks/useVerifyTypedData.test.ts +12 -0
  106. package/src/hooks/useVerifyTypedData.ts +39 -0
  107. package/src/hooks/useWaitForTransaction.test.ts +12 -0
  108. package/src/hooks/useWaitForTransaction.ts +41 -0
  109. package/src/hooks/useWatchConnection.test.ts +12 -0
  110. package/src/hooks/useWatchConnection.ts +34 -0
  111. package/src/hooks/useWatchConnectors.test.ts +12 -0
  112. package/src/hooks/useWatchConnectors.ts +34 -0
  113. package/src/hooks/useWatchHeight.test.ts +12 -0
  114. package/src/hooks/useWatchHeight.ts +45 -0
  115. package/src/hydrate.ts +34 -0
  116. package/src/types/properties.ts +9 -0
  117. package/src/utils/query.ts +80 -0
  118. package/tsconfig.build.json +13 -0
  119. package/tsconfig.json +7 -0
@@ -0,0 +1,53 @@
1
+ 'use client'
2
+
3
+ import { useMutation } from '@tanstack/react-query'
4
+ import {
5
+ type TransferNameParameters,
6
+ type TransferNameReturnType,
7
+ transferName,
8
+ } from '@growae/reactive'
9
+ import type { Compute } from '@growae/reactive'
10
+ import type { ConfigParameter } from '../types/properties.js'
11
+ import type { UseMutationReturnType } from '../utils/query.js'
12
+ import { useConfig } from './useConfig.js'
13
+
14
+ export type UseTransferNameParameters<context = unknown> = Compute<
15
+ ConfigParameter & {
16
+ mutation?: {
17
+ onSuccess?: (data: TransferNameReturnType, variables: TransferNameParameters, context: context) => void
18
+ onError?: (error: Error, variables: TransferNameParameters, context: context) => void
19
+ }
20
+ }
21
+ >
22
+
23
+ export type UseTransferNameReturnType<context = unknown> = Compute<
24
+ UseMutationReturnType<
25
+ TransferNameReturnType,
26
+ Error,
27
+ TransferNameParameters,
28
+ context
29
+ > & {
30
+ transferName: (variables: TransferNameParameters) => void
31
+ transferNameAsync: (variables: TransferNameParameters) => Promise<TransferNameReturnType>
32
+ }
33
+ >
34
+
35
+ export function useTransferName<context = unknown>(
36
+ parameters: UseTransferNameParameters<context> = {},
37
+ ): UseTransferNameReturnType<context> {
38
+ const config = useConfig(parameters)
39
+
40
+ const mutation = useMutation({
41
+ mutationKey: ['transferName'],
42
+ mutationFn: (variables: TransferNameParameters) =>
43
+ transferName(config, variables),
44
+ ...parameters.mutation,
45
+ })
46
+
47
+ type Return = UseTransferNameReturnType<context>
48
+ return {
49
+ ...(mutation as unknown as Return),
50
+ transferName: mutation.mutate as Return['transferName'],
51
+ transferNameAsync: mutation.mutateAsync as Return['transferNameAsync'],
52
+ }
53
+ }
@@ -0,0 +1,12 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { useUpdateName } from './useUpdateName.js'
3
+
4
+ describe('useUpdateName', () => {
5
+ it('should be a function', () => {
6
+ expect(typeof useUpdateName).toBe('function')
7
+ })
8
+
9
+ it('should be exported', () => {
10
+ expect(useUpdateName).toBeDefined()
11
+ })
12
+ })
@@ -0,0 +1,53 @@
1
+ 'use client'
2
+
3
+ import { useMutation } from '@tanstack/react-query'
4
+ import {
5
+ type UpdateNameParameters,
6
+ type UpdateNameReturnType,
7
+ updateName,
8
+ } from '@growae/reactive'
9
+ import type { Compute } from '@growae/reactive'
10
+ import type { ConfigParameter } from '../types/properties.js'
11
+ import type { UseMutationReturnType } from '../utils/query.js'
12
+ import { useConfig } from './useConfig.js'
13
+
14
+ export type UseUpdateNameParameters<context = unknown> = Compute<
15
+ ConfigParameter & {
16
+ mutation?: {
17
+ onSuccess?: (data: UpdateNameReturnType, variables: UpdateNameParameters, context: context) => void
18
+ onError?: (error: Error, variables: UpdateNameParameters, context: context) => void
19
+ }
20
+ }
21
+ >
22
+
23
+ export type UseUpdateNameReturnType<context = unknown> = Compute<
24
+ UseMutationReturnType<
25
+ UpdateNameReturnType,
26
+ Error,
27
+ UpdateNameParameters,
28
+ context
29
+ > & {
30
+ updateName: (variables: UpdateNameParameters) => void
31
+ updateNameAsync: (variables: UpdateNameParameters) => Promise<UpdateNameReturnType>
32
+ }
33
+ >
34
+
35
+ export function useUpdateName<context = unknown>(
36
+ parameters: UseUpdateNameParameters<context> = {},
37
+ ): UseUpdateNameReturnType<context> {
38
+ const config = useConfig(parameters)
39
+
40
+ const mutation = useMutation({
41
+ mutationKey: ['updateName'],
42
+ mutationFn: (variables: UpdateNameParameters) =>
43
+ updateName(config, variables),
44
+ ...parameters.mutation,
45
+ })
46
+
47
+ type Return = UseUpdateNameReturnType<context>
48
+ return {
49
+ ...(mutation as unknown as Return),
50
+ updateName: mutation.mutate as Return['updateName'],
51
+ updateNameAsync: mutation.mutateAsync as Return['updateNameAsync'],
52
+ }
53
+ }
@@ -0,0 +1,12 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { useVerifyMessage } from './useVerifyMessage.js'
3
+
4
+ describe('useVerifyMessage', () => {
5
+ it('should be a function', () => {
6
+ expect(typeof useVerifyMessage).toBe('function')
7
+ })
8
+
9
+ it('should be exported', () => {
10
+ expect(useVerifyMessage).toBeDefined()
11
+ })
12
+ })
@@ -0,0 +1,39 @@
1
+ 'use client'
2
+
3
+ import {
4
+ type VerifyMessageParameters,
5
+ type VerifyMessageReturnType,
6
+ type VerifyMessageErrorType,
7
+ verifyMessage,
8
+ } from '@growae/reactive'
9
+ import type { Compute } from '@growae/reactive'
10
+ import { type UseQueryReturnType, useQuery } from '../utils/query.js'
11
+ import type { ConfigParameter } from '../types/properties.js'
12
+ import { useConfig } from './useConfig.js'
13
+
14
+ export type UseVerifyMessageParameters = Compute<
15
+ VerifyMessageParameters & ConfigParameter & { enabled?: boolean }
16
+ >
17
+
18
+ export type UseVerifyMessageReturnType = UseQueryReturnType<
19
+ VerifyMessageReturnType,
20
+ VerifyMessageErrorType
21
+ >
22
+
23
+ export function useVerifyMessage(
24
+ parameters: UseVerifyMessageParameters = {} as UseVerifyMessageParameters,
25
+ ): UseVerifyMessageReturnType {
26
+ const config = useConfig(parameters)
27
+
28
+ return useQuery({
29
+ queryKey: ['verifyMessage', {
30
+ message: parameters.message,
31
+ signature: parameters.signature,
32
+ address: parameters.address,
33
+ }],
34
+ queryFn: () => verifyMessage(config, parameters),
35
+ enabled: Boolean(
36
+ parameters.message && parameters.signature && parameters.address,
37
+ ) && (parameters.enabled ?? true),
38
+ }) as UseVerifyMessageReturnType
39
+ }
@@ -0,0 +1,12 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { useVerifyTypedData } from './useVerifyTypedData.js'
3
+
4
+ describe('useVerifyTypedData', () => {
5
+ it('should be a function', () => {
6
+ expect(typeof useVerifyTypedData).toBe('function')
7
+ })
8
+
9
+ it('should be exported', () => {
10
+ expect(useVerifyTypedData).toBeDefined()
11
+ })
12
+ })
@@ -0,0 +1,39 @@
1
+ 'use client'
2
+
3
+ import {
4
+ type VerifyTypedDataParameters,
5
+ type VerifyTypedDataReturnType,
6
+ type VerifyTypedDataErrorType,
7
+ verifyTypedData,
8
+ } from '@growae/reactive'
9
+ import type { Compute } from '@growae/reactive'
10
+ import { type UseQueryReturnType, useQuery } from '../utils/query.js'
11
+ import type { ConfigParameter } from '../types/properties.js'
12
+ import { useConfig } from './useConfig.js'
13
+
14
+ export type UseVerifyTypedDataParameters = Compute<
15
+ VerifyTypedDataParameters & ConfigParameter & { enabled?: boolean }
16
+ >
17
+
18
+ export type UseVerifyTypedDataReturnType = UseQueryReturnType<
19
+ VerifyTypedDataReturnType,
20
+ VerifyTypedDataErrorType
21
+ >
22
+
23
+ export function useVerifyTypedData(
24
+ parameters: UseVerifyTypedDataParameters = {} as UseVerifyTypedDataParameters,
25
+ ): UseVerifyTypedDataReturnType {
26
+ const config = useConfig(parameters)
27
+
28
+ return useQuery({
29
+ queryKey: ['verifyTypedData', {
30
+ data: parameters.data,
31
+ signature: parameters.signature,
32
+ address: parameters.address,
33
+ }],
34
+ queryFn: () => verifyTypedData(config, parameters),
35
+ enabled: Boolean(
36
+ parameters.data && parameters.signature && parameters.address,
37
+ ) && (parameters.enabled ?? true),
38
+ }) as UseVerifyTypedDataReturnType
39
+ }
@@ -0,0 +1,12 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { useWaitForTransaction } from './useWaitForTransaction.js'
3
+
4
+ describe('useWaitForTransaction', () => {
5
+ it('should be a function', () => {
6
+ expect(typeof useWaitForTransaction).toBe('function')
7
+ })
8
+
9
+ it('should be exported', () => {
10
+ expect(useWaitForTransaction).toBeDefined()
11
+ })
12
+ })
@@ -0,0 +1,41 @@
1
+ 'use client'
2
+
3
+ import {
4
+ type WaitForTransactionParameters,
5
+ type WaitForTransactionReturnType,
6
+ type WaitForTransactionErrorType,
7
+ waitForTransaction,
8
+ } from '@growae/reactive'
9
+ import type { Compute } from '@growae/reactive'
10
+ import { type UseQueryReturnType, useQuery } from '../utils/query.js'
11
+ import type { ConfigParameter } from '../types/properties.js'
12
+ import { useConfig } from './useConfig.js'
13
+ import { useNetworkId } from './useNetworkId.js'
14
+
15
+ export type UseWaitForTransactionParameters = Compute<
16
+ WaitForTransactionParameters & ConfigParameter & { enabled?: boolean }
17
+ >
18
+
19
+ export type UseWaitForTransactionReturnType = UseQueryReturnType<
20
+ WaitForTransactionReturnType,
21
+ WaitForTransactionErrorType
22
+ >
23
+
24
+ export function useWaitForTransaction(
25
+ parameters: UseWaitForTransactionParameters = {} as UseWaitForTransactionParameters,
26
+ ): UseWaitForTransactionReturnType {
27
+ const config = useConfig(parameters)
28
+ const networkId = useNetworkId({ config })
29
+
30
+ return useQuery({
31
+ queryKey: ['waitForTransaction', {
32
+ hash: parameters.hash,
33
+ networkId: parameters.networkId ?? networkId,
34
+ }],
35
+ queryFn: () => waitForTransaction(config, {
36
+ ...parameters,
37
+ networkId: parameters.networkId ?? networkId,
38
+ }),
39
+ enabled: Boolean(parameters.hash) && (parameters.enabled ?? true),
40
+ }) as UseWaitForTransactionReturnType
41
+ }
@@ -0,0 +1,12 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { useWatchConnection } from './useWatchConnection.js'
3
+
4
+ describe('useWatchConnection', () => {
5
+ it('should be a function', () => {
6
+ expect(typeof useWatchConnection).toBe('function')
7
+ })
8
+
9
+ it('should be exported', () => {
10
+ expect(useWatchConnection).toBeDefined()
11
+ })
12
+ })
@@ -0,0 +1,34 @@
1
+ 'use client'
2
+
3
+ import { watchConnection } from '@growae/reactive'
4
+ import type { Connection, Compute } from '@growae/reactive'
5
+ import { useEffect, useRef } from 'react'
6
+ import type { ConfigParameter } from '../types/properties.js'
7
+ import { useConfig } from './useConfig.js'
8
+
9
+ export type UseWatchConnectionParameters = Compute<
10
+ ConfigParameter & {
11
+ onChange: (connection: Connection | undefined) => void
12
+ enabled?: boolean
13
+ }
14
+ >
15
+
16
+ export type UseWatchConnectionReturnType = void
17
+
18
+ export function useWatchConnection(
19
+ parameters: UseWatchConnectionParameters,
20
+ ): UseWatchConnectionReturnType {
21
+ const { enabled = true } = parameters
22
+ const config = useConfig(parameters)
23
+
24
+ const onChangeRef = useRef(parameters.onChange)
25
+ onChangeRef.current = parameters.onChange
26
+
27
+ useEffect(() => {
28
+ if (!enabled) return
29
+ if (!onChangeRef.current) return
30
+ return watchConnection(config, {
31
+ onChange: (connection) => onChangeRef.current?.(connection),
32
+ })
33
+ }, [config, enabled])
34
+ }
@@ -0,0 +1,12 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { useWatchConnectors } from './useWatchConnectors.js'
3
+
4
+ describe('useWatchConnectors', () => {
5
+ it('should be a function', () => {
6
+ expect(typeof useWatchConnectors).toBe('function')
7
+ })
8
+
9
+ it('should be exported', () => {
10
+ expect(useWatchConnectors).toBeDefined()
11
+ })
12
+ })
@@ -0,0 +1,34 @@
1
+ 'use client'
2
+
3
+ import { watchConnectors } from '@growae/reactive'
4
+ import type { Connector, Compute } from '@growae/reactive'
5
+ import { useEffect, useRef } from 'react'
6
+ import type { ConfigParameter } from '../types/properties.js'
7
+ import { useConfig } from './useConfig.js'
8
+
9
+ export type UseWatchConnectorsParameters = Compute<
10
+ ConfigParameter & {
11
+ onChange: (connectors: readonly Connector[]) => void
12
+ enabled?: boolean
13
+ }
14
+ >
15
+
16
+ export type UseWatchConnectorsReturnType = void
17
+
18
+ export function useWatchConnectors(
19
+ parameters: UseWatchConnectorsParameters,
20
+ ): UseWatchConnectorsReturnType {
21
+ const { enabled = true } = parameters
22
+ const config = useConfig(parameters)
23
+
24
+ const onChangeRef = useRef(parameters.onChange)
25
+ onChangeRef.current = parameters.onChange
26
+
27
+ useEffect(() => {
28
+ if (!enabled) return
29
+ if (!onChangeRef.current) return
30
+ return watchConnectors(config, {
31
+ onChange: (connectors) => onChangeRef.current?.(connectors),
32
+ })
33
+ }, [config, enabled])
34
+ }
@@ -0,0 +1,12 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { useWatchHeight } from './useWatchHeight.js'
3
+
4
+ describe('useWatchHeight', () => {
5
+ it('should be a function', () => {
6
+ expect(typeof useWatchHeight).toBe('function')
7
+ })
8
+
9
+ it('should be exported', () => {
10
+ expect(useWatchHeight).toBeDefined()
11
+ })
12
+ })
@@ -0,0 +1,45 @@
1
+ 'use client'
2
+
3
+ import { watchHeight } from '@growae/reactive'
4
+ import type { Compute } from '@growae/reactive'
5
+ import { useEffect, useRef } from 'react'
6
+ import type { ConfigParameter } from '../types/properties.js'
7
+ import { useConfig } from './useConfig.js'
8
+ import { useNetworkId } from './useNetworkId.js'
9
+
10
+ export type UseWatchHeightParameters = Compute<
11
+ ConfigParameter & {
12
+ onHeight: (height: number) => void
13
+ onError?: (error: Error) => void
14
+ enabled?: boolean
15
+ interval?: number
16
+ networkId?: string
17
+ }
18
+ >
19
+
20
+ export type UseWatchHeightReturnType = void
21
+
22
+ export function useWatchHeight(
23
+ parameters: UseWatchHeightParameters,
24
+ ): UseWatchHeightReturnType {
25
+ const { enabled = true, interval, networkId: paramNetworkId } = parameters
26
+ const config = useConfig(parameters)
27
+ const configNetworkId = useNetworkId({ config })
28
+ const networkId = paramNetworkId ?? configNetworkId
29
+
30
+ const onHeightRef = useRef(parameters.onHeight)
31
+ const onErrorRef = useRef(parameters.onError)
32
+ onHeightRef.current = parameters.onHeight
33
+ onErrorRef.current = parameters.onError
34
+
35
+ useEffect(() => {
36
+ if (!enabled) return
37
+ if (!onHeightRef.current) return
38
+ return watchHeight(config, {
39
+ onChange: (height) => onHeightRef.current?.(height),
40
+ onError: (error) => onErrorRef.current?.(error),
41
+ interval,
42
+ networkId,
43
+ })
44
+ }, [config, enabled, interval, networkId])
45
+ }
package/src/hydrate.ts ADDED
@@ -0,0 +1,34 @@
1
+ 'use client'
2
+
3
+ import { hydrate } from '@growae/reactive'
4
+ import type { Config, State } from '@growae/reactive'
5
+ import { type ReactElement, useEffect, useRef } from 'react'
6
+
7
+ export type HydrateProps = {
8
+ config: Config
9
+ initialState?: State | undefined
10
+ reconnectOnMount?: boolean | undefined
11
+ }
12
+
13
+ export function Hydrate(parameters: React.PropsWithChildren<HydrateProps>) {
14
+ const { children, config, initialState, reconnectOnMount = true } = parameters
15
+
16
+ const { onMount } = hydrate(config, {
17
+ initialState,
18
+ reconnectOnMount,
19
+ })
20
+
21
+ if (!config._internal.ssr) onMount()
22
+
23
+ const active = useRef(true)
24
+ useEffect(() => {
25
+ if (!active.current) return
26
+ if (!config._internal.ssr) return
27
+ onMount()
28
+ return () => {
29
+ active.current = false
30
+ }
31
+ }, [])
32
+
33
+ return children as ReactElement
34
+ }
@@ -0,0 +1,9 @@
1
+ import type { Config } from '@growae/reactive'
2
+
3
+ export type ConfigParameter<config extends Config = Config> = {
4
+ config?: Config | config | undefined
5
+ }
6
+
7
+ export type EnabledParameter = {
8
+ enabled?: boolean | undefined
9
+ }
@@ -0,0 +1,80 @@
1
+ 'use client'
2
+
3
+ import {
4
+ type DefaultError,
5
+ type MutateFunction,
6
+ type QueryKey,
7
+ useQuery as tanstack_useQuery,
8
+ type UseMutationOptions,
9
+ type UseMutationResult,
10
+ type UseQueryOptions,
11
+ type UseQueryResult,
12
+ useMutation,
13
+ } from '@tanstack/react-query'
14
+ import { hashFn } from '@growae/reactive/query'
15
+ import type { Compute, ExactPartial, LooseOmit, UnionStrictOmit } from '@growae/reactive'
16
+
17
+ export { useMutation }
18
+
19
+ export type UseMutationParameters<
20
+ data = unknown,
21
+ error = Error,
22
+ variables = void,
23
+ context = unknown,
24
+ > = Compute<
25
+ LooseOmit<
26
+ UseMutationOptions<data, error, Compute<variables>, context>,
27
+ 'mutationFn' | 'mutationKey' | 'throwOnError'
28
+ >
29
+ >
30
+
31
+ export type UseMutationReturnType<
32
+ data = unknown,
33
+ error = Error,
34
+ variables = void,
35
+ context = unknown,
36
+ mutate = MutateFunction,
37
+ mutateAsync = MutateFunction,
38
+ > = Compute<
39
+ UnionStrictOmit<
40
+ UseMutationResult<data, error, variables, context>,
41
+ 'mutate' | 'mutateAsync'
42
+ > & {
43
+ mutate: mutate
44
+ mutateAsync: mutateAsync
45
+ }
46
+ >
47
+
48
+ export function useQuery<queryFnData, error, data, queryKey extends QueryKey>(
49
+ parameters: UseQueryParameters<queryFnData, error, data, queryKey> & {
50
+ queryKey: QueryKey
51
+ },
52
+ ): UseQueryReturnType<data, error> {
53
+ const result = tanstack_useQuery({
54
+ ...(parameters as any),
55
+ queryKeyHashFn: hashFn,
56
+ }) as UseQueryReturnType<data, error>
57
+ result.queryKey = parameters.queryKey
58
+ return result
59
+ }
60
+
61
+ export type UseQueryParameters<
62
+ queryFnData = unknown,
63
+ error = DefaultError,
64
+ data = queryFnData,
65
+ queryKey extends QueryKey = QueryKey,
66
+ > = Compute<
67
+ ExactPartial<
68
+ LooseOmit<UseQueryOptions<queryFnData, error, data, queryKey>, 'initialData'>
69
+ > & {
70
+ initialData?:
71
+ | UseQueryOptions<queryFnData, error, data, queryKey>['initialData']
72
+ | undefined
73
+ }
74
+ >
75
+
76
+ export type UseQueryReturnType<data = unknown, error = DefaultError> = Compute<
77
+ UseQueryResult<data, error> & {
78
+ queryKey: QueryKey
79
+ }
80
+ >
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist/esm",
6
+ "declarationDir": "dist/types",
7
+ "declaration": true,
8
+ "declarationMap": true,
9
+ "sourceMap": true
10
+ },
11
+ "include": ["src/**/*.ts", "src/**/*.tsx"],
12
+ "exclude": ["src/**/*.test.ts", "src/**/*.test-d.ts"]
13
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src"
5
+ },
6
+ "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.test.ts", "src/**/*.test-d.ts"]
7
+ }