@intrig/react 1.0.6 → 1.0.10

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.
package/.babelrc ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "presets": [
3
+ [
4
+ "@nx/react/babel",
5
+ {
6
+ "runtime": "automatic",
7
+ "useBuiltIns": "usage"
8
+ }
9
+ ]
10
+ ],
11
+ "plugins": []
12
+ }
package/README.md CHANGED
@@ -1,33 +1,35 @@
1
- # Intrig
1
+ # @intrig/react
2
2
 
3
- Intrig is a powerful tool for simplifying API integration in React applications. It provides enhanced documentation, API version management, and developer tooling for seamless frontend-backend integration.
3
+ @intrig/react is a placeholder project for code generation, specifically tailored for React projects. This package is part of the Intrig toolset, designed to streamline the development process by providing autogenerated code and utilities for React applications.
4
4
 
5
- ## Getting Started
6
-
7
- Prerequisites
5
+ ## Features
8
6
 
9
- * Node.js v16+.
10
- * Nx build tool.
7
+ - **Code Generation for React Projects**: Generate boilerplate code to kickstart React development quickly and efficiently.
8
+ - **Easy Integration**: Works seamlessly with React to create scalable and maintainable applications.
11
9
 
12
- ## Installation
10
+ ## Getting Started
13
11
 
14
- Clone the repository and install dependencies:
12
+ To start using @intrig/react, simply install the package in your React project:
15
13
 
16
14
  ```bash
17
- $ git clone https://github.com/your-username/intrig.git
18
- $ cd intrig
19
- $ npm install
20
-
15
+ npm install @intrig/react
21
16
  ```
22
17
 
