🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

axios-cache-data

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

axios-cache-data - npm Package Compare versions

Comparing version
1.20.9
to
1.20.10
+1
-1
package.json
{
"name": "axios-cache-data",
"version": "1.20.9",
"version": "1.20.10",
"type": "module",

@@ -5,0 +5,0 @@ "description": "自定义缓存数据,接口数据不会经常变化,请求本地化缓存减少服务器压力",

+33
-37

@@ -6,11 +6,10 @@ <!-- @format -->

![axios-data](https://badgen.net/badge/axios-cache-data/1.20.9/blue?icon=github)
![axios](https://badgen.net/badge/axios/^1.2.2/green?icon=github) ![vite](https://badgen.net/badge/vite/^4.0.3/blue?icon=github)
![axios](https://badgen.net/badge/axios/^1.2.2/green?icon=github)
![vite](https://badgen.net/badge/vite/^4.0.3/blue?icon=github)
> `axios`官方文档在这 [axios](https://axios-http.com/)
> `axios` 官方文档在这 [axios](https://axios-http.com/)
> Tip:被缓存后所有的响应信息都是初次请求状态,服务器删除该数据或者修改该数据都不会有感知 ,可以设置缓存时间
> 增加请求分组
> **提示**:被缓存后所有的响应信息都是初次请求状态,服务器删除或修改该数据都不会有感知。可以设置缓存时间或增加请求分组。
> Tip:代理 axios 对象 实现缓存
> Tip: 取消 axios adapter 的实现方式
> **提示**:代理 `axios` 对象实现缓存,取消 `axios adapter` 的实现方式。

@@ -20,10 +19,10 @@ <!-- TOC -->

- [axios-cache-data](#axios-cache-data)
- [1. 创建缓存类](#1-创建缓存类)
- [2. 或者创建缓存实例](#2-或者创建缓存实例)
- [3. 强制更新或使用缓存](#3-强制更新或使用缓存)
- [3.1. 序列化数据](#31-序列化数据)
- [4. 参数说明](#4-参数说明)
- [4.1. 创建适配器的公共参数](#41-创建适配器的公共参数)
- [4.2. 每一个请求缓存参数](#42-每一个请求缓存参数)
- [5. 执行流程图](#5-执行流程图)
- [1. 创建缓存类](#1-创建缓存类)
- [2. 创建缓存实例](#2-创建缓存实例)
- [3. 强制更新或使用缓存](#3-强制更新或使用缓存)
- [3.1. 序列化数据](#31-序列化数据)
- [4. 参数说明](#4-参数说明)
- [4.1. 创建适配器的公共参数](#41-创建适配器的公共参数)
- [4.2. 每个请求的缓存参数](#42-每个请求的缓存参数)
- [5. 执行流程图](#5-执行流程图)

@@ -58,3 +57,3 @@ <!-- /TOC -->

## 2. 或者创建缓存实例
## 2. 创建缓存实例

@@ -149,31 +148,28 @@ ```typescript

---
### 4.1. 创建适配器的公共参数
| 参数 | 介绍 | 默认值 |
|:-----------:|:----------------------------------------:|---------------------------|
| maxAge | 缓存最大时间 | **1000 _ 60 _ 60** (单位毫秒) |
| key | 生成 key 标识 | **HTTP_CACHE_CACHE** |
| storage | 缓存组件 | **window.sessionStorage** |
| prefix | 网络缓 key 存前缀 | **AXIOS-CACHE** |
| enableCache | 开启全局缓存 | **false** |
| generateKey | 生成一个请求唯一标识 key | |
| message | 消息序列化和反序列化 | |
| valid | 验证消息是否正确防止消息错误却被缓存 | |
| proxy | 指定缓存方法`request` `get` `post` `delete`... | ["get","post"] |
| 参数 | 介绍 | 默认值 |
|--------------|-------------------------------|------------------------------|
| maxAge | 缓存最大时间 | **1000 _ 60 _ 60** (单位毫秒) |
| key | 生成 key 标识 | **HTTP_CACHE_CACHE** |
| storage | 缓存组件 | **window.sessionStorage** |
| prefix | 网络缓 key 存前缀 | **AXIOS-CACHE** |
| enableCache | 开启全局缓存 | **false** |
| generateKey | 生成一个请求唯一标识 key | |
| message | 消息序列化和反序列化 | |
| valid | 验证消息是否正确防止消息错误却被缓存 | |
| proxy | 指定缓存方法 | ["get", "post"] |
### 4.2. 每一个请求缓存参数
### 4.2. 每个请求的缓存参数
| hit | 是否需要命中缓存(优先级高于全局配置) |
|:------:|:-----------------------:|
| force | 强制走网络请求,并刷新缓存(如果开启了缓存的) |
| expire | 过期时间 |
| valid | 验证消息是否正确防止消息错误却被缓存 |
| group | 对请求分组,在清除缓存可以定向清除 |
| 参数 | 介绍 |
|---------|-----------------------------------|
| hit | 是否需要命中缓存(优先级高于全局配置) |
| force | 强制走网络请求,并刷新缓存 |
| expire | 过期时间 |
| valid | 验证消息是否正确防止消息错误却被缓存 |
| group | 对请求分组,在清除缓存时可定向清除 |
> hit false 全局开启缓存也不会命中缓存
## 5. 执行流程图
![](流程图.png)
/** @format */
import { Deserialization, Serialization } from "@/cache";
export declare abstract class HttpCacheLike {
protected storage: Storage;
protected readonly prefix: string;
protected message: {
serialization: Serialization;
deserialization: Deserialization;
};
protected constructor(storage: Storage, message: {
serialization: Serialization;
deserialization: Deserialization;
}, prefix: string);
abstract get(key: string, timeout: number): unknown | null;
abstract set(requestKey: string, axiosPromise: unknown): void;
abstract clear(key?: string): void;
abstract clear(key: string, group: string | number): void;
}
export declare class HttpCache extends HttpCacheLike {
constructor(storage: Storage, message: {
serialization: Serialization;
deserialization: Deserialization;
}, prefix: any);
/**
* 是否过期
* @param {number} expired 过期时间
* @param {number} oldTime 旧的时间
* @return {true} 过期
* @return {false} 没有过期
* @private
*/
private static isExpired;
get(key: string, timeout: number): any | null;
set(key: string, axiosPromise: object): void;
/**
* 删除key
* @param {string} key 存在则删除 不存在则删除所有 包含key 的
*/
clear(key?: string | null, group?: string | number): void;
}
/** @format */
import { CacheMessage, Deserialization, Serialization } from "@/cache/HttpMessage";
export declare class SerializationMessageImpl extends Serialization {
serialization(value: CacheMessage): string;
}
export declare class DeserializationMessageImpl extends Deserialization {
deserialization(value: string): CacheMessage;
}
/** @format */
type CacheMessage = {
readonly value: any;
readonly expire: number;
};
declare abstract class Serialization {
/**
*将对象序列化
* @param value
* @returns {string}
*/
abstract serialization(value: CacheMessage): string;
serializationKey(value: string): string;
}
declare abstract class Deserialization {
abstract deserialization(value: string): CacheMessage;
deserializationKey(value: string): string;
}
export { Serialization, Deserialization, CacheMessage };
export * from "./HttpCache";
export * from "./HttpCacheMessage";
export * from "./HttpMessage";
declare class LRUCacheSessionStorage<T extends string> implements Storage {
maxSize: number;
cache: {
[key: string]: T;
};
queue: string[];
readonly length: number;
[name: string]: any;
clear(): void;
getItem(key: string): string | null;
key(index: number): string | null;
removeItem(key: string): void;
setItem(key: string, value: T): void;
constructor(maxSize?: number);
}
export { LRUCacheSessionStorage };
import { CacheInstance } from "@/type";
import { AxiosRequestConfig, AxiosResponse } from "axios";
declare abstract class Cache {
protected readonly defaultOptions: Required<CacheInstance>;
private readonly cache;
private proxyMethod;
protected constructor(options: CacheInstance);
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
clear(config?: AxiosRequestConfig): void;
private getOnfulfilled;
private realRequest;
private proxyRequest;
private beforeStore;
private key;
}
declare class CacheAxios extends Cache {
constructor(options?: CacheInstance);
static create(options?: CacheInstance): CacheAxios;
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
clear(config?: AxiosRequestConfig<any> | undefined): void;
getUri(config: AxiosResponse<any>): Promise<AxiosResponse<any, any>>;
get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
postForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
putForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
patchForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
private beforeConfig;
}
export default CacheAxios;
import { CacheInstance } from "@/type";
import { AxiosInstance, CreateAxiosDefaults } from "axios";
declare module "axios" {
interface AxiosStatic {
withCache(config: CreateAxiosDefaults, options: CacheInstance): AxiosInstance;
}
interface AxiosInstance {
clear(): void;
}
interface AxiosRequestConfig {
readonly hit?: boolean;
readonly force?: boolean;
readonly expire?: number;
readonly group?: string | number;
valid?(response: AxiosResponse): boolean;
}
}
declare function http(config: CreateAxiosDefaults, options?: CacheInstance): AxiosInstance;
declare namespace http {
var proxy: typeof import("./CacheHttp").proxy;
}
declare function proxy(instance: AxiosInstance, options?: CacheInstance): AxiosInstance;
export { http, proxy };
/** @format */
export { http, proxy } from "@/core/CacheHttp";
export * from "@/core/Proxy";
export { default } from "@/core/CacheAxios";
import { HttpCache } from "@/cache";
import { CacheInstance } from "@/type";
interface CacheProxy<T extends object> {
(method: string, options: Omit<Required<CacheInstance>, "adapter"> & {
cache: HttpCache;
}): ProxyHandler<T>;
}
declare const ThreeProxy: CacheProxy<any>;
declare const TwoProxy: CacheProxy<any>;
declare const OneProxy: CacheProxy<any>;
export { CacheProxy, OneProxy, TwoProxy, ThreeProxy };
import { AxiosResponse as AxiosResponse$1, AxiosRequestConfig, CreateAxiosDefaults, AxiosInstance } from 'axios';
/** @format */
type CacheMessage = {
readonly value: any;
readonly expire: number;
};
declare abstract class Serialization {
/**
*将对象序列化
* @param value
* @returns {string}
*/
abstract serialization(value: CacheMessage): string;
serializationKey(value: string): string;
}
declare abstract class Deserialization {
abstract deserialization(value: string): CacheMessage;
deserializationKey(value: string): string;
}
/** @format */
interface RequestOption {
readonly hit?: boolean;
readonly force?: boolean;
readonly expire?: number;
readonly group?: string | number;
valid?(response: AxiosResponse$1): boolean;
}
interface CacheInstance {
/**
* @default 1000*60*60
*/
readonly maxAge?: number;
readonly key?: string;
readonly storage?: Storage;
readonly proxy?: Method[];
readonly adapter?: Adapter | Adapter[];
readonly enableCache?: boolean | ((url: string, method: string) => boolean);
/**
* 公共的key前缀 生成key需要获取到
*/
readonly prefix?: string;
/**
* 消息转换
*/
readonly message?: {
serialization: Serialization;
deserialization: Deserialization;
};
valid?(response: AxiosResponse$1): boolean;
generateKey?(key: string | undefined, group: string | number, url: string | undefined, method: unknown, header: unknown, params: string, data: string | object | any): string;
}
type Method = "request" | "get" | "delete" | "head" | "options" | "post" | "put" | "patch" | "clear";
type Adapter = (config: AxiosRequestConfig) => Promise<any>;
declare module "axios" {
interface AxiosStatic {
withCache(config: CreateAxiosDefaults, options: CacheInstance): AxiosInstance;
}
interface AxiosInstance {
clear(): void;
}
interface AxiosRequestConfig {
readonly hit?: boolean;
readonly force?: boolean;
readonly expire?: number;
readonly group?: string | number;
valid?(response: AxiosResponse): boolean;
}
}
declare function http(config: CreateAxiosDefaults, options?: CacheInstance): AxiosInstance;
declare namespace http {
var proxy: typeof proxy;
}
declare function proxy(instance: AxiosInstance, options?: CacheInstance): AxiosInstance;
declare abstract class Cache {
protected readonly defaultOptions: Required<CacheInstance>;
private readonly cache;
private proxyMethod;
protected constructor(options: CacheInstance);
request<T = any, R = AxiosResponse$1<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
clear(config?: AxiosRequestConfig): void;
private getOnfulfilled;
private realRequest;
private proxyRequest;
private beforeStore;
private key;
}
declare class CacheAxios extends Cache {
constructor(options?: CacheInstance);
static create(options?: CacheInstance): CacheAxios;
request<T = any, R = AxiosResponse$1<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
clear(config?: AxiosRequestConfig<any> | undefined): void;
getUri(config: AxiosResponse$1<any>): Promise<AxiosResponse$1<any, any>>;
get<T = any, R = AxiosResponse$1<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
delete<T = any, R = AxiosResponse$1<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
head<T = any, R = AxiosResponse$1<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
options<T = any, R = AxiosResponse$1<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
post<T = any, R = AxiosResponse$1<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
put<T = any, R = AxiosResponse$1<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
patch<T = any, R = AxiosResponse$1<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
postForm<T = any, R = AxiosResponse$1<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
putForm<T = any, R = AxiosResponse$1<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
patchForm<T = any, R = AxiosResponse$1<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
private beforeConfig;
}
export { CacheInstance, Method, RequestOption, CacheAxios as default, http, proxy };
import e from"axios";function t(e){return Object.is(e,void 0)||Object.is(e,null)}class s extends class{storage;prefix;message;constructor(e,t,s){this.storage=e,this.message=t,this.prefix=s}}{constructor(e,t,s){super(e,t,s)}static isExpired(e,t){return(new Date).getTime()>=t+e}get(e,t){const r=this.message.serialization.serializationKey(`${this.prefix}:${e}`),i=this.storage.getItem(r);if(null==i)return null;const a=this.message.deserialization.deserialization(i);return s.isExpired(a.expire,t)?(this.storage.removeItem(r),null):a.value??null}set(e,t){try{this.storage.setItem(this.message.serialization.serializationKey(`${this.prefix}:${e}`),this.message.serialization.serialization({value:t,expire:(new Date).getTime()}))}catch(e){}}clear(e,s){const r=t(e),i=t(s);if(r&&i)for(let e=0;e<this.storage.length;++e){const t=this.storage.key(e);if(null===t)continue;this.message.deserialization.deserializationKey(t).startsWith(this.prefix)&&this.storage.removeItem(t)}else if(i)for(let t=0;t<this.storage.length;++t){const s=this.storage.key(t);if(null===s)continue;this.message.deserialization.deserializationKey(s).startsWith(`${this.prefix}:${e}`)&&this.storage.removeItem(s)}else if(i||r)console.log("error clear group");else for(let t=0;t<this.storage.length;++t){const r=this.storage.key(t);if(null===r)continue;this.message.deserialization.deserializationKey(r).startsWith(`${this.prefix}:${e}&${s}`)&&this.storage.removeItem(r)}}}class r extends class{serializationKey(e){return e}}{serialization(e){return JSON.stringify(e)}}class i extends class{deserializationKey(e){return e}}{deserialization(e){return JSON.parse(e)}}class a{maxSize;cache;queue;length;clear(){}getItem(e){return this.cache.hasOwnProperty(e)?(this.queue.splice(this.queue.indexOf(e),1),this.queue.push(e),sessionStorage.setItem("LRUCacheSessionStorageQueue",JSON.stringify(this.queue)),this.cache[e]):null}key(e){return Object.keys(this.cache)[e]}removeItem(e){Reflect.deleteProperty(this.cache,e),this.queue.splice(this.queue.indexOf(e),1),sessionStorage.setItem("LRUCacheSessionStorage",JSON.stringify(this.cache)),sessionStorage.setItem("LRUCacheSessionStorageQueue",JSON.stringify(this.queue))}setItem(e,t){if(this.cache.hasOwnProperty(e))this.queue.splice(this.queue.indexOf(e),1);else if(this.queue.length===this.maxSize){const e=this.queue.shift();delete this.cache[e]}this.queue.push(e),this.cache[e]=t,sessionStorage.setItem("LRUCacheSessionStorage",JSON.stringify(this.cache)),sessionStorage.setItem("LRUCacheSessionStorageQueue",JSON.stringify(this.queue))}constructor(e){this.maxSize=e||10,this.cache=JSON.parse(sessionStorage.getItem("LRUCacheSessionStorage")||"{}"),this.queue=JSON.parse(sessionStorage.getItem("LRUCacheSessionStorageQueue")||"[]")}}const n=["get","post"];function o(e,t,s,r,i,a,n){return btoa(encodeURIComponent(`${e}&${t}&${s}${r}&${JSON.stringify(i)}&${JSON.stringify(a)}-${JSON.stringify(n)}`))}function u(e){const{maxAge:t=36e5,key:s="HTTP_CACHE_CACHE",storage:u=new a,prefix:l="AXIOS-CACHE",enableCache:h=!1,generateKey:c=o,valid:f=(()=>!0),message:p={deserialization:new i,serialization:new r},proxy:g=n}=e;return{maxAge:t,key:s,storage:u,enableCache:h,generateKey:c,valid:f,message:p,proxy:g,prefix:l}}function l(e,t){return Object.is(e,null)||Object.is(e,void 0)?t.default:Reflect.apply(e,this,t.args)}function h(e,...t){if(Object.is(e,null)||Object.is(e,void 0))return null;if("function"==typeof e)try{return Reflect.apply(e,this,t)}catch(e){return null}return e}function c(e,t,s,r){return l(s.valid,{default:!0,args:[e]})&&l(r.valid,{default:!0,args:[e]})&&r.cache.set(t,e),e}function f(e,t,s,r,i,a,n,o){const{hit:u,force:l}=r;if(!1===u||!h(e.enableCache,t,s)&&!0!==u)return Reflect.apply(i,a,n);if(!0===l)return Promise.resolve(Reflect.apply(i,a,n)).then((t=>c(t,o,r,e)));const f=e.cache.get(o,r.expire??e.maxAge);return null===f?Promise.resolve(Reflect.apply(i,a,n)).then((t=>c(t,o,r,e))):Promise.resolve(f)}const p=function(e,t){return{apply(s,r,i){const a={hit:i[2]?.hit,force:i[2]?.force,expire:i[2]?.expire,valid:i[2]?.valid},n=i[0],o=t.generateKey(t.key,i[2]?.group??"DEFAULT_GROUP",i[0],e,i[2]?.header??null,i[2]?.params??null,i[1]??null);return f(t,n,e,a,s,r,i,o)}}},g=function(e,t){return{apply(s,r,i){const a={hit:i[1]?.hit,force:i[1]?.force,expire:i[1]?.expire,valid:i[1]?.valid},n=i[0],o=t.generateKey(t.key,i[1]?.group??"DEFAULT_GROUP",i[0],e,i[1]?.header,i[1]?.params??null,i[1]?.data??null);return f(t,n,e,a,s,r,i,o)}}},d=function(e,t){return{apply(s,r,i){const a={hit:i[0]?.hit,force:i[0]?.force,expire:i[0]?.expire,valid:i[0]?.valid},n=i[0].url,o=i[1].method??"get",u=t.generateKey(t.key,i[0]?.group??"DEFAULT_GROUP",i[0]?.url,e,i[0]?.header,i[0]?.params??null,i[0]?.data??null);return f(t,n,o,a,s,r,i,u)}}};function y(t,s={}){return m(e.create(t),s)}function m(e,t={}){let r=Object.assign(u(t));r=Object.assign(r,{cache:new s(r.storage,r.message,r.prefix)});const i=function(e,t,s){const r={};for(let i=0;i<t.length;++i){const a=Reflect.get(e,t[i],e);if(!a)continue;const n=t[i];switch(n){case"delete":case"head":case"options":case"get":r[n]=new Proxy(a,g(n,s));break;case"request":r[n]=new Proxy(a,d(n,s));break;case"post":case"put":case"patch":r[n]=new Proxy(a,p(n,s))}}return r}(e,r.proxy,r);i.clear=()=>r.cache.clear();const a={};return r.proxy.forEach((e=>a[e]=!0)),new Proxy(e,{get:(e,t,s)=>i[t]?i[t]:Reflect.get(e,t,s),apply(e,t,s){if(!s||0===s.length)return Reflect.apply(e,t,s);const i="string"==typeof s[0],n=(i?s[1]?.method:s[0]?.method)??"get";if(a[n]){const a=i?g(n,r):d(n,r);return a.apply&&a.apply(e,t,s)}return Reflect.apply(e,t,s)}})}function x(e){return"object"==typeof e&&"function"==typeof e.then&&"function"==typeof e.catch}y.proxy=m,e.withCache=y,e.clear=function(){throw new Error("方法没有实现")};class O extends class{defaultOptions;cache;proxyMethod={clear:!1,delete:!1,get:!1,head:!1,options:!1,patch:!1,post:!1,put:!1,request:!1};constructor(e){this.defaultOptions=Object.assign(u(e),{adapter:e.adapter?Array.isArray(e.adapter)?e.adapter:[e.adapter]:[]}),this.cache=new s(this.defaultOptions.storage,this.defaultOptions.message,this.defaultOptions.prefix),this.defaultOptions.proxy.forEach((e=>this.proxyMethod[e]=!0))}request(e){if(!this.beforeStore(e)){const t=this.key(e);return e.force?this.realRequest(e).then(this.getOnfulfilled(t,e)):this.proxyRequest(e,t)}return this.realRequest(e)}clear(e){this.cache.clear(this.key(e))}getOnfulfilled(e,t){return s=>function(e,t,s,r,i){return s.valid&&s.valid(e)||r.valid(e)?i.set(t,e):i.clear(t),e}(s,e,t,this.defaultOptions,this.cache)}realRequest(e){if(Array.isArray(this.defaultOptions.adapter))for(let t=0;t<this.defaultOptions.adapter.length;++t){const s=this.defaultOptions.adapter[t](e);if(null!=s)return x(s)?s:Promise.resolve(s)}return Promise.reject("adapter not implements!!!")}proxyRequest(e,t){let s;return(s=this.cache.get(t,e.expire??this.defaultOptions.maxAge))?x(s)?s:Promise.resolve(s):this.realRequest(e).then(this.getOnfulfilled(t,e))}beforeStore(e){return!this.proxyMethod[e.method]||!1===e.hit||!e.hit&&!h(this.defaultOptions.enableCache,e.url,e.method)}key(e){return e?this.defaultOptions.generateKey(this.defaultOptions.key,e?.group??"DEFAULT_GROUP",e.url,e.method,e.headers,e.params,e.data):null}}{constructor(e={}){super(e)}static create(e={}){return new O(e)}request(e){return e.method=e.method??"get",super.request(e)}clear(e){super.clear(e)}getUri(e){return this.request(e)}get(e,t={}){return this.request(this.beforeConfig(t,e))}delete(e,t={}){return this.request(this.beforeConfig(t,e,"delete"))}head(e,t={}){return this.request(this.beforeConfig(t,e,"head"))}options(e,t={}){return this.request(this.beforeConfig(t,e,"options"))}post(e,t,s={}){return this.request(this.beforeConfig(s,e,"post",t))}put(e,t,s={}){return this.request(this.beforeConfig(s,e,"put",t))}patch(e,t,s={}){return this.request(this.beforeConfig(s,e,"patch",t))}postForm(e,t,s={}){return this.request(this.beforeConfig(s,e,"postForm",t))}putForm(e,t,s={}){return this.request(this.beforeConfig(s,e,"putForm",t))}patchForm(e,t,s={}){return this.request(this.beforeConfig(s,e,"patchForm",t))}beforeConfig(e,t,s,r){return e.url=t??e.url,e.method=s??e.method,e.data=r??e.data,e}}export{O as default,y as http,m as proxy};
import { AxiosRequestConfig } from "axios";
type Adapter = (config: AxiosRequestConfig) => Promise<any>;
export { Adapter };
/** @format */
import { Deserialization, Serialization } from "@/cache";
import { Adapter, Method } from "@/type";
import { AxiosResponse } from "axios";
interface RequestOption {
readonly hit?: boolean;
readonly force?: boolean;
readonly expire?: number;
readonly group?: string | number;
valid?(response: AxiosResponse): boolean;
}
interface CacheInstance {
/**
* @default 1000*60*60
*/
readonly maxAge?: number;
readonly key?: string;
readonly storage?: Storage;
readonly proxy?: Method[];
readonly adapter?: Adapter | Adapter[];
readonly enableCache?: boolean | ((url: string, method: string) => boolean);
/**
* 公共的key前缀 生成key需要获取到
*/
readonly prefix?: string;
/**
* 消息转换
*/
readonly message?: {
serialization: Serialization;
deserialization: Deserialization;
};
valid?(response: AxiosResponse): boolean;
generateKey?(key: string | undefined, group: string | number, url: string | undefined, method: unknown, header: unknown, params: string, data: string | object | any): string;
}
export type { CacheInstance, RequestOption };
import { AxiosRequestConfig, AxiosResponse } from "axios";
type RequestExecute = (<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>) => Promise<R>) | (<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>) => Promise<R>) | (<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>) => Promise<R>) | (() => void);
export { RequestExecute };
export { CacheInstance, RequestOption } from "@/type/CacheType";
export { Method } from "@/type/Method";
export { RequestExecute } from "@/type/ExecuteType";
export { Adapter } from "@/type/Adapter";
export type Method = "request" | "get" | "delete" | "head" | "options" | "post" | "put" | "patch" | "clear";
import { HttpCache } from "@/cache";
import { CacheInstance, RequestOption } from "@/type";
import { AxiosInstance } from "axios";
declare function getResponse(options: Omit<Required<CacheInstance>, "adapter"> & {
cache: HttpCache;
}, url: string, method: string, requestOption: RequestOption, target: AxiosInstance, thisArg: any, argArray: any[], generateKey: string): any;
export { getResponse };
export { createOptions, defaultGenerateKey, extend, createProxyMethods } from "@/util/util";
export { getResponse } from "@/util/CacheResponse";
import { HttpCache } from "@/cache/HttpCache";
import { CacheInstance, Method } from "@/type";
import { RequestExecute } from "@/type/ExecuteType";
import { AxiosInstance } from "axios/index";
declare function defaultGenerateKey(key: string | undefined, group: string | number, url: string | undefined, method: unknown, header: unknown, params: string, data: string | object): string;
declare function createProxyMethods(target: AxiosInstance, methods: Method[], options: Omit<Required<CacheInstance>, "adapter"> & {
cache: HttpCache;
}): Record<Method, RequestExecute>;
declare function createOptions(options: CacheInstance): Omit<Required<CacheInstance>, "adapter">;
declare function extendFn<T>(this: any, target: ((...args: any[]) => T) | undefined, options: {
args: any[];
default: T;
}): T;
/**
* 对参数可能是函数的类型执行
* @param target 目标
* @returns
*/
declare function extend<T>(target: T): T | null;
/**
* 对参数可能是函数的类型执行
* @param target 函数
* @param args
* @returns
*/
declare function extend<T>(this: any, target: (...args: any[]) => T, ...args: any[]): T | null;
declare function extend<T>(this: any, target: T | ((...args: any[]) => T), ...args: any[]): T | null;
export { createOptions, defaultGenerateKey, extend, createProxyMethods, extendFn };