81 lines
2.0 KiB
TypeScript
81 lines
2.0 KiB
TypeScript
export interface EmailRequest {
|
|
id: number;
|
|
email: string;
|
|
status: string;
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
}
|
|
|
|
export interface CreateEmailRequestDto {
|
|
email: string;
|
|
status?: string;
|
|
}
|
|
|
|
export interface UpdateEmailRequestDto {
|
|
email?: string;
|
|
status?: string;
|
|
}
|
|
|
|
const API_BASE = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8080';
|
|
|
|
export async function fetchEmailRequests(token: string): Promise<EmailRequest[]> {
|
|
const response = await fetch(`${API_BASE}/api/email`, {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch email requests: ${response.statusText}`);
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function createEmailRequest(token: string, data: CreateEmailRequestDto): Promise<EmailRequest> {
|
|
const response = await fetch(`${API_BASE}/api/email`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
body: JSON.stringify(data),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to create email request: ${response.statusText}`);
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function updateEmailRequest(token: string, id: number, data: UpdateEmailRequestDto): Promise<EmailRequest> {
|
|
const response = await fetch(`${API_BASE}/api/email/${id}`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
body: JSON.stringify(data),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to update email request: ${response.statusText}`);
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function deleteEmailRequest(token: string, id: number): Promise<void> {
|
|
const response = await fetch(`${API_BASE}/api/email/${id}`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to delete email request: ${response.statusText}`);
|
|
}
|
|
}
|