This commit is contained in:
seb
2026-07-31 20:37:43 +02:00
parent d5d30fb286
commit 9582370b0b
15 changed files with 174 additions and 70 deletions

View File

@@ -8,7 +8,7 @@ function logApi(tag, details) {
} catch {
text = String(details);
}
console.log(`[api] ${tag} ${text}`);
console[tag === 'error-body' ? 'error' : 'log'](`[api] ${tag} ${text}`);
}
function readableHeaders(headers) {
@@ -81,7 +81,7 @@ async function http(path, { method = 'GET', body } = {}) {
return json;
}
async function httpMultipart(path, { method = 'POST', formData } = {}) {
async function httpBinary(path, { method = 'POST', body, contentType } = {}) {
const url = new URL(path, window.location.origin).href;
logApi('request', {
@@ -90,13 +90,15 @@ async function httpMultipart(path, { method = 'POST', formData } = {}) {
url,
origin: window.location.origin,
credentials: 'include',
multipart: true,
contentType,
bodyBytes: body?.byteLength ?? body?.size ?? null,
});
const res = await fetch(path, {
method,
headers: { 'Content-Type': contentType },
credentials: 'include',
body: formData,
body,
});
const responseHeaders = readableHeaders(res.headers);
@@ -176,17 +178,15 @@ export const article = {
},
/**
* POST /api/article/:sku/photos — multipart/form-data
* Body: file (image/avif, max 1000×1000, prepared client-side), optional sort (default 0)
* POST /api/article/:sku/image
* Body: raw AVIF bytes (Content-Type: image/avif, max 1000×1000, prepared client-side via WASM)
* → 201 { article }
*/
uploadPhoto(sku, file, { sort = 0 } = {}) {
const formData = new FormData();
formData.append('file', file);
formData.append('sort', String(sort));
return httpMultipart(`/api/article/${encodeURIComponent(sku)}/photos`, {
uploadImage(sku, file) {
return httpBinary(`/api/article/${encodeURIComponent(sku)}/image`, {
method: 'POST',
formData,
contentType: 'image/avif',
body: file,
});
},
};