feat: add config.service

pull/56/head
moonrailgun 2 years ago
parent dfcf129a3f
commit fe8e532a37

@ -96,7 +96,7 @@ export {
// model // model
export * as model from './model/__all__'; export * as model from './model/__all__';
export { fetchAvailableServices } from './model/common'; export { fetchAvailableServices } from './model/common';
export { fetchGlobalConfig } from './model/config'; export { fetchGlobalClientConfig } from './model/config';
export { export {
createDMConverse, createDMConverse,
appendDMConverseMembers, appendDMConverseMembers,

@ -21,8 +21,8 @@ export function getGlobalConfig() {
}; };
} }
export async function fetchGlobalConfig(): Promise<GlobalConfig> { export async function fetchGlobalClientConfig(): Promise<GlobalConfig> {
const { data: config } = await request.get('/api/config/global'); const { data: config } = await request.get('/api/config/client');
globalConfig = { globalConfig = {
...globalConfig, ...globalConfig,

@ -10,7 +10,7 @@ import {
setTokenGetter, setTokenGetter,
showErrorToasts, showErrorToasts,
t, t,
fetchGlobalConfig, fetchGlobalClientConfig,
request, request,
isValidStr, isValidStr,
isDevelopment, isDevelopment,
@ -107,7 +107,7 @@ setErrorHook((err) => {
/** /**
* *
*/ */
fetchGlobalConfig().catch((e) => { fetchGlobalClientConfig().catch((e) => {
showErrorToasts(t('全局配置加载失败')); showErrorToasts(t('全局配置加载失败'));
console.error('全局配置加载失败', e); console.error('全局配置加载失败', e);
}); });

@ -1,14 +1,11 @@
import { import { TcService, TcPureContext, config } from 'tailchat-server-sdk';
TcService,
TcDbService,
TcPureContext,
config,
} from 'tailchat-server-sdk';
/** /**
* *
*/ */
class ConfigService extends TcService { class ConfigService extends TcService {
config = {};
get serviceName(): string { get serviceName(): string {
return 'config'; return 'config';
} }
@ -19,19 +16,52 @@ class ConfigService extends TcService {
* *
* 使 * 使
*/ */
this.registerAction('global', this.globalConfig); this.registerAction('client', this.client);
this.registerAction('all', this.all, {
visibility: 'public',
});
this.registerAction('get', this.get, {
visibility: 'public',
params: {
key: 'string',
},
});
this.registerAction('set', this.set, {
visibility: 'public',
params: {
key: 'string',
value: 'any',
},
});
this.registerAuthWhitelist(['/config/global']); this.registerAuthWhitelist(['/config/client']);
} }
/** /**
* *
*
* 使d
*/ */
async globalConfig(ctx: TcPureContext) { async client(ctx: TcPureContext) {
return { return {
uploadFileLimit: config.storage.limit, uploadFileLimit: config.storage.limit,
}; };
} }
async all(ctx: TcPureContext) {
return this.config;
}
async get(ctx: TcPureContext<{ key: string }>) {
return this.config[ctx.params.key] ?? null;
}
async set(ctx: TcPureContext<{ key: string; value: string }>) {
const { key, value } = ctx.params;
this.config[key] = value;
await this.broker.broadcast('config.updated', this.config);
}
} }
export default ConfigService; export default ConfigService;

Loading…
Cancel
Save