@intrig/plugin-react 0.0.1 → 0.0.2-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.
Files changed (58) hide show
  1. package/dist/index.cjs +4260 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.js +4235 -0
  4. package/package.json +6 -3
  5. package/.swcrc +0 -29
  6. package/README.md +0 -7
  7. package/eslint.config.mjs +0 -19
  8. package/project.json +0 -29
  9. package/rollup.config.cjs +0 -54
  10. package/rollup.config.mjs +0 -33
  11. package/src/index.ts +0 -2
  12. package/src/lib/code-generator.ts +0 -79
  13. package/src/lib/get-endpoint-documentation.ts +0 -35
  14. package/src/lib/get-schema-documentation.ts +0 -11
  15. package/src/lib/internal-types.ts +0 -15
  16. package/src/lib/plugin-react.ts +0 -22
  17. package/src/lib/templates/context.template.ts +0 -74
  18. package/src/lib/templates/docs/__snapshots__/async-hook.spec.ts.snap +0 -889
  19. package/src/lib/templates/docs/__snapshots__/download-hook.spec.ts.snap +0 -1445
  20. package/src/lib/templates/docs/__snapshots__/react-hook.spec.ts.snap +0 -1371
  21. package/src/lib/templates/docs/__snapshots__/sse-hook.spec.ts.snap +0 -2008
  22. package/src/lib/templates/docs/async-hook.spec.ts +0 -92
  23. package/src/lib/templates/docs/async-hook.ts +0 -226
  24. package/src/lib/templates/docs/download-hook.spec.ts +0 -182
  25. package/src/lib/templates/docs/download-hook.ts +0 -170
  26. package/src/lib/templates/docs/react-hook.spec.ts +0 -97
  27. package/src/lib/templates/docs/react-hook.ts +0 -323
  28. package/src/lib/templates/docs/schema.ts +0 -105
  29. package/src/lib/templates/docs/sse-hook.spec.ts +0 -207
  30. package/src/lib/templates/docs/sse-hook.ts +0 -221
  31. package/src/lib/templates/extra.template.ts +0 -198
  32. package/src/lib/templates/index.template.ts +0 -14
  33. package/src/lib/templates/intrigMiddleware.template.ts +0 -21
  34. package/src/lib/templates/logger.template.ts +0 -67
  35. package/src/lib/templates/media-type-utils.template.ts +0 -191
  36. package/src/lib/templates/network-state.template.ts +0 -702
  37. package/src/lib/templates/packageJson.template.ts +0 -63
  38. package/src/lib/templates/provider/__tests__/provider-templates.spec.ts +0 -209
  39. package/src/lib/templates/provider/axios-config.template.ts +0 -49
  40. package/src/lib/templates/provider/hooks.template.ts +0 -240
  41. package/src/lib/templates/provider/interfaces.template.ts +0 -72
  42. package/src/lib/templates/provider/intrig-provider-stub.template.ts +0 -73
  43. package/src/lib/templates/provider/intrig-provider.template.ts +0 -185
  44. package/src/lib/templates/provider/main.template.ts +0 -48
  45. package/src/lib/templates/provider/reducer.template.ts +0 -50
  46. package/src/lib/templates/provider/status-trap.template.ts +0 -80
  47. package/src/lib/templates/provider.template.ts +0 -698
  48. package/src/lib/templates/source/controller/method/asyncFunctionHook.template.ts +0 -196
  49. package/src/lib/templates/source/controller/method/clientIndex.template.ts +0 -38
  50. package/src/lib/templates/source/controller/method/download.template.ts +0 -256
  51. package/src/lib/templates/source/controller/method/params.template.ts +0 -31
  52. package/src/lib/templates/source/controller/method/requestHook.template.ts +0 -220
  53. package/src/lib/templates/source/type/typeTemplate.ts +0 -257
  54. package/src/lib/templates/swcrc.template.ts +0 -25
  55. package/src/lib/templates/tsconfig.template.ts +0 -37
  56. package/src/lib/templates/type-utils.template.ts +0 -28
  57. package/tsconfig.json +0 -13
  58. package/tsconfig.lib.json +0 -20
