@@ -33,10 +33,15 @@ export type Experimental_UseObjectOptions<RESULT> = {
33
33
34
34
export type Experimental_UseObjectHelpers < RESULT , INPUT > = {
35
35
/**
36
- * Calls the API with the provided input as JSON body .
36
+ * @deprecated Use `submit` instead .
37
37
*/
38
38
setInput : ( input : INPUT ) => void ;
39
39
40
+ /**
41
+ * Calls the API with the provided input as JSON body.
42
+ */
43
+ submit : ( input : INPUT ) => void ;
44
+
40
45
/**
41
46
* The current value for the generated object. Updated as the API streams JSON chunks.
42
47
*/
@@ -76,55 +81,58 @@ function useObject<RESULT, INPUT = any>({
76
81
const [ error , setError ] = useState < undefined | unknown > ( undefined ) ;
77
82
const [ isLoading , setIsLoading ] = useState ( false ) ;
78
83
79
- return {
80
- async setInput ( input ) {
81
- try {
82
- setIsLoading ( true ) ;
83
-
84
- const response = await fetch ( api , {
85
- method : 'POST' ,
86
- headers : { 'Content-Type' : 'application/json' } ,
87
- body : JSON . stringify ( input ) ,
88
- } ) ;
89
-
90
- if ( ! response . ok ) {
91
- throw new Error (
92
- ( await response . text ( ) ) ?? 'Failed to fetch the response.' ,
93
- ) ;
94
- }
95
-
96
- if ( response . body == null ) {
97
- throw new Error ( 'The response body is empty.' ) ;
98
- }
99
-
100
- let accumulatedText = '' ;
101
- let latestObject : DeepPartial < RESULT > | undefined = undefined ;
102
-
103
- response . body . pipeThrough ( new TextDecoderStream ( ) ) . pipeTo (
104
- new WritableStream < string > ( {
105
- write ( chunk ) {
106
- accumulatedText += chunk ;
107
-
108
- const currentObject = parsePartialJson (
109
- accumulatedText ,
110
- ) as DeepPartial < RESULT > ;
111
-
112
- if ( ! isDeepEqualData ( latestObject , currentObject ) ) {
113
- latestObject = currentObject ;
114
-
115
- mutate ( currentObject ) ;
116
- }
117
- } ,
118
- } ) ,
84
+ const submit = async ( input : INPUT ) => {
85
+ try {
86
+ setIsLoading ( true ) ;
87
+
88
+ const response = await fetch ( api , {
89
+ method : 'POST' ,
90
+ headers : { 'Content-Type' : 'application/json' } ,
91
+ body : JSON . stringify ( input ) ,
92
+ } ) ;
93
+
94
+ if ( ! response . ok ) {
95
+ throw new Error (
96
+ ( await response . text ( ) ) ?? 'Failed to fetch the response.' ,
119
97
) ;
98
+ }
120
99
121
- setError ( undefined ) ;
122
- } catch ( error ) {
123
- setError ( error ) ;
124
- } finally {
125
- setIsLoading ( false ) ;
100
+ if ( response . body == null ) {
101
+ throw new Error ( 'The response body is empty.' ) ;
126
102
}
127
- } ,
103
+
104
+ let accumulatedText = '' ;
105
+ let latestObject : DeepPartial < RESULT > | undefined = undefined ;
106
+
107
+ response . body . pipeThrough ( new TextDecoderStream ( ) ) . pipeTo (
108
+ new WritableStream < string > ( {
109
+ write ( chunk ) {
110
+ accumulatedText += chunk ;
111
+
112
+ const currentObject = parsePartialJson (
113
+ accumulatedText ,
114
+ ) as DeepPartial < RESULT > ;
115
+
116
+ if ( ! isDeepEqualData ( latestObject , currentObject ) ) {
117
+ latestObject = currentObject ;
118
+
119
+ mutate ( currentObject ) ;
120
+ }
121
+ } ,
122
+ } ) ,
123
+ ) ;
124
+
125
+ setError ( undefined ) ;
126
+ } catch ( error ) {
127
+ setError ( error ) ;
128
+ } finally {
129
+ setIsLoading ( false ) ;
130
+ }
131
+ } ;
132
+
133
+ return {
134
+ setInput : submit , // Deprecated
135
+ submit,
128
136
object : data ,
129
137
error,
130
138
isLoading,
0 commit comments