@snail-js/api 0.1.14 → 0.1.15

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.
Files changed (36) hide show
  1. package/README.md +223 -73
  2. package/README_EN.md +592 -0
  3. package/dist/cache/index.d.ts +6 -2
  4. package/dist/cache/indexDBCache.d.ts +7 -22
  5. package/dist/cache/localstorageCache.d.ts +7 -22
  6. package/dist/cache/memoryCache.d.ts +8 -23
  7. package/dist/core/index.d.ts +4 -1
  8. package/dist/core/snailApi.d.ts +22 -0
  9. package/dist/core/snailMethod.d.ts +54 -0
  10. package/dist/core/snailServer.d.ts +34 -0
  11. package/dist/core/snailSse.d.ts +20 -0
  12. package/dist/decorators/api.d.ts +9 -9
  13. package/dist/decorators/{param.d.ts → args.d.ts} +6 -0
  14. package/dist/decorators/cache.d.ts +11 -6
  15. package/dist/decorators/sse.d.ts +2 -3
  16. package/dist/decorators/strategy.d.ts +1 -1
  17. package/dist/index.d.ts +2 -2
  18. package/dist/snail-api.js +1125 -531
  19. package/dist/snail-api.umd.cjs +1126 -532
  20. package/dist/typings/api.option.d.ts +16 -0
  21. package/dist/typings/apiProxy.d.ts +5 -4
  22. package/dist/typings/cache.management.option.d.ts +16 -10
  23. package/dist/typings/cache.type.d.ts +17 -2
  24. package/dist/typings/index.d.ts +2 -1
  25. package/dist/typings/request.method.d.ts +9 -8
  26. package/dist/typings/response.data.d.ts +7 -3
  27. package/dist/typings/snail.method.d.ts +11 -0
  28. package/dist/typings/snail.option.d.ts +5 -1
  29. package/dist/typings/sse.d.ts +5 -1
  30. package/dist/typings/versioning.option.d.ts +2 -2
  31. package/dist/utils/function.d.ts +29 -8
  32. package/dist/versioning/index.d.ts +1 -0
  33. package/dist/versioning/versioning.d.ts +10 -5
  34. package/package.json +2 -1
  35. package/dist/core/snail.d.ts +0 -32
  36. package/dist/typings/api.config.d.ts +0 -9
