feat: add frontend/src/api/mailApi.ts
This commit is contained in:
parent
79a02d2c0b
commit
e4f634f3fb
46
frontend/src/api/mailApi.ts
Normal file
46
frontend/src/api/mailApi.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import axios from 'axios'
|
||||
import { useMailStore } from '../store/mailStore'
|
||||
|
||||
const api = axios.create({ baseURL: '/api' })
|
||||
|
||||
api.interceptors.request.use(cfg => {
|
||||
const token = useMailStore.getState().token
|
||||
if (token) cfg.headers.Authorization = `Bearer ${token}`
|
||||
return cfg
|
||||
})
|
||||
|
||||
api.interceptors.response.use(
|
||||
r => r,
|
||||
e => {
|
||||
if (e.response?.status === 401) useMailStore.getState().logout()
|
||||
return Promise.reject(e)
|
||||
}
|
||||
)
|
||||
|
||||
export const authApi = {
|
||||
login: (username: string, password: string) =>
|
||||
api.post('/auth/login', { username, password }).then(r => r.data),
|
||||
logout: () => api.post('/auth/logout'),
|
||||
}
|
||||
|
||||
export const mailApi = {
|
||||
folders: () => api.get('/mail/folders').then(r => r.data),
|
||||
messages: (folder: string, page = 1, q?: string) =>
|
||||
api.get('/mail/messages', { params: { folder, page, per_page: 50, q } }).then(r => r.data),
|
||||
detail: (uid: string, folder: string) =>
|
||||
api.get(`/mail/messages/${uid}`, { params: { folder } }).then(r => r.data),
|
||||
markRead: (uid: string, folder: string, read = true) =>
|
||||
api.put(`/mail/messages/${uid}/read`, null, { params: { folder, read } }),
|
||||
move: (uid: string, folder: string, target: string) =>
|
||||
api.put(`/mail/messages/${uid}/move`, { target_folder: target }, { params: { folder } }),
|
||||
delete: (uid: string, folder: string) =>
|
||||
api.delete(`/mail/messages/${uid}`, { params: { folder } }),
|
||||
send: (data: {
|
||||
to: string; cc?: string; bcc?: string; subject: string
|
||||
body: string; is_html?: boolean; reply_to_uid?: string
|
||||
}) => api.post('/mail/send', data),
|
||||
attachmentUrl: (uid: string, partId: string, folder: string) =>
|
||||
`/api/mail/messages/${uid}/attachments/${partId}?folder=${encodeURIComponent(folder)}`,
|
||||
}
|
||||
|
||||
export default api
|
||||
Loading…
Reference in New Issue
Block a user