field-fix: POST/PUT/PATCH without body sends '{}' in mobile api helper

- api() in apps/mobile/src/lib/api.ts now sends JSON.stringify({}) for
  methods with a body slot when no body is provided, instead of sending
  undefined with content-type: application/json.
- Fixes scan start crash (POST /v1/scans/:id/start) and latent
  resend-verification crash (POST /v1/auth/resend-verification).
- Server strictness left intact; uploadImage unchanged.
- Added unit tests for GET/HEAD/POST/PUT body behaviour.
This commit is contained in:
Sven (AAMOS AI)
2026-08-07 15:39:46 +07:00
parent 8637eaa6c3
commit 0078084a2b
2 changed files with 105 additions and 2 deletions
+4 -2
View File
@@ -63,13 +63,15 @@ export async function api<T = unknown>(
options: { method?: string; body?: unknown; retry?: boolean } = {},
): Promise<T> {
const { accessToken } = useAuth.getState();
const method = options.method ?? "GET";
const hasBodySlot = method !== "GET" && method !== "HEAD";
const res = await fetch(`${API_BASE}${path}`, {
method: options.method ?? "GET",
method,
headers: {
"content-type": "application/json",
...(accessToken ? { authorization: `Bearer ${accessToken}` } : {}),
},
body: options.body != null ? JSON.stringify(options.body) : undefined,
body: hasBodySlot ? JSON.stringify(options.body ?? {}) : undefined,
});
if (res.status === 401 && options.retry !== false) {