1
+ import { IndiekitError } from "@indiekit/error" ;
1
2
import { wrapElement } from "../../lib/utils/wrap-element.js" ;
2
3
3
4
export const FileInputFieldController = class extends HTMLElement {
@@ -8,6 +9,7 @@ export const FileInputFieldController = class extends HTMLElement {
8
9
9
10
/** @type {HTMLElement } */
10
11
this . $uploadProgress = this . querySelector ( ".file-input__progress" ) ;
12
+ /** @type {HTMLInputElement } */
11
13
this . $fileInputPath = this . querySelector ( ".file-input__path" ) ;
12
14
this . $fileInputPicker = this . querySelector ( ".file-input__picker" ) ;
13
15
this . $fileInputPickerTemplate = this . querySelector ( "#file-input-picker" ) ;
@@ -76,21 +78,23 @@ export const FileInputFieldController = class extends HTMLElement {
76
78
this . $fileInputPath . readOnly = true ;
77
79
78
80
const endpointResponse = await fetch ( this . endpoint , {
79
- method : "POST" ,
80
81
body : formData ,
82
+ method : "POST" ,
83
+ headers : {
84
+ Accept : "application/json" ,
85
+ } ,
81
86
} ) ;
82
87
83
- if ( endpointResponse . ok ) {
84
- this . $fileInputPath . value =
85
- await endpointResponse . headers . get ( "location" ) ;
86
- } else {
87
- this . showErrorMessage ( endpointResponse . statusText ) ;
88
+ if ( ! endpointResponse . ok ) {
89
+ throw await IndiekitError . fromFetch ( endpointResponse ) ;
88
90
}
89
91
92
+ this . $fileInputPath . value =
93
+ await endpointResponse . headers . get ( "location" ) ;
90
94
this . $fileInputPath . readOnly = false ;
91
95
this . $uploadProgress . hidden = true ;
92
96
} catch ( error ) {
93
- this . showErrorMessage ( error ) ;
97
+ this . showErrorMessage ( error . message ) ;
94
98
this . $fileInputPath . readOnly = false ;
95
99
this . $uploadProgress . hidden = true ;
96
100
}
0 commit comments