API版本化:迁移设置卡券与通知入口

This commit is contained in:
Christ committed 2026-08-14 17:53:21 +08:00
1 parent 78a256c51e
commit 42e37f7898
9 files changed
+269 -37

No files matched your search

+89 -6
View File
@@ -5,18 +5,18 @@ import {
deleteItemPublishBatch, deleteItem, cancelItemPublishBatch, retryFailedItemPublishBatch,
checkPasswordLoginStatus,
completeQRVerification,
createNotificationChannel,
createNotificationChannel, getAllAISettings, getAccountAISettings, updateAccountAISettings, fetchAIModels,
getAccountDetails, getAccountRuntimeStatuses, updateAccountStatus,
getAutomationIssues,
getItems, getItemDetail, syncItemsFromAccount,
getItemPublishBatches, getItemPublishBatch, startItemPublishBatch,
getNotificationChannels,
getNotificationChannels, getMessageNotifications, getAccountBindings,
getOrders, getOrderDetail, updateOrder,
getOrderAnalytics,
getReplyRules,
getShippingRules,
getShippingRulesPage,
getSystemSettings,
getSystemSettings, getCards, createCard, updateCard, deleteCard, getCardDetails, batchCreateCards, appendCardData,
getValidOrders,
publishItem, recommendPublishCategory, previewItemPublishBatch,
logout,
@@ -31,7 +31,7 @@ import {
updateAccountLoginInfo,
updateAccountSettings, updateAccountRemark, updateAccountAutoConfirm, updateAccountPauseDuration, getLongLoginSettings, setLongLoginSettings, refreshAccountProfile,
updateItem,
updateNotificationChannel,
updateNotificationChannel, deleteNotificationChannel, setMessageNotification, deleteMessageNotification, deleteAccountNotifications, setAccountBindings, testNotificationChannel,
updateSystemSettings,
updateShippingRule,
getChatSessions,
@@ -52,7 +52,7 @@ test('updateSystemSettings uses one atomic bulk request', async () => {
vi.stubGlobal('fetch', fetchMock);
await updateSystemSettings({ theme_color: 'blue', renewal_log_retention_days: 15 });
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith('/system-settings', expect.objectContaining({ method: 'PUT', credentials: 'include' }));
expect(fetchMock).toHaveBeenCalledWith('/api/v1/settings/system', expect.objectContaining({ method: 'PUT', credentials: 'include' }));
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ theme_color: 'blue', renewal_log_retention_days: 15 });
});
@@ -590,7 +590,7 @@ test('updateNotificationChannel supports partial enabled updates', async () => {
await updateNotificationChannel('7', { enabled: false });
expect(fetchMock).toHaveBeenCalledWith('/notification-channels/7', expect.objectContaining({
expect(fetchMock).toHaveBeenCalledWith('/api/v1/notifications/channels/7', expect.objectContaining({
method: 'PUT',
credentials: 'include',
}));
@@ -922,3 +922,86 @@ const runVersionedItemBatchAPITest = async () => {
};
test('item sync and batch publish APIs use versioned compatibility routes', runVersionedItemBatchAPITest);
// 设置、卡券和通知 API 使用版本化兼容入口。
const runVersionedSettingsCardNotificationAPITest = async () => {
// fetchMock 是设置、卡券和通知请求的测试替身。
const fetchMock = vi.fn()
.mockResolvedValueOnce(jsonResponse({ data: {} }))
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({}))
.mockResolvedValueOnce(jsonResponse({ ai_enabled: false }))
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({ models: ['qwen-plus'] }))
.mockResolvedValueOnce(jsonResponse([]))
.mockResolvedValueOnce(jsonResponse({ success: true, id: 1 }))
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({ id: 1, name: '卡券', type: 'text', text_content: 'CARD' }))
.mockResolvedValueOnce(jsonResponse({ success: true, total: 0, created: 0, failed: 0, rows: [] }))
.mockResolvedValueOnce(jsonResponse({ success: true, added: 1 }))
.mockResolvedValueOnce(jsonResponse([]))
.mockResolvedValueOnce(jsonResponse({ success: true, id: 1 }))
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({}))
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({ channel_ids: [] }))
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({ success: true }));
vi.stubGlobal('fetch', fetchMock);
await getSystemSettings();
await updateSystemSettings({ theme_color: 'blue' });
await getAllAISettings();
await getAccountAISettings('acc1');
await updateAccountAISettings('acc1', { ai_enabled: true });
await fetchAIModels('https://ai.example.com');
await getCards();
await createCard({ name: '卡券', type: 'text', text_content: 'CARD' });
await updateCard(1, { enabled: false });
await deleteCard(1);
await getCardDetails(1);
await batchCreateCards(new File(['name,type,content\n卡券,text,CARD'], 'cards.csv', { type: 'text/csv' }));
await appendCardData(1, 'CARD-2');
await getNotificationChannels();
await createNotificationChannel({ name: '通知', type: 'bark', config: {} });
await updateNotificationChannel('1', { enabled: false });
await deleteNotificationChannel('1');
await getMessageNotifications();
await setMessageNotification('acc1', 1, true);
await deleteMessageNotification('1');
await deleteAccountNotifications('acc1');
await getAccountBindings('acc1');
await setAccountBindings('acc1', [1]);
await testNotificationChannel('1');
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/settings/system', expect.objectContaining({ method: 'GET' }));
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/settings/system', expect.objectContaining({ method: 'PUT' }));
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/settings/ai-reply', expect.objectContaining({ method: 'GET' }));
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/settings/ai-reply/acc1', expect.objectContaining({ method: 'GET' }));
expect(fetchMock).toHaveBeenNthCalledWith(5, '/api/v1/settings/ai-reply/acc1', expect.objectContaining({ method: 'PUT' }));
expect(fetchMock).toHaveBeenNthCalledWith(6, '/api/v1/settings/ai-models', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(7, '/api/v1/cards', expect.objectContaining({ method: 'GET' }));
expect(fetchMock).toHaveBeenNthCalledWith(8, '/api/v1/cards', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(9, '/api/v1/cards/1', expect.objectContaining({ method: 'PUT' }));
expect(fetchMock).toHaveBeenNthCalledWith(10, '/api/v1/cards/1', expect.objectContaining({ method: 'DELETE' }));
expect(fetchMock).toHaveBeenNthCalledWith(11, '/api/v1/cards/1/details', expect.objectContaining({ method: 'GET' }));
expect(fetchMock).toHaveBeenNthCalledWith(12, '/api/v1/cards/batch', expect.objectContaining({ method: 'POST', body: expect.any(FormData) }));
expect(fetchMock).toHaveBeenNthCalledWith(13, '/api/v1/cards/1/append-data', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(14, '/api/v1/notifications/channels', expect.objectContaining({ method: 'GET' }));
expect(fetchMock).toHaveBeenNthCalledWith(15, '/api/v1/notifications/channels', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(16, '/api/v1/notifications/channels/1', expect.objectContaining({ method: 'PUT' }));
expect(fetchMock).toHaveBeenNthCalledWith(17, '/api/v1/notifications/channels/1', expect.objectContaining({ method: 'DELETE' }));
expect(fetchMock).toHaveBeenNthCalledWith(18, '/api/v1/notifications/messages', expect.objectContaining({ method: 'GET' }));
expect(fetchMock).toHaveBeenNthCalledWith(19, '/api/v1/notifications/accounts/acc1/bindings', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(20, '/api/v1/notifications/messages/1', expect.objectContaining({ method: 'DELETE' }));
expect(fetchMock).toHaveBeenNthCalledWith(21, '/api/v1/notifications/messages/account/acc1', expect.objectContaining({ method: 'DELETE' }));
expect(fetchMock).toHaveBeenNthCalledWith(22, '/api/v1/notifications/accounts/acc1/bindings', expect.objectContaining({ method: 'GET' }));
expect(fetchMock).toHaveBeenNthCalledWith(23, '/api/v1/notifications/accounts/acc1/bindings', expect.objectContaining({ method: 'POST' }));
expect(fetchMock).toHaveBeenNthCalledWith(24, '/api/v1/notifications/channels/1/test', expect.objectContaining({ method: 'POST' }));
};
test('settings, card, and notification APIs use versioned compatibility routes', runVersionedSettingsCardNotificationAPITest);
+24 -24
View File
@@ -290,7 +290,7 @@ export const updateAccountLoginInfo = async (id: string, data: {
};
export const getAllAISettings = async (options?: RequestControlOptions): Promise<Record<string, AIReplySettings>> => {
return get('/ai-reply-settings', undefined, options);
return get('/api/v1/settings/ai-reply', undefined, options);
};
// Orders
@@ -449,25 +449,25 @@ const cardPayload = (data: Partial<Card>): Record<string, unknown> => ({
});
export const getCards = async (): Promise<Card[]> => {
const res = await get<Card[] | CardListResponse>('/cards');
const res = await get<Card[] | CardListResponse>('/api/v1/cards');
const cards = Array.isArray(res) ? res : (res.cards || []);
return cards.map(normalizeCard);
};
export const createCard = async (data: Partial<Card>): Promise<MutationIDResponse> => {
return post('/cards', cardPayload(data));
return post('/api/v1/cards', cardPayload(data));
};
export const updateCard = async (cardId: string | number, data: Partial<Card>): Promise<OperationResponse> => {
return put(`/cards/${cardId}`, cardPayload(data));
return put(`/api/v1/cards/${cardId}`, cardPayload(data));
};
export const deleteCard = async (cardId: string | number): Promise<OperationResponse> => {
return del(`/cards/${cardId}`);
return del(`/api/v1/cards/${cardId}`);
};
export const getCardDetails = async (cardId: string | number): Promise<Card> => {
const card = await get<Card>(`/cards/${cardId}/details`);
const card = await get<Card>(`/api/v1/cards/${cardId}/details`);
return normalizeCard(card);
};
@@ -481,12 +481,12 @@ export const batchCreateCards = async (file: File): Promise<CardBatchResponse> =
// 此处只收紧 TypeScript 响应契约。
const body = new FormData();
body.append('file', file);
return postForm('/cards/batch', body);
return postForm('/api/v1/cards/batch', body);
};
// 往 data 类型卡密组批量追加卡密号
export const appendCardData = async (cardId: string | number, content: string): Promise<CardAppendResponse> => {
return post(`/cards/${cardId}/append-data`, { content });
return post(`/api/v1/cards/${cardId}/append-data`, { content });
};
// Items
@@ -880,7 +880,7 @@ export const deleteReplyRule = async (id: string, cookieId: string): Promise<Ope
// Settings
export const getSystemSettings = async (): Promise<SystemSettings> => {
const res = await get<{data: SystemSettings}>('/system-settings');
const res = await get<{data: SystemSettings}>('/api/v1/settings/system');
return normalizeSettings(res.data || res); // handle {success:true, data: {...}} wrapper if exists
};
@@ -888,11 +888,11 @@ export const updateSystemSettings = async (settings: Partial<SystemSettings>): P
const payload = Object.fromEntries(
Object.entries(settings).filter(([, value]) => value !== undefined && value !== null),
);
return put('/system-settings', payload);
return put('/api/v1/settings/system', payload);
};
export const getAccountAISettings = async (cookieId: string, options?: RequestControlOptions): Promise<AIReplySettingsResponse> => {
return get(`/ai-reply-settings/${cookieId}`, undefined, options);
return get(`/api/v1/settings/ai-reply/${cookieId}`, undefined, options);
}
export const updateAccountAISettings = async (cookieId: string, settings: Partial<AIReplySettings>): Promise<OperationResponse> => {
@@ -903,11 +903,11 @@ export const updateAccountAISettings = async (cookieId: string, settings: Partia
max_bargain_rounds: settings.max_bargain_rounds ?? 3,
custom_prompts: settings.custom_prompts ?? ''
};
return put(`/ai-reply-settings/${cookieId}`, payload);
return put(`/api/v1/settings/ai-reply/${cookieId}`, payload);
}
export const fetchAIModels = async (baseUrl: string, apiKey: string = ''): Promise<string[]> => {
const result = await post<AIModelsResponse>('/ai-models', {
const result = await post<AIModelsResponse>('/api/v1/settings/ai-models', {
base_url: baseUrl,
api_key: apiKey,
});
@@ -933,7 +933,7 @@ const stringifyNotificationEventTypes = (events?: NotificationEventType[]): stri
};
export const getNotificationChannels = async (options?: RequestControlOptions): Promise<{ success: boolean; data?: NotificationChannel[] }> => {
const result = await get<NotificationChannelResponse[]>('/notification-channels', undefined, options);
const result = await get<NotificationChannelResponse[]>('/api/v1/notifications/channels', undefined, options);
const channels = (result || []).map((item: any) => {
let parsedConfig;
try {
@@ -956,7 +956,7 @@ export const getNotificationChannels = async (options?: RequestControlOptions):
}
export const createNotificationChannel = async (data: { name: string; type: string; config: Record<string, unknown>; event_types?: NotificationEventType[]; enabled?: boolean }): Promise<MutationIDResponse> => {
return post('/notification-channels', {
return post('/api/v1/notifications/channels', {
...data,
config: JSON.stringify(data.config),
event_types: stringifyNotificationEventTypes(data.event_types)
@@ -971,16 +971,16 @@ export const updateNotificationChannel = async (channelId: string, data: { name?
if ('event_types' in data) {
payload.event_types = stringifyNotificationEventTypes(data.event_types);
}
return put(`/notification-channels/${channelId}`, payload);
return put(`/api/v1/notifications/channels/${channelId}`, payload);
}
export const deleteNotificationChannel = async (channelId: string): Promise<OperationResponse> => {
return del(`/notification-channels/${channelId}`);
return del(`/api/v1/notifications/channels/${channelId}`);
}
// Message Notifications
export const getMessageNotifications = async (): Promise<{ success: boolean; data?: NotificationBinding[] }> => {
const result = await get<Record<string, NotificationBinding[]>>('/message-notifications');
const result = await get<Record<string, NotificationBinding[]>>('/api/v1/notifications/messages');
const notifications: NotificationBinding[] = [];
for (const [cookieId, channelList] of Object.entries(result || {})) {
if (Array.isArray(channelList)) {
@@ -998,30 +998,30 @@ export const getMessageNotifications = async (): Promise<{ success: boolean; dat
}
export const setMessageNotification = async (cookieId: string, channelId: number, enabled: boolean): Promise<OperationResponse> => {
return post(`/message-notifications/${cookieId}`, { channel_id: channelId, enabled });
return post(`/api/v1/notifications/accounts/${cookieId}/bindings`, { channel_id: channelId, enabled });
}
export const deleteMessageNotification = async (notificationId: string): Promise<OperationResponse> => {
return del(`/message-notifications/${notificationId}`);
return del(`/api/v1/notifications/messages/${notificationId}`);
}
export const deleteAccountNotifications = async (cookieId: string): Promise<OperationResponse> => {
return del(`/message-notifications/account/${cookieId}`);
return del(`/api/v1/notifications/messages/account/${cookieId}`);
}
// 账号 ↔ 渠道 绑定(覆盖式)
export const getAccountBindings = async (cookieId: string, options?: RequestControlOptions): Promise<number[]> => {
const result = await get<AccountBindingsResponse>(`/message-notifications/${cookieId}`, undefined, options);
const result = await get<AccountBindingsResponse>(`/api/v1/notifications/accounts/${cookieId}/bindings`, undefined, options);
return result?.channel_ids || [];
}
export const setAccountBindings = async (cookieId: string, channelIds: number[]): Promise<OperationResponse> => {
return post(`/message-notifications/${cookieId}`, { channel_ids: channelIds });
return post(`/api/v1/notifications/accounts/${cookieId}/bindings`, { channel_ids: channelIds });
}
// 测试发送
export const testNotificationChannel = async (channelId: string): Promise<OperationResponse> => {
return post(`/notification-channels/${channelId}/test`, {});
return post(`/api/v1/notifications/channels/${channelId}/test`, {});
}
// Default Reply