55 lines
1.2 KiB
JavaScript
55 lines
1.2 KiB
JavaScript
|
|
import { apiRequest, getPath } from './client.js';
|
||
|
|
|
||
|
|
export function normalizeLabelImagePayload(payload) {
|
||
|
|
if (!payload) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (payload instanceof Blob) {
|
||
|
|
return {
|
||
|
|
objectUrl: URL.createObjectURL(payload),
|
||
|
|
contentType: payload.type,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
if (payload?.imageUrl) {
|
||
|
|
return {
|
||
|
|
objectUrl: payload.imageUrl,
|
||
|
|
contentType: payload.contentType || 'image/png',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
if (payload?.imageSvg) {
|
||
|
|
const blob = new Blob([payload.imageSvg], { type: 'image/svg+xml' });
|
||
|
|
return {
|
||
|
|
objectUrl: URL.createObjectURL(blob),
|
||
|
|
contentType: 'image/svg+xml',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
if (payload?.label) {
|
||
|
|
return {
|
||
|
|
objectUrl: `data:image/png;base64,${payload.label}`,
|
||
|
|
contentType: 'image/png',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function previewLabel(store, body) {
|
||
|
|
const payload = await apiRequest(store, getPath('items'), {
|
||
|
|
method: 'POST',
|
||
|
|
body,
|
||
|
|
accept: 'image/svg+xml, image/png, application/json',
|
||
|
|
query: { label: 1, preview: 1 },
|
||
|
|
});
|
||
|
|
|
||
|
|
const image = normalizeLabelImagePayload(payload);
|
||
|
|
if (image) {
|
||
|
|
return image;
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new Error('Label preview response did not include an image.');
|
||
|
|
}
|