@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.
- package/README.md +223 -73
- package/README_EN.md +592 -0
- package/dist/cache/index.d.ts +6 -2
- package/dist/cache/indexDBCache.d.ts +7 -22
- package/dist/cache/localstorageCache.d.ts +7 -22
- package/dist/cache/memoryCache.d.ts +8 -23
- package/dist/core/index.d.ts +4 -1
- package/dist/core/snailApi.d.ts +22 -0
- package/dist/core/snailMethod.d.ts +54 -0
- package/dist/core/snailServer.d.ts +34 -0
- package/dist/core/snailSse.d.ts +20 -0
- package/dist/decorators/api.d.ts +9 -9
- package/dist/decorators/{param.d.ts → args.d.ts} +6 -0
- package/dist/decorators/cache.d.ts +11 -6
- package/dist/decorators/sse.d.ts +2 -3
- package/dist/decorators/strategy.d.ts +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/snail-api.js +1125 -531
- package/dist/snail-api.umd.cjs +1126 -532
- package/dist/typings/api.option.d.ts +16 -0
- package/dist/typings/apiProxy.d.ts +5 -4
- package/dist/typings/cache.management.option.d.ts +16 -10
- package/dist/typings/cache.type.d.ts +17 -2
- package/dist/typings/index.d.ts +2 -1
- package/dist/typings/request.method.d.ts +9 -8
- package/dist/typings/response.data.d.ts +7 -3
- package/dist/typings/snail.method.d.ts +11 -0
- package/dist/typings/snail.option.d.ts +5 -1
- package/dist/typings/sse.d.ts +5 -1
- package/dist/typings/versioning.option.d.ts +2 -2
- package/dist/utils/function.d.ts +29 -8
- package/dist/versioning/index.d.ts +1 -0
- package/dist/versioning/versioning.d.ts +10 -5
- package/package.json +2 -1
- package/dist/core/snail.d.ts +0 -32
- package/dist/typings/api.config.d.ts +0 -9
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RequestMethod } from "./request.method";
|
|
2
|
+
import { SnailServer } from "../core";
|
|
3
|
+
export interface ApiOptions {
|
|
4
|
+
name?: string;
|
|
5
|
+
timeout?: number;
|
|
6
|
+
version?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface MethodOption {
|
|
9
|
+
name?: string;
|
|
10
|
+
method: RequestMethod;
|
|
11
|
+
path: string;
|
|
12
|
+
}
|
|
13
|
+
export interface ApiInstanceOptions {
|
|
14
|
+
serverInstance: SnailServer;
|
|
15
|
+
enableLog?: boolean;
|
|
16
|
+
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { ResponseData } from "./response.data";
|
|
1
|
+
import { ResponseData, ResponseJsonData, SpecialResponseData, StandardResponseData } from "./response.data";
|
|
2
|
+
import { SnailMethod } from "../core/snailMethod";
|
|
2
3
|
export type ApiResponse<T> = Promise<{
|
|
3
4
|
data: T | null;
|
|
4
5
|
error: Error | null;
|
|
5
6
|
hitCache?: boolean;
|
|
6
7
|
}>;
|
|
7
|
-
export type ApiProxy<T,
|
|
8
|
-
[K in keyof T]: T[K] extends (...args: infer A) => any ? <
|
|
9
|
-
[KK in DK]:
|
|
8
|
+
export type ApiProxy<T extends object, RT extends ResponseData = StandardResponseData<ResponseJsonData>, DK extends string = "data"> = {
|
|
9
|
+
[K in keyof T]: T[K] extends (...args: infer A) => any ? RT extends SpecialResponseData ? <RD>(...args: A) => SnailMethod<RD extends SpecialResponseData ? RD : never> : <RD>(...args: A) => RD extends SpecialResponseData ? SnailMethod<RD> : SnailMethod<RT & {
|
|
10
|
+
[KK in DK]: RD;
|
|
10
11
|
}> : T[K];
|
|
11
12
|
};
|
|
@@ -1,19 +1,25 @@
|
|
|
1
|
+
import { AdapterConstructor } from "./cache.type";
|
|
1
2
|
export declare enum CacheType {
|
|
2
3
|
Memory = 0,
|
|
3
4
|
IndexDB = 1,
|
|
4
|
-
LocalStorage = 2
|
|
5
|
+
LocalStorage = 2,
|
|
6
|
+
Custom = 3
|
|
5
7
|
}
|
|
6
|
-
|
|
8
|
+
export type MemoryCacheOption = {
|
|
7
9
|
type: CacheType.Memory;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
+
} & CacheCommonOption;
|
|
11
|
+
export type IndexDBCacheOption = {
|
|
10
12
|
type: CacheType.IndexDB;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
+
} & CacheCommonOption;
|
|
14
|
+
export type LocalStorageCacheOption = {
|
|
13
15
|
type: CacheType.LocalStorage;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
+
} & CacheCommonOption;
|
|
17
|
+
export type CustomCacheOption = {
|
|
18
|
+
type: CacheType.Custom;
|
|
19
|
+
adapter: AdapterConstructor;
|
|
20
|
+
options?: any;
|
|
21
|
+
} & CacheCommonOption;
|
|
22
|
+
export interface CacheCommonOption {
|
|
16
23
|
ttl?: number;
|
|
17
24
|
}
|
|
18
|
-
export type CacheManagementOption =
|
|
19
|
-
export {};
|
|
25
|
+
export type CacheManagementOption = MemoryCacheOption | IndexDBCacheOption | LocalStorageCacheOption | CustomCacheOption;
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import MemoryCache from "../cache/memoryCache";
|
|
2
2
|
import LocalStorageCache from "../cache/localstorageCache";
|
|
3
3
|
import IndexDBCache from "../cache/indexDBCache";
|
|
4
|
-
export interface
|
|
4
|
+
export interface CacheSetData<T = any> {
|
|
5
5
|
data: T;
|
|
6
6
|
exp: number;
|
|
7
7
|
}
|
|
8
|
-
export type
|
|
8
|
+
export type CacheGetData<T = any> = {
|
|
9
|
+
error: null;
|
|
10
|
+
data: T;
|
|
11
|
+
} | {
|
|
12
|
+
error: Error | Event;
|
|
13
|
+
data: null;
|
|
14
|
+
};
|
|
15
|
+
export type CacheStorage = MemoryCache | LocalStorageCache | IndexDBCache | CacheStorageAdapter;
|
|
16
|
+
export declare abstract class CacheStorageAdapter {
|
|
17
|
+
abstract get<T = any>(key: string): Promise<CacheGetData<T>>;
|
|
18
|
+
abstract set<T = any>(key: string, value: CacheSetData<T>): Promise<boolean>;
|
|
19
|
+
abstract delete(key: string): Promise<boolean>;
|
|
20
|
+
abstract clear(): Promise<boolean>;
|
|
21
|
+
abstract keys(): Promise<string[]>;
|
|
22
|
+
}
|
|
23
|
+
export type AdapterConstructor = new (options?: any) => CacheStorageAdapter;
|
package/dist/typings/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from "./request.body";
|
|
2
2
|
export * from "./request.method";
|
|
3
3
|
export * from "./snail.option";
|
|
4
|
-
export * from "./api.
|
|
4
|
+
export * from "./api.option";
|
|
5
5
|
export * from "./versioning.option";
|
|
6
6
|
export * from "./cache.management.option";
|
|
7
7
|
export * from "./cache.type";
|
|
@@ -9,3 +9,4 @@ export * from "./response.data";
|
|
|
9
9
|
export * from "./strategy";
|
|
10
10
|
export * from "./apiProxy";
|
|
11
11
|
export * from "./sse";
|
|
12
|
+
export * from "./snail.method";
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
export declare enum
|
|
2
|
-
GET = "
|
|
3
|
-
HEAD = "
|
|
4
|
-
POST = "
|
|
5
|
-
PUT = "
|
|
6
|
-
DELETE = "
|
|
7
|
-
PATCH = "
|
|
8
|
-
OPTIONS = "
|
|
1
|
+
export declare enum RequestMethodEnum {
|
|
2
|
+
GET = "Get",
|
|
3
|
+
HEAD = "Head",
|
|
4
|
+
POST = "Post",
|
|
5
|
+
PUT = "Put",
|
|
6
|
+
DELETE = "delete",
|
|
7
|
+
PATCH = "Patch",
|
|
8
|
+
OPTIONS = "Options"
|
|
9
9
|
}
|
|
10
|
+
export type RequestMethod = Uppercase<`${RequestMethodEnum}`> | Lowercase<`${RequestMethodEnum}`> | `${RequestMethodEnum}` | RequestMethodEnum;
|
|
@@ -4,7 +4,11 @@ export interface PaginationData<T> {
|
|
|
4
4
|
size: number;
|
|
5
5
|
record: T[];
|
|
6
6
|
}
|
|
7
|
-
export
|
|
8
|
-
|
|
7
|
+
export type ResponseJsonData = Record<string, any> | object;
|
|
8
|
+
export type SpecialResponseData = string | ArrayBuffer | Blob | FormData;
|
|
9
|
+
export type StandardResponseData<T extends ResponseJsonData = Record<string, any>> = {
|
|
10
|
+
code: number;
|
|
9
11
|
message: string;
|
|
10
|
-
|
|
12
|
+
data: T;
|
|
13
|
+
};
|
|
14
|
+
export type ResponseData = ResponseJsonData | StandardResponseData | SpecialResponseData;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AxiosResponse } from "axios";
|
|
2
|
+
import { ResponseData, ResponseJsonData, StandardResponseData, SpecialResponseData } from "./response.data";
|
|
3
|
+
export type EventHandler<T = any> = (data?: T) => void;
|
|
4
|
+
export declare enum EventType {
|
|
5
|
+
Success = "success",
|
|
6
|
+
Error = "error",
|
|
7
|
+
hitCache = "hitCache",
|
|
8
|
+
Finish = "finish"
|
|
9
|
+
}
|
|
10
|
+
export type SnailMethodEventType = `${EventType}` | EventType | string;
|
|
11
|
+
export type SendRequest<RD extends ResponseData = StandardResponseData<ResponseJsonData>> = () => Promise<RD extends SpecialResponseData ? AxiosResponse<RD> : RD>;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { CacheManagementOption } from "./cache.management.option";
|
|
2
|
+
import { RequestMethod } from "./request.method";
|
|
3
|
+
export type CacheForType = "All" | "all" | RequestMethod | RequestMethod[];
|
|
2
4
|
export interface SnailOption {
|
|
5
|
+
name?: string;
|
|
3
6
|
baseURL?: string;
|
|
4
|
-
|
|
7
|
+
cacheManage?: CacheManagementOption;
|
|
8
|
+
cacheFor?: CacheForType;
|
|
5
9
|
enableLog?: boolean;
|
|
6
10
|
timeout?: number;
|
|
7
11
|
}
|
package/dist/typings/sse.d.ts
CHANGED
|
@@ -4,8 +4,12 @@ export type SseProxy<T extends object> = {
|
|
|
4
4
|
eventSource: EventSource;
|
|
5
5
|
} : T[K];
|
|
6
6
|
};
|
|
7
|
-
export declare class
|
|
7
|
+
export declare class SseEventListener {
|
|
8
8
|
eventName: string;
|
|
9
9
|
emit: (event: any) => any;
|
|
10
10
|
options: boolean | AddEventListenerOptions;
|
|
11
11
|
}
|
|
12
|
+
export declare class SseOptions {
|
|
13
|
+
withCredentials?: boolean;
|
|
14
|
+
version?: string;
|
|
15
|
+
}
|
|
@@ -22,8 +22,8 @@ export interface VersioningQueryOption extends VersioningCommonOption {
|
|
|
22
22
|
export interface VersioningCustomOption extends VersioningCommonOption {
|
|
23
23
|
type: VersioningType.Custom;
|
|
24
24
|
extractor: (requestOptions: unknown) => {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
type: VersioningType;
|
|
26
|
+
result: string | Record<string, any>;
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
export type VersioningOption = VersioningUriOption | VersioningHeaderOption | VersioningQueryOption | VersioningCustomOption;
|
package/dist/utils/function.d.ts
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
data?: any;
|
|
5
|
-
version?: string;
|
|
6
|
-
params?: Record<string, any>;
|
|
7
|
-
headers?: Record<string, any>;
|
|
8
|
-
}>(apiInstance: T) => Promise<string>;
|
|
1
|
+
import { AxiosRequestConfig, AxiosResponse } from "axios";
|
|
2
|
+
import { Strategy } from "../typings";
|
|
3
|
+
export declare function generateCacheKey(methodName: string, version: string | undefined, request: AxiosRequestConfig): Promise<string>;
|
|
9
4
|
export declare function generateShortUniqueHash(str: string): Promise<string>;
|
|
10
5
|
export declare function recordToString(record: Record<string, any>): string;
|
|
6
|
+
export declare function replacePlaceholders(route: string, values: {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}): string;
|
|
9
|
+
export declare const resolveUrl: (url?: string) => string;
|
|
10
|
+
export declare function applyStrategies<T extends "request">(data: AxiosRequestConfig, strategies: Array<new () => Strategy>, type: T): Promise<AxiosRequestConfig>;
|
|
11
|
+
export declare function applyStrategies<T extends "response">(data: AxiosResponse, strategies: Array<new () => Strategy>, type: T): Promise<AxiosResponse>;
|
|
12
|
+
/**
|
|
13
|
+
* 一个无用函数,避免tsc报错
|
|
14
|
+
*/
|
|
15
|
+
export declare const SnailPass: (...args: any) => void;
|
|
16
|
+
interface IJson {
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
}
|
|
19
|
+
interface IArgs {
|
|
20
|
+
params: IJson;
|
|
21
|
+
data: IJson;
|
|
22
|
+
querys: IJson;
|
|
23
|
+
}
|
|
24
|
+
export declare const buildRequestArgs: (target: any, propertyKey: string, args: any[]) => IArgs;
|
|
25
|
+
/**
|
|
26
|
+
* 判断响应的数据类型是否为 application/json
|
|
27
|
+
* @param {AxiosResponse} response - Axios 的响应对象
|
|
28
|
+
* @returns {boolean} - 如果响应的数据类型是 application/json 则返回 true,否则返回 false
|
|
29
|
+
*/
|
|
30
|
+
export declare function isSpecialResponse(response: AxiosResponse): boolean;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './versioning';
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import { VersioningOption } from "../typings";
|
|
1
|
+
import { VersioningType, VersioningOption } from "../typings";
|
|
2
2
|
export interface VersioningResult {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
params?: Record<string, any>;
|
|
3
|
+
type: VersioningType;
|
|
4
|
+
result: string | Record<string, any>;
|
|
6
5
|
}
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param version 版本号
|
|
9
|
+
* @param serverInstance 当前Server的实例
|
|
10
|
+
* @returns serverInstance上未配置版本管理器返回undefined, 否则返回版本管理器的结果
|
|
11
|
+
*/
|
|
12
|
+
export declare function applyVersioning(version: string, versioning: VersioningOption): VersioningResult | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snail-js/api",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15",
|
|
4
4
|
"description": "Http Request with Decorators Api, build on axios",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
32
|
"README.md",
|
|
33
|
+
"README_EN.md",
|
|
33
34
|
"dist"
|
|
34
35
|
],
|
|
35
36
|
"dependencies": {
|
package/dist/core/snail.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import "reflect-metadata";
|
|
2
|
-
import { Strategy, ApiProxy, SseProxy, ResponseData } from "../typings";
|
|
3
|
-
export declare class Snail<R extends {} = ResponseData, DK extends string = "data"> {
|
|
4
|
-
private axiosInstance;
|
|
5
|
-
private strategies;
|
|
6
|
-
private cacheStorage?;
|
|
7
|
-
private version?;
|
|
8
|
-
private sourceMap;
|
|
9
|
-
private eventSource;
|
|
10
|
-
registerStrategy(strategy: Strategy): void;
|
|
11
|
-
createApi<T extends object>(constructor: new () => T): ApiProxy<T, R, DK>;
|
|
12
|
-
private buildRequestArgs;
|
|
13
|
-
private getStrategies;
|
|
14
|
-
private applyStrategies;
|
|
15
|
-
private applyVersion;
|
|
16
|
-
private getCache;
|
|
17
|
-
private initCacheManage;
|
|
18
|
-
private getHitSource;
|
|
19
|
-
private setHitSource;
|
|
20
|
-
private setCache;
|
|
21
|
-
private expireCache;
|
|
22
|
-
private getServerConfig;
|
|
23
|
-
private getApiConfig;
|
|
24
|
-
private initAxios;
|
|
25
|
-
private generateSseUrl;
|
|
26
|
-
private handleResponse;
|
|
27
|
-
private handleError;
|
|
28
|
-
createSse<T extends object>(constructor: new () => T): SseProxy<T>;
|
|
29
|
-
private initSse;
|
|
30
|
-
private setSse;
|
|
31
|
-
private registerSseEvent;
|
|
32
|
-
}
|