18
+ This package will allow you to leverage autogenerated code for a variety of purposes within your React app, saving development time and reducing boilerplate.
19
+
20
+ ## Contributing
21
+
22
+ We welcome contributions! Feel free to check out the [contribution guide](https://github.com/intrigsoft/intrig/blob/main/CONTRIBUTING.md) in the main repository.
23
+
23
24
  ## License
24
25
 
25
- Intrig is open source software licensed under the [MIT License](LICENSE).
26
+ @intrig/react is licensed under the MIT License. See the [LICENSE](https://github.com/intrigsoft/intrig/blob/main/LICENSE) file for more information.
27
+
28
+ ## Links
26
29
 
27
- ## Contribution
30
+ - [Main GitHub Repository](https://github.com/intrigsoft/intrig)
28
31
 
29
- We welcome contributions! Please read our [contributing guide](CONTRIBUTING.md) to learn how you can get involved.
32
+ ## Support
30
33
 
31
- ## Contact
34
+ For questions, suggestions, or assistance, please open an issue on the [GitHub repository](https://github.com/intrigsoft/intrig/issues).
32
35
 
33
- For questions or feedback, feel free to reach out at [support@intrigsoft.com](mailto\:support@intrigsoft.com).
@@ -0,0 +1,12 @@
1
+ const nx = require('@nx/eslint-plugin');
2
+ const baseConfig = require('../../eslint.base.config.js');
3
+
4
+ module.exports = [
5
+ ...baseConfig,
6
+ ...nx.configs['flat/react'],
7
+ {
8
+ files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'],
9
+ // Override or add rules here
10
+ rules: {},
11
+ },
12
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intrig/react",
3
- "version": "1.0.6",
3
+ "version": "1.0.10",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "private": false,
@@ -22,8 +22,8 @@
22
22
  "fast-xml-parser": "^4.5.0"
23
23
  },
24
24
  "peerDependencies": {
25
- "react": "18.3.1",
26
- "react-dom": "18.3.1"
25
+ "react": ">=18.0.0 || ^19.0.0-rc",
26
+ "react-dom": ">=18.0.0 || ^19.0.0-rc"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/glob": "^8.1.0"
@@ -31,6 +31,5 @@
31
31
  "_moduleAliases": {
32
32
  "@intrig/react": "./src"
33
33
  },
34
- "type": "module",
35
- "module": "./index.esm.js"
36
- }
34
+ "type": "module"
35
+ }
package/project.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "client-react",
3
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "lib/client-react/src",
5
+ "projectType": "library",
6
+ "tags": ["react intrig code-generation"],
7
+ "// targets": "to see all targets run: nx show project client-react --web",
8
+ "targets": {}
9
+ }
@@ -0,0 +1,28 @@
1
+ const { withNx } = require('@nx/rollup/with-nx');
2
+ const url = require('@rollup/plugin-url');
3
+ const svg = require('@svgr/rollup');
4
+
5
+ module.exports = withNx(
6
+ {
7
+ main: './src/index.ts',
8
+ outputPath: '../../dist/lib/client-react',
9
+ tsConfig: './tsconfig.lib.json',
10
+ compiler: 'babel',
11
+ external: ['react', 'react-dom', 'react/jsx-runtime'],
12
+ format: ['esm'],
13
+ assets: [{ input: '.', output: '.', glob: 'README.md' }],
14
+ },
15
+ {
16
+ // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options
17
+ plugins: [
18
+ svg({
19
+ svgo: false,
20
+ titleProp: true,
21
+ ref: true,
22
+ }),
23
+ url({
24
+ limit: 10000, // 10kB
25
+ }),
26
+ ],
27
+ }
28
+ );
@@ -1,4 +1,4 @@
1
1
  export * from './useResolvedValue';
2
2
  export * from './useResolvedCachedValue';
3
- export * from './useAsPromise';
4
- export * from './useAsNetworkState';
3
+ export * from './useAsPromise'
4
+ export * from './useAsNetworkState'
@@ -0,0 +1,53 @@
1
+ import { error, init, NetworkState, pending, success } from '@intrig/react/network-state';
2
+ import { useCallback, useId, useMemo } from 'react';
3
+ import { useIntrigContext } from '@intrig/react/intrig-context';
4
+
5
+ /**
6
+ * A custom hook that integrates a promise-based operation with a network state management system.
7
+ * Tracks the network state (e.g., pending, success, error) for a given asynchronous function.
8
+ *
9
+ * @param fn A promise-based function that performs an asynchronous operation.
10
+ * @param key An optional string key to identify the network state uniquely. Defaults to 'default'.
11
+ * @return A tuple containing:
12
+ * 1. The current network state of the operation.
13
+ * 2. A function to execute the provided asynchronous operation.
14
+ * 3. A function to reset the network state back to the initial state.
15
+ */
16
+ export function useAsNetworkState<T, F extends ((...args: any) => Promise<T>)>(fn: F, key = 'default'): [NetworkState<T>, (...params: Parameters<F>) => void, () => void] {
17
+ const id = useId();
18
+
19
+ const context = useIntrigContext();
20
+
21
+ const networkState = useMemo(() => {
22
+ return context.state?.[`promiseState:${id}:${key}}`] ?? init()
23
+ }, [context.state?.[`promiseState:${id}:${key}}`]]);
24
+
25
+ const dispatch = useCallback(
26
+ (state: NetworkState<T>) => {
27
+ context.dispatch({ key, operation: id, source: 'promiseState', state });
28
+ },
29
+ [key, context.dispatch]
30
+ );
31
+
32
+ const execute = useCallback((...args: Parameters<F>) => {
33
+ dispatch(pending())
34
+ return fn(...args).then(
35
+ (data) => {
36
+ dispatch(success(data))
37
+ },
38
+ (e) => {
39
+ dispatch(error(e))
40
+ }
41
+ )
42
+ }, []);
43
+
44
+ const clear = useCallback(() => {
45
+ dispatch(init())
46
+ }, []);
47
+
48
+ return [
49
+ networkState,
50
+ execute,
51
+ clear
52
+ ]
53
+ }
@@ -1,4 +1,15 @@
1
- import { BinaryFunctionHook, BinaryHookOptions, BinaryProduceHook, ConstantHook, UnaryFunctionHook, UnaryHookOptions, UnaryProduceHook, UnitHook, UnitHookOptions } from '@intrig/react/network-state';
1
+ import {
2
+ BinaryFunctionHook,
3
+ BinaryHookOptions,
4
+ BinaryProduceHook,
5
+ ConstantHook, IntrigHook, IntrigHookOptions, isError, isSuccess, isValidationError, UnaryFunctionHook,
6
+ UnaryHookOptions,
7
+ UnaryProduceHook,
8
+ UnitHook,
9
+ UnitHookOptions
10
+ } from '@intrig/react/network-state';
11
+ import { useCallback, useEffect, useRef } from 'react';
12
+
2
13
  /**
3
14
  * Transforms a unit hook into a promise, enabling asynchronous usage of the hook's lifecycle or events.
4
15
  *
@@ -7,7 +18,8 @@ import { BinaryFunctionHook, BinaryHookOptions, BinaryProduceHook, ConstantHook,
7
18
  * @return {[() => Promise<never>, () => void]} A tuple containing a function that returns a promise resolving with the hook's completion
8
19
  * result or rejecting with an error, and a function to clean up the hook's resources when no longer needed.
9
20
  */
10
- export declare function useAsPromise<E>(hook: UnitHook<E>, options: UnitHookOptions): [() => Promise<never>, () => void];
21
+ export function useAsPromise<E>(hook: UnitHook<E>, options: UnitHookOptions): [() => Promise<never>, () => void];
22
+
11
23
  /**
12
24
  * Converts a constant hook into a promise-based interface that can be invoked and reset.
13
25
  *
@@ -15,7 +27,8 @@ export declare function useAsPromise<E>(hook: UnitHook<E>, options: UnitHookOpti
15
27
  * @param {UnitHookOptions} options - Configuration options for the hook behavior.
16
28
  * @return {[() => Promise<T>, () => void]} A tuple where the first element is a function that returns a promise resolving with the hook value, and the second element is a reset function.
17
29
  */
18
- export declare function useAsPromise<T, E>(hook: ConstantHook<T, E>, options: UnitHookOptions): [() => Promise<T>, () => void];
30
+ export function useAsPromise<T, E>(hook: ConstantHook<T, E>, options: UnitHookOptions): [() => Promise<T>, () => void];
31
+
19
32
  /**
20
33
  * Wraps the provided hook function to return a promise-based API, offering more flexibility
21
34
  * in asynchronous operations. This utility converts the hook into a callable function that
@@ -27,7 +40,8 @@ export declare function useAsPromise<T, E>(hook: ConstantHook<T, E>, options: Un
27
40
  * is a function that takes the hook's parameters and returns a `Promise`, and the second element
28
41
  * is a cleanup function for resource management.
29
42
  */
30
- export declare function useAsPromise<P, E>(hook: UnaryProduceHook<P, E>, options?: UnaryHookOptions<P>): [(params: P) => Promise<never>, () => void];
43
+ export function useAsPromise<P, E>(hook: UnaryProduceHook<P, E>, options?: UnaryHookOptions<P>): [(params: P) => Promise<never>, () => void];
44
+
31
45
  /**
32
46
  * Allows the usage of a hook's asynchronous behavior as a Promise.
33
47
  *
@@ -37,7 +51,8 @@ export declare function useAsPromise<P, E>(hook: UnaryProduceHook<P, E>, options
37
51
  * - A function that accepts parameters and returns a Promise resolving to the hook's result.
38
52
  * - A cleanup function to properly dispose of the hook when no longer needed.
39
53
  */
40
- export declare function useAsPromise<P, T, E>(hook: UnaryFunctionHook<P, T, E>, options?: UnaryHookOptions<P>): [(params: P) => Promise<T>, () => void];
54
+ export function useAsPromise<P, T, E>( hook: UnaryFunctionHook<P, T, E>, options?: UnaryHookOptions<P>): [(params: P) => Promise<T>, () => void];
55
+
41
56
  /**
42
57
  * Converts a binary hook into a promise-based function, allowing the hook to be used asynchronously.
43
58
  *
@@ -48,7 +63,8 @@ export declare function useAsPromise<P, T, E>(hook: UnaryFunctionHook<P, T, E>,
48
63
  * @param {BinaryHookOptions<P, B>} [options] Optional configuration for the binary hook.
49
64
  * @return {[(body: B, params: P) => Promise<never>, () => void]} A tuple where the first element is a function that returns a promise and the second is a cleanup function.
50
65
  */
51
- export declare function useAsPromise<P, B, E>(hook: BinaryProduceHook<P, B, E>, options?: BinaryHookOptions<P, B>): [(body: B, params: P) => Promise<never>, () => void];
66
+ export function useAsPromise<P, B, E>(hook: BinaryProduceHook<P, B, E>, options?: BinaryHookOptions<P, B>): [(body: B, params: P) => Promise<never>, () => void];
67
+
52
68
  /**
53
69
  * Wraps a binary function hook into a promise-based interface.
54
70
  *
@@ -56,4 +72,39 @@ export declare function useAsPromise<P, B, E>(hook: BinaryProduceHook<P, B, E>,
56
72
  * @param {BinaryHookOptions<P, B>} [options] Optional configuration options for the hook.
57
73
  * @return {[function(B, P): Promise<T>, function(): void]} Returns a tuple containing a function that invokes the hook and returns a promise, and a cleanup function to dispose of the hook's resources.
58
74
  */
59
- export declare function useAsPromise<P, B, T, E>(hook: BinaryFunctionHook<P, B, T, E>, options?: BinaryHookOptions<P, B>): [(body: B, params: P) => Promise<T>, () => void];
75
+ export function useAsPromise<P, B, T, E>(hook: BinaryFunctionHook<P, B, T, E>, options?: BinaryHookOptions<P, B>): [(body: B, params: P) => Promise<T>, () => void];
76
+
77
+ // **Implementation**
78
+ export function useAsPromise<P, B, T, E>(
79
+ hook: IntrigHook<P, B, T, E>,
80
+ options?: IntrigHookOptions<P, B>
81
+ ): [(...args: any[]) => Promise<T>, () => void] { // <- Compatible return type
82
+ const resolveRef = useRef<(value: T) => void>();
83
+ const rejectRef = useRef<(reason?: any) => void>();
84
+
85
+ const [state, dispatch, clear] = hook(options as any); // Casting to `any` to match all overloads
86
+
87
+ useEffect(() => {
88
+ if (isSuccess(state)) {
89
+ resolveRef.current?.(state.data);
90
+ clear();
91
+ } else if (isError(state)) {
92
+ rejectRef.current?.(state.error);
93
+ clear();
94
+ }
95
+ }, [state]);
96
+
97
+ const promiseFn = useCallback((...args: any[]) => {
98
+ return new Promise<T>((resolve, reject) => {
99
+ resolveRef.current = resolve;
100
+ rejectRef.current = reject;
101
+
102
+ const dispatchState = (dispatch as any)(...args);
103
+ if (isValidationError(dispatchState)) {
104
+ reject(dispatchState.error);
105
+ }
106
+ });
107
+ }, [dispatch]);
108
+
109
+ return [promiseFn, clear];
110
+ }
@@ -1,4 +1,15 @@
1
- import { BinaryFunctionHook, BinaryHookOptions, BinaryProduceHook, ConstantHook, UnaryFunctionHook, UnaryHookOptions, UnaryProduceHook, UnitHook, UnitHookOptions } from '@intrig/react/network-state';
1
+ import {
2
+ BinaryFunctionHook,
3
+ BinaryHookOptions,
4
+ BinaryProduceHook,
5
+ ConstantHook, IntrigHook, IntrigHookOptions, isSuccess, UnaryFunctionHook,
6
+ UnaryHookOptions,
7
+ UnaryProduceHook,
8
+ UnitHook,
9
+ UnitHookOptions
10
+ } from '@intrig/react/network-state';
11
+ import { useEffect, useState } from 'react';
12
+
2
13
  /**
3
14
  * Resolves and caches the computed value of a given hook.
4
15
  *
@@ -6,7 +17,8 @@ import { BinaryFunctionHook, BinaryHookOptions, BinaryProduceHook, ConstantHook,
6
17
  * @param {UnitHookOptions} options - Options to customize the behavior of the hook resolution and caching process.
7
18
  * @return {undefined} This function does not return a value.
8
19
  */
9
- export declare function useResolvedCachedValue<E>(hook: UnitHook<E>, options: UnitHookOptions): undefined;
20
+ export function useResolvedCachedValue<E>(hook: UnitHook<E>, options: UnitHookOptions): undefined;
21
+
10
22
  /**
11
23
  * Resolves and retrieves a cached value by utilizing a specified hook and options.
12
24
  *
@@ -14,7 +26,8 @@ export declare function useResolvedCachedValue<E>(hook: UnitHook<E>, options: Un
14
26
  * @param {UnitHookOptions} options - Options controlling the behavior or configuration of the hook.
15
27
  * @return {T | undefined} The resolved cached value or undefined if the value couldn't be determined.
16
28
  */
17
- export declare function useResolvedCachedValue<T, E>(hook: ConstantHook<T, E>, options: UnitHookOptions): T | undefined;
29
+ export function useResolvedCachedValue<T, E>(hook: ConstantHook<T, E>, options: UnitHookOptions): T | undefined;
30
+
18
31
  /**
19
32
  * Resolves and caches the value produced by the specified hook.
20
33
  * This utility helps in managing hook-driven computational results within a component context by caching
@@ -24,7 +37,8 @@ export declare function useResolvedCachedValue<T, E>(hook: ConstantHook<T, E>, o
24
37
  * @param {UnaryHookOptions<P>} options - The configuration options that control how the hook is executed.
25
38
  * @return {undefined} Returns undefined since the resolution and caching of the value are managed internally.
26
39
  */
27
- export declare function useResolvedCachedValue<P, E>(hook: UnaryProduceHook<P, E>, options: UnaryHookOptions<P>): undefined;
40
+ export function useResolvedCachedValue<P, E>(hook: UnaryProduceHook<P, E>, options: UnaryHookOptions<P>): undefined;
41
+
28
42
  /**
29
43
  * Resolves a cached value using the provided hook and options.
30
44
  *
@@ -33,7 +47,8 @@ export declare function useResolvedCachedValue<P, E>(hook: UnaryProduceHook<P, E
33
47
  * @param {UnaryHookOptions<P>} options - Configuration settings or arguments required by the hook to retrieve the cached value.
34
48
  * @return {T | undefined} A value of type T if resolved successfully, or undefined if not available.
35
49
  */
36
- export declare function useResolvedCachedValue<P, T, E>(hook: UnaryFunctionHook<P, T, E>, options: UnaryHookOptions<P>): T | undefined;
50
+ export function useResolvedCachedValue<P, T, E>(hook: UnaryFunctionHook<P, T, E>, options: UnaryHookOptions<P>): T | undefined;
51
+
37
52
  /**
38
53
  * Resolves and caches the value produced by the provided binary hook function.
39
54
  * This function ensures that the hook's resolved value is reused efficiently across invocations
@@ -43,7 +58,8 @@ export declare function useResolvedCachedValue<P, T, E>(hook: UnaryFunctionHook<
43
58
  * @param {BinaryHookOptions<P, B>} options - Configuration options for controlling the behavior of the hook and its caching mechanism.
44
59
  * @return {undefined} - Returns undefined as the resolution is managed internally via the hook's lifecycle.
45
60
  */
46
- export declare function useResolvedCachedValue<P, B, E>(hook: BinaryProduceHook<P, B, E>, options: BinaryHookOptions<P, B>): undefined;
61
+ export function useResolvedCachedValue<P, B, E>(hook: BinaryProduceHook<P, B, E>, options: BinaryHookOptions<P, B>): undefined;
62
+
47
63
  /**
48
64
  * A hook utility function that resolves and caches a value based on the given binary function hook and options.
49
65
  *
@@ -51,4 +67,20 @@ export declare function useResolvedCachedValue<P, B, E>(hook: BinaryProduceHook<
51
67
  * @param {BinaryHookOptions<P, B>} options - The options object containing parameters and configuration for the hook.
52
68
  * @return {T | undefined} The resolved and cached value, or undefined if it cannot be resolved.
53
69
  */
54
- export declare function useResolvedCachedValue<P, B, T, E>(hook: BinaryFunctionHook<P, B, T, E>, options: BinaryHookOptions<P, B>): T | undefined;
70
+ export function useResolvedCachedValue<P, B, T, E>(hook: BinaryFunctionHook<P, B, T, E>, options: BinaryHookOptions<P, B>): T | undefined;
71
+
72
+ // **Implementation**
73
+ export function useResolvedCachedValue<P, B, T, E>(hook: IntrigHook<P, B, T, E>, options: IntrigHookOptions<P, B>): T | undefined {
74
+ const [cachedValue, setCachedValue] = useState<T | undefined>();
75
+
76
+ const [state] = hook(options as any); // Ensure compatibility with different hook types
77
+
78
+ useEffect(() => {
79
+ if (isSuccess(state)) {
80
+ setCachedValue(state.data);
81
+ }
82
+ // Do not clear cached value if state is unsuccessful
83
+ }, [state]);
84
+
85
+ return cachedValue;
86
+ }
@@ -1,4 +1,15 @@
1
- import { BinaryFunctionHook, BinaryHookOptions, BinaryProduceHook, ConstantHook, UnaryFunctionHook, UnaryHookOptions, UnaryProduceHook, UnitHook, UnitHookOptions } from '@intrig/react/network-state';
1
+ import {
2
+ BinaryFunctionHook,
3
+ BinaryHookOptions,
4
+ BinaryProduceHook,
5
+ ConstantHook, IntrigHook, IntrigHookOptions, isSuccess, UnaryFunctionHook,
6
+ UnaryHookOptions,
7
+ UnaryProduceHook,
8
+ UnitHook,
9
+ UnitHookOptions
10
+ } from '@intrig/react/network-state';
11
+ import { useEffect, useState } from 'react';
12
+
2
13
  /**
3
14
  * A function that resolves the value provided by a unit hook based on specified options.
4
15
  *
@@ -6,7 +17,8 @@ import { BinaryFunctionHook, BinaryHookOptions, BinaryProduceHook, ConstantHook,
6
17
  * @param {UnitHookOptions} options - Configuration options to control how the unit hook is resolved.
7
18
  * @return {undefined} Returns undefined after resolving the unit hook value.
8
19
  */
9
- export declare function useResolvedValue<E>(hook: UnitHook<E>, options: UnitHookOptions): undefined;
20
+ export function useResolvedValue<E>(hook: UnitHook<E>, options: UnitHookOptions): undefined;
21
+
10
22
  /**
11
23
  * Resolves the value of a given constant hook based on the provided options.
12
24
  *
@@ -14,7 +26,7 @@ export declare function useResolvedValue<E>(hook: UnitHook<E>, options: UnitHook
14
26
  * @param {UnitHookOptions} options - The options used to configure the resolution of the hook value.
15
27
  * @return {T | undefined} The resolved value of the hook or undefined if the hook cannot be resolved.
16
28
  */
17
- export declare function useResolvedValue<T, E>(hook: ConstantHook<T, E>, options: UnitHookOptions): T | undefined;
29
+ export function useResolvedValue<T, E>(hook: ConstantHook<T, E>, options: UnitHookOptions): T | undefined;
18
30
  /**
19
31
  * This function is a utility hook that evaluates and resolves the value of the provided hook function.
20
32
  * It takes a hook function and its corresponding options as arguments to produce side effects or state updates.
@@ -23,7 +35,8 @@ export declare function useResolvedValue<T, E>(hook: ConstantHook<T, E>, options
23
35
  * @param {UnaryHookOptions<P>} options - Configuration options for the hook, such as parameters or behaviors.
24
36
  * @return {undefined} This function does not return a value; it operates through side effects or resolves implicitly.
25
37
  */
26
- export declare function useResolvedValue<P, E>(hook: UnaryProduceHook<P, E>, options: UnaryHookOptions<P>): undefined;
38
+ export function useResolvedValue<P, E>(hook: UnaryProduceHook<P, E>, options: UnaryHookOptions<P>): undefined;
39
+
27
40
  /**
28
41
  * A custom hook that provides a resolved value based on the provided unary function hook and options.
29
42
  *
@@ -31,7 +44,8 @@ export declare function useResolvedValue<P, E>(hook: UnaryProduceHook<P, E>, opt
31
44
  * @param {UnaryHookOptions<P>} options - The configuration object containing parameters for the unary hook execution.
32
45
  * @return {T | undefined} The resolved value of type T if successful, or undefined if no value is resolved.
33
46
  */
34
- export declare function useResolvedValue<P, T, E>(hook: UnaryFunctionHook<P, T, E>, options: UnaryHookOptions<P>): T | undefined;
47
+ export function useResolvedValue<P, T, E>(hook: UnaryFunctionHook<P, T, E>, options: UnaryHookOptions<P>): T | undefined;
48
+
35
49
  /**
36
50
  * A custom hook that processes a binary produce hook and its options to produce a resolved value.
37
51
  *
@@ -39,7 +53,8 @@ export declare function useResolvedValue<P, T, E>(hook: UnaryFunctionHook<P, T,
39
53
  * @param {BinaryHookOptions<P, B>} options - The options provided to the binary hook for processing.
40
54
  * @return {undefined} Returns `undefined` after the hook is resolved.
41
55
  */
42
- export declare function useResolvedValue<P, B, E>(hook: BinaryProduceHook<P, B, E>, options: BinaryHookOptions<P, B>): undefined;
56
+ export function useResolvedValue<P, B, E>(hook: BinaryProduceHook<P, B, E>, options: BinaryHookOptions<P, B>): undefined;
57
+
43
58
  /**
44
59
  * Resolves a value based on the provided binary function hook and options.
45
60
  *
@@ -47,4 +62,21 @@ export declare function useResolvedValue<P, B, E>(hook: BinaryProduceHook<P, B,
47
62
  * @param {BinaryHookOptions<P, B>} options - Configuration options for the binary function hook.
48
63
  * @return {T | undefined} The resolved value of type T, or undefined if the resolution fails.
49
64
  */
50
- export declare function useResolvedValue<P, B, T, E>(hook: BinaryFunctionHook<P, B, T, E>, options: BinaryHookOptions<P, B>): T | undefined;
65
+ export function useResolvedValue<P, B, T, E>(hook: BinaryFunctionHook<P, B, T, E>, options: BinaryHookOptions<P, B>): T | undefined;
66
+
67
+ // **Implementation**
68
+ export function useResolvedValue<P, B, T, E>(hook: IntrigHook<P, B, T, E>, options: IntrigHookOptions<P, B>): T | undefined {
69
+ const [value, setValue] = useState<T | undefined>();
70
+
71
+ const [state] = hook(options as any); // Ensure compatibility with different hook types
72
+
73
+ useEffect(() => {
74
+ if (isSuccess(state)) {
75
+ setValue(state.data);
76
+ } else {
77
+ setValue(undefined);
78
+ }
79
+ }, [state]); // Add `state` to the dependency array to ensure updates
80
+
81
+ return value;
82
+ }
@@ -1,4 +1,4 @@
1
1
  export * from './intrig-provider';
2
2
  export * from './network-state';
3
- export * from './extra/index';
4
- export * from './media-type-utils';
3
+ export * from './extra/index'
4
+ export * from './media-type-utils'
@@ -0,0 +1,66 @@
1
+ import { NetworkAction, NetworkState } from '@intrig/react/network-state';
2
+ import { AxiosProgressEvent } from 'axios';
3
+ import { ZodSchema } from 'zod';
4
+ import { createContext, useContext } from 'react';
5
+ import { DefaultConfigs } from '@intrig/react/intrig-provider';
6
+
7
+ type GlobalState = Record<string, NetworkState>;
8
+
9
+ interface RequestType<T = any> {
10
+ method: 'get' | 'post' | 'put' | 'delete';
11
+ url: string;
12
+ headers?: Record<string, string>;
13
+ params?: Record<string, any>;
14
+ data?: any; // This allows transformations, while retaining flexibility.
15
+ originalData?: T; // Keeps track of the original data type.
16
+ onUploadProgress?: (event: AxiosProgressEvent) => void;
17
+ onDownloadProgress?: (event: AxiosProgressEvent) => void;
18
+ signal?: AbortSignal;
19
+ key: string;
20
+ source: string
21
+ }
22
+
23
+ /**
24
+ * Defines the ContextType interface for managing global state, dispatching actions,
25
+ * and holding a collection of Axios instances.
26
+ *
27
+ * @interface ContextType
28
+ * @property {GlobalState} state - The global state of the application.
29
+ * @property {React.Dispatch<NetworkAction<unknown>>} dispatch - The dispatch function to send network actions.
30
+ * @property {Record<string, AxiosInstance>} axios - A record of Axios instances for making HTTP requests.
31
+ */
32
+ export interface ContextType {
33
+ state: GlobalState;
34
+ filteredState: GlobalState;
35
+ dispatch: React.Dispatch<NetworkAction<unknown, unknown>>;
36
+ configs: DefaultConfigs;
37
+ execute: <T, E = unknown>(request: RequestType, dispatch: (state: NetworkState<T, E>) => void, schema: ZodSchema<T> | undefined, errorSchema: ZodSchema<E> | undefined) => Promise<void>;
38
+ }
39
+
40
+ /**
41
+ * Context object created using `createContext` function. Provides a way to share state, dispatch functions,
42
+ * and axios instance across components without having to pass props down manually at every level.
43
+ *
44
+ * @type {ContextType}
45
+ */
46
+ const Context = createContext<ContextType>({
47
+ state: {},
48
+ filteredState: {},
49
+ dispatch() {
50
+ //noop
51
+ },
52
+ configs: {},
53
+ async execute() {
54
+ //noop
55
+ }
56
+ });
57
+
58
+ export function useIntrigContext() {
59
+ return useContext(Context);
60
+ }
61
+
62
+ export {
63
+ Context,
64
+ GlobalState,
65
+ RequestType,
66
+ }