You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailchat/web/src/plugin/common/index.ts

120 lines
2.7 KiB
TypeScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 同步加载的代码
* 用于导出常见的模块依赖
*/
import _pick from 'lodash/pick';
export * from './reg';
export { useGroupPanelParams } from '@/routes/Main/Content/Group/utils';
export {
openModal,
closeModal,
ModalWrapper,
useModalContext,
} from '@/components/Modal';
export { Loadable } from '@/components/Loadable';
export { getGlobalState } from '@/utils/global-state-helper';
export { getJWTUserInfo } from '@/utils/jwt-helper';
export { dataUrlToFile } from '@/utils/file-helper';
export {
urlSearchStringify,
urlSearchParse,
appendUrlSearch,
} from '@/utils/url-helper';
export { useGroupIdContext } from '@/context/GroupIdContext';
import { request, RequestConfig, useUserInfo } from 'tailchat-shared';
export {
getServiceUrl,
getCachedUserInfo,
getCachedConverseInfo,
localTrans,
getLanguage,
sharedEvent,
useAsync,
useAsyncFn,
useAsyncRefresh,
useAsyncRequest,
uploadFile,
showToasts,
showErrorToasts,
fetchAvailableServices,
isValidStr,
useGroupPanelInfo,
sendMessage,
} from 'tailchat-shared';
export { useLocation, useHistory } from 'react-router';
export {
/**
* @deprecated please use createMetaFormSchema
*/
createMetaFormSchema as createFastFormSchema,
/**
* @deprecated please use metaFormFieldSchema
*/
metaFormFieldSchema as fieldSchema,
} from 'tailchat-design';
/**
* 插件版本的useUserInfo
*/
export function useCurrentUserInfo() {
const userInfo = useUserInfo();
return _pick(userInfo, ['email', 'nickname', 'discriminator', 'avatar']);
}
/**
* 处理axios的request config
*
* 为了防止用户的jwt因为请求被传递到其他地方
*/
function purgeRequestConfig(config?: RequestConfig) {
if (!config) {
return undefined;
}
return _pick(config, [
'transformRequest',
'transformResponse',
'headers',
'params',
'data',
'timeout',
'withCredentials',
'xsrfCookieName',
'xsrfHeaderName',
]);
}
/**
* 插件仅可以通过这种方式进行网络请求发送
*/
export function createPluginRequest(pluginName: string) {
return {
get(actionName: string, config?: RequestConfig) {
return request.get(
`/api/plugin:${pluginName}/${actionName.replaceAll('.', '/')}`,
purgeRequestConfig(config)
);
},
post(actionName: string, data?: any, config?: RequestConfig) {
return request.post(
`/api/plugin:${pluginName}/${actionName.replaceAll('.', '/')}`,
data,
purgeRequestConfig(config)
);
},
};
}
/**
* 发起一个网络请求
*
* 与上面的相比是不限定在plugin中的
*/
export function postRequest(url: string, data?: any, config?: RequestConfig) {
return request.post(`/api${url}`, data, purgeRequestConfig(config));
}