feat(edit): 编辑上报细节优化

This commit is contained in:
2026-10-06 18:30:05 +08:00
parent e20cd7547f
commit 79b310efe7
92 changed files with 2764 additions and 368 deletions
+16 -2
View File
@@ -1,4 +1,6 @@
import type { CollectedProductPayload } from '@atlas/contracts';
import { requestError } from './request-errors.js';
import { validateCollectedPayload } from './collection-payload-validation.js';
const apiOrigin = (import.meta.env.ATLAS_API_ORIGIN as string | undefined) ?? 'http://localhost:8200';
const webOrigin = (import.meta.env.ATLAS_WEB_ORIGIN as string | undefined) ?? 'http://localhost:8100';
@@ -10,7 +12,10 @@ async function saveToken(token: string) { await browser.storage.local.set({ [sto
async function clearTokens() { await browser.storage.local.remove(storageKey); }
async function request(path: string, options: RequestInit = {}, token?: string) {
const response = await fetch(`${apiOrigin}/api${path}`, { ...options, headers: { Accept: 'application/json', ...(options.headers ?? {}), ...(token ? { Authorization: `Bearer ${token}` } : {}) } });
if (!response.ok) { const body = await response.json().catch(() => null) as { error?: { message?: string } } | null; throw Object.assign(new Error(body?.error?.message ?? `请求失败(HTTP ${response.status})`), { status: response.status }); }
if (!response.ok) {
const body: unknown = await response.json().catch(() => null);
throw requestError(body, response.status);
}
return response.status === 204 ? undefined : response.json() as Promise<{ data: unknown }>;
}
async function activeAccessToken() {
@@ -43,7 +48,16 @@ export async function login() {
}
throw new Error('登录超时,请在 Atlas ERP 网页完成登录后重试');
}
export async function submit(payload: CollectedProductPayload) { const token = await activeAccessToken(); if (!token) throw new Error('请先登录 Atlas ERP'); return request('/extension/materials', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Idempotency-Key': crypto.randomUUID() }, body: JSON.stringify(payload) }, token) as Promise<{ data: { editor_entry_path: string; product_id: string; collection_item: { id: string }; reused: boolean } }>; }
export async function submit(payload: CollectedProductPayload) {
const validated = validateCollectedPayload(payload);
const token = await activeAccessToken();
if (!token) throw new Error('请先登录 Atlas ERP');
return request('/extension/materials', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Idempotency-Key': crypto.randomUUID() },
body: JSON.stringify(validated),
}, token) as Promise<{ data: { editor_entry_path: string; product_id: string; collection_item: { id: string }; reused: boolean } }>;
}
export async function openErp(path = '/') { await browser.tabs.create({ url: new URL(path, webOrigin).toString() }); }
async function readWebAccessToken(tabId: number) {
try {