81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
|
|
import { http } from './http';
|
||
|
|
|
||
|
|
export interface WebhookItem {
|
||
|
|
id: string;
|
||
|
|
name: string;
|
||
|
|
status: string;
|
||
|
|
createdAt: string;
|
||
|
|
updatedAt?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface WebhookDataSource {
|
||
|
|
list(): Promise<WebhookItem[]>;
|
||
|
|
create(data: Partial<WebhookItem>): Promise<WebhookItem>;
|
||
|
|
update(id: string, data: Partial<WebhookItem>): Promise<WebhookItem>;
|
||
|
|
delete(id: string): Promise<void>;
|
||
|
|
}
|
||
|
|
|
||
|
|
class WebhookApiDataSource implements WebhookDataSource {
|
||
|
|
async list() {
|
||
|
|
const response = await http.get('/api/webhook/list');
|
||
|
|
return response.data.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async create(data: Partial<WebhookItem>) {
|
||
|
|
const response = await http.post('/api/webhook', data);
|
||
|
|
return response.data.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async update(id: string, data: Partial<WebhookItem>) {
|
||
|
|
const response = await http.put(`/api/webhook/${id}`, data);
|
||
|
|
return response.data.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async delete(id: string) {
|
||
|
|
await http.delete(`/api/webhook/${id}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class WebhookMockDataSource implements WebhookDataSource {
|
||
|
|
private items: WebhookItem[] = [
|
||
|
|
{ id: '1', name: '示例项目 1', status: 'ACTIVE', createdAt: new Date().toISOString() },
|
||
|
|
{ id: '2', name: '示例项目 2', status: 'INACTIVE', createdAt: new Date().toISOString() },
|
||
|
|
];
|
||
|
|
|
||
|
|
async list() {
|
||
|
|
return this.items;
|
||
|
|
}
|
||
|
|
|
||
|
|
async create(data: Partial<WebhookItem>) {
|
||
|
|
const item: WebhookItem = {
|
||
|
|
id: String(this.items.length + 1),
|
||
|
|
name: data.name || '',
|
||
|
|
status: data.status || 'ACTIVE',
|
||
|
|
createdAt: new Date().toISOString(),
|
||
|
|
};
|
||
|
|
this.items.push(item);
|
||
|
|
return item;
|
||
|
|
}
|
||
|
|
|
||
|
|
async update(id: string, data: Partial<WebhookItem>) {
|
||
|
|
const index = this.items.findIndex(item => item.id === id);
|
||
|
|
if (index >= 0) {
|
||
|
|
this.items[index] = { ...this.items[index], ...data };
|
||
|
|
return this.items[index];
|
||
|
|
}
|
||
|
|
throw new Error('Item not found');
|
||
|
|
}
|
||
|
|
|
||
|
|
async delete(id: string) {
|
||
|
|
const index = this.items.findIndex(item => item.id === id);
|
||
|
|
if (index >= 0) {
|
||
|
|
this.items.splice(index, 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const useMock = process.env.NODE_ENV === 'development' || process.env.REACT_APP_USE_MOCK === 'true';
|
||
|
|
export const WebhookDataSource: WebhookDataSource = useMock
|
||
|
|
? new WebhookMockDataSource()
|
||
|
|
: new WebhookApiDataSource();
|