@intrig/react 0.0.1 → 1.0.6

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/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "name": "@intrig/react",
3
- "version": "0.0.1",
3
+ "version": "1.0.6",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "private": false,
7
7
  "exports": {
8
8
  ".": {
9
- "import": "./index.mjs",
10
- "require": "./index.js"
9
+ "import": "./src/index.js",
10
+ "types": "./src/index.d.ts"
11
+ },
12
+ "./*": {
13
+ "import": "./src/*.js",
14
+ "types": "./src/*.d.ts"
11
15
  }
12
16
  },
13
17
  "dependencies": {
@@ -0,0 +1,4 @@
1
+ export * from './useResolvedValue';
2
+ export * from './useResolvedCachedValue';
3
+ export * from './useAsPromise';
4
+ export * from './useAsNetworkState';
@@ -0,0 +1,13 @@
1
+ import { NetworkState } from '@intrig/react/network-state';
2
+ /**
3
+ * A custom hook that integrates a promise-based operation with a network state management system.
4
+ * Tracks the network state (e.g., pending, success, error) for a given asynchronous function.
5
+ *
6
+ * @param fn A promise-based function that performs an asynchronous operation.
7
+ * @param key An optional string key to identify the network state uniquely. Defaults to 'default'.
8
+ * @return A tuple containing:
9
+ * 1. The current network state of the operation.
10
+ * 2. A function to execute the provided asynchronous operation.
11
+ * 3. A function to reset the network state back to the initial state.
12
+ */
13
+ export declare function useAsNetworkState<T, F extends ((...args: any) => Promise<T>)>(fn: F, key?: string): [NetworkState<T>, (...params: Parameters<F>) => void, () => void];
@@ -0,0 +1,59 @@
1
+ import { BinaryFunctionHook, BinaryHookOptions, BinaryProduceHook, ConstantHook, UnaryFunctionHook, UnaryHookOptions, UnaryProduceHook, UnitHook, UnitHookOptions } from '@intrig/react/network-state';
2
+ /**
3
+ * Transforms a unit hook into a promise, enabling asynchronous usage of the hook's lifecycle or events.
4
+ *
5
+ * @param {UnitHook<E>} hook - The unit hook to be used as a promise.
6
+ * @param {UnitHookOptions} options - Configuration options for the unit hook.
7
+ * @return {[() => Promise<never>, () => void]} A tuple containing a function that returns a promise resolving with the hook's completion
8
+ * result or rejecting with an error, and a function to clean up the hook's resources when no longer needed.
9
+ */
10
+ export declare function useAsPromise<E>(hook: UnitHook<E>, options: UnitHookOptions): [() => Promise<never>, () => void];
11
+ /**
12
+ * Converts a constant hook into a promise-based interface that can be invoked and reset.
13
+ *
14
+ * @param {ConstantHook<T, E>} hook - The constant hook to be converted.
15
+ * @param {UnitHookOptions} options - Configuration options for the hook behavior.
16
+ * @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
+ */
18
+ export declare function useAsPromise<T, E>(hook: ConstantHook<T, E>, options: UnitHookOptions): [() => Promise<T>, () => void];
19
+ /**
20
+ * Wraps the provided hook function to return a promise-based API, offering more flexibility
21
+ * in asynchronous operations. This utility converts the hook into a callable function that
22
+ * returns a `Promise` and provides a way to clean up resources.
23
+ *
24
+ * @param {UnaryProduceHook<P, E>} hook - The hook function to be wrapped into a promise-based API.
25
+ * @param {UnaryHookOptions<P>} [options] - Optional parameters to configure the behavior of the hook.
26
+ * @return {[function(P): Promise<never>, function(): void]} Returns a tuple where the first element
27
+ * is a function that takes the hook's parameters and returns a `Promise`, and the second element
28
+ * is a cleanup function for resource management.
29
+ */
30
+ export declare function useAsPromise<P, E>(hook: UnaryProduceHook<P, E>, options?: UnaryHookOptions<P>): [(params: P) => Promise<never>, () => void];
31
+ /**
32
+ * Allows the usage of a hook's asynchronous behavior as a Promise.
33
+ *
34
+ * @param {UnaryFunctionHook<P, T, E>} hook - The hook function to be used as a Promise.
35
+ * @param {UnaryHookOptions<P>} [options] - Optional configuration object for the hook.
36
+ * @return {[function(P): Promise<T>, function(): void]} A tuple containing:
37
+ * - A function that accepts parameters and returns a Promise resolving to the hook's result.
38
+ * - A cleanup function to properly dispose of the hook when no longer needed.
39
+ */
40
+ export declare function useAsPromise<P, T, E>(hook: UnaryFunctionHook<P, T, E>, options?: UnaryHookOptions<P>): [(params: P) => Promise<T>, () => void];
41
+ /**
42
+ * Converts a binary hook into a promise-based function, allowing the hook to be used asynchronously.
43
+ *
44
+ * @template P The type of the parameters for the hook.
45
+ * @template B The type of the body for the hook.
46
+ * @template E The type of the error handled by the hook.
47
+ * @param {BinaryProduceHook<P, B, E>} hook The binary hook to be converted into a promise-based function.
48
+ * @param {BinaryHookOptions<P, B>} [options] Optional configuration for the binary hook.
49
+ * @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
+ */
51
+ export declare function useAsPromise<P, B, E>(hook: BinaryProduceHook<P, B, E>, options?: BinaryHookOptions<P, B>): [(body: B, params: P) => Promise<never>, () => void];
52
+ /**
53
+ * Wraps a binary function hook into a promise-based interface.
54
+ *
55
+ * @param {BinaryFunctionHook<P, B, T, E>} hook The binary function hook to be wrapped.
56
+ * @param {BinaryHookOptions<P, B>} [options] Optional configuration options for the hook.
57
+ * @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
+ */
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];
@@ -0,0 +1,54 @@
1
+ import { BinaryFunctionHook, BinaryHookOptions, BinaryProduceHook, ConstantHook, UnaryFunctionHook, UnaryHookOptions, UnaryProduceHook, UnitHook, UnitHookOptions } from '@intrig/react/network-state';
2
+ /**
3
+ * Resolves and caches the computed value of a given hook.
4
+ *
5
+ * @param {UnitHook<E>} hook - The hook to be resolved and cached. Represents a unit of work or computation.
6
+ * @param {UnitHookOptions} options - Options to customize the behavior of the hook resolution and caching process.
7
+ * @return {undefined} This function does not return a value.
8
+ */
9
+ export declare function useResolvedCachedValue<E>(hook: UnitHook<E>, options: UnitHookOptions): undefined;
10
+ /**
11
+ * Resolves and retrieves a cached value by utilizing a specified hook and options.
12
+ *
13
+ * @param {ConstantHook<T, E>} hook - The hook function used to compute or retrieve the cached value.
14
+ * @param {UnitHookOptions} options - Options controlling the behavior or configuration of the hook.
15
+ * @return {T | undefined} The resolved cached value or undefined if the value couldn't be determined.
16
+ */
17
+ export declare function useResolvedCachedValue<T, E>(hook: ConstantHook<T, E>, options: UnitHookOptions): T | undefined;
18
+ /**
19
+ * Resolves and caches the value produced by the specified hook.
20
+ * This utility helps in managing hook-driven computational results within a component context by caching
21
+ * and avoiding unnecessary recalculations when the inputs remain the same.
22
+ *
23
+ * @param {UnaryProduceHook<P, E>} hook - The hook function to execute, responsible for producing a resolved value.
24
+ * @param {UnaryHookOptions<P>} options - The configuration options that control how the hook is executed.
25
+ * @return {undefined} Returns undefined since the resolution and caching of the value are managed internally.
26
+ */
27
+ export declare function useResolvedCachedValue<P, E>(hook: UnaryProduceHook<P, E>, options: UnaryHookOptions<P>): undefined;
28
+ /**
29
+ * Resolves a cached value using the provided hook and options.
30
+ *
31
+ * @param {UnaryFunctionHook<P, T, E>} hook - A hook function that accepts parameters of type P,
32
+ * processes them, and returns a value of type T or possibly throws an error of type E.
33
+ * @param {UnaryHookOptions<P>} options - Configuration settings or arguments required by the hook to retrieve the cached value.
34
+ * @return {T | undefined} A value of type T if resolved successfully, or undefined if not available.
35
+ */
36
+ export declare function useResolvedCachedValue<P, T, E>(hook: UnaryFunctionHook<P, T, E>, options: UnaryHookOptions<P>): T | undefined;
37
+ /**
38
+ * Resolves and caches the value produced by the provided binary hook function.
39
+ * This function ensures that the hook's resolved value is reused efficiently across invocations
40
+ * based on the provided options.
41
+ *
42
+ * @param {BinaryProduceHook<P, B, E>} hook - A hook function that produces the binary value to be resolved and cached.
43
+ * @param {BinaryHookOptions<P, B>} options - Configuration options for controlling the behavior of the hook and its caching mechanism.
44
+ * @return {undefined} - Returns undefined as the resolution is managed internally via the hook's lifecycle.
45
+ */
46
+ export declare function useResolvedCachedValue<P, B, E>(hook: BinaryProduceHook<P, B, E>, options: BinaryHookOptions<P, B>): undefined;
47
+ /**
48
+ * A hook utility function that resolves and caches a value based on the given binary function hook and options.
49
+ *
50
+ * @param {BinaryFunctionHook<P, B, T, E>} hook - The binary function hook used to compute the value.
51
+ * @param {BinaryHookOptions<P, B>} options - The options object containing parameters and configuration for the hook.
52
+ * @return {T | undefined} The resolved and cached value, or undefined if it cannot be resolved.
53
+ */
54
+ export declare function useResolvedCachedValue<P, B, T, E>(hook: BinaryFunctionHook<P, B, T, E>, options: BinaryHookOptions<P, B>): T | undefined;
@@ -0,0 +1,50 @@
1
+ import { BinaryFunctionHook, BinaryHookOptions, BinaryProduceHook, ConstantHook, UnaryFunctionHook, UnaryHookOptions, UnaryProduceHook, UnitHook, UnitHookOptions } from '@intrig/react/network-state';
2
+ /**
3
+ * A function that resolves the value provided by a unit hook based on specified options.
4
+ *
5
+ * @param {UnitHook<E>} hook - The unit hook function that provides the value to be resolved.
6
+ * @param {UnitHookOptions} options - Configuration options to control how the unit hook is resolved.
7
+ * @return {undefined} Returns undefined after resolving the unit hook value.
8
+ */
9
+ export declare function useResolvedValue<E>(hook: UnitHook<E>, options: UnitHookOptions): undefined;
10
+ /**
11
+ * Resolves the value of a given constant hook based on the provided options.
12
+ *
13
+ * @param {ConstantHook<T, E>} hook - The constant hook to be resolved.
14
+ * @param {UnitHookOptions} options - The options used to configure the resolution of the hook value.
15
+ * @return {T | undefined} The resolved value of the hook or undefined if the hook cannot be resolved.
16
+ */
17
+ export declare function useResolvedValue<T, E>(hook: ConstantHook<T, E>, options: UnitHookOptions): T | undefined;
18
+ /**
19
+ * This function is a utility hook that evaluates and resolves the value of the provided hook function.
20
+ * It takes a hook function and its corresponding options as arguments to produce side effects or state updates.
21
+ *
22
+ * @param {UnaryProduceHook<P, E>} hook - A hook function that is responsible for creating or updating a state or effect.
23
+ * @param {UnaryHookOptions<P>} options - Configuration options for the hook, such as parameters or behaviors.
24
+ * @return {undefined} This function does not return a value; it operates through side effects or resolves implicitly.
25
+ */
26
+ export declare function useResolvedValue<P, E>(hook: UnaryProduceHook<P, E>, options: UnaryHookOptions<P>): undefined;
27
+ /**
28
+ * A custom hook that provides a resolved value based on the provided unary function hook and options.
29
+ *
30
+ * @param {UnaryFunctionHook<P, T, E>} hook - The primary unary function hook that processes the input and resolves a value.
31
+ * @param {UnaryHookOptions<P>} options - The configuration object containing parameters for the unary hook execution.
32
+ * @return {T | undefined} The resolved value of type T if successful, or undefined if no value is resolved.
33
+ */
34
+ export declare function useResolvedValue<P, T, E>(hook: UnaryFunctionHook<P, T, E>, options: UnaryHookOptions<P>): T | undefined;
35
+ /**
36
+ * A custom hook that processes a binary produce hook and its options to produce a resolved value.
37
+ *
38
+ * @param {BinaryProduceHook<P, B, E>} hook - The binary produce hook to be resolved.
39
+ * @param {BinaryHookOptions<P, B>} options - The options provided to the binary hook for processing.
40
+ * @return {undefined} Returns `undefined` after the hook is resolved.
41
+ */
42
+ export declare function useResolvedValue<P, B, E>(hook: BinaryProduceHook<P, B, E>, options: BinaryHookOptions<P, B>): undefined;
43
+ /**
44
+ * Resolves a value based on the provided binary function hook and options.
45
+ *
46
+ * @param {BinaryFunctionHook<P, B, T, E>} hook - The binary function hook that defines the logic to resolve the value.
47
+ * @param {BinaryHookOptions<P, B>} options - Configuration options for the binary function hook.
48
+ * @return {T | undefined} The resolved value of type T, or undefined if the resolution fails.
49
+ */
50
+ export declare function useResolvedValue<P, B, T, E>(hook: BinaryFunctionHook<P, B, T, E>, options: BinaryHookOptions<P, B>): T | undefined;
package/src/index.d.ts CHANGED
@@ -1,2 +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';
@@ -28,9 +28,9 @@ interface RequestType<T = any> {
28
28
  export interface ContextType {
29
29
  state: GlobalState;
30
30
  filteredState: GlobalState;
31
- dispatch: React.Dispatch<NetworkAction<unknown>>;
31
+ dispatch: React.Dispatch<NetworkAction<unknown, unknown>>;
32
32
  configs: DefaultConfigs;
33
- execute: <T>(request: RequestType, dispatch: (state: NetworkState<T>) => void, schema: ZodSchema<T> | undefined) => Promise<void>;
33
+ execute: <T, E = unknown>(request: RequestType, dispatch: (state: NetworkState<T, E>) => void, schema: ZodSchema<T> | undefined, errorSchema: ZodSchema<E> | undefined) => Promise<void>;
34
34
  }
35
35
  /**
36
36
  * Context object created using `createContext` function. Provides a way to share state, dispatch functions,
@@ -48,11 +48,12 @@ export interface StatusTrapProps {
48
48
  * @return {React.ReactElement} The context provider component with filtered state and custom dispatch.
49
49
  */
50
50
  export declare function StatusTrap({ children, type, propagate, }: PropsWithChildren<StatusTrapProps>): import("react/jsx-runtime").JSX.Element;
51
- export interface NetworkStateProps<T> {
51
+ export interface NetworkStateProps<T, E = unknown> {
52
52
  key: string;
53
53
  operation: string;
54
54
  source: string;
55
55
  schema?: ZodSchema<T>;
56
+ errorSchema?: ZodSchema<E>;
56
57
  debounceDelay?: number;
57
58
  }
58
59
  /**
@@ -71,23 +72,23 @@ export interface NetworkStateProps<T> {
71
72
  * Returns a state object representing the current network state,
72
73
  * a function to execute the network request, and a function to clear the request.
73
74
  */
74
- export declare function useNetworkState<T>({ key, operation, source, schema, debounceDelay: requestDebounceDelay, }: NetworkStateProps<T>): [
75
- NetworkState<T>,
75
+ export declare function useNetworkState<T, E = unknown>({ key, operation, source, schema, errorSchema, debounceDelay: requestDebounceDelay, }: NetworkStateProps<T>): [
76
+ NetworkState<T, E>,
76
77
  (request: RequestType) => void,
77
78
  clear: () => void,
78
- (state: NetworkState<T>) => void
79
+ (state: NetworkState<T, E>) => void
79
80
  ];
80
81
  /**
81
82
  * Handles central error extraction from the provided context.
82
83
  * It filters the state to retain error states and maps them to a structured error object with additional context information.
83
84
  * @return {Object[]} An array of objects representing the error states with context information such as source, operation, and key.
84
85
  */
85
- export declare function useCentralErrorHandling(): {
86
+ export declare function useCentralError(): {
86
87
  source: string;
87
88
  operation: string;
88
89
  key: string;
89
90
  state: "error";
90
- error: any;
91
+ error: unknown;
91
92
  statusCode?: number;
92
93
  request?: any;
93
94
  }[];
@@ -97,4 +98,4 @@ export declare function useCentralErrorHandling(): {
97
98
  *
98
99
  * @return {NetworkState} The aggregated network state based on the pending states and their progress.
99
100
  */
100
- export declare function useCentralPendingStateHandling(): NetworkState<unknown>;
101
+ export declare function useCentralPendingState(): NetworkState<unknown, unknown>;
@@ -0,0 +1,3 @@
1
+ import { ZodSchema } from 'zod';
2
+ export declare function transform<T>(request: Request, mediaType: string, schema: ZodSchema): Promise<T>;
3
+ export declare function transformResponse<T>(data: any, mediaType: string, schema: ZodSchema): Promise<T>;
@@ -1,3 +1,4 @@
1
+ import { ZodError } from 'zod';
1
2
  /**
2
3
  * State of an asynchronous call. Network state follows the state diagram given below.
3
4
  *
@@ -21,31 +22,31 @@
21
22
  *
22
23
  * </pre>
23
24
  */
24
- export interface NetworkState<T = unknown> {
25
+ export interface NetworkState<T = unknown, E = unknown> {
25
26
  state: 'init' | 'pending' | 'success' | 'error';
26
27
  }
27
28
  /**
28
29
  * Network call is not yet started
29
30
  */
30
- export interface InitState<T> extends NetworkState<T> {
31
+ export interface InitState<T, E = unknown> extends NetworkState<T, E> {
31
32
  state: 'init';
32
33
  }
33
34
  /**
34
35
  * Checks whether the state is init state
35
36
  * @param state
36
37
  */
37
- export declare function isInit<T>(state: NetworkState<T>): state is InitState<T>;
38
+ export declare function isInit<T, E = unknown>(state: NetworkState<T, E>): state is InitState<T, E>;
38
39
  /**
39
40
  * Initializes a new state.
40
41
  *
41
42
  * @template T The type of the state.
42
43
  * @return {InitState<T>} An object representing the initial state.
43
44
  */
44
- export declare function init<T>(): InitState<T>;
45
+ export declare function init<T, E = unknown>(): InitState<T, E>;
45
46
  /**
46
47
  * Network call is not yet completed
47
48
  */
48
- export interface PendingState<T> extends NetworkState<T> {
49
+ export interface PendingState<T, E = unknown> extends NetworkState<T, E> {
49
50
  state: 'pending';
50
51
  progress?: Progress;
51
52
  }
@@ -69,17 +70,17 @@ export interface Progress {
69
70
  * Checks whether the state is pending state
70
71
  * @param state
71
72
  */
72
- export declare function isPending<T>(state: NetworkState<T>): state is PendingState<T>;
73
+ export declare function isPending<T, E = unknown>(state: NetworkState<T, E>): state is PendingState<T, E>;
73
74
  /**
74
75
  * Generates a PendingState object with a state of "pending".
75
76
  *
76
77
  * @return {PendingState<T>} An object representing the pending state.
77
78
  */
78
- export declare function pending<T>(progress?: Progress | undefined): PendingState<T>;
79
+ export declare function pending<T, E = unknown>(progress?: Progress | undefined): PendingState<T, E>;
79
80
  /**
80
81
  * Network call is completed with success state
81
82
  */
82
- export interface SuccessState<T> extends NetworkState<T> {
83
+ export interface SuccessState<T, E = unknown> extends NetworkState<T, E> {
83
84
  state: 'success';
84
85
  data: T;
85
86
  }
@@ -87,20 +88,20 @@ export interface SuccessState<T> extends NetworkState<T> {
87
88
  * Checks whether the state is success response
88
89
  * @param state
89
90
  */
90
- export declare function isSuccess<T>(state: NetworkState<T>): state is SuccessState<T>;
91
+ export declare function isSuccess<T, E = unknown>(state: NetworkState<T, E>): state is SuccessState<T, E>;
91
92
  /**
92
93
  * Creates a success state object with the provided data.
93
94
  *
94
95
  * @param {T} data - The data to be included in the success state.
95
96
  * @return {SuccessState<T>} An object representing a success state containing the provided data.
96
97
  */
97
- export declare function success<T>(data: T): SuccessState<T>;
98
+ export declare function success<T, E = unknown>(data: T): SuccessState<T, E>;
98
99
  /**
99
100
  * Network call is completed with error response
100
101
  */
101
- export interface ErrorState<T> extends NetworkState<T> {
102
+ export interface ErrorState<T, E = unknown> extends NetworkState<T, E> {
102
103
  state: 'error';
103
- error: any;
104
+ error: E;
104
105
  statusCode?: number;
105
106
  request?: any;
106
107
  }
@@ -108,7 +109,7 @@ export interface ErrorState<T> extends NetworkState<T> {
108
109
  * Checks whether the state is error state
109
110
  * @param state
110
111
  */
111
- export declare function isError<T>(state: NetworkState<T>): state is ErrorState<T>;
112
+ export declare function isError<T, E = unknown>(state: NetworkState<T, E>): state is ErrorState<T, E>;
112
113
  /**
113
114
  * Constructs an ErrorState object representing an error.
114
115
  *
@@ -116,7 +117,7 @@ export declare function isError<T>(state: NetworkState<T>): state is ErrorState<
116
117
  * @param {string} [statusCode] - An optional status code associated with the error.
117
118
  * @return {ErrorState<T>} An object representing the error state.
118
119
  */
119
- export declare function error<T>(error: any, statusCode?: number, request?: any): ErrorState<T>;
120
+ export declare function error<T, E = unknown>(error: E, statusCode?: number, request?: any): ErrorState<T>;
120
121
  /**
121
122
  * Represents an error state with additional contextual information.
122
123
  *
@@ -128,7 +129,7 @@ export declare function error<T>(error: any, statusCode?: number, request?: any)
128
129
  * @property {string} operation - The operation being performed when the error occurred.
129
130
  * @property {string} key - A unique key identifying the specific error instance.
130
131
  */
131
- export interface ErrorWithContext<T = unknown> extends ErrorState<T> {
132
+ export interface ErrorWithContext<T = unknown, E = unknown> extends ErrorState<T, E> {
132
133
  source: string;
133
134
  operation: string;
134
135
  key: string;
@@ -141,25 +142,55 @@ export interface ErrorWithContext<T = unknown> extends ErrorState<T> {
141
142
  * @property {NetworkState<any>} state - The current state of the network action
142
143
  * @property {string} key - The unique identifier for the network action
143
144
  */
144
- export interface NetworkAction<T> {
145
+ export interface NetworkAction<T, E> {
145
146
  key: string;
146
147
  source: string;
147
148
  operation: string;
148
- state: NetworkState<T>;
149
+ state: NetworkState<T, E>;
149
150
  handled?: boolean;
150
151
  }
151
152
  type HookWithKey = {
152
153
  key: string;
153
154
  };
154
- export type DeleteHook<P> = ((key?: string) => [NetworkState<never>, (params: P) => void, () => void]) & HookWithKey;
155
- export type DeleteHookOp<P> = ((key?: string) => [NetworkState<never>, (params?: P) => void, () => void]) & HookWithKey;
156
- export type GetHook<P, T> = ((key?: string) => [NetworkState<T>, (params: P) => void, () => void]) & HookWithKey;
157
- export type GetHookOp<P, T> = ((key?: string) => [NetworkState<T>, (params?: P) => void, () => void]) & HookWithKey;
158
- export type PostHook<P, T, B> = ((key?: string) => [NetworkState<T>, (body: B, params: P) => void, () => void]) & HookWithKey;
159
- export type PostHookOp<P, T, B> = ((key?: string) => [NetworkState<T>, (body: B, params?: P) => void, () => void]) & HookWithKey;
160
- export type PutHook<P, T, B> = ((key?: string) => [NetworkState<T>, (body: B, params: P) => void, () => void]) & HookWithKey;
161
- export type PutHookOp<P, T, B> = ((key?: string) => [NetworkState<T>, (body: B, params?: P) => void, () => void]) & HookWithKey;
162
- export type IntrigHook<P = undefined, B = undefined, T = any> = DeleteHook<P> | GetHook<P, T> | PostHook<P, T, B> | PutHook<P, T, B> | PostHookOp<P, T, B> | PutHookOp<P, T, B> | GetHookOp<P, T> | DeleteHookOp<P>;
155
+ export type UnitHookOptions = {
156
+ key?: string;
157
+ fetchOnMount?: false;
158
+ clearOnUnmount?: boolean;
159
+ } | {
160
+ key?: string;
161
+ fetchOnMount: true;
162
+ params?: Record<string, any>;
163
+ clearOnUnmount?: boolean;
164
+ };
165
+ export type UnitHook<E = unknown> = ((options: UnitHookOptions) => [NetworkState<never, E>, (params?: Record<string, any>) => DispatchState<any>, () => void]) & HookWithKey;
166
+ export type ConstantHook<T, E = unknown> = ((options: UnitHookOptions) => [NetworkState<T, E>, (params?: Record<string, any>) => DispatchState<any>, () => void]) & HookWithKey;
167
+ export type UnaryHookOptions<P> = {
168
+ key?: string;
169
+ fetchOnMount?: false;
170
+ clearOnUnmount?: boolean;
171
+ } | {
172
+ key?: string;
173
+ fetchOnMount: true;
174
+ params: P;
175
+ clearOnUnmount?: boolean;
176
+ };
177
+ export type UnaryProduceHook<P, E = unknown> = ((options?: UnaryHookOptions<P>) => [NetworkState<never, E>, (params: P) => DispatchState<any>, () => void]) & HookWithKey;
178
+ export type UnaryFunctionHook<P, T, E = unknown> = ((options?: UnaryHookOptions<P>) => [NetworkState<T, E>, (params: P) => DispatchState<any>, () => void]) & HookWithKey;
179
+ export type BinaryHookOptions<P, B> = {
180
+ key?: string;
181
+ fetchOnMount?: false;
182
+ clearOnUnmount?: boolean;
183
+ } | {
184
+ key?: string;
185
+ fetchOnMount: true;
186
+ params: P;
187
+ body: B;
188
+ clearOnUnmount?: boolean;
189
+ };
190
+ export type BinaryProduceHook<P, B, E = unknown> = ((options?: BinaryHookOptions<P, B>) => [NetworkState<never, E>, (body: B, params: P) => DispatchState<any>, () => void]) & HookWithKey;
191
+ export type BinaryFunctionHook<P, B, T, E = unknown> = ((options?: BinaryHookOptions<P, B>) => [NetworkState<T, E>, (body: B, params: P) => DispatchState<any>, () => void]) & HookWithKey;
192
+ export type IntrigHookOptions<P = undefined, B = undefined> = UnitHookOptions | UnaryHookOptions<P> | BinaryHookOptions<P, B>;
193
+ export type IntrigHook<P = undefined, B = undefined, T = any, E = unknown> = UnitHook<E> | ConstantHook<T, E> | UnaryProduceHook<P, E> | UnaryFunctionHook<P, T, E> | BinaryProduceHook<P, B, E> | BinaryFunctionHook<P, B, T, E>;
163
194
  /**
164
195
  * Represents the dispatch state of a process.
165
196
  *
@@ -220,4 +251,111 @@ export declare function validationError<T>(error: any): ValidationError<T>;
220
251
  * @return {boolean} - Returns true if the provided DispatchState object is a ValidationError, otherwise returns false.
221
252
  */
222
253
  export declare function isValidationError<T>(value: DispatchState<T>): value is ValidationError<T>;
254
+ /**
255
+ * Represents an error structure with a specified type and associated data.
256
+ *
257
+ * @template T - The type of the data associated with the error.
258
+ * @template E - The type of the error detail.
259
+ *
260
+ * @property {string} type - A string representing the type of the error.
261
+ */
262
+ export interface IntrigError<T, E> {
263
+ type: string;
264
+ }
265
+ /**
266
+ * Represents an error encountered during a network operation.
267
+ * Extends from the `IntrigError` interface, adding network-specific properties.
268
+ *
269
+ * @template T - The type of the intrinsic data associated with the error.
270
+ * @template E - The type of the error details, defaulting to `unknown`.
271
+ *
272
+ * @property {string} type - A constant property representing the error type, always set to 'network'.
273
+ * @property {string} statusCode - A string representation of the HTTP status code associated with the error, indicating the nature of the network failure.
274
+ * @property {E} error - The detailed error information specific to the failure, type extends from the generic E, allowing flexibility in the error details.
275
+ * @property {any} request - The request object that was attempted when the network error occurred, providing context for what operation failed.
276
+ */
277
+ export interface NetworkError<T, E = unknown> extends IntrigError<T, E> {
278
+ type: 'network';
279
+ statusCode: string;
280
+ error: E;
281
+ request: any;
282
+ }
283
+ /**
284
+ * Constructs a network error object.
285
+ *
286
+ * @param error The error object corresponding to the network request.
287
+ * @param statusCode A string representing the HTTP status code returned.
288
+ * @param request The request object associated with the network operation.
289
+ * @return A NetworkError object containing the error type, status code, error details, and the original request.
290
+ */
291
+ export declare function networkError<T, E>(error: E, statusCode: string, request: any): NetworkError<T, E>;
292
+ /**
293
+ * Determines if the provided IntrigError is of type 'network'.
294
+ *
295
+ * @param {IntrigError<T, E>} value - The error value to check the type of.
296
+ * @return {boolean} - Returns true if the error is of type 'network', otherwise false.
297
+ */
298
+ export declare function isNetworkError<T, E>(value: IntrigError<T, E>): value is NetworkError<T, E>;
299
+ /**
300
+ * Interface representing a request validation error.
301
+ *
302
+ * This error occurs when a validation process on a request fails. It extends the IntrigError interface
303
+ * by adding specific properties related to request validation.
304
+ *
305
+ * @template T - The type of the data associated with the error.
306
+ * @template E - The optional type of additional error information. Defaults to unknown.
307
+ *
308
+ * @extends IntrigError<T, E>
309
+ *
310
+ * @property {string} type - A string literal indicating the error type as 'request-validation'.
311
+ * @property {ZodError} error - An instance of ZodError containing detailed validation error information.
312
+ */
313
+ export interface RequestValidationError<T, E = unknown> extends IntrigError<T, E> {
314
+ type: 'request-validation';
315
+ error: ZodError;
316
+ }
317
+ /**
318
+ * Constructs a RequestValidationError object encapsulating the ZodError.
319
+ *
320
+ * @param {ZodError} error - The error object resulting from Zod schema validation.
321
+ * @return {RequestValidationError<T, E>} A RequestValidationError object containing the validation error information.
322
+ */
323
+ export declare function requestValidationError<T, E>(error: ZodError): RequestValidationError<T, E>;
324
+ /**
325
+ * Determines if a given error is of type RequestValidationError.
326
+ *
327
+ * @param value The error object to check, which implements the IntrigError interface.
328
+ * @return A boolean indicating whether the error is a RequestValidationError.
329
+ */
330
+ export declare function isRequestValidationError<T, E>(value: IntrigError<T, E>): value is RequestValidationError<T, E>;
331
+ /**
332
+ * ResponseValidationError interface is designed to extend the capabilities of the IntrigError interface,
333
+ * specifically for handling errors related to response validation.
334
+ *
335
+ * @template T - Represents the type of the data or payload associated with the error.
336
+ * @template E - Represents the type of any additional error information. Defaults to unknown.
337
+ *
338
+ * @extends IntrigError
339
+ *
340
+ * @property type - A string literal that identifies the type of error as 'response-validation'.
341
+ * @property error - An instance of ZodError representing the validation error encountered.
342
+ */
343
+ export interface ResponseValidationError<T, E = unknown> extends IntrigError<T, E> {
344
+ type: 'response-validation';
345
+ error: ZodError;
346
+ }
347
+ /**
348
+ * Constructs a ResponseValidationError object with a specified error.
349
+ *
350
+ * @param {ZodError} error - The validation error encountered during response validation.
351
+ * @return {ResponseValidationError<T, E>} An error object containing the type of error and the validation error details.
352
+ */
353
+ export declare function responseValidationError<T, E>(error: ZodError): ResponseValidationError<T, E>;
354
+ /**
355
+ * Determines if the given error is a response validation error.
356
+ *
357
+ * @param {IntrigError<T, E>} value - The error object to assess.
358
+ * @return {boolean} True if the error is a response validation error, otherwise false.
359
+ */
360
+ export declare function isResponseValidationError<T, E>(value: IntrigError<T, E>): value is ResponseValidationError<T, E>;
223
361
  export {};