@intrig/next 1.0.7 → 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/eslint.config.cjs +19 -0
- package/jest.config.ts +10 -0
- package/package.json +5 -7
- package/project.json +20 -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/useLocalReducer.ts +61 -0
- package/src/extra/{useResolvedCachedValue.d.ts → useResolvedCachedValue.ts} +39 -7
- package/src/extra/{useResolvedValue.d.ts → useResolvedValue.ts} +39 -7
- package/src/extra.ts +190 -0
- package/src/{index.d.ts → index.ts} +1 -1
- package/src/intrig-context.ts +66 -0
- package/src/intrig-layout.tsx +18 -0
- package/src/intrig-middleware.ts +31 -0
- package/src/intrig-provider.tsx +454 -0
- package/src/logger.ts +13 -0
- package/src/media-type-utils.ts +184 -0
- package/src/{network-state.d.ts → network-state.tsx} +174 -92
- package/tsconfig.json +28 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +14 -0
- package/src/extra/index.js +0 -5
- package/src/extra/index.js.map +0 -1
- package/src/extra/useAsNetworkState.d.ts +0 -13
- package/src/extra/useAsNetworkState.js +0 -41
- package/src/extra/useAsNetworkState.js.map +0 -1
- package/src/extra/useAsPromise.js +0 -30
- package/src/extra/useAsPromise.js.map +0 -1
- package/src/extra/useLocalReducer.d.ts +0 -6
- package/src/extra/useLocalReducer.js +0 -50
- package/src/extra/useLocalReducer.js.map +0 -1
- package/src/extra/useResolvedCachedValue.js +0 -15
- package/src/extra/useResolvedCachedValue.js.map +0 -1
- package/src/extra/useResolvedValue.js +0 -17
- package/src/extra/useResolvedValue.js.map +0 -1
- package/src/extra.d.ts +0 -52
- package/src/extra.js +0 -92
- package/src/extra.js.map +0 -1
- package/src/index.js +0 -4
- package/src/index.js.map +0 -1
- package/src/intrig-context.d.ts +0 -42
- package/src/intrig-context.js +0 -21
- package/src/intrig-context.js.map +0 -1
- package/src/intrig-middleware.d.ts +0 -2
- package/src/intrig-middleware.js +0 -24
- package/src/intrig-middleware.js.map +0 -1
- package/src/intrig-provider.d.ts +0 -102
- package/src/intrig-provider.js +0 -296
- package/src/intrig-provider.js.map +0 -1
- package/src/logger.d.ts +0 -7
- package/src/logger.js +0 -11
- package/src/logger.js.map +0 -1
- package/src/media-type-utils.d.ts +0 -4
- package/src/media-type-utils.js +0 -121
- package/src/media-type-utils.js.map +0 -1
- package/src/network-state.js +0 -185
- package/src/network-state.js.map +0 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ZodError } from 'zod';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* State of an asynchronous call. Network state follows the state diagram given below.
|
|
4
5
|
*
|
|
@@ -23,33 +24,44 @@ import { ZodError } from 'zod';
|
|
|
23
24
|
* </pre>
|
|
24
25
|
*/
|
|
25
26
|
export interface NetworkState<T = unknown, E = unknown> {
|
|
26
|
-
|
|
27
|
+
state: 'init' | 'pending' | 'success' | 'error';
|
|
27
28
|
}
|
|
29
|
+
|
|
28
30
|
/**
|
|
29
31
|
* Network call is not yet started
|
|
30
32
|
*/
|
|
31
33
|
export interface InitState<T, E = unknown> extends NetworkState<T, E> {
|
|
32
|
-
|
|
34
|
+
state: 'init';
|
|
33
35
|
}
|
|
36
|
+
|
|
34
37
|
/**
|
|
35
38
|
* Checks whether the state is init state
|
|
36
39
|
* @param state
|
|
37
40
|
*/
|
|
38
|
-
export
|
|
41
|
+
export function isInit<T, E = unknown>(state: NetworkState<T, E>): state is InitState<T, E> {
|
|
42
|
+
return state.state === 'init';
|
|
43
|
+
}
|
|
44
|
+
|
|
39
45
|
/**
|
|
40
46
|
* Initializes a new state.
|
|
41
47
|
*
|
|
42
48
|
* @template T The type of the state.
|
|
43
49
|
* @return {InitState<T>} An object representing the initial state.
|
|
44
50
|
*/
|
|
45
|
-
export
|
|
51
|
+
export function init<T, E = unknown>(): InitState<T, E> {
|
|
52
|
+
return {
|
|
53
|
+
state: 'init',
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
46
57
|
/**
|
|
47
58
|
* Network call is not yet completed
|
|
48
59
|
*/
|
|
49
60
|
export interface PendingState<T, E = unknown> extends NetworkState<T, E> {
|
|
50
|
-
|
|
51
|
-
|
|
61
|
+
state: 'pending';
|
|
62
|
+
progress?: Progress;
|
|
52
63
|
}
|
|
64
|
+
|
|
53
65
|
/**
|
|
54
66
|
* Interface representing progress information for an upload or download operation.
|
|
55
67
|
*
|
|
@@ -62,54 +74,80 @@ export interface PendingState<T, E = unknown> extends NetworkState<T, E> {
|
|
|
62
74
|
* @property {number} [total] - The total amount of data to be loaded (if known).
|
|
63
75
|
*/
|
|
64
76
|
export interface Progress {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
77
|
+
type?: 'upload' | 'download';
|
|
78
|
+
loaded: number;
|
|
79
|
+
total?: number;
|
|
68
80
|
}
|
|
81
|
+
|
|
69
82
|
/**
|
|
70
83
|
* Checks whether the state is pending state
|
|
71
84
|
* @param state
|
|
72
85
|
*/
|
|
73
|
-
export
|
|
86
|
+
export function isPending<T, E = unknown>(state: NetworkState<T, E>): state is PendingState<T, E> {
|
|
87
|
+
return state.state === 'pending';
|
|
88
|
+
}
|
|
89
|
+
|
|
74
90
|
/**
|
|
75
91
|
* Generates a PendingState object with a state of "pending".
|
|
76
92
|
*
|
|
77
93
|
* @return {PendingState<T>} An object representing the pending state.
|
|
78
94
|
*/
|
|
79
|
-
export
|
|
95
|
+
export function pending<T, E = unknown>(
|
|
96
|
+
progress: Progress | undefined = undefined
|
|
97
|
+
): PendingState<T, E> {
|
|
98
|
+
return {
|
|
99
|
+
state: 'pending',
|
|
100
|
+
progress,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
80
104
|
/**
|
|
81
105
|
* Network call is completed with success state
|
|
82
106
|
*/
|
|
83
107
|
export interface SuccessState<T, E = unknown> extends NetworkState<T, E> {
|
|
84
|
-
|
|
85
|
-
|
|
108
|
+
state: 'success';
|
|
109
|
+
data: T;
|
|
86
110
|
}
|
|
111
|
+
|
|
87
112
|
/**
|
|
88
113
|
* Checks whether the state is success response
|
|
89
114
|
* @param state
|
|
90
115
|
*/
|
|
91
|
-
export
|
|
116
|
+
export function isSuccess<T, E = unknown>(state: NetworkState<T, E>): state is SuccessState<T, E> {
|
|
117
|
+
return state.state === 'success';
|
|
118
|
+
}
|
|
119
|
+
|
|
92
120
|
/**
|
|
93
121
|
* Creates a success state object with the provided data.
|
|
94
122
|
*
|
|
95
123
|
* @param {T} data - The data to be included in the success state.
|
|
96
124
|
* @return {SuccessState<T>} An object representing a success state containing the provided data.
|
|
97
125
|
*/
|
|
98
|
-
export
|
|
126
|
+
export function success<T, E = unknown>(data: T): SuccessState<T, E> {
|
|
127
|
+
return {
|
|
128
|
+
state: 'success',
|
|
129
|
+
data,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
99
133
|
/**
|
|
100
134
|
* Network call is completed with error response
|
|
101
135
|
*/
|
|
102
136
|
export interface ErrorState<T, E = unknown> extends NetworkState<T, E> {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
137
|
+
state: 'error';
|
|
138
|
+
error: E;
|
|
139
|
+
statusCode?: number;
|
|
140
|
+
request?: any;
|
|
107
141
|
}
|
|
142
|
+
|
|
108
143
|
/**
|
|
109
144
|
* Checks whether the state is error state
|
|
110
145
|
* @param state
|
|
111
146
|
*/
|
|
112
|
-
export
|
|
147
|
+
export function isError<T, E = unknown>(state: NetworkState<T, E>): state is ErrorState<T, E> {
|
|
148
|
+
return state.state === 'error';
|
|
149
|
+
}
|
|
150
|
+
|
|
113
151
|
/**
|
|
114
152
|
* Constructs an ErrorState object representing an error.
|
|
115
153
|
*
|
|
@@ -117,7 +155,19 @@ export declare function isError<T, E = unknown>(state: NetworkState<T, E>): stat
|
|
|
117
155
|
* @param {string} [statusCode] - An optional status code associated with the error.
|
|
118
156
|
* @return {ErrorState<T>} An object representing the error state.
|
|
119
157
|
*/
|
|
120
|
-
export
|
|
158
|
+
export function error<T, E = unknown>(
|
|
159
|
+
error: E,
|
|
160
|
+
statusCode?: number,
|
|
161
|
+
request?: any
|
|
162
|
+
): ErrorState<T> {
|
|
163
|
+
return {
|
|
164
|
+
state: 'error',
|
|
165
|
+
error,
|
|
166
|
+
statusCode,
|
|
167
|
+
request,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
121
171
|
/**
|
|
122
172
|
* Represents an error state with additional contextual information.
|
|
123
173
|
*
|
|
@@ -130,10 +180,11 @@ export declare function error<T, E = unknown>(error: E, statusCode?: number, req
|
|
|
130
180
|
* @property {string} key - A unique key identifying the specific error instance.
|
|
131
181
|
*/
|
|
132
182
|
export interface ErrorWithContext<T = unknown, E = unknown> extends ErrorState<T, E> {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
183
|
+
source: string;
|
|
184
|
+
operation: string;
|
|
185
|
+
key: string;
|
|
136
186
|
}
|
|
187
|
+
|
|
137
188
|
/**
|
|
138
189
|
* Represents an action in the network context.
|
|
139
190
|
*
|
|
@@ -143,57 +194,37 @@ export interface ErrorWithContext<T = unknown, E = unknown> extends ErrorState<T
|
|
|
143
194
|
* @property {string} key - The unique identifier for the network action
|
|
144
195
|
*/
|
|
145
196
|
export interface NetworkAction<T, E> {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
197
|
+
key: string;
|
|
198
|
+
source: string;
|
|
199
|
+
operation: string;
|
|
200
|
+
state: NetworkState<T, E>;
|
|
201
|
+
handled?: boolean;
|
|
151
202
|
}
|
|
203
|
+
|
|
152
204
|
type HookWithKey = {
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
clearOnUnmount?: boolean;
|
|
159
|
-
} | {
|
|
160
|
-
key?: string;
|
|
161
|
-
fetchOnMount: true;
|
|
162
|
-
params?: Record<string, any>;
|
|
163
|
-
clearOnUnmount?: boolean;
|
|
164
|
-
};
|
|
205
|
+
key: string;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
export type UnitHookOptions = { key?: string; fetchOnMount?: false; clearOnUnmount?: boolean } | { key?: string; fetchOnMount: true; params?: Record<string, any>; clearOnUnmount?: boolean };
|
|
165
210
|
export type UnitHook<E = unknown> = ((options: UnitHookOptions) => [NetworkState<never, E>, (params?: Record<string, any>) => DispatchState<any>, () => void]) & HookWithKey;
|
|
166
211
|
export type ConstantHook<T, E = unknown> = ((options: UnitHookOptions) => [NetworkState<T, E>, (params?: Record<string, any>) => DispatchState<any>, () => void]) & HookWithKey;
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
fetchOnMount?: false;
|
|
170
|
-
clearOnUnmount?: boolean;
|
|
171
|
-
} | {
|
|
172
|
-
key?: string;
|
|
173
|
-
fetchOnMount: true;
|
|
174
|
-
params: P;
|
|
175
|
-
clearOnUnmount?: boolean;
|
|
176
|
-
};
|
|
212
|
+
|
|
213
|
+
export type UnaryHookOptions<P> = { key?: string, fetchOnMount?: false, clearOnUnmount?: boolean } | { key?: string, fetchOnMount: true, params: P, clearOnUnmount?: boolean };
|
|
177
214
|
export type UnaryProduceHook<P, E = unknown> = ((options?: UnaryHookOptions<P>) => [NetworkState<never, E>, (params: P) => DispatchState<any>, () => void]) & HookWithKey;
|
|
178
215
|
export type UnaryFunctionHook<P, T, E = unknown> = ((options?: UnaryHookOptions<P>) => [NetworkState<T, E>, (params: P) => DispatchState<any>, () => void]) & HookWithKey;
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
fetchOnMount?: false;
|
|
182
|
-
clearOnUnmount?: boolean;
|
|
183
|
-
} | {
|
|
184
|
-
key?: string;
|
|
185
|
-
fetchOnMount: true;
|
|
186
|
-
params: P;
|
|
187
|
-
body: B;
|
|
188
|
-
clearOnUnmount?: boolean;
|
|
189
|
-
};
|
|
216
|
+
|
|
217
|
+
export type BinaryHookOptions<P, B> = { key?: string, fetchOnMount?: false, clearOnUnmount?: boolean } | { key?: string, fetchOnMount: true, params: P, body: B, clearOnUnmount?: boolean };
|
|
190
218
|
export type BinaryProduceHook<P, B, E = unknown> = ((options?: BinaryHookOptions<P, B>) => [NetworkState<never, E>, (body: B, params: P) => DispatchState<any>, () => void]) & HookWithKey;
|
|
191
219
|
export type BinaryFunctionHook<P, B, T, E = unknown> = ((options?: BinaryHookOptions<P, B>) => [NetworkState<T, E>, (body: B, params: P) => DispatchState<any>, () => void]) & HookWithKey;
|
|
220
|
+
|
|
192
221
|
export type IntrigHookOptions<P = undefined, B = undefined> = UnitHookOptions | UnaryHookOptions<P> | BinaryHookOptions<P, B>;
|
|
193
222
|
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>;
|
|
223
|
+
|
|
194
224
|
export interface AsyncRequestOptions {
|
|
195
|
-
|
|
225
|
+
hydrate?: boolean;
|
|
196
226
|
}
|
|
227
|
+
|
|
197
228
|
/**
|
|
198
229
|
* Represents the dispatch state of a process.
|
|
199
230
|
*
|
|
@@ -203,8 +234,9 @@ export interface AsyncRequestOptions {
|
|
|
203
234
|
* @property {string} state The current state of the dispatch process.
|
|
204
235
|
*/
|
|
205
236
|
export interface DispatchState<T> {
|
|
206
|
-
|
|
237
|
+
state: string
|
|
207
238
|
}
|
|
239
|
+
|
|
208
240
|
/**
|
|
209
241
|
* Represents a successful dispatch state.
|
|
210
242
|
*
|
|
@@ -214,46 +246,65 @@ export interface DispatchState<T> {
|
|
|
214
246
|
*
|
|
215
247
|
* @property {string} state - The state of the dispatch, always 'success'.
|
|
216
248
|
*/
|
|
217
|
-
export interface SuccessfulDispatch<T> extends DispatchState<T>
|
|
218
|
-
|
|
249
|
+
export interface SuccessfulDispatch<T> extends DispatchState<T>{
|
|
250
|
+
state: 'success'
|
|
219
251
|
}
|
|
252
|
+
|
|
220
253
|
/**
|
|
221
254
|
* Indicates a successful dispatch state.
|
|
222
255
|
*
|
|
223
256
|
* @return {DispatchState<T>} An object representing a successful state.
|
|
224
257
|
*/
|
|
225
|
-
export
|
|
258
|
+
export function successfulDispatch<T>(): DispatchState<T> {
|
|
259
|
+
return {
|
|
260
|
+
state: 'success'
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
226
264
|
/**
|
|
227
265
|
* Determines if the provided dispatch state represents a successful dispatch.
|
|
228
266
|
*
|
|
229
267
|
* @param {DispatchState<T>} value - The dispatch state to check.
|
|
230
268
|
* @return {value is SuccessfulDispatch<T>} - True if the dispatch state indicates success, false otherwise.
|
|
231
269
|
*/
|
|
232
|
-
export
|
|
270
|
+
export function isSuccessfulDispatch<T>(value: DispatchState<T>): value is SuccessfulDispatch<T> {
|
|
271
|
+
return value.state === 'success'
|
|
272
|
+
}
|
|
273
|
+
|
|
233
274
|
/**
|
|
234
275
|
* ValidationError interface represents a specific type of dispatch state
|
|
235
276
|
* where a validation error has occurred.
|
|
236
277
|
*
|
|
237
278
|
* @typeparam T - The type of the data associated with this dispatch state.
|
|
238
279
|
*/
|
|
239
|
-
export interface ValidationError<T> extends DispatchState<T>
|
|
240
|
-
|
|
241
|
-
|
|
280
|
+
export interface ValidationError<T> extends DispatchState<T>{
|
|
281
|
+
state: 'validation-error'
|
|
282
|
+
error: any
|
|
242
283
|
}
|
|
284
|
+
|
|
243
285
|
/**
|
|
244
286
|
* Generates a ValidationError object.
|
|
245
287
|
*
|
|
246
288
|
* @param error The error details that caused the validation to fail.
|
|
247
289
|
* @return The ValidationError object containing the error state and details.
|
|
248
290
|
*/
|
|
249
|
-
export
|
|
291
|
+
export function validationError<T>(error: any): ValidationError<T> {
|
|
292
|
+
return {
|
|
293
|
+
state: 'validation-error',
|
|
294
|
+
error
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
250
298
|
/**
|
|
251
299
|
* Determines if a provided DispatchState object is a ValidationError.
|
|
252
300
|
*
|
|
253
301
|
* @param {DispatchState<T>} value - The DispatchState object to evaluate.
|
|
254
302
|
* @return {boolean} - Returns true if the provided DispatchState object is a ValidationError, otherwise returns false.
|
|
255
303
|
*/
|
|
256
|
-
export
|
|
304
|
+
export function isValidationError<T>(value: DispatchState<T>): value is ValidationError<T> {
|
|
305
|
+
return value.state === 'validation-error'
|
|
306
|
+
}
|
|
307
|
+
|
|
257
308
|
/**
|
|
258
309
|
* Represents an error structure with a specified type and associated data.
|
|
259
310
|
*
|
|
@@ -263,8 +314,9 @@ export declare function isValidationError<T>(value: DispatchState<T>): value is
|
|
|
263
314
|
* @property {string} type - A string representing the type of the error.
|
|
264
315
|
*/
|
|
265
316
|
export interface IntrigError<T, E> {
|
|
266
|
-
|
|
317
|
+
type: string
|
|
267
318
|
}
|
|
319
|
+
|
|
268
320
|
/**
|
|
269
321
|
* Represents an error encountered during a network operation.
|
|
270
322
|
* Extends from the `IntrigError` interface, adding network-specific properties.
|
|
@@ -277,12 +329,13 @@ export interface IntrigError<T, E> {
|
|
|
277
329
|
* @property {E} error - The detailed error information specific to the failure, type extends from the generic E, allowing flexibility in the error details.
|
|
278
330
|
* @property {any} request - The request object that was attempted when the network error occurred, providing context for what operation failed.
|
|
279
331
|
*/
|
|
280
|
-
export interface NetworkError<T, E = unknown> extends IntrigError<T, E>
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
332
|
+
export interface NetworkError<T, E = unknown> extends IntrigError<T, E>{
|
|
333
|
+
type: 'network'
|
|
334
|
+
statusCode: string
|
|
335
|
+
error: E
|
|
336
|
+
request: any
|
|
285
337
|
}
|
|
338
|
+
|
|
286
339
|
/**
|
|
287
340
|
* Constructs a network error object.
|
|
288
341
|
*
|
|
@@ -291,14 +344,25 @@ export interface NetworkError<T, E = unknown> extends IntrigError<T, E> {
|
|
|
291
344
|
* @param request The request object associated with the network operation.
|
|
292
345
|
* @return A NetworkError object containing the error type, status code, error details, and the original request.
|
|
293
346
|
*/
|
|
294
|
-
export
|
|
347
|
+
export function networkError<T, E>(error: E, statusCode: string, request: any): NetworkError<T, E> {
|
|
348
|
+
return {
|
|
349
|
+
type: 'network',
|
|
350
|
+
statusCode,
|
|
351
|
+
error,
|
|
352
|
+
request
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
295
356
|
/**
|
|
296
357
|
* Determines if the provided IntrigError is of type 'network'.
|
|
297
358
|
*
|
|
298
359
|
* @param {IntrigError<T, E>} value - The error value to check the type of.
|
|
299
360
|
* @return {boolean} - Returns true if the error is of type 'network', otherwise false.
|
|
300
361
|
*/
|
|
301
|
-
export
|
|
362
|
+
export function isNetworkError<T, E>(value: IntrigError<T, E>): value is NetworkError<T, E> {
|
|
363
|
+
return value.type === 'network'
|
|
364
|
+
}
|
|
365
|
+
|
|
302
366
|
/**
|
|
303
367
|
* Interface representing a request validation error.
|
|
304
368
|
*
|
|
@@ -313,24 +377,34 @@ export declare function isNetworkError<T, E>(value: IntrigError<T, E>): value is
|
|
|
313
377
|
* @property {string} type - A string literal indicating the error type as 'request-validation'.
|
|
314
378
|
* @property {ZodError} error - An instance of ZodError containing detailed validation error information.
|
|
315
379
|
*/
|
|
316
|
-
export interface RequestValidationError<T, E = unknown> extends IntrigError<T, E>
|
|
317
|
-
|
|
318
|
-
|
|
380
|
+
export interface RequestValidationError<T, E = unknown> extends IntrigError<T, E>{
|
|
381
|
+
type: 'request-validation'
|
|
382
|
+
error: ZodError
|
|
319
383
|
}
|
|
384
|
+
|
|
320
385
|
/**
|
|
321
386
|
* Constructs a RequestValidationError object encapsulating the ZodError.
|
|
322
387
|
*
|
|
323
388
|
* @param {ZodError} error - The error object resulting from Zod schema validation.
|
|
324
389
|
* @return {RequestValidationError<T, E>} A RequestValidationError object containing the validation error information.
|
|
325
390
|
*/
|
|
326
|
-
export
|
|
391
|
+
export function requestValidationError<T, E>(error: ZodError): RequestValidationError<T, E> {
|
|
392
|
+
return {
|
|
393
|
+
type: 'request-validation',
|
|
394
|
+
error
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
327
398
|
/**
|
|
328
399
|
* Determines if a given error is of type RequestValidationError.
|
|
329
400
|
*
|
|
330
401
|
* @param value The error object to check, which implements the IntrigError interface.
|
|
331
402
|
* @return A boolean indicating whether the error is a RequestValidationError.
|
|
332
403
|
*/
|
|
333
|
-
export
|
|
404
|
+
export function isRequestValidationError<T, E>(value: IntrigError<T, E>): value is RequestValidationError<T, E> {
|
|
405
|
+
return value.type === 'request-validation'
|
|
406
|
+
}
|
|
407
|
+
|
|
334
408
|
/**
|
|
335
409
|
* ResponseValidationError interface is designed to extend the capabilities of the IntrigError interface,
|
|
336
410
|
* specifically for handling errors related to response validation.
|
|
@@ -343,22 +417,30 @@ export declare function isRequestValidationError<T, E>(value: IntrigError<T, E>)
|
|
|
343
417
|
* @property type - A string literal that identifies the type of error as 'response-validation'.
|
|
344
418
|
* @property error - An instance of ZodError representing the validation error encountered.
|
|
345
419
|
*/
|
|
346
|
-
export interface ResponseValidationError<T, E = unknown> extends IntrigError<T, E>
|
|
347
|
-
|
|
348
|
-
|
|
420
|
+
export interface ResponseValidationError<T, E = unknown> extends IntrigError<T, E>{
|
|
421
|
+
type: 'response-validation'
|
|
422
|
+
error: ZodError
|
|
349
423
|
}
|
|
424
|
+
|
|
350
425
|
/**
|
|
351
426
|
* Constructs a ResponseValidationError object with a specified error.
|
|
352
427
|
*
|
|
353
428
|
* @param {ZodError} error - The validation error encountered during response validation.
|
|
354
429
|
* @return {ResponseValidationError<T, E>} An error object containing the type of error and the validation error details.
|
|
355
430
|
*/
|
|
356
|
-
export
|
|
431
|
+
export function responseValidationError<T, E>(error: ZodError): ResponseValidationError<T, E> {
|
|
432
|
+
return {
|
|
433
|
+
type: 'response-validation',
|
|
434
|
+
error
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
357
438
|
/**
|
|
358
439
|
* Determines if the given error is a response validation error.
|
|
359
440
|
*
|
|
360
441
|
* @param {IntrigError<T, E>} value - The error object to assess.
|
|
361
442
|
* @return {boolean} True if the error is a response validation error, otherwise false.
|
|
362
443
|
*/
|
|
363
|
-
export
|
|
364
|
-
|
|
444
|
+
export function isResponseValidationError<T, E>(value: IntrigError<T, E>): value is ResponseValidationError<T, E> {
|
|
445
|
+
return value.type === 'response-validation'
|
|
446
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"target": "ES2020",
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"baseUrl": ".",
|
|
12
|
+
"paths": {
|
|
13
|
+
"@intrig/next/*": ["./src/*"],
|
|
14
|
+
"intrig-hook": ["src/config/intrig"]
|
|
15
|
+
},
|
|
16
|
+
"jsx": "react-jsx"
|
|
17
|
+
},
|
|
18
|
+
"files": [],
|
|
19
|
+
"include": [],
|
|
20
|
+
"references": [
|
|
21
|
+
{
|
|
22
|
+
"path": "./tsconfig.lib.json"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"path": "./tsconfig.spec.json"
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../../dist/out-tsc",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"types": ["jest", "node"]
|
|
7
|
+
},
|
|
8
|
+
"include": [
|
|
9
|
+
"jest.config.ts",
|
|
10
|
+
"src/**/*.test.ts",
|
|
11
|
+
"src/**/*.spec.ts",
|
|
12
|
+
"src/**/*.d.ts"
|
|
13
|
+
]
|
|
14
|
+
}
|
package/src/extra/index.js
DELETED
package/src/extra/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../lib/client-next/src/extra/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,0BAA0B,CAAC;AACzC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,qBAAqB,CAAA"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { NetworkState } from '@intrig/next/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];
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { error, init, pending, success } from '@intrig/next/network-state';
|
|
2
|
-
import { useCallback, useId, useMemo } from 'react';
|
|
3
|
-
import { useIntrigContext } from '@intrig/next/intrig-context';
|
|
4
|
-
/**
|
|
5
|
-
* A custom hook that integrates a promise-based operation with a network state management system.
|
|
6
|
-
* Tracks the network state (e.g., pending, success, error) for a given asynchronous function.
|
|
7
|
-
*
|
|
8
|
-
* @param fn A promise-based function that performs an asynchronous operation.
|
|
9
|
-
* @param key An optional string key to identify the network state uniquely. Defaults to 'default'.
|
|
10
|
-
* @return A tuple containing:
|
|
11
|
-
* 1. The current network state of the operation.
|
|
12
|
-
* 2. A function to execute the provided asynchronous operation.
|
|
13
|
-
* 3. A function to reset the network state back to the initial state.
|
|
14
|
-
*/
|
|
15
|
-
export function useAsNetworkState(fn, key = 'default') {
|
|
16
|
-
let id = useId();
|
|
17
|
-
let context = useIntrigContext();
|
|
18
|
-
const networkState = useMemo(() => {
|
|
19
|
-
return context.state?.[`promiseState:${id}:${key}}`] ?? init();
|
|
20
|
-
}, [context.state?.[`promiseState:${id}:${key}}`]]);
|
|
21
|
-
const dispatch = useCallback((state) => {
|
|
22
|
-
context.dispatch({ key, operation: id, source: 'promiseState', state });
|
|
23
|
-
}, [key, context.dispatch]);
|
|
24
|
-
const execute = useCallback((...args) => {
|
|
25
|
-
dispatch(pending());
|
|
26
|
-
return fn(...args).then((data) => {
|
|
27
|
-
dispatch(success(data));
|
|
28
|
-
}, (e) => {
|
|
29
|
-
dispatch(error(e));
|
|
30
|
-
});
|
|
31
|
-
}, []);
|
|
32
|
-
const clear = useCallback(() => {
|
|
33
|
-
dispatch(init());
|
|
34
|
-
}, []);
|
|
35
|
-
return [
|
|
36
|
-
networkState,
|
|
37
|
-
execute,
|
|
38
|
-
clear
|
|
39
|
-
];
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=useAsNetworkState.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAsNetworkState.js","sourceRoot":"","sources":["../../../../../lib/client-next/src/extra/useAsNetworkState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,IAAI,EAAgB,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACzF,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAE/D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QACL,YAAY;QACZ,OAAO;QACP,KAAK;KACN,CAAA;AACH,CAAC"}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { isError, isSuccess, isValidationError } from '@intrig/next/network-state';
|
|
2
|
-
import { useCallback, useEffect, useRef } from 'react';
|
|
3
|
-
// **Implementation**
|
|
4
|
-
export function useAsPromise(hook, options) {
|
|
5
|
-
const resolveRef = useRef();
|
|
6
|
-
const rejectRef = useRef();
|
|
7
|
-
let [state, dispatch, clear] = hook(options); // Casting to `any` to match all overloads
|
|
8
|
-
useEffect(() => {
|
|
9
|
-
if (isSuccess(state)) {
|
|
10
|
-
resolveRef.current?.(state.data);
|
|
11
|
-
clear();
|
|
12
|
-
}
|
|
13
|
-
else if (isError(state)) {
|
|
14
|
-
rejectRef.current?.(state.error);
|
|
15
|
-
clear();
|
|
16
|
-
}
|
|
17
|
-
}, [state]);
|
|
18
|
-
const promiseFn = useCallback((...args) => {
|
|
19
|
-
return new Promise((resolve, reject) => {
|
|
20
|
-
resolveRef.current = resolve;
|
|
21
|
-
rejectRef.current = reject;
|
|
22
|
-
let dispatchState = dispatch(...args);
|
|
23
|
-
if (isValidationError(dispatchState)) {
|
|
24
|
-
reject(dispatchState.error);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}, [dispatch]);
|
|
28
|
-
return [promiseFn, clear];
|
|
29
|
-
}
|
|
30
|
-
//# sourceMappingURL=useAsPromise.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAsPromise.js","sourceRoot":"","sources":["../../../../../lib/client-next/src/extra/useAsPromise.ts"],"names":[],"mappings":"AAAA,OAAO,EAIwC,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAKnF,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAkEvD,qBAAqB;AACrB,MAAM,UAAU,YAAY,CAC1B,IAA4B,EAC5B,OAAiC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|
-
key?: string;
|
|
5
|
-
}
|
|
6
|
-
export declare function useLocalReducer<T, E, F extends (event: E, curState?: T) => T | Promise<T>>(fn: F, options: LocalReducerOptions<T, E>): (NetworkState<T, unknown> | ((event: E) => void))[];
|