Skip to content

Commit 29a4b3f

Browse files
committed
fix: ensure options defaults to FetchOptions type
1 parent 57c6066 commit 29a4b3f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/runtime/composables/useSanctumForm.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,35 @@ const invokeHooks = <T>(
6363
*/
6464
const resolveSubmitOptions = (
6565
form: SanctumForm<any>,
66-
options: FetchOptions,
66+
options: FetchOptions = {} as FetchOptions, // Ensures `options` defaults to FetchOptions type
6767
): FetchOptions => ({
6868
...options,
6969
async onRequest(context) {
7070
form.processing = true;
7171
form.setErrors({});
72-
invokeHooks(options.onRequest, context);
72+
if ('onRequest' in options && options.onRequest) {
73+
invokeHooks(options.onRequest, context);
74+
}
7375
},
7476
async onRequestError(context) {
7577
form.processing = false;
76-
invokeHooks(options.onRequestError, context);
78+
if ('onRequestError' in options && options.onRequestError) {
79+
invokeHooks(options.onRequestError, context);
80+
}
7781
},
7882
async onResponse(context) {
7983
form.processing = false;
80-
invokeHooks(options.onResponse, context);
84+
if ('onResponse' in options && options.onResponse) {
85+
invokeHooks(options.onResponse, context);
86+
}
8187
},
8288
async onResponseError(context) {
8389
if (context.response.status === 422) {
8490
form.setErrors(context.response._data.errors);
8591
}
86-
invokeHooks(options.onResponseError, context);
92+
if ('onResponseError' in options && options.onResponseError) {
93+
invokeHooks(options.onResponseError, context);
94+
}
8795
},
8896
});
8997

@@ -130,7 +138,7 @@ export const useSanctumForm = <Data extends Record<string, unknown>>(
130138
},
131139
processing: false,
132140
async submit<T = any, R extends ResponseType = 'json'>(
133-
options: FetchOptions<ResponseType> = {},
141+
options?: FetchOptions<ResponseType>,
134142
): Promise<MappedResponseType<R, T>> {
135143
const methodType = resolveMethod(method);
136144
let preparedData: Data | FormData = form.data();
@@ -147,7 +155,7 @@ export const useSanctumForm = <Data extends Record<string, unknown>>(
147155
return useSanctumFetch(resolveUrl(url), {
148156
method: methodType,
149157
body: preparedData,
150-
...resolveSubmitOptions(form, options as FetchOptions<ResponseType>),
158+
...resolveSubmitOptions(form, options),
151159
}) as Promise<MappedResponseType<R, T>>;
152160
},
153161
errors: {},

0 commit comments

Comments
 (0)