@@ -9,27 +9,30 @@ const TIMEOUT = 2000
99 * @param {object } [options] - Handler options
1010 * @param {(this: HTMLFormElement) => void } [options.onBeforeSubmit] - Callback before submission
1111 * @param {(this: HTMLFormElement, response: Response) => void } [options.onSuccess] - Callback on successful response
12- * @param {(this: HTMLFormElement, error: any ) => void } [options.onError] - Callback on error
12+ * @param {(this: HTMLFormElement, error: Error ) => void } [options.onError] - Callback on error
1313 */
1414export default ( $form , options = { } ) => {
1515 if ( ! $form || ! ( $form instanceof HTMLFormElement ) ) {
16- throw Error ( 'setSubmit must be called with an HTMLFormElement' )
16+ throw new Error ( 'setSubmit must be called with an HTMLFormElement' )
1717 }
1818
1919 const method = $form . method
2020 const action = $form . action
2121
2222 if ( ! method || ! action ) {
23- throw Error ( 'Form method and action must be defined' )
23+ throw new Error ( 'Form method and action must be defined' )
2424 }
2525
26- const doSubmit = async ( ) => {
26+ async function doSubmit ( ) {
2727 if ( options . onBeforeSubmit ) {
2828 options . onBeforeSubmit . call ( $form )
2929 }
3030
31+ /** @type {RequestInit } */
3132 const fetchOptions = { method : method , body : new FormData ( $form ) }
32- if ( AbortSignal . timeout !== undefined ) {
33+
34+ // Check for timeout support
35+ if ( 'AbortSignal' in window && 'timeout' in AbortSignal ) {
3336 fetchOptions . signal = AbortSignal . timeout ( TIMEOUT )
3437 }
3538
@@ -41,7 +44,7 @@ export default ($form, options = {}) => {
4144 throw new Error ( `Response status: ${ response . status } ` )
4245 }
4346 } catch ( e ) {
44- if ( options . onError ) {
47+ if ( options . onError && e instanceof Error ) {
4548 options . onError . apply ( $form , [ e ] )
4649 }
4750 return
0 commit comments