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/shared/cache/utils.ts

26 lines
699 B
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import type { FetchQueryOptions } from 'react-query';
import { queryClient } from './';
/**
* 构建缓存请求
* TODO: 这里的类型真的不好写, 先用any来过滤内部的, 只保证外部使用ok
*
* @example
* const queryData = buildCachedRequest('key')((arg1, arg2) => {
* return request.post(...)
* })
*/
export function buildCachedRequest<
R extends any,
F extends (...args: any) => Promise<R>
>(name: string, fn: F, options?: FetchQueryOptions): F {
return ((...args: any) => {
return queryClient.fetchQuery(
[name, JSON.stringify(args)],
() => fn(...args),
options
);
}) as any;
}