48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { CaptchaResult, Setting } from "../types";
|
|
import { axios } from "./BaseAPI";
|
|
|
|
function getBaseURI(): string {
|
|
const baseURL = axios.defaults.baseURL;
|
|
if (!baseURL || baseURL === "undefined") {
|
|
return "";
|
|
}
|
|
return baseURL.replace(/\/$/, "");
|
|
}
|
|
|
|
const getCaptchaAPI = () => `${getBaseURI()}/captcha`;
|
|
|
|
async function captcha(width: number, height: number): Promise<CaptchaResult> {
|
|
return await axios.get(`/captcha?width=${width}&height=${height}`);
|
|
}
|
|
|
|
const getAttachmentReadAPI = (id: string) => `${getBaseURI()}/attach/read/${id}`;
|
|
|
|
const getAttachmentTempReadAPI = (id: string) => `${getBaseURI()}/attach/temp/read?id=${id}`;
|
|
|
|
async function settingMap(map: Record<string, string[]>): Promise<Map<string, Map<string, Setting>>> {
|
|
const raw = await axios.post("/setting/map", map);
|
|
const moduleMap = new Map<string, Map<string, Setting>>();
|
|
if (!raw || typeof raw !== "object") {
|
|
return moduleMap;
|
|
}
|
|
for (const [module, settingRawMap] of Object.entries(raw as Record<string, unknown>)) {
|
|
if (!settingRawMap || typeof settingRawMap !== "object") {
|
|
continue;
|
|
}
|
|
const settingMap = new Map<string, Setting>();
|
|
for (const [key, setting] of Object.entries(settingRawMap as Record<string, Setting>)) {
|
|
settingMap.set(key, setting);
|
|
}
|
|
moduleMap.set(module, settingMap);
|
|
}
|
|
return moduleMap;
|
|
}
|
|
|
|
export default {
|
|
getCaptchaAPI,
|
|
captcha,
|
|
getAttachmentReadAPI,
|
|
getAttachmentTempReadAPI,
|
|
settingMap
|
|
};
|