2020-11-23 17:36:33 +01:00
|
|
|
import apiClient from './api-client';
|
|
|
|
import { AxiosResponse } from 'axios';
|
2020-11-24 16:26:18 +01:00
|
|
|
import { Setting, SettingName } from '../models/setting';
|
2020-11-23 17:36:33 +01:00
|
|
|
import wrapPromise, { IWrapPromise } from '../lib/wrap-promise';
|
|
|
|
|
|
|
|
export default class SettingAPI {
|
2020-11-24 16:26:18 +01:00
|
|
|
async get (name: SettingName): Promise<Setting> {
|
2020-11-23 17:36:33 +01:00
|
|
|
const res: AxiosResponse = await apiClient.get(`/api/settings/${name}`);
|
|
|
|
return res?.data?.setting;
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:26:18 +01:00
|
|
|
async query (names: Array<SettingName>): Promise<Map<SettingName, any>> {
|
|
|
|
const res: AxiosResponse = await apiClient.get(`/api/settings/?names=[${names.join(',')}]`);
|
2021-03-24 17:31:50 +01:00
|
|
|
return SettingAPI.toSettingsMap(res?.data);
|
2020-11-24 16:26:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static get (name: SettingName): IWrapPromise<Setting> {
|
2020-11-23 17:36:33 +01:00
|
|
|
const api = new SettingAPI();
|
|
|
|
return wrapPromise(api.get(name));
|
|
|
|
}
|
2020-11-24 16:26:18 +01:00
|
|
|
|
|
|
|
static query(names: Array<SettingName>): IWrapPromise<Map<SettingName, any>> {
|
|
|
|
const api = new SettingAPI();
|
|
|
|
return wrapPromise(api.query(names));
|
|
|
|
}
|
2021-03-24 17:31:50 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
static toSettingsMap(data: Object): Map<SettingName, any> {
|
|
|
|
const dataArray: Array<Array<string | any>> = Object.entries(data);
|
|
|
|
const map = new Map();
|
|
|
|
dataArray.forEach(item => {
|
|
|
|
map.set(SettingName[item[0]], item[1]);
|
|
|
|
});
|
|
|
|
return map;
|
|
|
|
}
|
2020-11-23 17:36:33 +01:00
|
|
|
}
|
|
|
|
|