@intrig/next 1.0.7 → 1.0.11

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 (59) hide show
  1. package/eslint.config.cjs +19 -0
  2. package/jest.config.ts +10 -0
  3. package/package.json +3 -6
  4. package/project.json +20 -0
  5. package/src/extra/{index.d.ts → index.ts} +2 -2
  6. package/src/extra/useAsNetworkState.ts +53 -0
  7. package/src/extra/{useAsPromise.d.ts → useAsPromise.ts} +58 -7
  8. package/src/extra/useLocalReducer.ts +61 -0
  9. package/src/extra/{useResolvedCachedValue.d.ts → useResolvedCachedValue.ts} +39 -7
  10. package/src/extra/{useResolvedValue.d.ts → useResolvedValue.ts} +39 -7
  11. package/src/extra.ts +190 -0
  12. package/src/{index.d.ts → index.ts} +1 -1
  13. package/src/intrig-context.ts +64 -0
  14. package/src/intrig-layout.tsx +18 -0
  15. package/src/intrig-middleware.spec.ts +9 -0
  16. package/src/intrig-middleware.ts +31 -0
  17. package/src/intrig-provider.tsx +454 -0
  18. package/src/logger.ts +13 -0
  19. package/src/media-type-utils.ts +184 -0
  20. package/src/{network-state.d.ts → network-state.tsx} +174 -92
  21. package/tsconfig.json +28 -0
  22. package/tsconfig.lib.json +10 -0
  23. package/tsconfig.spec.json +14 -0
  24. package/src/extra/index.js +0 -5
  25. package/src/extra/index.js.map +0 -1
  26. package/src/extra/useAsNetworkState.d.ts +0 -13
  27. package/src/extra/useAsNetworkState.js +0 -41
  28. package/src/extra/useAsNetworkState.js.map +0 -1
  29. package/src/extra/useAsPromise.js +0 -30
  30. package/src/extra/useAsPromise.js.map +0 -1
  31. package/src/extra/useLocalReducer.d.ts +0 -6
  32. package/src/extra/useLocalReducer.js +0 -50
  33. package/src/extra/useLocalReducer.js.map +0 -1
  34. package/src/extra/useResolvedCachedValue.js +0 -15
  35. package/src/extra/useResolvedCachedValue.js.map +0 -1
  36. package/src/extra/useResolvedValue.js +0 -17
  37. package/src/extra/useResolvedValue.js.map +0 -1
  38. package/src/extra.d.ts +0 -52
  39. package/src/extra.js +0 -92
  40. package/src/extra.js.map +0 -1
  41. package/src/index.js +0 -4
  42. package/src/index.js.map +0 -1
  43. package/src/intrig-context.d.ts +0 -42
  44. package/src/intrig-context.js +0 -21
  45. package/src/intrig-context.js.map +0 -1
  46. package/src/intrig-middleware.d.ts +0 -2
  47. package/src/intrig-middleware.js +0 -24
  48. package/src/intrig-middleware.js.map +0 -1
  49. package/src/intrig-provider.d.ts +0 -102
  50. package/src/intrig-provider.js +0 -296
  51. package/src/intrig-provider.js.map +0 -1
  52. package/src/logger.d.ts +0 -7
  53. package/src/logger.js +0 -11
  54. package/src/logger.js.map +0 -1
  55. package/src/media-type-utils.d.ts +0 -4
  56. package/src/media-type-utils.js +0 -121
  57. package/src/media-type-utils.js.map +0 -1
  58. package/src/network-state.js +0 -185
  59. package/src/network-state.js.map +0 -1
