@intrig/next 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/eslint.config.cjs +19 -0
- package/jest.config.ts +10 -0
- package/package.json +7 -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.ts +3 -0
- 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} +176 -91
- 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/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.d.ts +0 -5
- package/src/index.js +0 -6
- 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 -1
- package/src/intrig-middleware.js +0 -15
- package/src/intrig-middleware.js.map +0 -1
- package/src/intrig-provider.d.ts +0 -101
- package/src/intrig-provider.js +0 -289
- package/src/intrig-provider.js.map +0 -1
- package/src/media-type-utils.d.ts +0 -3
- package/src/media-type-utils.js +0 -89
- 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,54 +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
|
+
|
|
224
|
+
export interface AsyncRequestOptions {
|
|
225
|
+
hydrate?: boolean;
|
|
226
|
+
}
|
|
227
|
+
|
|
194
228
|
/**
|
|
195
229
|
* Represents the dispatch state of a process.
|
|
196
230
|
*
|
|
@@ -200,8 +234,9 @@ export type IntrigHook<P = undefined, B = undefined, T = any, E = unknown> = Uni
|
|
|
200
234
|
* @property {string} state The current state of the dispatch process.
|
|
201
235
|
*/
|
|
202
236
|
export interface DispatchState<T> {
|
|
203
|
-
|
|
237
|
+
state: string
|
|
204
238
|
}
|
|
239
|
+
|
|
205
240
|
/**
|
|
206
241
|
* Represents a successful dispatch state.
|
|
207
242
|
*
|
|
@@ -211,46 +246,65 @@ export interface DispatchState<T> {
|
|
|
211
246
|
*
|
|
212
247
|
* @property {string} state - The state of the dispatch, always 'success'.
|
|
213
248
|
*/
|
|
214
|
-
export interface SuccessfulDispatch<T> extends DispatchState<T>
|
|
215
|
-
|
|
249
|
+
export interface SuccessfulDispatch<T> extends DispatchState<T>{
|
|
250
|
+
state: 'success'
|
|
216
251
|
}
|
|
252
|
+
|
|
217
253
|
/**
|
|
218
254
|
* Indicates a successful dispatch state.
|
|
219
255
|
*
|
|
220
256
|
* @return {DispatchState<T>} An object representing a successful state.
|
|
221
257
|
*/
|
|
222
|
-
export
|
|
258
|
+
export function successfulDispatch<T>(): DispatchState<T> {
|
|
259
|
+
return {
|
|
260
|
+
state: 'success'
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
223
264
|
/**
|
|
224
265
|
* Determines if the provided dispatch state represents a successful dispatch.
|
|
225
266
|
*
|
|
226
267
|
* @param {DispatchState<T>} value - The dispatch state to check.
|
|
227
268
|
* @return {value is SuccessfulDispatch<T>} - True if the dispatch state indicates success, false otherwise.
|
|
228
269
|
*/
|
|
229
|
-
export
|
|
270
|
+
export function isSuccessfulDispatch<T>(value: DispatchState<T>): value is SuccessfulDispatch<T> {
|
|
271
|
+
return value.state === 'success'
|
|
272
|
+
}
|
|
273
|
+
|
|
230
274
|
/**
|
|
231
275
|
* ValidationError interface represents a specific type of dispatch state
|
|
232
276
|
* where a validation error has occurred.
|
|
233
277
|
*
|
|
234
278
|
* @typeparam T - The type of the data associated with this dispatch state.
|
|
235
279
|
*/
|
|
236
|
-
export interface ValidationError<T> extends DispatchState<T>
|
|
237
|
-
|
|
238
|
-
|
|
280
|
+
export interface ValidationError<T> extends DispatchState<T>{
|
|
281
|
+
state: 'validation-error'
|
|
282
|
+
error: any
|
|
239
283
|
}
|
|
284
|
+
|
|
240
285
|
/**
|
|
241
286
|
* Generates a ValidationError object.
|
|
242
287
|
*
|
|
243
288
|
* @param error The error details that caused the validation to fail.
|
|
244
289
|
* @return The ValidationError object containing the error state and details.
|
|
245
290
|
*/
|
|
246
|
-
export
|
|
291
|
+
export function validationError<T>(error: any): ValidationError<T> {
|
|
292
|
+
return {
|
|
293
|
+
state: 'validation-error',
|
|
294
|
+
error
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
247
298
|
/**
|
|
248
299
|
* Determines if a provided DispatchState object is a ValidationError.
|
|
249
300
|
*
|
|
250
301
|
* @param {DispatchState<T>} value - The DispatchState object to evaluate.
|
|
251
302
|
* @return {boolean} - Returns true if the provided DispatchState object is a ValidationError, otherwise returns false.
|
|
252
303
|
*/
|
|
253
|
-
export
|
|
304
|
+
export function isValidationError<T>(value: DispatchState<T>): value is ValidationError<T> {
|
|
305
|
+
return value.state === 'validation-error'
|
|
306
|
+
}
|
|
307
|
+
|
|
254
308
|
/**
|
|
255
309
|
* Represents an error structure with a specified type and associated data.
|
|
256
310
|
*
|
|
@@ -260,8 +314,9 @@ export declare function isValidationError<T>(value: DispatchState<T>): value is
|
|
|
260
314
|
* @property {string} type - A string representing the type of the error.
|
|
261
315
|
*/
|
|
262
316
|
export interface IntrigError<T, E> {
|
|
263
|
-
|
|
317
|
+
type: string
|
|
264
318
|
}
|
|
319
|
+
|
|
265
320
|
/**
|
|
266
321
|
* Represents an error encountered during a network operation.
|
|
267
322
|
* Extends from the `IntrigError` interface, adding network-specific properties.
|
|
@@ -274,12 +329,13 @@ export interface IntrigError<T, E> {
|
|
|
274
329
|
* @property {E} error - The detailed error information specific to the failure, type extends from the generic E, allowing flexibility in the error details.
|
|
275
330
|
* @property {any} request - The request object that was attempted when the network error occurred, providing context for what operation failed.
|
|
276
331
|
*/
|
|
277
|
-
export interface NetworkError<T, E = unknown> extends IntrigError<T, E>
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
332
|
+
export interface NetworkError<T, E = unknown> extends IntrigError<T, E>{
|
|
333
|
+
type: 'network'
|
|
334
|
+
statusCode: string
|
|
335
|
+
error: E
|
|
336
|
+
request: any
|
|
282
337
|
}
|
|
338
|
+
|
|
283
339
|
/**
|
|
284
340
|
* Constructs a network error object.
|
|
285
341
|
*
|
|
@@ -288,14 +344,25 @@ export interface NetworkError<T, E = unknown> extends IntrigError<T, E> {
|
|
|
288
344
|
* @param request The request object associated with the network operation.
|
|
289
345
|
* @return A NetworkError object containing the error type, status code, error details, and the original request.
|
|
290
346
|
*/
|
|
291
|
-
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
|
+
|
|
292
356
|
/**
|
|
293
357
|
* Determines if the provided IntrigError is of type 'network'.
|
|
294
358
|
*
|
|
295
359
|
* @param {IntrigError<T, E>} value - The error value to check the type of.
|
|
296
360
|
* @return {boolean} - Returns true if the error is of type 'network', otherwise false.
|
|
297
361
|
*/
|
|
298
|
-
export
|
|
362
|
+
export function isNetworkError<T, E>(value: IntrigError<T, E>): value is NetworkError<T, E> {
|
|
363
|
+
return value.type === 'network'
|
|
364
|
+
}
|
|
365
|
+
|
|
299
366
|
/**
|
|
300
367
|
* Interface representing a request validation error.
|
|
301
368
|
*
|
|
@@ -310,24 +377,34 @@ export declare function isNetworkError<T, E>(value: IntrigError<T, E>): value is
|
|
|
310
377
|
* @property {string} type - A string literal indicating the error type as 'request-validation'.
|
|
311
378
|
* @property {ZodError} error - An instance of ZodError containing detailed validation error information.
|
|
312
379
|
*/
|
|
313
|
-
export interface RequestValidationError<T, E = unknown> extends IntrigError<T, E>
|
|
314
|
-
|
|
315
|
-
|
|
380
|
+
export interface RequestValidationError<T, E = unknown> extends IntrigError<T, E>{
|
|
381
|
+
type: 'request-validation'
|
|
382
|
+
error: ZodError
|
|
316
383
|
}
|
|
384
|
+
|
|
317
385
|
/**
|
|
318
386
|
* Constructs a RequestValidationError object encapsulating the ZodError.
|
|
319
387
|
*
|
|
320
388
|
* @param {ZodError} error - The error object resulting from Zod schema validation.
|
|
321
389
|
* @return {RequestValidationError<T, E>} A RequestValidationError object containing the validation error information.
|
|
322
390
|
*/
|
|
323
|
-
export
|
|
391
|
+
export function requestValidationError<T, E>(error: ZodError): RequestValidationError<T, E> {
|
|
392
|
+
return {
|
|
393
|
+
type: 'request-validation',
|
|
394
|
+
error
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
324
398
|
/**
|
|
325
399
|
* Determines if a given error is of type RequestValidationError.
|
|
326
400
|
*
|
|
327
401
|
* @param value The error object to check, which implements the IntrigError interface.
|
|
328
402
|
* @return A boolean indicating whether the error is a RequestValidationError.
|
|
329
403
|
*/
|
|
330
|
-
export
|
|
404
|
+
export function isRequestValidationError<T, E>(value: IntrigError<T, E>): value is RequestValidationError<T, E> {
|
|
405
|
+
return value.type === 'request-validation'
|
|
406
|
+
}
|
|
407
|
+
|
|
331
408
|
/**
|
|
332
409
|
* ResponseValidationError interface is designed to extend the capabilities of the IntrigError interface,
|
|
333
410
|
* specifically for handling errors related to response validation.
|
|
@@ -340,22 +417,30 @@ export declare function isRequestValidationError<T, E>(value: IntrigError<T, E>)
|
|
|
340
417
|
* @property type - A string literal that identifies the type of error as 'response-validation'.
|
|
341
418
|
* @property error - An instance of ZodError representing the validation error encountered.
|
|
342
419
|
*/
|
|
343
|
-
export interface ResponseValidationError<T, E = unknown> extends IntrigError<T, E>
|
|
344
|
-
|
|
345
|
-
|
|
420
|
+
export interface ResponseValidationError<T, E = unknown> extends IntrigError<T, E>{
|
|
421
|
+
type: 'response-validation'
|
|
422
|
+
error: ZodError
|
|
346
423
|
}
|
|
424
|
+
|
|
347
425
|
/**
|
|
348
426
|
* Constructs a ResponseValidationError object with a specified error.
|
|
349
427
|
*
|
|
350
428
|
* @param {ZodError} error - The validation error encountered during response validation.
|
|
351
429
|
* @return {ResponseValidationError<T, E>} An error object containing the type of error and the validation error details.
|
|
352
430
|
*/
|
|
353
|
-
export
|
|
431
|
+
export function responseValidationError<T, E>(error: ZodError): ResponseValidationError<T, E> {
|
|
432
|
+
return {
|
|
433
|
+
type: 'response-validation',
|
|
434
|
+
error
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
354
438
|
/**
|
|
355
439
|
* Determines if the given error is a response validation error.
|
|
356
440
|
*
|
|
357
441
|
* @param {IntrigError<T, E>} value - The error object to assess.
|
|
358
442
|
* @return {boolean} True if the error is a response validation error, otherwise false.
|
|
359
443
|
*/
|
|
360
|
-
export
|
|
361
|
-
|
|
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;;;;;;;;;;GAUG;AACH,MAAM,UAAU,iBAAiB,CAA8C,EAAK,EAAE,MAAc,SAAS;IAC3G,IAAI,EAAE,GAAG,KAAK,EAAE,CAAC;IAEjB,IAAI,OAAO,GAAG,gBAAgB,EAAE,CAAC;IAEjC,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,EAAE;QAChC,OAAO,OAAO,CAAC,KAAK,EAAE,CAAC,gBAAgB,EAAE,IAAI,GAAG,GAAG,CAAC,IAAI,IAAI,EAAE,CAAA;IAChE,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,gBAAgB,EAAE,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;IAEpD,MAAM,QAAQ,GAAG,WAAW,CAC1B,CAAC,KAAsB,EAAE,EAAE;QACzB,OAAO,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC;IAC1E,CAAC,EACD,CAAC,GAAG,EAAE,OAAO,CAAC,QAAQ,CAAC,CACxB,CAAC;IAEF,MAAM,OAAO,GAAG,WAAW,CAAC,CAAC,GAAG,IAAmB,EAAE,EAAE;QACrD,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAA;QACnB,OAAO,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,IAAI,CACrB,CAAC,IAAI,EAAE,EAAE;YACP,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAA;QACzB,CAAC,EACD,CAAC,CAAC,EAAE,EAAE;YACJ,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;QACpB,CAAC,CACF,CAAA;IACH,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE;QAC7B,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAA;IAClB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO;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;IAEjC,MAAM,UAAU,GAAG,MAAM,EAAsB,CAAC;IAChD,MAAM,SAAS,GAAG,MAAM,EAA0B,CAAC;IAEnD,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC,OAAc,CAAC,CAAC,CAAC,0CAA0C;IAE/F,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACrB,UAAU,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACjC,KAAK,EAAE,CAAC;QACV,CAAC;aAAM,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1B,SAAS,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACjC,KAAK,EAAE,CAAC;QACV,CAAC;IACH,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,MAAM,SAAS,GAAG,WAAW,CAAC,CAAC,GAAG,IAAW,EAAE,EAAE;QAC/C,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACxC,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;YAC7B,SAAS,CAAC,OAAO,GAAG,MAAM,CAAC;YAE3B,IAAI,aAAa,GAAI,QAAgB,CAAC,GAAG,IAAI,CAAC,CAAC;YAC/C,IAAI,iBAAiB,CAAC,aAAa,CAAC,EAAE,CAAC;gBACrC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC5B,CAAC"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { isSuccess } from '@intrig/next/network-state';
|
|
2
|
-
import { useEffect, useState } from 'react';
|
|
3
|
-
// **Implementation**
|
|
4
|
-
export function useResolvedCachedValue(hook, options) {
|
|
5
|
-
const [cachedValue, setCachedValue] = useState();
|
|
6
|
-
let [state] = hook(options); // Ensure compatibility with different hook types
|
|
7
|
-
useEffect(() => {
|
|
8
|
-
if (isSuccess(state)) {
|
|
9
|
-
setCachedValue(state.data);
|
|
10
|
-
}
|
|
11
|
-
// Do not clear cached value if state is unsuccessful
|
|
12
|
-
}, [state]);
|
|
13
|
-
return cachedValue;
|
|
14
|
-
}
|
|
15
|
-
//# sourceMappingURL=useResolvedCachedValue.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useResolvedCachedValue.js","sourceRoot":"","sources":["../../../../../lib/client-next/src/extra/useResolvedCachedValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAIwC,SAAS,EAKvD,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AA6D5C,qBAAqB;AACrB,MAAM,UAAU,sBAAsB,CAAa,IAA4B,EAAE,OAAgC;IAC/G,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG,QAAQ,EAAiB,CAAC;IAEhE,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,OAAc,CAAC,CAAC,CAAC,iDAAiD;IAErF,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACrB,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC7B,CAAC;QACD,qDAAqD;IACvD,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO,WAAW,CAAC;AACrB,CAAC"}
|