@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 +12 -0
- package/README.md +20 -18
- package/eslint.config.cjs +12 -0
- package/package.json +5 -6
- package/project.json +9 -0
- package/rollup.config.cjs +28 -0
- package/src/extra/{index.d.ts → index.ts} +2 -2
- package/src/extra/useAsNetworkState.ts +53 -0
- package/src/extra/{useAsPromise.d.ts → useAsPromise.ts} +58 -7
- package/src/extra/{useResolvedCachedValue.d.ts → useResolvedCachedValue.ts} +39 -7
- package/src/extra/{useResolvedValue.d.ts → useResolvedValue.ts} +39 -7
- package/src/{index.d.ts → index.ts} +2 -2
- package/src/intrig-context.ts +66 -0
- package/src/intrig-provider.tsx +449 -0
- package/src/logger.ts +13 -0
- package/src/media-type-utils.ts +184 -0
- package/src/{network-state.d.ts → network-state.tsx} +172 -91
- package/tsconfig.json +27 -0
- package/tsconfig.lib.json +32 -0
- package/tsconfig.spec.json +26 -0
- package/vite.config.ts +25 -0
- package/index.esm.d.ts +0 -1
- package/index.esm.js +0 -3856
- package/src/extra/useAsNetworkState.d.ts +0 -13
- package/src/intrig-context.d.ts +0 -43
- package/src/intrig-provider.d.ts +0 -101
- package/src/media-type-utils.d.ts +0 -3
|
@@ -1,13 +0,0 @@
|
|
|
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];
|
package/src/intrig-context.d.ts
DELETED
|
@@ -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, unknown>>;
|
|
32
|
-
configs: DefaultConfigs;
|
|
33
|
-
execute: <T, E = unknown>(request: RequestType, dispatch: (state: NetworkState<T, E>) => void, schema: ZodSchema<T> | undefined, errorSchema: ZodSchema<E> | 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, };
|
package/src/intrig-provider.d.ts
DELETED
|
@@ -1,101 +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, E = unknown> {
|
|
52
|
-
key: string;
|
|
53
|
-
operation: string;
|
|
54
|
-
source: string;
|
|
55
|
-
schema?: ZodSchema<T>;
|
|
56
|
-
errorSchema?: ZodSchema<E>;
|
|
57
|
-
debounceDelay?: number;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* useNetworkState is a custom hook that manages the network state within the specified context.
|
|
61
|
-
* It handles making network requests, dispatching appropriate states based on the request lifecycle,
|
|
62
|
-
* and allows aborting ongoing requests.
|
|
63
|
-
*
|
|
64
|
-
* @param {Object} params - The parameters required to configure and use the network state.
|
|
65
|
-
* @param {string} params.key - A unique identifier for the network request.
|
|
66
|
-
* @param {string} params.operation - The operation type related to the request.
|
|
67
|
-
* @param {string} params.source - The source or endpoint for the network request.
|
|
68
|
-
* @param {Object} params.schema - The schema used for validating the response data.
|
|
69
|
-
* @param {number} [params.debounceDelay] - The debounce delay for executing the network request.
|
|
70
|
-
*
|
|
71
|
-
* @return {[NetworkState<T>, (request: AxiosRequestConfig) => void, () => void]}
|
|
72
|
-
* Returns a state object representing the current network state,
|
|
73
|
-
* a function to execute the network request, and a function to clear the request.
|
|
74
|
-
*/
|
|
75
|
-
export declare function useNetworkState<T, E = unknown>({ key, operation, source, schema, errorSchema, debounceDelay: requestDebounceDelay, }: NetworkStateProps<T>): [
|
|
76
|
-
NetworkState<T, E>,
|
|
77
|
-
(request: RequestType) => void,
|
|
78
|
-
clear: () => void,
|
|
79
|
-
(state: NetworkState<T, E>) => void
|
|
80
|
-
];
|
|
81
|
-
/**
|
|
82
|
-
* Handles central error extraction from the provided context.
|
|
83
|
-
* It filters the state to retain error states and maps them to a structured error object with additional context information.
|
|
84
|
-
* @return {Object[]} An array of objects representing the error states with context information such as source, operation, and key.
|
|
85
|
-
*/
|
|
86
|
-
export declare function useCentralError(): {
|
|
87
|
-
source: string;
|
|
88
|
-
operation: string;
|
|
89
|
-
key: string;
|
|
90
|
-
state: "error";
|
|
91
|
-
error: unknown;
|
|
92
|
-
statusCode?: number;
|
|
93
|
-
request?: any;
|
|
94
|
-
}[];
|
|
95
|
-
/**
|
|
96
|
-
* Uses central pending state handling by aggregating pending states from context.
|
|
97
|
-
* It calculates the overall progress of pending states if any, or returns an initial state otherwise.
|
|
98
|
-
*
|
|
99
|
-
* @return {NetworkState} The aggregated network state based on the pending states and their progress.
|
|
100
|
-
*/
|
|
101
|
-
export declare function useCentralPendingState(): NetworkState<unknown, unknown>;
|