API版本化:迁移商品同步与批量发布入口

This commit is contained in:
Christ committed 2026-08-14 17:42:19 +08:00
1 parent e90a0b08d6
commit 78a256c51e
9 files changed
+160 -19

No files matched your search

+47 -6
View File
@@ -2,14 +2,14 @@ import { afterEach, expect, test, vi } from 'vitest';
import {
addAccount,
cancelPasswordLogin,
deleteItemPublishBatch, deleteItem,
deleteItemPublishBatch, deleteItem, cancelItemPublishBatch, retryFailedItemPublishBatch,
checkPasswordLoginStatus,
completeQRVerification,
createNotificationChannel,
getAccountDetails, getAccountRuntimeStatuses, updateAccountStatus,
getAutomationIssues,
getItems, getItemDetail,
getItemPublishBatches,
getItems, getItemDetail, syncItemsFromAccount,
getItemPublishBatches, getItemPublishBatch, startItemPublishBatch,
getNotificationChannels,
getOrders, getOrderDetail, updateOrder,
getOrderAnalytics,
@@ -18,7 +18,7 @@ import {
getShippingRulesPage,
getSystemSettings,
getValidOrders,
publishItem,
publishItem, recommendPublishCategory, previewItemPublishBatch,
logout,
importOrders,
passwordLogin,
@@ -90,7 +90,7 @@ test('getItemPublishBatches unwraps persisted batch list', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ batches: [{ id: 'batch-1', status: 'running' }] }));
vi.stubGlobal('fetch', fetchMock);
await expect(getItemPublishBatches(10)).resolves.toEqual([{ id: 'batch-1', status: 'running' }]);
expect(fetchMock).toHaveBeenCalledWith('/items/publish-batches?limit=10', expect.objectContaining({ credentials: 'include' }));
expect(fetchMock).toHaveBeenCalledWith('/api/v1/items/publish-batches?limit=10', expect.objectContaining({ credentials: 'include' }));
});
test('automation issue APIs expose and resolve quarantined work', async () => {
@@ -223,7 +223,7 @@ test('deleteItemPublishBatch removes an abandoned preview', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await deleteItemPublishBatch('preview-1');
expect(fetchMock).toHaveBeenCalledWith('/items/publish-batches/preview-1', expect.objectContaining({
expect(fetchMock).toHaveBeenCalledWith('/api/v1/items/publish-batches/preview-1', expect.objectContaining({
method: 'DELETE',
credentials: 'include',
}));
@@ -881,3 +881,44 @@ const runVersionedItemAPITest = async () => {
};
test('item list, detail, publish, update, and delete APIs use versioned compatibility routes', runVersionedItemAPITest);
// 商品同步、类目推荐和批量发布 API 使用版本化兼容入口。
const runVersionedItemBatchAPITest = async () => {
// fetchMock 是商品同步和批量发布请求的测试替身。
const fetchMock = vi.fn()
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({ success: true, category: { cat_id: 'cat-1' } }))
.mockResolvedValueOnce(jsonResponse({ success: true, preview_id: 'preview-1', total: 0, valid: 0, invalid: 0, rows: [] }))
.mockResolvedValueOnce(jsonResponse({ success: true, batch_id: 'batch-1' }))
.mockResolvedValueOnce(jsonResponse({ batches: [] }))
.mockResolvedValueOnce(jsonResponse({ id: 'batch-1', status: 'preview', rows: [] }))
.mockResolvedValueOnce(jsonResponse({ success: true, status: 'canceled' }))
.mockResolvedValueOnce(jsonResponse({ success: true, batch_id: 'batch-1' }))
.mockResolvedValueOnce(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await syncItemsFromAccount('acc1');
await recommendPublishCategory('acc1', '资料');
await previewItemPublishBatch({
file: new File(['order_id\nitem-1'], 'items.csv', { type: 'text/csv' }),
fallbackCategory: { catId: 'cat-1', catName: '资料', channelCatId: 'channel-1' },
});
await startItemPublishBatch('preview-1');
await getItemPublishBatches(10);
await getItemPublishBatch('batch-1');
await cancelItemPublishBatch('batch-1');
await retryFailedItemPublishBatch('batch-1');
await deleteItemPublishBatch('batch-1');
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/items/get-all-from-account', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/items/publish-categories/recommend', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/items/publish-batches/preview', expect.objectContaining({ method: 'POST', body: expect.any(FormData) }));
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/items/publish-batches', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(5, '/api/v1/items/publish-batches?limit=10', expect.objectContaining({ method: 'GET' }));
expect(fetchMock).toHaveBeenNthCalledWith(6, '/api/v1/items/publish-batches/batch-1', expect.objectContaining({ method: 'GET' }));
expect(fetchMock).toHaveBeenNthCalledWith(7, '/api/v1/items/publish-batches/batch-1/cancel', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(8, '/api/v1/items/publish-batches/batch-1/retry-failed', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(9, '/api/v1/items/publish-batches/batch-1', expect.objectContaining({ method: 'DELETE' }));
};
test('item sync and batch publish APIs use versioned compatibility routes', runVersionedItemBatchAPITest);
+9 -9
View File
@@ -506,7 +506,7 @@ export const getItems = async (cookieId?: string): Promise<Item[]> => {
}
export const syncItemsFromAccount = async (cookieId: string): Promise<ItemSyncResponse> => {
return post('/items/get-all-from-account', { cookie_id: cookieId });
return post('/api/v1/items/get-all-from-account', { cookie_id: cookieId });
}
export const deleteItem = async (cookieId: string, itemId: string): Promise<OperationResponse> => {
@@ -565,7 +565,7 @@ export const recommendPublishCategory = async (cookieId: string, keyword: string
// 该类型收口不改变凭证刷新和错误处理。
// 前端批量发布流程可直接复用 category。
// 旧路径继续由现有 Vite 代理转发。
return post('/items/publish-categories/recommend', { cookie_id: cookieId, keyword });
return post('/api/v1/items/publish-categories/recommend', { cookie_id: cookieId, keyword });
};
export const previewItemPublishBatch = async (form: {
@@ -589,32 +589,32 @@ export const previewItemPublishBatch = async (form: {
body.set('fallback_channel_category_id', form.fallbackCategory.channelCatId || '');
body.set('fallback_tb_category_id', form.fallbackCategory.tbCatId || '');
if (form.location) body.set('location', JSON.stringify(form.location));
return postForm('/items/publish-batches/preview', body);
return postForm('/api/v1/items/publish-batches/preview', body);
}
export const startItemPublishBatch = async (previewId: string): Promise<BatchIDResponse> => {
return post('/items/publish-batches', { preview_id: previewId });
return post('/api/v1/items/publish-batches', { preview_id: previewId });
}
export const getItemPublishBatch = async (batchId: string): Promise<ItemPublishBatchResponse> => {
return get(`/items/publish-batches/${batchId}`);
return get(`/api/v1/items/publish-batches/${batchId}`);
}
export const getItemPublishBatches = async (limit = 20): Promise<ItemPublishBatchResponse[]> => {
const res = await get<ItemPublishBatchListResponse>('/items/publish-batches', { limit });
const res = await get<ItemPublishBatchListResponse>('/api/v1/items/publish-batches', { limit });
return Array.isArray(res) ? res : (res.batches || []);
}
export const deleteItemPublishBatch = async (batchId: string): Promise<OperationResponse> => {
return del(`/items/publish-batches/${batchId}`);
return del(`/api/v1/items/publish-batches/${batchId}`);
}
export const cancelItemPublishBatch = async (batchId: string): Promise<BatchCancelResponse> => {
return post(`/items/publish-batches/${batchId}/cancel`, {});
return post(`/api/v1/items/publish-batches/${batchId}/cancel`, {});
}
export const retryFailedItemPublishBatch = async (batchId: string): Promise<BatchIDResponse> => {
return post(`/items/publish-batches/${batchId}/retry-failed`, {});
return post(`/api/v1/items/publish-batches/${batchId}/retry-failed`, {});
}
export const updateItem = async (cookieId: string, itemId: string, data: Partial<Item>): Promise<OperationResponse> => {