@intrig/react 0.0.1 → 0.0.2

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/src/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './intrig-provider';
2
- export * from './network-state';
@@ -1,43 +0,0 @@
1
- import { NetworkAction, NetworkState } from '@intrig/react/network-state';
2
- import { AxiosProgressEvent } from 'axios';
3
- import { ZodSchema } from 'zod';
4
- import { DefaultConfigs } from '@intrig/react/intrig-provider';
5
- type GlobalState = Record<string, NetworkState>;
6
- interface RequestType<T = any> {
7
- method: 'get' | 'post' | 'put' | 'delete';
8
- url: string;
9
- headers?: Record<string, string>;
10
- params?: Record<string, any>;
11
- data?: any;
12
- originalData?: T;
13
- onUploadProgress?: (event: AxiosProgressEvent) => void;
14
- onDownloadProgress?: (event: AxiosProgressEvent) => void;
15
- signal?: AbortSignal;
16
- key: string;
17
- source: string;
18
- }
19
- /**
20
- * Defines the ContextType interface for managing global state, dispatching actions,
21
- * and holding a collection of Axios instances.
22
- *
23
- * @interface ContextType
24
- * @property {GlobalState} state - The global state of the application.
25
- * @property {React.Dispatch<NetworkAction<unknown>>} dispatch - The dispatch function to send network actions.
26
- * @property {Record<string, AxiosInstance>} axios - A record of Axios instances for making HTTP requests.
27
- */
28
- export interface ContextType {
29
- state: GlobalState;
30
- filteredState: GlobalState;
31
- dispatch: React.Dispatch<NetworkAction<unknown>>;
32
- configs: DefaultConfigs;
33
- execute: <T>(request: RequestType, dispatch: (state: NetworkState<T>) => void, schema: ZodSchema<T> | undefined) => Promise<void>;
34
- }
35
- /**
36
- * Context object created using `createContext` function. Provides a way to share state, dispatch functions,
37
- * and axios instance across components without having to pass props down manually at every level.
38
- *
39
- * @type {ContextType}
40
- */
41
- declare let Context: import("react").Context<ContextType>;
42
- export declare function useIntrigContext(): ContextType;
43
- export { Context, GlobalState, RequestType, };
@@ -1,100 +0,0 @@
1
- import { PropsWithChildren } from 'react';
2
- import { IntrigHook, NetworkState } from './network-state';
3
- import { CreateAxiosDefaults } from 'axios';
4
- import { ZodSchema } from 'zod';
5
- import { RequestType } from './intrig-context';
6
- export interface DefaultConfigs extends CreateAxiosDefaults {
7
- debounceDelay?: number;
8
- }
9
- export interface IntrigProviderProps {
10
- configs?: Record<string, DefaultConfigs>;
11
- children: React.ReactNode;
12
- }
13
- /**
14
- * IntrigProvider is a context provider component that sets up global state management
15
- * and provides Axios instances for API requests.
16
- *
17
- * @param {Object} props - The properties object.
18
- * @param {React.ReactNode} props.children - The child components to be wrapped by the provider.
19
- * @param {Object} [props.configs={}] - Configuration object for Axios instances.
20
- * @param {Object} [props.configs.defaults={}] - Default configuration for Axios.
21
- * @param {Object} [props.configs.petstore={}] - Configuration specific to the petstore API.
22
- * @return {JSX.Element} A context provider component that wraps the provided children.
23
- */
24
- export declare function IntrigProvider({ children, configs, }: IntrigProviderProps): import("react/jsx-runtime").JSX.Element;
25
- export interface StubType<P, B, T> {
26
- <P, B, T>(hook: IntrigHook<P, B, T>, fn: (params: P, body: B, dispatch: (state: NetworkState<T>) => void) => Promise<void>): void;
27
- }
28
- export type WithStubSupport<T> = T & {
29
- stubs?: (stub: StubType<any, any, any>) => void;
30
- };
31
- export interface IntrigProviderStubProps {
32
- configs?: DefaultConfigs;
33
- stubs?: (stub: StubType<any, any, any>) => void;
34
- children: React.ReactNode;
35
- }
36
- export declare function IntrigProviderStub({ children, configs, stubs }: IntrigProviderStubProps): import("react/jsx-runtime").JSX.Element;
37
- export interface StatusTrapProps {
38
- type: 'pending' | 'error' | 'pending + error';
39
- propagate?: boolean;
40
- }
41
- /**
42
- * StatusTrap component is used to track and manage network request states.
43
- *
44
- * @param {Object} props - The properties object.
45
- * @param {React.ReactNode} props.children - The child elements to be rendered.
46
- * @param {string} props.type - The type of network state to handle ("error", "pending", "pending + error").
47
- * @param {boolean} [props.propagate=true] - Whether to propagate the event to the parent context.
48
- * @return {React.ReactElement} The context provider component with filtered state and custom dispatch.
49
- */
50
- export declare function StatusTrap({ children, type, propagate, }: PropsWithChildren<StatusTrapProps>): import("react/jsx-runtime").JSX.Element;
51
- export interface NetworkStateProps<T> {
52
- key: string;
53
- operation: string;
54
- source: string;
55
- schema?: ZodSchema<T>;
56
- debounceDelay?: number;
57
- }
58
- /**
59
- * useNetworkState is a custom hook that manages the network state within the specified context.
60
- * It handles making network requests, dispatching appropriate states based on the request lifecycle,
61
- * and allows aborting ongoing requests.
62
- *
63
- * @param {Object} params - The parameters required to configure and use the network state.
64
- * @param {string} params.key - A unique identifier for the network request.
65
- * @param {string} params.operation - The operation type related to the request.
66
- * @param {string} params.source - The source or endpoint for the network request.
67
- * @param {Object} params.schema - The schema used for validating the response data.
68
- * @param {number} [params.debounceDelay] - The debounce delay for executing the network request.
69
- *
70
- * @return {[NetworkState<T>, (request: AxiosRequestConfig) => void, () => void]}
71
- * Returns a state object representing the current network state,
72
- * a function to execute the network request, and a function to clear the request.
73
- */
74
- export declare function useNetworkState<T>({ key, operation, source, schema, debounceDelay: requestDebounceDelay, }: NetworkStateProps<T>): [
75
- NetworkState<T>,
76
- (request: RequestType) => void,
77
- clear: () => void,
78
- (state: NetworkState<T>) => void
79
- ];
80
- /**
81
- * Handles central error extraction from the provided context.
82
- * It filters the state to retain error states and maps them to a structured error object with additional context information.
83
- * @return {Object[]} An array of objects representing the error states with context information such as source, operation, and key.
84
- */
85
- export declare function useCentralErrorHandling(): {
86
- source: string;
87
- operation: string;
88
- key: string;
89
- state: "error";
90
- error: any;
91
- statusCode?: number;
92
- request?: any;
93
- }[];
94
- /**
95
- * Uses central pending state handling by aggregating pending states from context.
96
- * It calculates the overall progress of pending states if any, or returns an initial state otherwise.
97
- *
98
- * @return {NetworkState} The aggregated network state based on the pending states and their progress.
99
- */
100
- export declare function useCentralPendingStateHandling(): NetworkState<unknown>;
@@ -1,223 +0,0 @@
1
- /**
2
- * State of an asynchronous call. Network state follows the state diagram given below.
3
- *
4
- *
5
- * <pre>
6
- * ┌──────┐
7
- * ┌─────────────► Init ◄────────────┐
8
- * │ └▲────┬┘ │
9
- * │ │ │ │
10
- * │ Reset Execute │
11
- * Reset │ │ Reset
12
- * │ ┌──┴────┴──┐ │
13
- * │ ┌────► Pending ◄────┐ │
14
- * │ │ └──┬────┬──┘ │ │
15
- * │ Execute │ │ Execute │
16
- * │ │ │ │ │ │
17
- * │ │ OnSuccess OnError │ │
18
- * │ ┌────┴──┐ │ │ ┌──┴───┐ │
19
- * └─┤Success◄────┘ └────►Error ├─┘
20
- * └───────┘ └──────┘
21
- *
22
- * </pre>
23
- */
24
- export interface NetworkState<T = unknown> {
25
- state: 'init' | 'pending' | 'success' | 'error';
26
- }
27
- /**
28
- * Network call is not yet started
29
- */
30
- export interface InitState<T> extends NetworkState<T> {
31
- state: 'init';
32
- }
33
- /**
34
- * Checks whether the state is init state
35
- * @param state
36
- */
37
- export declare function isInit<T>(state: NetworkState<T>): state is InitState<T>;
38
- /**
39
- * Initializes a new state.
40
- *
41
- * @template T The type of the state.
42
- * @return {InitState<T>} An object representing the initial state.
43
- */
44
- export declare function init<T>(): InitState<T>;
45
- /**
46
- * Network call is not yet completed
47
- */
48
- export interface PendingState<T> extends NetworkState<T> {
49
- state: 'pending';
50
- progress?: Progress;
51
- }
52
- /**
53
- * Interface representing progress information for an upload or download operation.
54
- *
55
- * @typedef {object} Progress
56
- *
57
- * @property {'upload' | 'download'} type - The type of the operation.
58
- *
59
- * @property {number} loaded - The amount of data that has been loaded so far.
60
- *
61
- * @property {number} [total] - The total amount of data to be loaded (if known).
62
- */
63
- export interface Progress {
64
- type?: 'upload' | 'download';
65
- loaded: number;
66
- total?: number;
67
- }
68
- /**
69
- * Checks whether the state is pending state
70
- * @param state
71
- */
72
- export declare function isPending<T>(state: NetworkState<T>): state is PendingState<T>;
73
- /**
74
- * Generates a PendingState object with a state of "pending".
75
- *
76
- * @return {PendingState<T>} An object representing the pending state.
77
- */
78
- export declare function pending<T>(progress?: Progress | undefined): PendingState<T>;
79
- /**
80
- * Network call is completed with success state
81
- */
82
- export interface SuccessState<T> extends NetworkState<T> {
83
- state: 'success';
84
- data: T;
85
- }
86
- /**
87
- * Checks whether the state is success response
88
- * @param state
89
- */
90
- export declare function isSuccess<T>(state: NetworkState<T>): state is SuccessState<T>;
91
- /**
92
- * Creates a success state object with the provided data.
93
- *
94
- * @param {T} data - The data to be included in the success state.
95
- * @return {SuccessState<T>} An object representing a success state containing the provided data.
96
- */
97
- export declare function success<T>(data: T): SuccessState<T>;
98
- /**
99
- * Network call is completed with error response
100
- */
101
- export interface ErrorState<T> extends NetworkState<T> {
102
- state: 'error';
103
- error: any;
104
- statusCode?: number;
105
- request?: any;
106
- }
107
- /**
108
- * Checks whether the state is error state
109
- * @param state
110
- */
111
- export declare function isError<T>(state: NetworkState<T>): state is ErrorState<T>;
112
- /**
113
- * Constructs an ErrorState object representing an error.
114
- *
115
- * @param {any} error - The error object or message.
116
- * @param {string} [statusCode] - An optional status code associated with the error.
117
- * @return {ErrorState<T>} An object representing the error state.
118
- */
119
- export declare function error<T>(error: any, statusCode?: number, request?: any): ErrorState<T>;
120
- /**
121
- * Represents an error state with additional contextual information.
122
- *
123
- * @typedef {Object} ErrorWithContext
124
- * @template T
125
- * @extends ErrorState<T>
126
- *
127
- * @property {string} source - The origin of the error.
128
- * @property {string} operation - The operation being performed when the error occurred.
129
- * @property {string} key - A unique key identifying the specific error instance.
130
- */
131
- export interface ErrorWithContext<T = unknown> extends ErrorState<T> {
132
- source: string;
133
- operation: string;
134
- key: string;
135
- }
136
- /**
137
- * Represents an action in the network context.
138
- *
139
- * @template T - The type of data associated with the network action
140
- *
141
- * @property {NetworkState<any>} state - The current state of the network action
142
- * @property {string} key - The unique identifier for the network action
143
- */
144
- export interface NetworkAction<T> {
145
- key: string;
146
- source: string;
147
- operation: string;
148
- state: NetworkState<T>;
149
- handled?: boolean;
150
- }
151
- type HookWithKey = {
152
- key: string;
153
- };
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>;
163
- /**
164
- * Represents the dispatch state of a process.
165
- *
166
- * @template T The type of the state information.
167
- * @interface
168
- *
169
- * @property {string} state The current state of the dispatch process.
170
- */
171
- export interface DispatchState<T> {
172
- state: string;
173
- }
174
- /**
175
- * Represents a successful dispatch state.
176
- *
177
- * @template T - Type of the data associated with the dispatch.
178
- *
179
- * @extends DispatchState<T>
180
- *
181
- * @property {string} state - The state of the dispatch, always 'success'.
182
- */
183
- export interface SuccessfulDispatch<T> extends DispatchState<T> {
184
- state: 'success';
185
- }
186
- /**
187
- * Indicates a successful dispatch state.
188
- *
189
- * @return {DispatchState<T>} An object representing a successful state.
190
- */
191
- export declare function successfulDispatch<T>(): DispatchState<T>;
192
- /**
193
- * Determines if the provided dispatch state represents a successful dispatch.
194
- *
195
- * @param {DispatchState<T>} value - The dispatch state to check.
196
- * @return {value is SuccessfulDispatch<T>} - True if the dispatch state indicates success, false otherwise.
197
- */
198
- export declare function isSuccessfulDispatch<T>(value: DispatchState<T>): value is SuccessfulDispatch<T>;
199
- /**
200
- * ValidationError interface represents a specific type of dispatch state
201
- * where a validation error has occurred.
202
- *
203
- * @typeparam T - The type of the data associated with this dispatch state.
204
- */
205
- export interface ValidationError<T> extends DispatchState<T> {
206
- state: 'validation-error';
207
- error: any;
208
- }
209
- /**
210
- * Generates a ValidationError object.
211
- *
212
- * @param error The error details that caused the validation to fail.
213
- * @return The ValidationError object containing the error state and details.
214
- */
215
- export declare function validationError<T>(error: any): ValidationError<T>;
216
- /**
217
- * Determines if a provided DispatchState object is a ValidationError.
218
- *
219
- * @param {DispatchState<T>} value - The DispatchState object to evaluate.
220
- * @return {boolean} - Returns true if the provided DispatchState object is a ValidationError, otherwise returns false.
221
- */
222
- export declare function isValidationError<T>(value: DispatchState<T>): value is ValidationError<T>;
223
- export {};