@@ -24,7 +24,21 @@ type JsonValue = JsonPrimitive | JsonObject | JsonArray;
24
24
25
25
/* job and job configuration */
26
26
27
- export type ProdiaJob = Record < string , JsonValue > ;
27
+ export type ProdiaJob = {
28
+ type : string ;
29
+ config ?: Record < string , JsonValue > ;
30
+ } ;
31
+
32
+ type ProdiaJobComplete = ProdiaJob & {
33
+ id : string ;
34
+ created_at : string ;
35
+ updated_at : string ;
36
+ expires_at : string ;
37
+ metrics : {
38
+ elapsed : number ;
39
+ ips ?: number ;
40
+ } ;
41
+ } ;
28
42
29
43
export type ProdiaJobOptions = {
30
44
accept ?:
@@ -41,22 +55,20 @@ const defaultJobOptions: ProdiaJobOptions = {
41
55
accept : undefined ,
42
56
} ;
43
57
44
- export type ProdiaJobResponse = {
45
- job : ProdiaJob ;
46
-
47
- // currently these are the only output field for all job types.
48
- // they will return the raw bytes for that output.
49
- arrayBuffer : ( ) => Promise < ArrayBuffer > ;
50
- uint8Array : ( ) => Promise < Uint8Array > ;
51
- } ;
52
-
53
58
/* client & client configuration*/
54
59
55
60
export type Prodia = {
56
61
job : (
57
62
params : ProdiaJob ,
58
63
options ?: Partial < ProdiaJobOptions > ,
59
- ) => Promise < ProdiaJobResponse > ;
64
+ ) => Promise < {
65
+ job : ProdiaJobComplete ;
66
+
67
+ // currently these are the only output field for all job types.
68
+ // they will return the raw bytes for that output.
69
+ arrayBuffer : ( ) => Promise < ArrayBuffer > ;
70
+ uint8Array : ( ) => Promise < Uint8Array > ;
71
+ } > ;
60
72
} ;
61
73
62
74
export type CreateProdiaOptions = {
@@ -139,7 +151,7 @@ export const createProdia = ({
139
151
body : formData ,
140
152
} ) ;
141
153
142
- // We bail from the loop if we get a 2xx response to avoid sleeping unnecessarily.
154
+ // we bail from the loop if we get a 2xx response to avoid sleeping unnecessarily.
143
155
if ( response . status >= 200 && response . status < 300 ) {
144
156
break ;
145
157
}
@@ -151,6 +163,7 @@ export const createProdia = ({
151
163
}
152
164
153
165
const retryAfter = Number ( response . headers . get ( "Retry-After" ) ) || 1 ;
166
+
154
167
await new Promise ( ( resolve ) =>
155
168
setTimeout ( resolve , retryAfter * 1000 )
156
169
) ;
@@ -169,23 +182,11 @@ export const createProdia = ({
169
182
) ;
170
183
}
171
184
172
- if ( response . headers . get ( "Content-Type" ) === "application/json" ) {
173
- const body = await response . json ( ) ;
174
-
175
- if ( "error" in body && typeof body . error === "string" ) {
176
- throw new ProdiaUserError ( body . error ) ;
177
- }
178
-
179
- const lastStateHistory = body . state . history . slice ( - 1 ) [ 0 ] ;
180
-
181
- if ( lastStateHistory && "message" in lastStateHistory ) {
182
- throw new ProdiaUserError ( lastStateHistory . message ) ;
183
- }
184
-
185
- throw new Error ( "Job Failed: Bad Content-Type: application/json" ) ;
186
- }
187
-
188
- if ( response . status < 200 || response . status > 299 ) {
185
+ if (
186
+ ! response . headers . get ( "Content-Type" ) ?. startsWith (
187
+ "multipart/form-data" ,
188
+ )
189
+ ) {
189
190
throw new ProdiaBadResponseError (
190
191
`${ response . status } ${ await response . text ( ) } ` ,
191
192
) ;
@@ -197,10 +198,12 @@ export const createProdia = ({
197
198
new TextDecoder ( ) . decode (
198
199
await ( body . get ( "job" ) as Blob ) . arrayBuffer ( ) ,
199
200
) ,
200
- ) as ProdiaJob ;
201
+ ) as ProdiaJobComplete ;
201
202
202
- if ( "error" in job && typeof job . error === "string" ) {
203
- throw new ProdiaUserError ( job . error ) ;
203
+ if ( response . status >= 400 && response . status < 500 ) {
204
+ if ( "error" in job && typeof job . error === "string" ) {
205
+ throw new ProdiaUserError ( job . error + " " + `(id: ${ job . id } )` ) ;
206
+ }
204
207
}
205
208
206
209
return {
0 commit comments