@@ -1 +1,4 @@
1
- export * from './snail';
1
+ export { SnailServer } from "./snailServer";
2
+ export { SnailMethod } from "./snailMethod";
3
+ export { SnailApi } from "./snailApi";
4
+ export { SnailSse } from "./snailSse";
@@ -0,0 +1,22 @@
1
+ import { Strategy, ApiInstanceOptions } from "../typings";
2
+ export declare class SnailApi {
3
+ private Name;
4
+ private serverInstance;
5
+ private Version?;
6
+ private Url?;
7
+ private Timeout?;
8
+ private EnableLog?;
9
+ constructor(options: ApiInstanceOptions);
10
+ private init;
11
+ private initStrategy;
12
+ registerStrategies(...strategys: Array<new () => Strategy>): void;
13
+ private initName;
14
+ private getApiConfig;
15
+ private isNoCache;
16
+ get version(): string | undefined;
17
+ get url(): string | undefined;
18
+ get name(): string;
19
+ get noCache(): boolean;
20
+ get enableLog(): boolean | undefined;
21
+ get timeout(): number | undefined;
22
+ }
@@ -0,0 +1,54 @@
1
+ import { AxiosResponse, AxiosRequestConfig } from "axios";
2
+ import { EventHandler, Strategy, ResponseData, SendRequest, StandardResponseData, ResponseJsonData, SnailMethodEventType } from "../typings";
3
+ import { SnailApi } from "./snailApi";
4
+ /**
5
+ * SnailMethod
6
+ * 泛型参数: RT => response.data类型,默认标准json返回
7
+ * DK => response.data.data 类型
8
+ */
9
+ export declare class SnailMethod<RT extends ResponseData = StandardResponseData<ResponseJsonData>> {
10
+ private Name;
11
+ private apiInstance;
12
+ private target;
13
+ private strategies;
14
+ private Request;
15
+ private Response;
16
+ private Error;
17
+ private eventMap;
18
+ private onceWrapperMap;
19
+ private propertyKey;
20
+ private Url;
21
+ private Path;
22
+ private Method;
23
+ private Version?;
24
+ private Args;
25
+ constructor(apiInstance: SnailApi, target: Object, propertyKey: string | symbol, args?: []);
26
+ send: SendRequest<RT>;
27
+ private init;
28
+ private initUrl;
29
+ private initVersion;
30
+ private getExpireSources;
31
+ private createRequest;
32
+ onSuccess(handler: EventHandler<RT>): void;
33
+ onError(handler: EventHandler<RT>): void;
34
+ onHitCache(handler: EventHandler<RT>): void;
35
+ onFinish(handler: EventHandler<RT>): void;
36
+ on(eventName: SnailMethodEventType, handler: EventHandler<RT>): void;
37
+ once(eventName: SnailMethodEventType, handler: EventHandler<RT>): void;
38
+ emit(eventName: SnailMethodEventType, ...args: any): boolean;
39
+ off(eventName: SnailMethodEventType, handler: EventHandler<RT>): void;
40
+ private getCacheData;
41
+ private setCacheData;
42
+ get response(): AxiosResponse<RT, any>;
43
+ get request(): AxiosRequestConfig<any>;
44
+ get version(): string | undefined;
45
+ get name(): string;
46
+ get error(): any;
47
+ registerStrategies(...strategys: Array<new () => Strategy>): void;
48
+ private getStrategies;
49
+ private getMethodOptions;
50
+ private isNoCache;
51
+ private enableLog;
52
+ private applyHitSource;
53
+ private applyProgress;
54
+ }
@@ -0,0 +1,34 @@
1
+ import "reflect-metadata";
2
+ import { AxiosInstance } from "axios";
3
+ import { Strategy, ApiInstanceOptions, VersioningOption, CacheStorage, ApiProxy, ResponseData, StandardResponseData, ResponseJsonData, CacheForType } from "../typings";
4
+ import { SnailApi } from "./snailApi";
5
+ import { SnailSse } from "./snailSse";
6
+ export declare const CacheStorageMap: Map<string, CacheStorage>;
7
+ export declare const CacheTtlMap: Map<string, number>;
8
+ export declare const CacheForMap: Map<string, CacheForType>;
9
+ export declare const ExpireSourceMap: Map<string, Set<string>>;
10
+ export declare const AxiosInstanceMap: Map<string, AxiosInstance>;
11
+ export declare const StrategyMap: Map<string, (new () => Strategy)[]>;
12
+ export declare const VersioningMap: Map<string, VersioningOption>;
13
+ export declare class SnailServer<RT extends ResponseData = StandardResponseData<ResponseJsonData>, DK extends string = "data"> {
14
+ private Name;
15
+ private BaseURL;
16
+ private Version;
17
+ private EnableLog;
18
+ constructor();
19
+ private init;
20
+ private initLog;
21
+ private initStrategy;
22
+ private initVersioning;
23
+ registerStrategies(...strategys: Array<new () => Strategy>): void;
24
+ createApi<T extends SnailApi>(constructor: new (options: ApiInstanceOptions) => T): ApiProxy<T, RT, DK>;
25
+ private initCacheManage;
26
+ private initExpireSource;
27
+ private initAxios;
28
+ private getServerOptions;
29
+ createSse<T extends SnailSse>(constructor: new (server: SnailServer) => T): SnailSse;
30
+ get version(): string;
31
+ get name(): string;
32
+ get enableLog(): boolean;
33
+ get baseUrl(): string;
34
+ }
@@ -0,0 +1,20 @@
1
+ import { SnailServer } from "./snailServer";
2
+ export declare class SnailSse {
3
+ private _serverInstance;
4
+ private _baseUrl;
5
+ private _url;
6
+ private _version?;
7
+ private _withCredentials;
8
+ private _eventSource;
9
+ constructor(server: SnailServer);
10
+ private getSseOptions;
11
+ private initVersion;
12
+ open(): void;
13
+ close(): void;
14
+ private registerSseEvent;
15
+ private setEvent;
16
+ on(eventName: string, callback: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
17
+ off(eventName: string, callback: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
18
+ get url(): string;
19
+ get eventSource(): EventSource | null;
20
+ }
@@ -1,40 +1,40 @@
1
1
  import "reflect-metadata";
2
- import { ApiConfig } from "../typings";
2
+ import { ApiOptions } from "../typings";
3
3
  export declare const METHOD_KEY: unique symbol;
4
4
  export declare const API_CONFIG_KEY: unique symbol;
5
- export declare const Api: (url?: string, config?: ApiConfig) => ClassDecorator;
5
+ export declare const Api: (url?: string, config?: ApiOptions) => ClassDecorator;
6
6
  /**
7
7
  * 定义装饰的方法为Get请求
8
8
  * @param path 字符型,请求端点,默认为空
9
9
  */
10
- export declare const Get: (path?: string) => (target: any, propertyKey: string | symbol) => void;
10
+ export declare const Get: (path?: string, name?: string) => (target: any, propertyKey: string | symbol) => void;
11
11
  /**
12
12
  * 定义装饰的方法为Post请求
13
13
  * @param path 字符型,请求端点,默认为空
14
14
  */
15
- export declare const Post: (path?: string) => (target: any, propertyKey: string | symbol) => void;
15
+ export declare const Post: (path?: string, name?: string) => (target: any, propertyKey: string | symbol) => void;
16
16
  /**
17
17
  * 定义装饰的方法为Put请求
18
18
  * @param path 字符型,请求端点,默认为空
19
19
  */
20
- export declare const Put: (path?: string) => (target: any, propertyKey: string | symbol) => void;
20
+ export declare const Put: (path?: string, name?: string) => (target: any, propertyKey: string | symbol) => void;
21
21
  /**
22
22
  * 定义装饰的方法为Delete请求
23
23
  * @param path 字符型,请求端点,默认为空
24
24
  */
25
- export declare const Delete: (path?: string) => (target: any, propertyKey: string | symbol) => void;
25
+ export declare const Delete: (path?: string, name?: string) => (target: any, propertyKey: string | symbol) => void;
26
26
  /**
27
27
  * 定义装饰的方法为Patch请求
28
28
  * @param path 字符型,请求端点,默认为空
29
29
  */
30
- export declare const Patch: (path?: string) => (target: any, propertyKey: string | symbol) => void;
30
+ export declare const Patch: (path?: string, name?: string) => (target: any, propertyKey: string | symbol) => void;
31
31
  /**
32
32
  * 定义装饰的方法为Options请求
33
33
  * @param path 字符型,请求端点,默认为空
34
34
  */
35
- export declare const Options: (path?: string) => (target: any, propertyKey: string | symbol) => void;
35
+ export declare const Options: (path?: string, name?: string) => (target: any, propertyKey: string | symbol) => void;
36
36
  /**
37
37
  * 定义装饰的方法为Head请求
38
38
  * @param path 字符型,请求端点,默认为空
39
39
  */
40
- export declare const Head: (path?: string) => (target: any, propertyKey: string | symbol) => void;
40
+ export declare const Head: (path?: string, name?: string) => (target: any, propertyKey: string | symbol) => void;
@@ -6,6 +6,12 @@ export declare const REQUEST_ARGS_KEY: unique symbol;
6
6
  * @returns
7
7
  */
8
8
  export declare const Params: (key?: string) => (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
9
+ /**
10
+ * 将被装饰的参数定义为查询参数
11
+ * @param key 可选,查询参数的key,未给定key,则被装饰的参数应为对象
12
+ * @returns
13
+ */
14
+ export declare const Query: (key?: string) => (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
9
15
  /**
10
16
  * 将被装饰的参数定义为请求数据
11
17
  * @param key 可选,请求data的key,未给定key,则被装饰的参数应为对象
@@ -1,10 +1,15 @@
1
1
  import "reflect-metadata";
2
- export declare const CACHE_OPTIONS_KEY: unique symbol;
2
+ export declare const NO_CACHE_KEY: unique symbol;
3
+ export declare const CACHE_EXPIRE_SOURCE_KEY: unique symbol;
3
4
  /**
4
- * 为请求方法设置失效源,或设置不启用缓存
5
- * @param hitSource 失效源,字符型|null;
6
- * - 设置为null时,该请求方法不启用缓存;
7
- * - 设置为字符时,对应该Api下请求方法名称,当设置的名称方法请求成功时,被装饰的请求方法缓存失效
5
+ * 为请求方法或API设置不启用缓存
8
6
  * @returns
9
7
  */
10
- export declare const Cache: (hitSource: string | null) => (target: any, propertyKey?: string) => void;
8
+ export declare const NoCache: () => (target: any, propertyKey?: string) => void;
9
+ /**
10
+ * 为请求方法或API设置失效源,当此源请求成功时,清空被装饰的方法缓存
11
+ * 格式serverName.apiName.methodName
12
+ * @param sources 失效源,字符型数组;
13
+ * @returns
14
+ */
15
+ export declare const HitSource: (...sources: string[]) => (target: any, propertyKey?: string) => void;
@@ -1,14 +1,13 @@
1
1
  import "reflect-metadata";
2
2
  export declare const EVENT_SOURCE_OPTION_KEY: unique symbol;
3
+ import { SseOptions } from "../typings";
3
4
  /**
4
5
  * 将装饰的方法创建为SSE连接
5
6
  * @param path 路径,要连接到服务端sse端点路径
6
7
  * @param options 配置选项
7
8
  * @returns 调用被装饰的方法可获得返回{close,eventSource}
8
9
  */
9
- export declare const Sse: (path?: string, options?: {
10
- withCredentials: boolean;
11
- }) => (target: any, propertyKey: string) => void;
10
+ export declare const Sse: (path?: string, options?: SseOptions) => (target: object) => void;
12
11
  export declare const EVENT_SOURCE_EVENTS_KEY: unique symbol;
13
12
  /**
14
13
  * 将装饰的方法注册为eventSource的对应事件
@@ -7,4 +7,4 @@ export declare const STRATEGY_KEY: unique symbol;
7
7
  * - 传入的策略请使用new 实例化
8
8
  * @returns
9
9
  */
10
- export declare const UseStrategy: (...strategies: Strategy[]) => (target: any, propertyKey?: string) => void;
10
+ export declare const UseStrategy: (...strategies: Array<new () => Strategy>) => (target: any, propertyKey?: string) => void;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from "./core";
2
2
  export { Server } from "./decorators/server";
3
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";
4
+ export { NoCache, HitSource } from "./decorators/cache";
5
+ export { Params, Data, Query } from "./decorators/args";
6
6
  export { UseStrategy } from "./decorators/strategy";
7
7
  export { Versioning, Version } from "./decorators/versioning";
8
8
  export { Sse, SseEvent, OnSseError, OnSseOpen } from "./decorators/sse";