Skip to content

Commit

Permalink
Merge pull request #2 from qirolab/fix-use-sanctum-form
Browse files Browse the repository at this point in the history
Fix use sanctum form
  • Loading branch information
hkp22 authored Oct 23, 2024
2 parents 834575b + ac396a6 commit 1db8c35
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish --access public && git push --follow-tags",
"lint": "eslint . --ignore-pattern 'test/fixtures/laravel-api/*'",
"lint:fix": "eslint . --fix",
"lint:fix": "eslint . --fix --ignore-pattern 'test/fixtures/laravel-api/*'",
"format": "prettier --write .",
"test": "vitest run",
"test:watch": "vitest watch",
Expand Down
6 changes: 4 additions & 2 deletions playground/pages/profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const { user, refreshUser } = useSanctum<{
avatar: string;
}>();
const form = useSanctumForm<{
interface UserProfileForm extends Record<string, unknown> {
name: string;
email: string;
avatar: File | null;
}>('post', '/api/profile', {
}
const form = useSanctumForm<UserProfileForm>('patch', '/api/profile', {
name: user.value!.name,
email: user.value!.email,
avatar: null,
Expand Down
12 changes: 5 additions & 7 deletions src/runtime/composables/useSanctumForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ const resolveSubmitOptions = (
form.setErrors({});
options.onRequest?.(context);
},
async onRequestError(context) {
form.processing = false;
options.onRequestError?.(context);
},
async onResponse(context) {
form.processing = false;
options.onResponse?.(context);
Expand Down Expand Up @@ -115,7 +119,7 @@ export const useSanctumForm = <Data extends Record<string, unknown>>(
async submit<T = any, R extends ResponseType = 'json'>(
options: FetchOptions<R> = {},
): Promise<MappedResponseType<R, T>> {
let methodType = resolveMethod(method);
const methodType = resolveMethod(method);
let preparedData: Data | FormData = form.data();

// Convert to FormData if files are detected
Expand All @@ -124,12 +128,6 @@ export const useSanctumForm = <Data extends Record<string, unknown>>(
indices: true,
booleansAsIntegers: true,
}) as FormData;

// Method spoofing for file uploads with non-POST methods
if (methodType !== 'post') {
preparedData.append('_method', methodType);
methodType = 'post';
}
}

return useSanctumFetch(resolveUrl(url), {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/laravel-api/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
return UserResource::make($request->user());
})->middleware('auth:sanctum');

Route::post('/profile', function (Request $request) {
Route::patch('/profile', function (Request $request) {
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email'],
Expand Down

0 comments on commit 1db8c35

Please sign in to comment.