@intrig/react 1.0.7 → 1.0.11

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,178 +1,35 @@
1
- <p align="center">
2
- <img src="./docs/intrig-logo.svg" alt="Intrig Logo" width="200">
3
- </p>
1
+ # @intrig/react
4
2
 
5
- # Intrig
6
- ![npm](https://img.shields.io/npm/v/intrig)
7
- ![License](https://img.shields.io/github/license/intrigsoft/intrig)
8
- ![TypeScript](https://img.shields.io/github/package-json/dependency-version/intrigsoft/intrig/dev/typescript)
9
- ![React](https://img.shields.io/npm/dependency-version/@intrig/react/peer/react)
10
- ![Next.js](https://img.shields.io/npm/dependency-version/@intrig/next/peer/next)
11
-
12
- Intrig is a powerful API integration and documentation tool designed for frontend developers working with OpenAPI 3. It streamlines API consumption by generating boilerplate code, offering a rich documentation interface, and providing an intuitive way to explore and interact with API endpoints.
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.
13
4
 
14
5
  ## Features
15
6
 
16
- ### 🔹 API Documentation & Exploration
17
-
18
- - Generates structured documentation from OpenAPI 3 specifications.
19
- - Provides an interactive UI to explore API endpoints, including request and response types.
20
- - Enables developers to view version history of API definitions.
21
-
22
- ### 🔹 Code Generation
23
-
24
- - Generates strongly typed hooks for API calls, integrating seamlessly with React applications.
25
-
26
- - Provides hooks that follow React’s standard state management conventions, resembling `useState` syntax for simplicity and performance.
27
-
28
- ### 🔹 CLI Tooling
29
-
30
- - Allows automation of API integration workflows.
31
- - Supports syncing remote OpenAPI specifications.
32
- - Provides an easy way to manage API configurations.
33
-
34
-
35
-
36
- ## Installation
37
-
38
- Intrig CLI is published to npx. When you run `npx intrig init`, it adds necessary dependencies to the project, such as `@intrig/next` for Next.js projects or `@intrig/react` for React projects. It also adds `intrig` as a dev dependency to the project.
39
-
40
- ```sh
41
- npx intrig init
42
- ```
43
-
44
- ## Usage
45
-
46
- ### CLI Usage
47
-
48
- Intrig provides a command-line interface for managing API integrations efficiently. Below are the available commands:
49
-
50
- #### `init`
51
-
52
- Initializes a new Intrig configuration and installs necessary dependencies.
53
-
54
- ```sh
55
- npx intrig init
56
- ```
57
-
58
- #### `add`
59
-
60
- Adds a new API source to the Intrig configuration.
61
-
62
- ```sh
63
- intrig add [-s <value>] [-i <value>]
64
- ```
65
-
66
- - `-i, --id=<value>`: Unique ID for the API.
67
- - `-s, --source=<value>`: OpenAPI specification URL.
68
-
69
- #### `ls`
70
-
71
- Lists API sources from the Intrig configuration.
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.
72
9
 
73
- ```sh
74
- intrig ls [-d] [-f <value>]
75
- ```
76
-
77
- - `-d, --detailed`: Show detailed information.
78
- - `-f, --filter=<value>`: Filter sources by name or ID.
79
-
80
- #### `baseline`
81
-
82
- Creates a baseline version for API specifications.
83
-
84
- ```sh
85
- intrig baseline -i <value> -v <value> [-f]
86
- ```
87
-
88
- - `-i, --id=<value>`: API ID to baseline.
89
- - `-v, --version=<value>`: Version for the baseline.
90
- - `-f, --force`: Force baseline if version exists.
10
+ ## Getting Started
91
11
 
92
- #### `sync`
12
+ To start using @intrig/react, simply install the package in your React project:
93
13
 
94
- Synchronizes API specifications.
95
-
96
- ```sh
97
- intrig sync [-a | -i <value>...]
14
+ ```bash
15
+ npm install @intrig/react
98
16
  ```
99
17
 
100
- - `-a, --all`: Sync all APIs.
101
- - `-i, --ids=<value>...`: Comma-separated list of API IDs to sync.
102
-
103
- #### `generate`
104
-
105
- Regenerates code for API integration.
106
-
107
- ```sh
108
- intrig generate [-a | -i <value>...]
109
- ```
110
-
111
- - `-a, --all`: Sync all APIs.
112
- - `-i, --ids=<value>...`: Comma-separated list of API IDs to sync.
113
-
114
- #### `rm`
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.
115
19
 
116
- Removes an API source from the Intrig configuration.
117
-
118
- ```sh
119
- intrig rm [SOURCE]
120
- ```
121
-
122
- - `SOURCE`: ID of the source to remove.
123
-
124
- #### `insight`
125
-
126
- Starts the web view for Intrig insight.
127
-
128
- ```sh
129
- intrig insight [-p <value>] [-d]
130
- ```
131
-
132
- - `-p, --port=<value>`: Port for Next.js server (default: 7007).
133
- - `-d, --debug`: Enable debug mode for server-side logs.
134
-
135
- ## Configuration
136
-
137
- Intrig supports project-level configurations via `intrig.config.json`:
138
-
139
- ```json
140
- {
141
- "sources": [
142
- {
143
- "id": "example-api",
144
- "name": "Example API Definition",
145
- "specUrl": "https://example.com/api/openapi.json"
146
- },
147
- {
148
- "id": "analytics",
149
- "name": "Analytics API",
150
- "specUrl": "https://api.analytics.com/v3/openapi.json"
151
- }
152
- ],
153
- "generator": "next"
154
- }
155
- ```
20
+ ## Contributing
156
21
 
157
- - **`sources`**: Defines the API sources available for integration.
158
- - `id`: A unique identifier for the API.
159
- - `name`: A human-readable name for the API.
160
- - `specUrl`: The URL of the OpenAPI 3 specification.
161
- - **`generator`**: Specifies the type of code generation, such as `next` for Next.js projects.
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.
162
23
 
163
- ## Roadmap
24
+ ## License
164
25
 
165
- - OpenAPI-based documentation and code generation
166
- - ✅ CLI tool for API exploration and integration
167
- - ⏳ Streamline file download feature
168
- - ⏳ Remix integration
169
- - ⏳ Vue.js integration
26
+ @intrig/react is licensed under the MIT License. See the [LICENSE](https://github.com/intrigsoft/intrig/blob/main/LICENSE) file for more information.
170
27
 
171
- ## Contributing
28
+ ## Links
172
29
 
173
- We welcome contributions! Please check our issues board and submit pull requests.
30
+ - [Main GitHub Repository](https://github.com/intrigsoft/intrig)
174
31
 
175
- ## License
32
+ ## Support
176
33
 
177
- Apache License 2.0. See `LICENSE` file for details.
34
+ For questions, suggestions, or assistance, please open an issue on the [GitHub repository](https://github.com/intrigsoft/intrig/issues).
178
35
 
@@ -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.7",
3
+ "version": "1.0.11",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "private": false,
@@ -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: string = 'default'): [NetworkState<T>, (...params: Parameters<F>) => void, () => void] {
17
+ let id = useId();
18
+
19
+ let 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
+ let [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
+ let 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
+ let [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
+ let [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'