@snail-js/api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +146 -0
- package/dist/cache/index.d.ts +5 -0
- package/dist/cache/indexDBCache.d.ts +28 -0
- package/dist/cache/localstorageCache.d.ts +25 -0
- package/dist/cache/memoryCache.d.ts +26 -0
- package/dist/core/api.d.ts +40 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/snail.d.ts +20 -0
- package/dist/index.d.ts +3 -0
- package/dist/snail-api.mjs +2951 -0
- package/dist/snail-api.umd.js +2955 -0
- package/dist/typings/api.config.d.ts +28 -0
- package/dist/typings/cache.management.config.d.ts +19 -0
- package/dist/typings/cache.type.d.ts +8 -0
- package/dist/typings/content.type.d.ts +5 -0
- package/dist/typings/index.d.ts +8 -0
- package/dist/typings/request.body.d.ts +3 -0
- package/dist/typings/request.method.d.ts +9 -0
- package/dist/typings/response.data.d.ts +17 -0
- package/dist/typings/snail.config.d.ts +21 -0
- package/dist/typings/versioning.config.d.ts +30 -0
- package/dist/utils/function.d.ts +11 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/versioning/versioning.d.ts +7 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
<p>
|
|
2
|
+
<img src="https://img.shields.io/badge/TypeScript-1e80ff"></img>
|
|
3
|
+
<img src="https://img.shields.io/npm/v/axios?label=axios&labelColor=1e80ff&color=67C23A"></img>
|
|
4
|
+
</p>
|
|
5
|
+
## 项目介绍
|
|
6
|
+
|
|
7
|
+
- 基于 Axios 二次封装
|
|
8
|
+
- 提供请求基本实例`Snail`,请求实例`Api`
|
|
9
|
+
|
|
10
|
+
## 安装
|
|
11
|
+
|
|
12
|
+
`npm install @snail-js/api`
|
|
13
|
+
|
|
14
|
+
## 使用
|
|
15
|
+
|
|
16
|
+
1. 创建`Snail`实例
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { Snail, SnailConfig, VersioningType, CacheType } from "@snail-js/api";
|
|
20
|
+
import { AxiosRequestConfig } from "axios";
|
|
21
|
+
|
|
22
|
+
const options:SnailConfig = {
|
|
23
|
+
baseUrl:'api',
|
|
24
|
+
Versioning: {
|
|
25
|
+
type: VersioningType.Uri,
|
|
26
|
+
prefix: "v",
|
|
27
|
+
defaultVersion: "0.1.0",
|
|
28
|
+
},
|
|
29
|
+
timeout: 5000,
|
|
30
|
+
requestInterceptors: {
|
|
31
|
+
onFulfilled(config: AxiosRequestConfig) {
|
|
32
|
+
console.log("requestInterceptors:", config.url);
|
|
33
|
+
return config
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
responseInterceptors: {
|
|
37
|
+
onFulfilled(response) {
|
|
38
|
+
console.log(response);
|
|
39
|
+
return response
|
|
40
|
+
},
|
|
41
|
+
onRejected(error) {
|
|
42
|
+
console.log(error);
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
CacheManage: {
|
|
46
|
+
type: CacheType.LocalStorage,
|
|
47
|
+
ttl: 60, //缓存过期时间
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
export SnailInstance = new Snail(options)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
2. 创建请求方法实例
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { ApiConfig, RequestPipe } from "@snail-js/api";
|
|
57
|
+
|
|
58
|
+
import { SnailInstance } from "./snail";
|
|
59
|
+
const pipe: RequestPipe = (input) => {
|
|
60
|
+
const { data, headers } = input;
|
|
61
|
+
const newHeaders = {
|
|
62
|
+
...headers,
|
|
63
|
+
pipe: "RequestPipe",
|
|
64
|
+
};
|
|
65
|
+
return {
|
|
66
|
+
data,
|
|
67
|
+
headers: newHeaders,
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const transform = (data: any) => {
|
|
72
|
+
return {
|
|
73
|
+
...data,
|
|
74
|
+
transform: "transform",
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const options: ApiConfig = {
|
|
79
|
+
transform,
|
|
80
|
+
version: "0.3.0", //会覆盖defaultVersion
|
|
81
|
+
};
|
|
82
|
+
const Api = Snail.Get("test", options);
|
|
83
|
+
Api.use(pipe);
|
|
84
|
+
export const testApi = Api;
|
|
85
|
+
|
|
86
|
+
// 配置了hitSource,Api2请求成功时会使Api的缓存失效
|
|
87
|
+
// 当你更新了数据时,get请求的缓存失效,会发起请求,这很有用
|
|
88
|
+
const Api2 = Snail.Post("test",{hitSource:Api})
|
|
89
|
+
export const testApi2 = Api2
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
3. 使用请求
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { testApi } from "./testApi";
|
|
96
|
+
// 发送时也可临时更改version,便于测试
|
|
97
|
+
const res = await TestApi.send({ version: "0.1.0" });
|
|
98
|
+
const { Catch, error, data } = res;
|
|
99
|
+
if (error == null) {
|
|
100
|
+
console.log("Snail-Api:", data);
|
|
101
|
+
}
|
|
102
|
+
Catch((error) => {
|
|
103
|
+
console.log(error);
|
|
104
|
+
});
|
|
105
|
+
// success {error:null,data}
|
|
106
|
+
// error {error,data:null} 附带的相关错误数据会保存在error.cause中
|
|
107
|
+
// 某些服务端标记的code !=0的请求,会被捕获为错误,而且携带数据
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Snail 配置
|
|
111
|
+
|
|
112
|
+
- `baseUrl`:同`Axios`
|
|
113
|
+
- `Versioning`:版本管理器
|
|
114
|
+
- type:管理器类型,enum:Uri,Head,Query,Custom
|
|
115
|
+
- prifix:前缀
|
|
116
|
+
- defaultVersion:全局默认版本
|
|
117
|
+
- timenout:全局超时时间,会被 Api 的 timeout 值覆盖
|
|
118
|
+
- requestInterceptors:全局请求拦截器
|
|
119
|
+
- responseInterceptors:全局响应拦截器
|
|
120
|
+
- CacheManage:缓存管理器
|
|
121
|
+
- type:缓存管理器类型,CacheType,enum:localStorage,IndexDB,Memory
|
|
122
|
+
- ttl: 缓存过期时间
|
|
123
|
+
|
|
124
|
+
## Api 配置
|
|
125
|
+
|
|
126
|
+
- name?: 请求名称,用于hitSource来使缓存失效
|
|
127
|
+
- timeout?: 请求超时时间;会覆盖`SnailConfig.timeout`
|
|
128
|
+
- version?: 请求版本,会覆盖`SnailConfig.Versioning.defaultVersion`;
|
|
129
|
+
- transform?: `(data: any) => T`;响应数据转换器,用于对返回数据进行转换操作
|
|
130
|
+
- headers?: Record<string, string>;
|
|
131
|
+
- params?: Record<string, string>;
|
|
132
|
+
- hitSource?: string | Api;失效源
|
|
133
|
+
|
|
134
|
+
## send参数配置
|
|
135
|
+
|
|
136
|
+
- params?: Record<string, string>; 请求Query参数
|
|
137
|
+
- data?: RequestBody; 请求体数据
|
|
138
|
+
- version?: string; 请求版本,如设置,会临时使用此版本运行版本管理器
|
|
139
|
+
|
|
140
|
+
### 代码仓库
|
|
141
|
+
- 
|
|
142
|
+
- 
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
### 作者
|
|
146
|
+
- mc.lee
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import MemoryCache from "./memoryCache";
|
|
2
|
+
import LocalStorageCache from "./localstorageCache";
|
|
3
|
+
import IndexDBCache from "./indexDBCache";
|
|
4
|
+
import { CacheType } from "../typings";
|
|
5
|
+
export declare function createCache(type: CacheType, ttl: number): MemoryCache | LocalStorageCache | IndexDBCache | undefined;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export default class IndexDBCache {
|
|
2
|
+
ttl: number;
|
|
3
|
+
private db;
|
|
4
|
+
constructor(ttl: number);
|
|
5
|
+
init(): Promise<void>;
|
|
6
|
+
private openDB;
|
|
7
|
+
get<T = any>(key: string): Promise<{
|
|
8
|
+
error: null;
|
|
9
|
+
data: T;
|
|
10
|
+
} | {
|
|
11
|
+
error: any;
|
|
12
|
+
data: null;
|
|
13
|
+
}>;
|
|
14
|
+
set<T = any>(key: string, value: T): Promise<{
|
|
15
|
+
error: null;
|
|
16
|
+
data: true;
|
|
17
|
+
} | {
|
|
18
|
+
error: any;
|
|
19
|
+
data: null;
|
|
20
|
+
}>;
|
|
21
|
+
delete(key: string): Promise<{
|
|
22
|
+
error: null;
|
|
23
|
+
data: true;
|
|
24
|
+
} | {
|
|
25
|
+
error: any;
|
|
26
|
+
data: null;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export default class LocalStorageCache {
|
|
2
|
+
ttl: number;
|
|
3
|
+
constructor(ttl: number);
|
|
4
|
+
get<T = any>(key: string): Promise<{
|
|
5
|
+
error: null;
|
|
6
|
+
data: T;
|
|
7
|
+
} | {
|
|
8
|
+
error: any;
|
|
9
|
+
data: null;
|
|
10
|
+
}>;
|
|
11
|
+
set<T = any>(key: string, value: T): Promise<{
|
|
12
|
+
error: null;
|
|
13
|
+
data: true;
|
|
14
|
+
} | {
|
|
15
|
+
error: any;
|
|
16
|
+
data: null;
|
|
17
|
+
}>;
|
|
18
|
+
delete(key: string): Promise<{
|
|
19
|
+
error: null;
|
|
20
|
+
data: true;
|
|
21
|
+
} | {
|
|
22
|
+
error: any;
|
|
23
|
+
data: null;
|
|
24
|
+
}>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export default class MemoryCache {
|
|
2
|
+
private cache;
|
|
3
|
+
ttl: number;
|
|
4
|
+
constructor(ttl: number);
|
|
5
|
+
get<T = any>(key: string): Promise<{
|
|
6
|
+
error: null;
|
|
7
|
+
data: T;
|
|
8
|
+
} | {
|
|
9
|
+
error: any;
|
|
10
|
+
data: null;
|
|
11
|
+
}>;
|
|
12
|
+
set<T = any>(key: string, value: T): Promise<{
|
|
13
|
+
error: null;
|
|
14
|
+
data: true;
|
|
15
|
+
} | {
|
|
16
|
+
error: any;
|
|
17
|
+
data: null;
|
|
18
|
+
}>;
|
|
19
|
+
delete(key: string): Promise<{
|
|
20
|
+
error: null;
|
|
21
|
+
data: true;
|
|
22
|
+
} | {
|
|
23
|
+
error: any;
|
|
24
|
+
data: null;
|
|
25
|
+
}>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { RequestBody, RequestMethod, ApiConfig, SendOptions, ApiResponse, RequestPipe } from "../typings";
|
|
2
|
+
import { Snail } from "./snail";
|
|
3
|
+
/**
|
|
4
|
+
* 泛型 R response success 时 返回的data类型
|
|
5
|
+
* 泛型 E error 时 返回的error类型
|
|
6
|
+
* 泛型 D error 时 返回error携带的data类型
|
|
7
|
+
*/
|
|
8
|
+
export declare class Api<R = any, E = any, D = any> {
|
|
9
|
+
baseURL: string;
|
|
10
|
+
url: string;
|
|
11
|
+
data?: RequestBody;
|
|
12
|
+
params?: Record<string, any>;
|
|
13
|
+
headers?: Record<string, any>;
|
|
14
|
+
private key;
|
|
15
|
+
method: RequestMethod;
|
|
16
|
+
private hitCache;
|
|
17
|
+
context: Snail;
|
|
18
|
+
version?: string;
|
|
19
|
+
private versioning?;
|
|
20
|
+
private name?;
|
|
21
|
+
private hitSource?;
|
|
22
|
+
private timeout;
|
|
23
|
+
private transform?;
|
|
24
|
+
private pipes;
|
|
25
|
+
constructor(method: RequestMethod, url: string, context?: any, config?: ApiConfig<R>);
|
|
26
|
+
private handleVersioning;
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @param options 发送配置:子项params,data
|
|
30
|
+
* 泛型<R = any, E = any>R返回数据类型, E返回错误类型(code!=0)
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
send(options?: SendOptions): Promise<ApiResponse<R, E>>;
|
|
34
|
+
private checkCache;
|
|
35
|
+
delCache(version?: string): Promise<void>;
|
|
36
|
+
use(pipe: RequestPipe): void;
|
|
37
|
+
private processRequestPipes;
|
|
38
|
+
private sendRequest;
|
|
39
|
+
private lapsed;
|
|
40
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AxiosInstance } from "axios";
|
|
2
|
+
import { Api } from "./api";
|
|
3
|
+
import { SnailConfig, VersioningConfig, ApiConfig, CacheStorage } from "../typings";
|
|
4
|
+
export declare class Snail {
|
|
5
|
+
axiosInstance: AxiosInstance;
|
|
6
|
+
baseURL: string;
|
|
7
|
+
versioning?: VersioningConfig;
|
|
8
|
+
options: SnailConfig;
|
|
9
|
+
cacheStorage?: CacheStorage;
|
|
10
|
+
cacheSource: Api[];
|
|
11
|
+
constructor(options: SnailConfig);
|
|
12
|
+
Get<T = any, E = any, D = any>(url: string, options?: ApiConfig): Api<T, E, D>;
|
|
13
|
+
Post<T = any, E = any, D = any>(url: string, options?: ApiConfig): Api<T, E, D>;
|
|
14
|
+
Put<T = any, E = any, D = any>(url: string, options?: ApiConfig): Api<T, E, D>;
|
|
15
|
+
Delete<T = any, E = any, D = any>(url: string, options?: ApiConfig): Api<T, E, D>;
|
|
16
|
+
Patch<T = any, E = any, D = any>(url: string, options?: ApiConfig): Api<T, E, D>;
|
|
17
|
+
Head<T = any, E = any, D = any>(url: string, options?: ApiConfig): Api<T, E, D>;
|
|
18
|
+
Options<T = any, E = any, D = any>(url: string, options?: ApiConfig): Api<T, E, D>;
|
|
19
|
+
}
|
|
20
|
+
export declare const createSnail: (option: SnailConfig) => Snail;
|
package/dist/index.d.ts
ADDED