@ureq/business
v0.0.3
Published
Universal request library business logic
Readme
@ureq/business
业务抽象层,提供 Hash 和 Cache 服务的接口定义和默认实现。
安装
npm install @ureq/business
# 或
pnpm add @ureq/business注意:通常作为
@ureq/core的依赖自动安装,无需单独安装
功能
HashService
提供请求哈希生成功能,用于请求去重和缓存键生成。
import { DefaultHashService } from '@ureq/business';
const hashService = new DefaultHashService();
// 生成请求哈希
const hash = hashService.generateRequestHash(
'GET',
'/api/users',
null,
{ params: { page: 1 } }
);
// 生成字符串哈希
const strHash = hashService.hashString('some-string');CacheStore
提供缓存存储的接口定义。
interface CacheStore {
get<T>(key: string): Promise<T | null>;
set<T>(key: string, value: T, ttl: number): Promise<void>;
delete(key: string): Promise<void>;
clear(): Promise<void>;
}接口
HashService
interface HashService {
generateRequestHash(
method: string,
url: string,
data?: any,
options?: RequestOptions
): string;
hashString(str: string): string;
}CacheStore
interface CacheStore {
get<T>(key: string): Promise<T | null>;
set<T>(key: string, value: T, ttl: number): Promise<void>;
delete(key: string): Promise<void>;
clear(): Promise<void>;
}自定义实现
自定义 HashService
import { HashService } from '@ureq/business';
class MyHashService implements HashService {
generateRequestHash(method: string, url: string, data?: any, options?: any): string {
// 自定义哈希生成逻辑
return `${method}:${url}`;
}
hashString(str: string): string {
// 自定义字符串哈希
return btoa(str);
}
}自定义 CacheStore
import { CacheStore } from '@ureq/business';
class MyCache implements CacheStore {
async get<T>(key: string): Promise<T | null> {
// 实现获取逻辑
}
async set<T>(key: string, value: T, ttl: number): Promise<void> {
// 实现存储逻辑
}
async delete(key: string): Promise<void> {
// 实现删除逻辑
}
async clear(): Promise<void> {
// 实现清空逻辑
}
}文档
查看完整文档:https://sunny-117.github.io/ureq
License
MIT
