@snail-js/api 0.1.2 → 0.1.4
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 +105 -6
- package/dist/core/snail.d.ts +17 -3
- package/dist/decorators/api.d.ts +30 -2
- package/dist/decorators/cache.d.ts +9 -3
- package/dist/decorators/param.d.ts +11 -1
- package/dist/decorators/progress.d.ts +4 -0
- package/dist/decorators/server.d.ts +6 -1
- package/dist/decorators/sse.d.ts +31 -0
- package/dist/decorators/strategy.d.ts +6 -0
- package/dist/decorators/versioning.d.ts +10 -0
- package/dist/index.d.ts +7 -6
- package/dist/snail-api.js +257 -85
- package/dist/snail-api.umd.cjs +257 -85
- package/dist/typings/apiProxy.d.ts +1 -1
- package/dist/typings/index.d.ts +1 -0
- package/dist/typings/snail.option.d.ts +0 -2
- package/dist/typings/sse.d.ts +11 -0
- package/dist/versioning/versioning.d.ts +1 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
<img src="https://img.shields.io/badge/TypeScript-1e80ff"></img>
|
|
3
3
|
<img src="https://img.shields.io/npm/v/axios?label=axios&labelColor=1e80ff&color=67C23A"></img>
|
|
4
4
|
</p>
|
|
5
|
+
|
|
5
6
|
## 项目介绍
|
|
6
7
|
|
|
7
8
|
- 基于 Axios 二次封装
|
|
@@ -48,6 +49,7 @@ import { Snail, Server } from "@snail-js/api";
|
|
|
48
49
|
timeout: 5000,
|
|
49
50
|
})
|
|
50
51
|
class BackEnd extends Snail {}
|
|
52
|
+
|
|
51
53
|
export const Service = new BackEnd();
|
|
52
54
|
```
|
|
53
55
|
|
|
@@ -88,16 +90,17 @@ if (error !== null) {
|
|
|
88
90
|
- `baseUrl`:同`Axios`,使用`vite.proxy`时,请使用`\`开头,直接跨域请求请填写完整地址
|
|
89
91
|
- `Versioning`:版本管理器
|
|
90
92
|
- type:管理器类型,enum:Uri,Head,Query,Custom
|
|
91
|
-
- prifix
|
|
93
|
+
- prifix:前缀,字符串;添加在版本号前面的字符,默认为`v`
|
|
92
94
|
- defaultVersion:全局默认版本
|
|
93
95
|
- timenout:全局超时时间,会被 Api 的 timeout 值覆盖
|
|
94
96
|
- CacheManage:缓存管理器
|
|
95
|
-
- type:缓存管理器类型,CacheType
|
|
97
|
+
- type:缓存管理器类型,CacheType,`enum:localStorage,IndexDB,Memory`
|
|
96
98
|
- ttl: 缓存过期时间
|
|
99
|
+
- enableLog: 是否打印日志
|
|
97
100
|
|
|
98
101
|
## Api 配置
|
|
99
102
|
|
|
100
|
-
- url?: api 请求端点,与 Server 中的`baseUrl
|
|
103
|
+
- url?: api 请求端点,与 Server 中的`baseUrl`拼接请求地址,不要使用`/`开头
|
|
101
104
|
- timeout?: 请求超时时间;会覆盖`Server.timeout`
|
|
102
105
|
- version?: 请求版本,会覆盖`Server.Versioning.defaultVersion`;
|
|
103
106
|
|
|
@@ -287,6 +290,8 @@ export type VersioningOption =
|
|
|
287
290
|
|
|
288
291
|
### 临时版本修改器`@Version`
|
|
289
292
|
|
|
293
|
+
- 临时改变方法请求的版本
|
|
294
|
+
|
|
290
295
|
```typescript
|
|
291
296
|
@Api("test")
|
|
292
297
|
class Test {
|
|
@@ -299,12 +304,106 @@ class Test {
|
|
|
299
304
|
> 临时改变 api 版本,便于测试
|
|
300
305
|
|
|
301
306
|
### 缓存装饰器`@Cache`
|
|
302
|
-
|
|
307
|
+
|
|
308
|
+
- `@Cache(string | null)`
|
|
309
|
+
- 当设置为 null 时,此方法不应用缓存
|
|
310
|
+
- 当设置为 string 时,应为此 Api 类下的方法名称,当设置的此名称方法被调用且正常响应时,被装饰的方法缓存失效
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
@Api("test")
|
|
314
|
+
class Test {
|
|
315
|
+
@Get("HelloWorld")
|
|
316
|
+
@Cache("test2")
|
|
317
|
+
test1() {}
|
|
318
|
+
|
|
319
|
+
@Post()
|
|
320
|
+
test2() {}
|
|
321
|
+
|
|
322
|
+
@Get()
|
|
323
|
+
@Cache(null)
|
|
324
|
+
test3() {}
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
> 当请求`[Post]test`成功时,`[Get]test/HelloWorld`的缓存失效
|
|
329
|
+
> 注意:`test2`方法请求成功的前提是需要设置`@Cache(null)`,否则仅第一次请求会发送,后续请求需等待缓存管理设置的 ttl 时间到期才会发送请求
|
|
330
|
+
> 因此,若未设置`test2`方法的`@Cache(null)`,仅第一次请求会使`[Get]test/HelloWorld`的缓存失效,后续需等待 ttl 时间到期,才会继续失效
|
|
331
|
+
|
|
332
|
+
> `test3`方法请求成功时,不缓存
|
|
333
|
+
|
|
334
|
+
> 注意:要使用缓存,请配置`@Server({CacheManage})`缓存管理器
|
|
335
|
+
|
|
336
|
+
### 上传进度装饰器`@UploadProgress`
|
|
337
|
+
|
|
338
|
+
- `@UploadProgress((progress: AxiosProgressEvent) => void)`
|
|
339
|
+
|
|
340
|
+
## Server Send Event 服务端推送
|
|
341
|
+
|
|
342
|
+
### 创建 sse 端点
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
@Api("sse")
|
|
346
|
+
class ServerSend {
|
|
347
|
+
@Sse()
|
|
348
|
+
create() {}
|
|
349
|
+
|
|
350
|
+
@OnSseOpen()
|
|
351
|
+
handleOpen(event: Event) {
|
|
352
|
+
console.log("sse-open:", event);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
@OnSseError()
|
|
356
|
+
handleError(event: Event) {
|
|
357
|
+
console.log("sse-error:", event);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// 处理默认message事件
|
|
361
|
+
@SseEvent()
|
|
362
|
+
handleEvent(event: MessageEvent) {
|
|
363
|
+
console.log("sse-event[message]:", event.data);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// 处理自定义名称事件
|
|
367
|
+
@SseEvent("chunk")
|
|
368
|
+
handleEvent(event: Event) {
|
|
369
|
+
console.log("sse-event[chunk]:", event);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export const Sse = Service.createSse(ServerSend);
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### 服务端推送装饰器`@Sse`
|
|
377
|
+
- `@Sse(path:string,options:{withCredentials: boolean})`
|
|
378
|
+
- 创建一个服务端推送连接,当被装饰的方法调用时打开连接
|
|
379
|
+
- 被装饰的方法调用后会返回`{eventSource:EventSource,close:function}`
|
|
380
|
+
- eventSource: sse连接实例
|
|
381
|
+
- close: 关闭此sse连接的方法
|
|
382
|
+
|
|
383
|
+
### 注册`onopen`装饰器`@OnSseOpen`
|
|
384
|
+
- 当`@Sse`装饰的方法被调用时,将`@OnSseOpen`装饰的方法注册为`@Sse`装饰的方法返回的`EventSource`实例`onopen`处理函数
|
|
385
|
+
|
|
386
|
+
### 注册`onerror`装饰器`@OnSseError`
|
|
387
|
+
- 当`@Sse`装饰的方法被调用时,将`@OnSseError`装饰的方法注册为`@Sse`装饰的方法返回的`EventSource`实例`onerror`处理函数
|
|
388
|
+
|
|
389
|
+
### 事件处理装饰器`@SseEvent`
|
|
390
|
+
- `@SseEvent(eventName?:string)`
|
|
391
|
+
- 未传入`eventName`,默认注册为`message`事件处理器
|
|
392
|
+
- 传入`eventName`,注册为对应名称的事件处理器
|
|
393
|
+
|
|
303
394
|
|
|
304
395
|
### 代码仓库
|
|
305
396
|
|
|
306
|
-
|
|
307
|
-
|
|
397
|
+
<p>
|
|
398
|
+
<a src="https://gitee.com/limich/snail">
|
|
399
|
+
<img src="https://img.shields.io/badge/snail-js?style=flat&label=gitee&labelColor=F56C6C&link=https%3A%2F%2Fgitee.com%2Flimich%2Fsnail"></img>
|
|
400
|
+
</a>
|
|
401
|
+
</p>
|
|
402
|
+
<p>
|
|
403
|
+
<a src="https://github.com/limingchang/snail">
|
|
404
|
+
<img src="https://img.shields.io/badge/snail-js?style=flat&label=github&labelColor=F56C6C&link=https%3A%2F%2Fgihub.com%2Flimingchang%2Fsnail"></img>
|
|
405
|
+
</a>
|
|
406
|
+
</p>
|
|
308
407
|
|
|
309
408
|
### 作者
|
|
310
409
|
|
package/dist/core/snail.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
-
import { Strategy, ApiProxy, ResponseData } from "../typings";
|
|
2
|
+
import { Strategy, ApiProxy, SseProxy, ResponseData } from "../typings";
|
|
3
3
|
export declare class Snail<R extends {
|
|
4
4
|
data: any;
|
|
5
5
|
} = ResponseData> {
|
|
@@ -7,14 +7,28 @@ export declare class Snail<R extends {
|
|
|
7
7
|
private strategies;
|
|
8
8
|
private cacheStorage?;
|
|
9
9
|
private version?;
|
|
10
|
+
private sourceMap;
|
|
11
|
+
private eventSource;
|
|
10
12
|
registerStrategy(strategy: Strategy): void;
|
|
11
13
|
createApi<T extends object>(constructor: new () => T): ApiProxy<T, R>;
|
|
12
14
|
private buildRequestArgs;
|
|
13
15
|
private getStrategies;
|
|
14
16
|
private applyStrategies;
|
|
15
17
|
private applyVersion;
|
|
16
|
-
private
|
|
17
|
-
private
|
|
18
|
+
private getCache;
|
|
19
|
+
private initCacheManage;
|
|
20
|
+
private getHitSource;
|
|
21
|
+
private setHitSource;
|
|
22
|
+
private setCache;
|
|
23
|
+
private expireCache;
|
|
24
|
+
private getServerConfig;
|
|
25
|
+
private getApiConfig;
|
|
26
|
+
private initAxios;
|
|
27
|
+
private generateSseUrl;
|
|
18
28
|
private handleResponse;
|
|
19
29
|
private handleError;
|
|
30
|
+
createSse<T extends object>(constructor: new () => T): SseProxy<T>;
|
|
31
|
+
private initSse;
|
|
32
|
+
private setSse;
|
|
33
|
+
private registerSseEvent;
|
|
20
34
|
}
|
package/dist/decorators/api.d.ts
CHANGED
|
@@ -1,12 +1,40 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
import { ApiConfig } from "../typings";
|
|
3
|
-
export declare const METHOD_KEY
|
|
4
|
-
export declare const API_CONFIG_KEY
|
|
3
|
+
export declare const METHOD_KEY: unique symbol;
|
|
4
|
+
export declare const API_CONFIG_KEY: unique symbol;
|
|
5
5
|
export declare const Api: (url?: string, config?: ApiConfig) => ClassDecorator;
|
|
6
|
+
/**
|
|
7
|
+
* 定义装饰的方法为Get请求
|
|
8
|
+
* @param path 字符型,请求端点,默认为空
|
|
9
|
+
*/
|
|
6
10
|
export declare const Get: (path?: string) => (target: any, propertyKey: string | symbol) => void;
|
|
11
|
+
/**
|
|
12
|
+
* 定义装饰的方法为Post请求
|
|
13
|
+
* @param path 字符型,请求端点,默认为空
|
|
14
|
+
*/
|
|
7
15
|
export declare const Post: (path?: string) => (target: any, propertyKey: string | symbol) => void;
|
|
16
|
+
/**
|
|
17
|
+
* 定义装饰的方法为Put请求
|
|
18
|
+
* @param path 字符型,请求端点,默认为空
|
|
19
|
+
*/
|
|
8
20
|
export declare const Put: (path?: string) => (target: any, propertyKey: string | symbol) => void;
|
|
21
|
+
/**
|
|
22
|
+
* 定义装饰的方法为Delete请求
|
|
23
|
+
* @param path 字符型,请求端点,默认为空
|
|
24
|
+
*/
|
|
9
25
|
export declare const Delete: (path?: string) => (target: any, propertyKey: string | symbol) => void;
|
|
26
|
+
/**
|
|
27
|
+
* 定义装饰的方法为Patch请求
|
|
28
|
+
* @param path 字符型,请求端点,默认为空
|
|
29
|
+
*/
|
|
10
30
|
export declare const Patch: (path?: string) => (target: any, propertyKey: string | symbol) => void;
|
|
31
|
+
/**
|
|
32
|
+
* 定义装饰的方法为Options请求
|
|
33
|
+
* @param path 字符型,请求端点,默认为空
|
|
34
|
+
*/
|
|
11
35
|
export declare const Options: (path?: string) => (target: any, propertyKey: string | symbol) => void;
|
|
36
|
+
/**
|
|
37
|
+
* 定义装饰的方法为Head请求
|
|
38
|
+
* @param path 字符型,请求端点,默认为空
|
|
39
|
+
*/
|
|
12
40
|
export declare const Head: (path?: string) => (target: any, propertyKey: string | symbol) => void;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
export declare const CACHE_OPTIONS_KEY: unique symbol;
|
|
3
|
+
/**
|
|
4
|
+
* 为请求方法设置失效源,或设置不启用缓存
|
|
5
|
+
* @param hitSource 失效源,字符型|null;
|
|
6
|
+
* - 设置为null时,该请求方法不启用缓存;
|
|
7
|
+
* - 设置为字符时,对应该Api下请求方法名称,当设置的名称方法请求成功时,被装饰的请求方法缓存失效
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare const Cache: (hitSource: string | null) => (target: any, propertyKey?: string) => void;
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
export declare const REQUEST_ARGS_KEY: unique symbol;
|
|
3
|
+
/**
|
|
4
|
+
* 将被装饰的参数定义为查询参数
|
|
5
|
+
* @param key 可选,查询参数的key,未给定key,则被装饰的参数应为对象
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
3
8
|
export declare const Params: (key?: string) => (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
|
|
4
|
-
|
|
9
|
+
/**
|
|
10
|
+
* 将被装饰的参数定义为请求数据
|
|
11
|
+
* @param key 可选,请求data的key,未给定key,则被装饰的参数应为对象
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export declare const Data: (key?: string) => (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
import { SnailOption } from "../typings";
|
|
3
|
-
export declare const SERVER_CONFIG_KEY
|
|
3
|
+
export declare const SERVER_CONFIG_KEY: unique symbol;
|
|
4
|
+
/**
|
|
5
|
+
* 装饰一个继承Snail 的 class,创建后端交互基本实例
|
|
6
|
+
* @param config @type SnailOption
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
4
9
|
export declare const Server: (config: SnailOption) => (target: any) => void;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
export declare const EVENT_SOURCE_OPTION_KEY: unique symbol;
|
|
3
|
+
/**
|
|
4
|
+
* 将装饰的方法创建为SSE连接
|
|
5
|
+
* @param path 路径,要连接到服务端sse端点路径
|
|
6
|
+
* @param options 配置选项
|
|
7
|
+
* @returns 调用被装饰的方法可获得返回{close,eventSource}
|
|
8
|
+
*/
|
|
9
|
+
export declare const Sse: (path?: string, options?: {
|
|
10
|
+
withCredentials: boolean;
|
|
11
|
+
}) => (target: any, propertyKey: string) => void;
|
|
12
|
+
export declare const EVENT_SOURCE_EVENTS_KEY: unique symbol;
|
|
13
|
+
/**
|
|
14
|
+
* 将装饰的方法注册为eventSource的对应事件
|
|
15
|
+
* @param eventName 事件名称,可选,默认处理message事件
|
|
16
|
+
* @param options boolean | AddEventListenerOptions 可选,注册事件选项
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
export declare const SseEvent: (eventName?: string, options?: boolean | AddEventListenerOptions) => (target: any, propertyKey: string) => void;
|
|
20
|
+
export declare const EVENT_SOURCE_OPEN_KEY: unique symbol;
|
|
21
|
+
/**
|
|
22
|
+
* 将装饰的方法注册为eventSource.onOpen
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
export declare const OnSseOpen: () => (target: any, propertyKey: string) => void;
|
|
26
|
+
export declare const EVENT_SOURCE_ERROR_KEY: unique symbol;
|
|
27
|
+
/**
|
|
28
|
+
* 将装饰的方法注册eventSource.onError
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
export declare const OnSseError: () => (target: any, propertyKey: string) => void;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
import { Strategy } from "../typings";
|
|
3
3
|
export declare const STRATEGY_KEY: unique symbol;
|
|
4
|
+
/**
|
|
5
|
+
* 策略装饰器,为请求添加请求策略或响应策略
|
|
6
|
+
* @param strategies @type Strategy[]
|
|
7
|
+
* - 传入的策略请使用new 实例化
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
4
10
|
export declare const UseStrategy: (...strategies: Strategy[]) => (target: any, propertyKey?: string) => void;
|
|
@@ -2,5 +2,15 @@ import "reflect-metadata";
|
|
|
2
2
|
import { VersioningOption } from "../typings";
|
|
3
3
|
export declare const VERSIONING_KEY: unique symbol;
|
|
4
4
|
export declare const VERSION_KEY: unique symbol;
|
|
5
|
+
/**
|
|
6
|
+
* 版本管理装饰器,为请求设置全局版本管理器
|
|
7
|
+
* @param options 版本管理选项
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
5
10
|
export declare const Versioning: (options: VersioningOption) => (target: any) => void;
|
|
11
|
+
/**
|
|
12
|
+
* 为方法设置版本
|
|
13
|
+
* @param version 字符型,临时设置被装饰的请求版本
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
6
16
|
export declare const Version: (version: string) => (target: any, propertyKey: string) => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export * from "./core";
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
2
|
+
export { Server } from "./decorators/server";
|
|
3
|
+
export { Api, Get, Put, Post, Patch, Options, Head, Delete, } from "./decorators/api";
|
|
4
|
+
export { Cache } from "./decorators/cache";
|
|
5
|
+
export { Params, Data } from "./decorators/param";
|
|
6
|
+
export { UseStrategy } from "./decorators/strategy";
|
|
7
|
+
export { Versioning, Version } from "./decorators/versioning";
|
|
8
|
+
export { Sse, SseEvent, OnSseError, OnSseOpen } from "./decorators/sse";
|
|
8
9
|
export * from "./typings";
|
|
9
10
|
export * from "./utils";
|