@@ -0,0 +1,184 @@
1
+ import { ZodSchema } from 'zod';
2
+ import { XMLParser } from 'fast-xml-parser';
3
+
4
+ type Encoders = {
5
+ [k: string]: <T>(request: T,
6
+ mediaType: string,
7
+ schema: ZodSchema) => Promise<any>;
8
+ };
9
+
10
+ const encoders: Encoders = {};
11
+
12
+ export function encode<T>(request: T, mediaType: string, schema: ZodSchema) {
13
+ if (encoders[mediaType]) {
14
+ return encoders[mediaType](request, mediaType, schema);
15
+ }
16
+ return request;
17
+ }
18
+
19
+ encoders['application/json'] = async (request, mediaType, schema) => {
20
+ return request;
21
+ }
22
+
23
+ function appendFormData(
24
+ formData: FormData,
25
+ data: any,
26
+ parentKey: string
27
+ ): void {
28
+ if (data instanceof Blob || typeof data === 'string') {
29
+ formData.append(parentKey, data);
30
+ } else if (data !== null && typeof data === 'object') {
31
+ if (Array.isArray(data)) {
32
+ data.forEach((item: any, index: number) => {
33
+ const key = `${parentKey}`;
34
+ appendFormData(formData, item, key);
35
+ });
36
+ } else {
37
+ Object.keys(data).forEach((key: string) => {
38
+ const newKey = parentKey ? `${parentKey}[${key}]` : key;
39
+ appendFormData(formData, data[key], newKey);
40
+ });
41
+ }
42
+ } else {
43
+ formData.append(parentKey, data == null ? '' : String(data));
44
+ }
45
+ }
46
+
47
+
48
+ encoders['multipart/form-data'] = async (request, mediaType, schema) => {
49
+ let _request = request as Record<string, any>;
50
+ let formData = new FormData();
51
+ Object.keys(_request).forEach((key: string) => {
52
+ appendFormData(formData, _request[key], key);
53
+ });
54
+ return formData;
55
+ }
56
+
57
+ encoders['application/octet-stream'] = async (request, mediaType, schema) => {
58
+ return request;
59
+ }
60
+
61
+ encoders['application/x-www-form-urlencoded'] = async (request, mediaType, schema) => {
62
+ let formData = new FormData();
63
+ for (let key in request) {
64
+ const value = request[key];
65
+ formData.append(key, value instanceof Blob || typeof value === 'string' ? value : String(value));
66
+ }
67
+ return formData;
68
+ }
69
+
70
+ type Transformers = {
71
+ [k: string]: <T>(
72
+ request: Request,
73
+ mediaType: string,
74
+ schema: ZodSchema
75
+ ) => Promise<T>;
76
+ };
77
+
78
+ const transformers: Transformers = {};
79
+
80
+ export function transform<T>(
81
+ request: Request,
82
+ mediaType: string,
83
+ schema: ZodSchema
84
+ ): Promise<T> {
85
+ if (transformers[mediaType]) {
86
+ return transformers[mediaType](request, mediaType, schema);
87
+ }
88
+ throw new Error(`Unsupported media type: ` + mediaType);
89
+ }
90
+
91
+ transformers['application/json'] = async (request, mediaType, schema) => {
92
+ return schema.parse(await request.json());
93
+ };
94
+
95
+ transformers['multipart/form-data'] = async (request, mediaType, schema) => {
96
+ let formData = await request.formData();
97
+ let content: Record<string, any> = {};
98
+ formData.forEach((value, key) => {
99
+ if (content[key]) {
100
+ if (!(content[key] instanceof Array)) {
101
+ content[key] = [content[key]];
102
+ }
103
+ content[key].push(value);
104
+ } else {
105
+ content[key] = value
106
+ }
107
+ });
108
+ return schema.parse(content);
109
+ };
110
+
111
+ transformers['application/octet-stream'] = async (
112
+ request,
113
+ mediaType,
114
+ schema
115
+ ) => {
116
+ return schema.parse(
117
+ new Blob([await request.arrayBuffer()], {
118
+ type: 'application/octet-stream',
119
+ })
120
+ );
121
+ };
122
+
123
+ transformers['application/x-www-form-urlencoded'] = async (
124
+ request,
125
+ mediaType,
126
+ schema
127
+ ) => {
128
+ let formData = await request.formData();
129
+ let content: Record<string, any> = {};
130
+ formData.forEach((value, key) => (content[key] = value));
131
+ return schema.parse(content);
132
+ };
133
+
134
+ transformers['application/xml'] = async (request, mediaType, schema) => {
135
+ let xmlParser = new XMLParser();
136
+ let content = await xmlParser.parse(await request.text());
137
+ return schema.parse(await content);
138
+ };
139
+
140
+ transformers['text/plain'] = async (request, mediaType, schema) => {
141
+ return schema.parse(await request.text());
142
+ };
143
+
144
+ transformers['text/html'] = async (request, mediaType, schema) => {
145
+ return schema.parse(await request.text());
146
+ };
147
+
148
+ transformers['text/css'] = async (request, mediaType, schema) => {
149
+ return schema.parse(await request.text());
150
+ };
151
+
152
+ transformers['text/javascript'] = async (request, mediaType, schema) => {
153
+ return schema.parse(await request.text());
154
+ };
155
+
156
+ type ResponseTransformers = {
157
+ [k: string]: <T>(
158
+ data: any,
159
+ mediaType: string,
160
+ schema: ZodSchema
161
+ ) => Promise<T>;
162
+ };
163
+
164
+ const responseTransformers: ResponseTransformers = {};
165
+
166
+ responseTransformers['application/json'] = async (data, mediaType, schema) => {
167
+ return schema.parse(data);
168
+ };
169
+
170
+ responseTransformers['application/xml'] = async (data, mediaType, schema) => {
171
+ let parsed = new XMLParser().parse(data);
172
+ return schema.parse(parsed);
173
+ }
174
+
175
+ export async function transformResponse<T>(
176
+ data: any,
177
+ mediaType: string,
178
+ schema: ZodSchema
179
+ ): Promise<T> {
180
+ if (responseTransformers[mediaType]) {
181
+ return await responseTransformers[mediaType](data, mediaType, schema);
182
+ }
183
+ return data
184
+ }
@@ -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
- state: 'init' | 'pending' | 'success' | 'error';
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
- state: 'init';
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 declare function isInit<T, E = unknown>(state: NetworkState<T, E>): state is InitState<T, E>;
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 declare function init<T, E = unknown>(): InitState<T, E>;
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
- state: 'pending';
51
- progress?: Progress;
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
- type?: 'upload' | 'download';
66
- loaded: number;
67
- total?: number;
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 declare function isPending<T, E = unknown>(state: NetworkState<T, E>): state is PendingState<T, E>;
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 declare function pending<T, E = unknown>(progress?: Progress | undefined): PendingState<T, E>;
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
- state: 'success';
85
- data: T;
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 declare function isSuccess<T, E = unknown>(state: NetworkState<T, E>): state is SuccessState<T, E>;
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 declare function success<T, E = unknown>(data: T): SuccessState<T, E>;
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
- state: 'error';
104
- error: E;
105
- statusCode?: number;
106
- request?: any;
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 declare function isError<T, E = unknown>(state: NetworkState<T, E>): state is ErrorState<T, E>;
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 declare function error<T, E = unknown>(error: E, statusCode?: number, request?: any): ErrorState<T>;
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
- source: string;
134
- operation: string;
135
- key: string;
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
- key: string;
147
- source: string;
148
- operation: string;
149
- state: NetworkState<T, E>;
150
- handled?: boolean;
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
- key: string;
154
- };
155
- export type UnitHookOptions = {
156
- key?: string;
157
- fetchOnMount?: false;
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
- export type UnaryHookOptions<P> = {
168
- key?: string;
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
- export type BinaryHookOptions<P, B> = {
180
- key?: string;
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
- hydrate?: boolean;
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
- state: string;
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
- state: 'success';
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 declare function successfulDispatch<T>(): DispatchState<T>;
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 declare function isSuccessfulDispatch<T>(value: DispatchState<T>): value is SuccessfulDispatch<T>;
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
- state: 'validation-error';
241
- error: any;
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 declare function validationError<T>(error: any): ValidationError<T>;
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 declare function isValidationError<T>(value: DispatchState<T>): value is ValidationError<T>;
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
- type: string;
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
- type: 'network';
282
- statusCode: string;
283
- error: E;
284
- request: any;
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 declare function networkError<T, E>(error: E, statusCode: string, request: any): NetworkError<T, E>;
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 declare function isNetworkError<T, E>(value: IntrigError<T, E>): value is NetworkError<T, E>;
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
- type: 'request-validation';
318
- error: ZodError;
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 declare function requestValidationError<T, E>(error: ZodError): RequestValidationError<T, E>;
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 declare function isRequestValidationError<T, E>(value: IntrigError<T, E>): value is RequestValidationError<T, E>;
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
- type: 'response-validation';
348
- error: ZodError;
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 declare function responseValidationError<T, E>(error: ZodError): ResponseValidationError<T, E>;
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 declare function isResponseValidationError<T, E>(value: IntrigError<T, E>): value is ResponseValidationError<T, E>;
364
- export {};
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
+ }