100 lines
2.7 KiB
JavaScript
100 lines
2.7 KiB
JavaScript
|
|
import { fetcher } from 'pankow';
|
|
import { API_ORIGIN } from '../constants.js';
|
|
|
|
function create() {
|
|
const accessToken = localStorage.token;
|
|
|
|
return {
|
|
async list(domain, search = '') {
|
|
let result;
|
|
try {
|
|
result = await fetcher.get(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes`, { page: 1, per_page: 1000, access_token: accessToken });
|
|
} catch (e) {
|
|
return [e];
|
|
}
|
|
|
|
if (result.status !== 200) return [result];
|
|
return [null, result.body.mailboxes];
|
|
},
|
|
async get(domain, name) {
|
|
let result;
|
|
try {
|
|
result = await fetcher.get(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes/${name}`, { access_token: accessToken });
|
|
} catch (e) {
|
|
return [e];
|
|
}
|
|
|
|
if (result.status !== 200) return [result];
|
|
return [null, result.body.mailbox];
|
|
},
|
|
async add(domain, name, options) {
|
|
const data = {
|
|
name: name,
|
|
ownerId: options.ownerId,
|
|
ownerType: options.ownerType,
|
|
active: !!options.active,
|
|
enablePop3: !!options.enablePop3,
|
|
storageQuota: options.storageQuota ||0,
|
|
messagesQuota: options.messagesQuota || 0,
|
|
};
|
|
|
|
let result;
|
|
try {
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes`, data, { access_token: accessToken });
|
|
} catch (e) {
|
|
return [e];
|
|
}
|
|
|
|
if (result.status !== 201) return [result];
|
|
return [null];
|
|
},
|
|
async update(domain, name, options) {
|
|
const data = {
|
|
ownerId: options.ownerId,
|
|
ownerType: options.ownerType,
|
|
active: !!options.active,
|
|
enablePop3: !!options.enablePop3,
|
|
storageQuota: options.storageQuota ||0,
|
|
messagesQuota: options.messagesQuota || 0,
|
|
};
|
|
|
|
let result;
|
|
try {
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes/${name}`, data, { access_token: accessToken });
|
|
} catch (e) {
|
|
return [e];
|
|
}
|
|
|
|
if (result.status !== 204) return [result];
|
|
return [null];
|
|
},
|
|
async remove(domain, name, deleteMails = false) {
|
|
let result;
|
|
try {
|
|
result = await fetcher.del(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes/${name}`, { deleteMails }, { access_token: accessToken });
|
|
} catch (e) {
|
|
return [e];
|
|
}
|
|
|
|
if (result.status !== 201) return [result];
|
|
return [null];
|
|
},
|
|
async setAliases(domain, name, aliases) {
|
|
let result;
|
|
try {
|
|
result = await fetcher.put(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes/${name}/aliases`, { aliases }, { access_token: accessToken });
|
|
} catch (e) {
|
|
return [e];
|
|
}
|
|
|
|
if (result.status !== 202) return [result];
|
|
return [null];
|
|
},
|
|
};
|
|
}
|
|
|
|
export default {
|
|
create,
|
|
};
|