@ai-sdk/react 0.0.16 → 0.0.18

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/src/use-object.ts CHANGED
@@ -40,6 +40,11 @@ Custom fetch implementation. You can use it as a middleware to intercept request
40
40
  or to provide a custom fetch implementation for e.g. testing.
41
41
  */
42
42
  fetch?: FetchFunction;
43
+
44
+ /**
45
+ * Callback function to be called when an error is encountered.
46
+ */
47
+ onError?: (error: Error) => void;
43
48
  };
44
49
 
45
50
  export type Experimental_UseObjectHelpers<RESULT, INPUT> = {
@@ -80,6 +85,7 @@ function useObject<RESULT, INPUT = any>({
80
85
  schema, // required, in the future we will use it for validation
81
86
  initialValue,
82
87
  fetch,
88
+ onError,
83
89
  }: Experimental_UseObjectOptions<RESULT>): Experimental_UseObjectHelpers<
84
90
  RESULT,
85
91
  INPUT
@@ -167,6 +173,10 @@ function useObject<RESULT, INPUT = any>({
167
173
  return;
168
174
  }
169
175
 
176
+ if (onError && error instanceof Error) {
177
+ onError(error);
178
+ }
179
+
170
180
  setError(error);
171
181
  }
172
182
  };
@@ -9,10 +9,15 @@ import { z } from 'zod';
9
9
  import { experimental_useObject } from './use-object';
10
10
 
11
11
  describe('text stream', () => {
12
+ let onErrorResult: Error | undefined;
13
+
12
14
  const TestComponent = () => {
13
15
  const { object, error, submit, isLoading, stop } = experimental_useObject({
14
16
  api: '/api/use-object',
15
17
  schema: z.object({ content: z.string() }),
18
+ onError(error) {
19
+ onErrorResult = error;
20
+ },
16
21
  });
17
22
 
18
23
  return (
@@ -35,6 +40,7 @@ describe('text stream', () => {
35
40
 
36
41
  beforeEach(() => {
37
42
  render(<TestComponent />);
43
+ onErrorResult = undefined;
38
44
  });
39
45
 
40
46
  afterEach(() => {
@@ -68,6 +74,7 @@ describe('text stream', () => {
68
74
  it('should not have an error', async () => {
69
75
  await screen.findByTestId('error');
70
76
  expect(screen.getByTestId('error')).toBeEmptyDOMElement();
77
+ expect(onErrorResult).toBeUndefined();
71
78
  });
72
79
  },
73
80
  );
@@ -155,6 +162,7 @@ describe('text stream', () => {
155
162
 
156
163
  await screen.findByTestId('error');
157
164
  expect(screen.getByTestId('error')).toHaveTextContent('Not found');
165
+ expect(onErrorResult).toBeInstanceOf(Error);
158
166
  },
159
167
  ),
160
168
  );