Skip to content

Commit 1db8c35

Browse files
authored
Merge pull request #2 from qirolab/fix-use-sanctum-form
Fix use sanctum form
2 parents 834575b + ac396a6 commit 1db8c35

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
4545
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish --access public && git push --follow-tags",
4646
"lint": "eslint . --ignore-pattern 'test/fixtures/laravel-api/*'",
47-
"lint:fix": "eslint . --fix",
47+
"lint:fix": "eslint . --fix --ignore-pattern 'test/fixtures/laravel-api/*'",
4848
"format": "prettier --write .",
4949
"test": "vitest run",
5050
"test:watch": "vitest watch",

playground/pages/profile.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ const { user, refreshUser } = useSanctum<{
99
avatar: string;
1010
}>();
1111
12-
const form = useSanctumForm<{
12+
interface UserProfileForm extends Record<string, unknown> {
1313
name: string;
1414
email: string;
1515
avatar: File | null;
16-
}>('post', '/api/profile', {
16+
}
17+
18+
const form = useSanctumForm<UserProfileForm>('patch', '/api/profile', {
1719
name: user.value!.name,
1820
email: user.value!.email,
1921
avatar: null,

src/runtime/composables/useSanctumForm.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ const resolveSubmitOptions = (
5858
form.setErrors({});
5959
options.onRequest?.(context);
6060
},
61+
async onRequestError(context) {
62+
form.processing = false;
63+
options.onRequestError?.(context);
64+
},
6165
async onResponse(context) {
6266
form.processing = false;
6367
options.onResponse?.(context);
@@ -115,7 +119,7 @@ export const useSanctumForm = <Data extends Record<string, unknown>>(
115119
async submit<T = any, R extends ResponseType = 'json'>(
116120
options: FetchOptions<R> = {},
117121
): Promise<MappedResponseType<R, T>> {
118-
let methodType = resolveMethod(method);
122+
const methodType = resolveMethod(method);
119123
let preparedData: Data | FormData = form.data();
120124

121125
// Convert to FormData if files are detected
@@ -124,12 +128,6 @@ export const useSanctumForm = <Data extends Record<string, unknown>>(
124128
indices: true,
125129
booleansAsIntegers: true,
126130
}) as FormData;
127-
128-
// Method spoofing for file uploads with non-POST methods
129-
if (methodType !== 'post') {
130-
preparedData.append('_method', methodType);
131-
methodType = 'post';
132-
}
133131
}
134132

135133
return useSanctumFetch(resolveUrl(url), {

test/fixtures/laravel-api/routes/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
return UserResource::make($request->user());
1010
})->middleware('auth:sanctum');
1111

12-
Route::post('/profile', function (Request $request) {
12+
Route::patch('/profile', function (Request $request) {
1313
$request->validate([
1414
'name' => ['required', 'string', 'max:255'],
1515
'email' => ['required', 'email'],

0 commit comments

Comments
 (0)