@@ -1,702 +0,0 @@
1
- import {typescript} from "@intrig/plugin-sdk";
2
- import * as path from 'path'
3
-
4
- export function networkStateTemplate() {
5
- const ts = typescript(path.resolve("src", "network-state.tsx"))
6
- return ts`import { ZodError } from 'zod';
7
- import {AxiosResponseHeaders, RawAxiosResponseHeaders} from "axios";
8
-
9
- /**
10
- * State of an asynchronous call. Network state follows the state diagram given below.
11
- *
12
- *
13
- * <pre>
14
- * ┌──────┐
15
- * ┌─────────────► Init ◄────────────┐
16
- * │ └▲────┬┘ │
17
- * │ │ │ │
18
- * │ Reset Execute │
19
- * Reset │ │ Reset
20
- * │ ┌──┴────┴──┐ │
21
- * │ ┌────► Pending ◄────┐ │
22
- * │ │ └──┬────┬──┘ │ │
23
- * │ Execute │ │ Execute │
24
- * │ │ │ │ │ │
25
- * │ │ OnSuccess OnError │ │
26
- * │ ┌────┴──┐ │ │ ┌──┴───┐ │
27
- * └─┤Success◄────┘ └────►Error ├─┘
28
- * └───────┘ └──────┘
29
- *
30
- * </pre>
31
- */
32
- export interface NetworkState<T = unknown> {
33
- state: 'init' | 'pending' | 'success' | 'error';
34
- }
35
-
36
- /**
37
- * Network call is not yet started
38
- */
39
- export interface InitState<T> extends NetworkState<T> {
40
- state: 'init';
41
- }
42
-
43
- /**
44
- * Checks whether the state is init state
45
- * @param state
46
- */
47
- export function isInit<T>(
48
- state: NetworkState<T>,
49
- ): state is InitState<T> {
50
- return state.state === 'init';
51
- }
52
-
53
- /**
54
- * Initializes a new state.
55
- *
56
- * @template T The type of the state.
57
- * @return {InitState<T>} An object representing the initial state.
58
- */
59
- export function init<T>(): InitState<T> {
60
- return {
61
- state: 'init',
62
- };
63
- }
64
-
65
- /**
66
- * Network call is not yet completed
67
- */
68
- export interface PendingState<T> extends NetworkState<T> {
69
- state: 'pending';
70
- progress?: Progress;
71
- data?: T;
72
- }
73
-
74
- /**
75
- * Interface representing progress information for an upload or download operation.
76
- *
77
- * @typedef {object} Progress
78
- *
79
- * @property {'upload' | 'download'} type - The type of the operation.
80
- *
81
- * @property {number} loaded - The amount of data that has been loaded so far.
82
- *
83
- * @property {number} [total] - The total amount of data to be loaded (if known).
84
- */
85
- export interface Progress {
86
- type?: 'upload' | 'download';
87
- loaded: number;
88
- total?: number;
89
- }
90
-
91
- /**
92
- * Checks whether the state is pending state
93
- * @param state
94
- */
95
- export function isPending<T>(
96
- state: NetworkState<T>,
97
- ): state is PendingState<T> {
98
- return state.state === 'pending';
99
- }
100
-
101
- /**
102
- * Generates a PendingState object with a state of "pending".
103
- *
104
- * @return {PendingState<T>} An object representing the pending state.
105
- */
106
- export function pending<T>(
107
- progress: Progress | undefined = undefined,
108
- data: T | undefined = undefined,
109
- ): PendingState<T> {
110
- return {
111
- state: 'pending',
112
- progress,
113
- data,
114
- };
115
- }
116
-
117
- /**
118
- * Network call is completed with success state
119
- */
120
- export interface SuccessState<T> extends NetworkState<T> {
121
- state: 'success';
122
- data: T;
123
- headers?: Record<string, any | undefined>;
124
- }
125
-
126
- /**
127
- * Checks whether the state is success response
128
- * @param state
129
- */
130
- export function isSuccess<T>(
131
- state: NetworkState<T>,
132
- ): state is SuccessState<T> {
133
- return state.state === 'success';
134
- }
135
-
136
- /**
137
- * Creates a success state object with the provided data.
138
- *
139
- * @param {T} data - The data to be included in the success state.
140
- * @param headers
141
- * @return {SuccessState<T>} An object representing a success state containing the provided data.
142
- */
143
- export function success<T>(
144
- data: T,
145
- headers?: Record<string, any | undefined>,
146
- ): SuccessState<T> {
147
- return {
148
- state: 'success',
149
- data,
150
- headers,
151
- };
152
- }
153
-
154
- /**
155
- * Network call is completed with error response
156
- */
157
- export interface ErrorState<T> extends NetworkState<T> {
158
- state: 'error';
159
- error: IntrigError;
160
- }
161
-
162
- /**
163
- * Checks whether the state is error state
164
- * @param state
165
- */
166
- export function isError<T>(
167
- state: NetworkState<T>,
168
- ): state is ErrorState<T> {
169
- return state.state === 'error';
170
- }
171
-
172
- /**
173
- * Constructs an ErrorState object representing an error.
174
- *
175
- * @param {any} error - The error object or message.
176
- * @param {string} [statusCode] - An optional status code associated with the error.
177
- * @return {ErrorState<T>} An object representing the error state.
178
- */
179
- export function error<T>(
180
- error: IntrigError,
181
- ): ErrorState<T> {
182
- return {
183
- state: 'error',
184
- error
185
- };
186
- }
187
-
188
- /**
189
- * Represents the base structure for error information in the application.
190
- *
191
- * This interface is used to define the type of error encountered in various contexts.
192
- *
193
- * Properties:
194
- * - type: Specifies the category of the error which determines its nature and source.
195
- * - 'http': Indicates the error is related to HTTP operations.
196
- * - 'network': Indicates the error occurred due to network issues.
197
- * - 'request-validation': Represents errors that occurred during request validation.
198
- * - 'response-validation': Represents errors that occurred during response validation.
199
- * - 'config': Pertains to errors associated with configuration issues.
200
- */
201
- export interface IntrigErrorBase {
202
- type: 'http' | 'network' | 'request-validation' | 'response-validation' | 'config';
203
- }
204
-
205
- /**
206
- * Interface representing an HTTP-related error.
207
- * Extends the \`IntrigErrorBase\` base interface to provide information specific to HTTP errors.
208
- *
209
- * @property type - The type identifier for the error, always set to 'http'.
210
- * @property status - The HTTP status code associated with the error.
211
- * @property url - The URL that caused the error.
212
- * @property method - The HTTP method used in the request that resulted in the error.
213
- * @property headers - Optional. The HTTP headers relevant to the request and/or response, represented as a record of key-value pairs.
214
- * @property body - Optional. The parsed body of the server error, if available.
215
- */
216
- export interface HttpError extends IntrigErrorBase {
217
- type: 'http';
218
- status: number;
219
- url: string;
220
- method: string;
221
- headers?: RawAxiosResponseHeaders | AxiosResponseHeaders;
222
- body?: unknown; // parsed server error body if any
223
- }
224
-
225
- /**
226
- * Determines if the given error is an HTTP error.
227
- *
228
- * @param {IntrigError} error - The error object to check.
229
- * @return {boolean} Returns true if the error is an instance of HttpError; otherwise, false.
230
- */
231
- export function isHttpError(error: IntrigError): error is HttpError {
232
- return error.type === 'http';
233
- }
234
-
235
- /**
236
- * Creates an object representing an HTTP error.
237
- *
238
- * @param {number} status - The HTTP status code of the error.
239
- * @param {string} url - The URL associated with the HTTP error.
240
- * @param {string} method - The HTTP method that resulted in the error.
241
- * @param {Record<string, string | string[] | undefined>} [headers] - Optional headers involved in the HTTP error.
242
- * @param {unknown} [body] - Optional body data related to the HTTP error.
243
- * @return {HttpError} An object encapsulating details about the HTTP error.
244
- */
245
- export function httpError(
246
- status: number,
247
- url: string,
248
- method: string,
249
- headers?: RawAxiosResponseHeaders | AxiosResponseHeaders,
250
- body?: unknown
251
- ): HttpError {
252
- return {
253
- type: 'http',
254
- status,
255
- url,
256
- method,
257
- headers,
258
- body
259
- };
260
- }
261
-
262
- /**
263
- * Represents a network-related error. This error type is used to indicate issues during network operations.
264
- * Extends the base error functionality from IntrigErrorBase.
265
- *
266
- * Properties:
267
- * - \`type\`: Specifies the type of error as 'network'.
268
- * - \`reason\`: Indicates the specific reason for the network error. Possible values include:
269
- * - 'timeout': Occurs when the network request times out.
270
- * - 'dns': Represents DNS resolution issues.
271
- * - 'offline': Indicates the device is offline or has no network connectivity.
272
- * - 'aborted': The network request was aborted.
273
- * - 'unknown': An unspecified network issue occurred.
274
- * - \`request\`: Optional property representing the network request that caused the error. Its structure can vary depending on the implementation context.
275
- */
276
- export interface NetworkError extends IntrigErrorBase {
277
- type: 'network';
278
- reason: 'timeout' | 'dns' | 'offline' | 'aborted' | 'unknown';
279
- request?: any;
280
- }
281
-
282
- /**
283
- * Determines if the provided error is of type NetworkError.
284
- *
285
- * @param {IntrigError} error - The error object to be checked.
286
- * @return {boolean} Returns true if the error is of type NetworkError, otherwise false.
287
- */
288
- export function isNetworkError(error: IntrigError): error is NetworkError {
289
- return error.type === 'network';
290
- }
291
-
292
- /**
293
- * Creates a network error object with the specified reason and optional request details.
294
- *
295
- * @param {'timeout' | 'dns' | 'offline' | 'aborted' | 'unknown'} reason - The reason for the network error.
296
- * @param {any} [request] - Optional information about the network request that caused the error.
297
- * @return {NetworkError} An object representing the network error.
298
- */
299
- export function networkError(
300
- reason: 'timeout' | 'dns' | 'offline' | 'aborted' | 'unknown',
301
- request?: any,
302
- ): NetworkError {
303
- return {
304
- type: 'network',
305
- reason,
306
- request,
307
- };
308
- }
309
-
310
- /**
311
- * Represents an error that occurs during request validation.
312
- *
313
- * This interface extends the \`IntrigErrorBase\` to provide
314
- * additional details about validation errors in the request.
315
- *
316
- * \`RequestValidationError\` includes a specific error type
317
- * identifier, details about the validation error, and information
318
- * about the fields that failed validation.
319
- *
320
- * The \`type\` property indicates the error type as 'request-validation'.
321
- * The \`error\` property holds the ZodError object with detailed validation
322
- * errors from the Zod library.
323
- * The \`fieldErrors\` property is a mapping of field names to an array
324
- * of validation error messages for that field.
325
- */
326
- export interface RequestValidationError extends IntrigErrorBase {
327
- type: 'request-validation';
328
- error: ZodError;
329
- }
330
-
331
- /**
332
- * Checks if the given error is of type RequestValidationError.
333
- *
334
- * @param {IntrigError} error - The error object to be checked.
335
- * @return {boolean} Returns true if the error is a RequestValidationError; otherwise, false.
336
- */
337
- export function isRequestValidationError(error: IntrigError): error is RequestValidationError {
338
- return error.type === 'request-validation';
339
- }
340
-
341
- /**
342
- * Constructs a RequestValidationError object, capturing details about validation errors.
343
- *
344
- * @param {ZodError} error - The primary Zod validation error object containing detailed error information.
345
- * @param {Record<string, string[]>} fieldErrors - An object mapping field names to arrays of validation error messages.
346
- * @return {RequestValidationError} An object representing the request validation error, including the error type, detailed error, and field-specific errors.
347
- */
348
- export function requestValidationError(
349
- error: ZodError
350
- ): RequestValidationError {
351
- return {
352
- type: 'request-validation',
353
- error
354
- };
355
- }
356
-
357
- /**
358
- * Describes an error encountered during response validation, typically
359
- * when the structure or content of a response does not meet the expected schema.
360
- *
361
- * This interface extends the \`IntrigErrorBase\` to provide additional
362
- * details specific to validation issues.
363
- *
364
- * The \`type\` property is a discriminative field, always set to 'response-validation',
365
- * for identifying this specific kind of error.
366
- *
367
- * The \`error\` property contains a \`ZodError\` object, which provides structured
368
- * details about the validation failure, including paths and specific issues.
369
- *
370
- * The optional \`raw\` property may hold the unprocessed or unparsed response data,
371
- * which can be useful for debugging and troubleshooting.
372
- */
373
- export interface ResponseValidationError extends IntrigErrorBase {
374
- type: 'response-validation';
375
- error: ZodError;
376
- // optional: raw/unparsed response for troubleshooting
377
- raw?: unknown;
378
- }
379
-
380
- /**
381
- * Determines if the provided error is of type ResponseValidationError.
382
- *
383
- * @param {IntrigError} error - The error object to be evaluated.
384
- * @return {boolean} Returns true if the error is a ResponseValidationError, otherwise false.
385
- */
386
- export function isResponseValidationError(error: IntrigError): error is ResponseValidationError {
387
- return error.type === 'response-validation';
388
- }
389
-
390
- /**
391
- * Constructs a ResponseValidationError object to represent a response validation failure.
392
- *
393
- * @param {ZodError} error - The error object representing the validation issue.
394
- * @param {unknown} [raw] - Optional raw data related to the validation error.
395
- * @return {ResponseValidationError} An object containing the type of error, the validation error object, and optional raw data.
396
- */
397
- export function responseValidationError(
398
- error: ZodError,
399
- raw?: unknown,
400
- ): ResponseValidationError {
401
- return {
402
- type: 'response-validation',
403
- error,
404
- raw,
405
- };
406
- }
407
-
408
- /**
409
- * Represents an error related to configuration issues.
410
- * ConfigError is an extension of IntrigErrorBase, designed specifically
411
- * to handle and provide details about errors encountered in application
412
- * configuration.
413
- *
414
- * @interface ConfigError
415
- * @extends IntrigErrorBase
416
- *
417
- * @property type - Identifies the error type as 'config'.
418
- * @property message - Describes the details of the configuration error encountered.
419
- */
420
- export interface ConfigError extends IntrigErrorBase {
421
- type: 'config';
422
- message: string;
423
- }
424
-
425
- /**
426
- * Determines if the provided error is a configuration error.
427
- *
428
- * @param {IntrigError} error - The error object to be checked.
429
- * @return {boolean} Returns true if the error is of type 'ConfigError', otherwise false.
430
- */
431
- export function isConfigError(error: IntrigError): error is ConfigError {
432
- return error.type === 'config';
433
- }
434
-
435
- /**
436
- * Generates a configuration error object with a specified error message.
437
- *
438
- * @param {string} message - The error message to be associated with the configuration error.
439
- * @return {ConfigError} The configuration error object containing the error type and message.
440
- */
441
- export function configError(message: string): ConfigError {
442
- return {
443
- type: 'config',
444
- message,
445
- };
446
- }
447
-
448
- /**
449
- * Represents a union type for errors that may occur while handling HTTP requests,
450
- * network operations, request and response validations, or configuration issues.
451
- *
452
- * This type encompasses various error types to provide a unified representation
453
- * for different error scenarios during the execution of a program.
454
- *
455
- * Types:
456
- * - HttpError: Represents an error occurred in HTTP responses.
457
- * - NetworkError: Represents an error related to underlying network operations.
458
- * - RequestValidationError: Represents an error in request validation logic.
459
- * - ResponseValidationError: Represents an error in response validation logic.
460
- * - ConfigError: Represents an error related to configuration issues.
461
- */
462
- export type IntrigError =
463
- | HttpError
464
- | NetworkError
465
- | RequestValidationError
466
- | ResponseValidationError
467
- | ConfigError;
468
-
469
- /**
470
- * Represents an error state with additional contextual information.
471
- *
472
- * @typedef {Object} ErrorWithContext
473
- * @template T
474
- * @extends ErrorState<T>
475
- *
476
- * @property {string} source - The origin of the error.
477
- * @property {string} operation - The operation being performed when the error occurred.
478
- * @property {string} key - A unique key identifying the specific error instance.
479
- */
480
- export interface ErrorWithContext<T = unknown>
481
- extends ErrorState<T> {
482
- source: string;
483
- operation: string;
484
- key: string;
485
- }
486
-
487
- /**
488
- * Represents an action in the network context.
489
- *
490
- * @template T - The type of data associated with the network action
491
- *
492
- * @property {NetworkState<any>} state - The current state of the network action
493
- * @property {string} key - The unique identifier for the network action
494
- */
495
- export interface NetworkAction<T> {
496
- key: string;
497
- source: string;
498
- operation: string;
499
- state: NetworkState<T>;
500
- handled?: boolean;
501
- }
502
-
503
- type HookWithKey = {
504
- key: string;
505
- };
506
-
507
- export type UnitHookOptions =
508
- | { key?: string; fetchOnMount?: false; clearOnUnmount?: boolean }
509
- | {
510
- key?: string;
511
- fetchOnMount: true;
512
- params?: Record<string, any>;
513
- clearOnUnmount?: boolean;
514
- };
515
- export type UnitHook = ((
516
- options: UnitHookOptions,
517
- ) => [
518
- NetworkState<never>,
519
- (params?: Record<string, any>) => DispatchState<any>,
520
- () => void,
521
- ]) &
522
- HookWithKey;
523
- export type ConstantHook<T> = ((
524
- options: UnitHookOptions,
525
- ) => [
526
- NetworkState<T>,
527
- (params?: Record<string, any>) => DispatchState<any>,
528
- () => void,
529
- ]) &
530
- HookWithKey;
531
-
532
- export type UnaryHookOptions<P> =
533
- | { key?: string; fetchOnMount?: false; clearOnUnmount?: boolean }
534
- | { key?: string; fetchOnMount: true; params: P; clearOnUnmount?: boolean };
535
- export type UnaryProduceHook<P> = ((
536
- options?: UnaryHookOptions<P>,
537
- ) => [NetworkState<never>, (params: P) => DispatchState<any>, () => void]) &
538
- HookWithKey;
539
- export type UnaryFunctionHook<P, T> = ((
540
- options?: UnaryHookOptions<P>,
541
- ) => [NetworkState<T>, (params: P) => DispatchState<any>, () => void]) &
542
- HookWithKey;
543
-
544
- export type BinaryHookOptions<P, B> =
545
- | { key?: string; fetchOnMount?: false; clearOnUnmount?: boolean }
546
- | {
547
- key?: string;
548
- fetchOnMount: true;
549
- params: P;
550
- body: B;
551
- clearOnUnmount?: boolean;
552
- };
553
- export type BinaryProduceHook<P, B> = ((
554
- options?: BinaryHookOptions<P, B>,
555
- ) => [
556
- NetworkState<never>,
557
- (body: B, params: P) => DispatchState<any>,
558
- () => void,
559
- ]) &
560
- HookWithKey;
561
- export type BinaryFunctionHook<P, B, T> = ((
562
- options?: BinaryHookOptions<P, B>,
563
- ) => [
564
- NetworkState<T>,
565
- (body: B, params: P) => DispatchState<any>,
566
- () => void,
567
- ]) &
568
- HookWithKey;
569
-
570
- export type IntrigHookOptions<P = undefined, B = undefined> =
571
- | UnitHookOptions
572
- | UnaryHookOptions<P>
573
- | BinaryHookOptions<P, B>;
574
- export type IntrigHook<P = undefined, B = undefined, T = any> =
575
- | UnitHook
576
- | ConstantHook<T>
577
- | UnaryProduceHook<P>
578
- | UnaryFunctionHook<P, T>
579
- | BinaryProduceHook<P, B>
580
- | BinaryFunctionHook<P, B, T>;
581
-
582
- export interface AsyncRequestOptions {
583
- hydrate?: boolean;
584
- key?: string;
585
- }
586
-
587
- // Async hook variants for transient (promise-returning) network requests
588
-
589
- export type UnaryFunctionAsyncHook<P, T> = (() => [
590
- (params: P) => Promise<T>,
591
- () => void,
592
- ]) & {
593
- key: string;
594
- };
595
-
596
- export type BinaryFunctionAsyncHook<P, B, T> = (() => [
597
- (body: B, params: P) => Promise<T>,
598
- () => void,
599
- ]) & {
600
- key: string;
601
- };
602
-
603
- export type UnaryProduceAsyncHook<P> = (() => [
604
- (params: P) => Promise<void>,
605
- () => void,
606
- ]) & {
607
- key: string;
608
- };
609
-
610
- export type BinaryProduceAsyncHook<P, B> = (() => [
611
- (body: B, params: P) => Promise<void>,
612
- () => void,
613
- ]) & {
614
- key: string;
615
- };
616
-
617
- /**
618
- * Represents the dispatch state of a process.
619
- *
620
- * @template T The type of the state information.
621
- * @interface
622
- *
623
- * @property {string} state The current state of the dispatch process.
624
- */
625
- export interface DispatchState<T> {
626
- state: string;
627
- }
628
-
629
- /**
630
- * Represents a successful dispatch state.
631
- *
632
- * @template T - Type of the data associated with the dispatch.
633
- *
634
- * @extends DispatchState<T>
635
- *
636
- * @property {string} state - The state of the dispatch, always 'success'.
637
- */
638
- export interface SuccessfulDispatch<T> extends DispatchState<T> {
639
- state: 'success';
640
- }
641
-
642
- /**
643
- * Indicates a successful dispatch state.
644
- *
645
- * @return {DispatchState<T>} An object representing a successful state.
646
- */
647
- export function successfulDispatch<T>(): DispatchState<T> {
648
- return {
649
- state: 'success',
650
- };
651
- }
652
-
653
- /**
654
- * Determines if the provided dispatch state represents a successful dispatch.
655
- *
656
- * @param {DispatchState<T>} value - The dispatch state to check.
657
- * @return {value is SuccessfulDispatch<T>} - True if the dispatch state indicates success, false otherwise.
658
- */
659
- export function isSuccessfulDispatch<T>(
660
- value: DispatchState<T>,
661
- ): value is SuccessfulDispatch<T> {
662
- return value.state === 'success';
663
- }
664
-
665
- /**
666
- * ValidationError interface represents a specific type of dispatch state
667
- * where a validation error has occurred.
668
- *
669
- * @typeparam T - The type of the data associated with this dispatch state.
670
- */
671
- export interface ValidationError<T> extends DispatchState<T> {
672
- state: 'validation-error';
673
- error: any;
674
- }
675
-
676
- /**
677
- * Generates a ValidationError object.
678
- *
679
- * @param error The error details that caused the validation to fail.
680
- * @return The ValidationError object containing the error state and details.
681
- */
682
- export function validationError<T>(error: any): ValidationError<T> {
683
- return {
684
- state: 'validation-error',
685
- error,
686
- };
687
- }
688
-
689
- /**
690
- * Determines if a provided DispatchState object is a ValidationError.
691
- *
692
- * @param {DispatchState<T>} value - The DispatchState object to evaluate.
693
- * @return {boolean} - Returns true if the provided DispatchState object is a ValidationError, otherwise returns false.
694
- */
695
- export function isValidationError<T>(
696
- value: DispatchState<T>,
697
- ): value is ValidationError<T> {
698
- return value.state === 'validation-error';
699
- }
700
-
701
- `
702
- }