@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/dist/snail-api.umd.cjs
CHANGED
|
@@ -3686,6 +3686,13 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3686
3686
|
})(CacheType || {});
|
|
3687
3687
|
class Strategy {
|
|
3688
3688
|
}
|
|
3689
|
+
class RegisterSseEvent {
|
|
3690
|
+
constructor() {
|
|
3691
|
+
__publicField(this, "eventName");
|
|
3692
|
+
__publicField(this, "emit");
|
|
3693
|
+
__publicField(this, "options");
|
|
3694
|
+
}
|
|
3695
|
+
}
|
|
3689
3696
|
function createCache(type, ttl) {
|
|
3690
3697
|
if (type === CacheType.Memory) {
|
|
3691
3698
|
return new MemoryCache(ttl);
|
|
@@ -3745,8 +3752,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3745
3752
|
versioning
|
|
3746
3753
|
);
|
|
3747
3754
|
}
|
|
3748
|
-
const METHOD_KEY = "SNAIL_METHOD_KEY";
|
|
3749
|
-
const API_CONFIG_KEY = "SNAIL_API_CONFIG_KEY";
|
|
3755
|
+
const METHOD_KEY = Symbol("SNAIL_METHOD_KEY");
|
|
3756
|
+
const API_CONFIG_KEY = Symbol("SNAIL_API_CONFIG_KEY");
|
|
3750
3757
|
const Api = (url = "", config) => {
|
|
3751
3758
|
return (target) => {
|
|
3752
3759
|
Reflect.defineMetadata(API_CONFIG_KEY, { url, ...config }, target);
|
|
@@ -3770,7 +3777,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3770
3777
|
const Patch = createMethodDecorator(RequestMethod.PATCH);
|
|
3771
3778
|
const Options = createMethodDecorator(RequestMethod.OPTIONS);
|
|
3772
3779
|
const Head = createMethodDecorator(RequestMethod.HEAD);
|
|
3773
|
-
const SERVER_CONFIG_KEY = "SANIL_SERVER_CONFIG_KEY";
|
|
3780
|
+
const SERVER_CONFIG_KEY = Symbol("SANIL_SERVER_CONFIG_KEY");
|
|
3774
3781
|
const Server = (config) => {
|
|
3775
3782
|
return (target) => {
|
|
3776
3783
|
Reflect.defineMetadata(SERVER_CONFIG_KEY, config, target);
|
|
@@ -3785,7 +3792,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3785
3792
|
};
|
|
3786
3793
|
const REQUEST_ARGS_KEY = Symbol("SNAIL_REQUEST_ARGS_KEY");
|
|
3787
3794
|
const Params = (key) => createArgsDecorator("params", key);
|
|
3788
|
-
const Data = () => createArgsDecorator("data");
|
|
3795
|
+
const Data = (key) => createArgsDecorator("data", key);
|
|
3789
3796
|
const createArgsDecorator = (type, key) => {
|
|
3790
3797
|
return (target, propertyKey, parameterIndex) => {
|
|
3791
3798
|
const args = Reflect.getMetadata(REQUEST_ARGS_KEY, target, propertyKey) || [];
|
|
@@ -3805,11 +3812,50 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3805
3812
|
Reflect.defineMetadata(VERSION_KEY, version, target, propertyKey);
|
|
3806
3813
|
};
|
|
3807
3814
|
};
|
|
3808
|
-
const
|
|
3809
|
-
const Cache = (
|
|
3815
|
+
const CACHE_OPTIONS_KEY = Symbol("SNALI_CACHE_OPTIONS_KEY");
|
|
3816
|
+
const Cache = (hitSource) => {
|
|
3817
|
+
return (target, propertyKey) => {
|
|
3818
|
+
const key = propertyKey ? `${CACHE_OPTIONS_KEY.toString()}_${propertyKey}` : CACHE_OPTIONS_KEY;
|
|
3819
|
+
Reflect.defineMetadata(key, hitSource, target);
|
|
3820
|
+
};
|
|
3821
|
+
};
|
|
3822
|
+
const UPLOAD_PROGRESS_KEY = Symbol("SNAIL_UPLOAD_PROGRESS_KEY");
|
|
3823
|
+
const EVENT_SOURCE_OPTION_KEY = Symbol("SNAIL_EVENT_SOURCE_OPTION_KEY");
|
|
3824
|
+
const Sse = (path, options) => {
|
|
3810
3825
|
return (target, propertyKey) => {
|
|
3811
|
-
|
|
3812
|
-
|
|
3826
|
+
Reflect.defineMetadata(
|
|
3827
|
+
EVENT_SOURCE_OPTION_KEY,
|
|
3828
|
+
{
|
|
3829
|
+
path,
|
|
3830
|
+
withCredentials: options == null ? void 0 : options.withCredentials
|
|
3831
|
+
},
|
|
3832
|
+
target,
|
|
3833
|
+
propertyKey
|
|
3834
|
+
);
|
|
3835
|
+
};
|
|
3836
|
+
};
|
|
3837
|
+
const EVENT_SOURCE_EVENTS_KEY = Symbol("EVENT_SOURCE_EVENTS_KEY");
|
|
3838
|
+
const SseEvent = (eventName, options) => {
|
|
3839
|
+
return (target, propertyKey) => {
|
|
3840
|
+
const events = Reflect.getMetadata(EVENT_SOURCE_EVENTS_KEY, target) || [];
|
|
3841
|
+
events.push({
|
|
3842
|
+
eventName: eventName ? eventName : "message",
|
|
3843
|
+
emit: target[propertyKey],
|
|
3844
|
+
options
|
|
3845
|
+
});
|
|
3846
|
+
Reflect.defineMetadata(EVENT_SOURCE_EVENTS_KEY, events, target);
|
|
3847
|
+
};
|
|
3848
|
+
};
|
|
3849
|
+
const EVENT_SOURCE_OPEN_KEY = Symbol("EVENT_SOURCE_OPEN_KEY");
|
|
3850
|
+
const OnSseOpen = () => {
|
|
3851
|
+
return (target, propertyKey) => {
|
|
3852
|
+
Reflect.defineMetadata(EVENT_SOURCE_OPEN_KEY, target[propertyKey], target);
|
|
3853
|
+
};
|
|
3854
|
+
};
|
|
3855
|
+
const EVENT_SOURCE_ERROR_KEY = Symbol("EVENT_SOURCE_ERROR_KEY");
|
|
3856
|
+
const OnSseError = () => {
|
|
3857
|
+
return (target, propertyKey) => {
|
|
3858
|
+
Reflect.defineMetadata(EVENT_SOURCE_ERROR_KEY, target[propertyKey], target);
|
|
3813
3859
|
};
|
|
3814
3860
|
};
|
|
3815
3861
|
class Snail {
|
|
@@ -3819,39 +3865,26 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3819
3865
|
__publicField(this, "strategies", []);
|
|
3820
3866
|
__publicField(this, "cacheStorage");
|
|
3821
3867
|
__publicField(this, "version");
|
|
3868
|
+
__publicField(this, "sourceMap", /* @__PURE__ */ new Map());
|
|
3869
|
+
__publicField(this, "eventSource");
|
|
3822
3870
|
}
|
|
3823
3871
|
registerStrategy(strategy) {
|
|
3824
3872
|
this.strategies.push(strategy);
|
|
3825
3873
|
}
|
|
3826
3874
|
createApi(constructor) {
|
|
3827
3875
|
const instance = new constructor();
|
|
3828
|
-
const serverConfig =
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
);
|
|
3832
|
-
console.log("
|
|
3833
|
-
|
|
3834
|
-
|
|
3835
|
-
|
|
3836
|
-
timeout: serverConfig.timeout
|
|
3837
|
-
});
|
|
3838
|
-
}
|
|
3839
|
-
const cacheManage = Reflect.getMetadata(
|
|
3840
|
-
CACHE_KEY,
|
|
3841
|
-
this
|
|
3842
|
-
);
|
|
3843
|
-
if (!this.cacheStorage && cacheManage !== void 0) {
|
|
3844
|
-
this.cacheStorage = createCache(cacheManage.type, cacheManage.ttl || 300);
|
|
3845
|
-
}
|
|
3846
|
-
const apiConfig = Reflect.getMetadata(
|
|
3847
|
-
API_CONFIG_KEY,
|
|
3848
|
-
constructor
|
|
3849
|
-
);
|
|
3850
|
-
console.log("ApiConfig:", apiConfig);
|
|
3876
|
+
const serverConfig = this.getServerConfig();
|
|
3877
|
+
const { baseURL, timeout, CacheManage, enableLog } = serverConfig;
|
|
3878
|
+
enableLog && console.log("serverConfig:", serverConfig);
|
|
3879
|
+
this.initAxios({ baseURL, timeout });
|
|
3880
|
+
enableLog && console.log("CacheManage:", CacheManage);
|
|
3881
|
+
this.initCacheManage(CacheManage);
|
|
3882
|
+
const apiConfig = this.getApiConfig(constructor);
|
|
3883
|
+
enableLog && console.log("ApiConfig:", apiConfig);
|
|
3851
3884
|
this.version = apiConfig.version;
|
|
3852
3885
|
return new Proxy(instance, {
|
|
3853
3886
|
get: (target, propertyKey) => {
|
|
3854
|
-
console.log("proxy:", target, "|", propertyKey);
|
|
3887
|
+
enableLog && console.log("proxy:", target, "|", propertyKey);
|
|
3855
3888
|
if (typeof propertyKey !== "string") return;
|
|
3856
3889
|
const methodConfig = Reflect.getMetadata(
|
|
3857
3890
|
METHOD_KEY,
|
|
@@ -3861,7 +3894,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3861
3894
|
if (!methodConfig) return target[propertyKey];
|
|
3862
3895
|
return async (...args) => {
|
|
3863
3896
|
const url = methodConfig.path == "" ? apiConfig.url : apiConfig.url + `/${methodConfig.path}`;
|
|
3864
|
-
console.log("url:", url);
|
|
3897
|
+
enableLog && console.log("url:", url);
|
|
3865
3898
|
let request = {
|
|
3866
3899
|
// baseURL: serverConfig.baseURL,
|
|
3867
3900
|
url,
|
|
@@ -3872,35 +3905,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3872
3905
|
...request,
|
|
3873
3906
|
...this.applyVersion(request, target, propertyKey)
|
|
3874
3907
|
};
|
|
3875
|
-
console.log("版本管理参数:", request);
|
|
3908
|
+
enableLog && console.log("版本管理参数:", request);
|
|
3876
3909
|
const strategies = this.getStrategies(target, propertyKey);
|
|
3877
|
-
const methodCache = this.getCacheConfig(target, propertyKey) || void 0;
|
|
3878
|
-
if (methodCache == void 0) {
|
|
3879
|
-
const cached = await this.applyCache(request, cacheManage);
|
|
3880
|
-
if (cached && cached.error === null) {
|
|
3881
|
-
let cacheResponse = {
|
|
3882
|
-
data: cached.data,
|
|
3883
|
-
status: 304,
|
|
3884
|
-
headers: {
|
|
3885
|
-
hitCache: true
|
|
3886
|
-
},
|
|
3887
|
-
statusText: "get response data from cache",
|
|
3888
|
-
config: { headers: new AxiosHeaders() }
|
|
3889
|
-
};
|
|
3890
|
-
const responseStrategies = strategies.filter(
|
|
3891
|
-
(strategy) => strategy.applyResponse
|
|
3892
|
-
);
|
|
3893
|
-
const strategyResponse = await this.applyStrategies(
|
|
3894
|
-
cacheResponse,
|
|
3895
|
-
responseStrategies,
|
|
3896
|
-
"response"
|
|
3897
|
-
);
|
|
3898
|
-
return strategyResponse;
|
|
3899
|
-
}
|
|
3900
|
-
}
|
|
3901
3910
|
const params = this.buildRequestArgs(target, propertyKey, args);
|
|
3902
3911
|
request = { ...request, ...params };
|
|
3903
|
-
console.log("构建请求参数:", request);
|
|
3912
|
+
enableLog && console.log("构建请求参数:", request);
|
|
3904
3913
|
const requestStrategies = strategies.filter(
|
|
3905
3914
|
(strategy) => strategy.applyRequest
|
|
3906
3915
|
);
|
|
@@ -3909,10 +3918,32 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3909
3918
|
requestStrategies,
|
|
3910
3919
|
"request"
|
|
3911
3920
|
);
|
|
3921
|
+
const hitSource = this.getHitSource(target, propertyKey);
|
|
3922
|
+
enableLog && console.log("hitSource:", hitSource);
|
|
3923
|
+
if (typeof hitSource == "string") {
|
|
3924
|
+
this.setHitSource(request, hitSource);
|
|
3925
|
+
}
|
|
3926
|
+
if (hitSource !== null) {
|
|
3927
|
+
const cachedResponse = await this.getCache(request, strategies);
|
|
3928
|
+
if (cachedResponse) {
|
|
3929
|
+
enableLog && console.warn("数据从缓存获取");
|
|
3930
|
+
return this.handleResponse(cachedResponse, true);
|
|
3931
|
+
}
|
|
3932
|
+
}
|
|
3933
|
+
const onUploadProgress = Reflect.getMetadata(
|
|
3934
|
+
UPLOAD_PROGRESS_KEY,
|
|
3935
|
+
target,
|
|
3936
|
+
propertyKey
|
|
3937
|
+
);
|
|
3912
3938
|
try {
|
|
3913
|
-
console.log("send request:", request);
|
|
3914
|
-
const response = await this.axiosInstance(
|
|
3915
|
-
|
|
3939
|
+
enableLog && console.log("send request:", request);
|
|
3940
|
+
const response = await this.axiosInstance({
|
|
3941
|
+
...request,
|
|
3942
|
+
onUploadProgress
|
|
3943
|
+
});
|
|
3944
|
+
hitSource !== null && this.setCache(request, response);
|
|
3945
|
+
this.expireCache(propertyKey);
|
|
3946
|
+
return this.handleResponse(response, false);
|
|
3916
3947
|
} catch (error) {
|
|
3917
3948
|
return this.handleError(error);
|
|
3918
3949
|
}
|
|
@@ -3923,7 +3954,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3923
3954
|
buildRequestArgs(target, propertyKey, args) {
|
|
3924
3955
|
const requestArgs = { params: {}, data: {} };
|
|
3925
3956
|
const paramConfigs = Reflect.getMetadata(REQUEST_ARGS_KEY, target, propertyKey) || [];
|
|
3926
|
-
console.log("请求参数:", paramConfigs);
|
|
3927
3957
|
paramConfigs.forEach(({ index, type, key }) => {
|
|
3928
3958
|
const value = args[index];
|
|
3929
3959
|
if (type === "params" && !key && typeof value === "object") {
|
|
@@ -3943,14 +3973,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3943
3973
|
}
|
|
3944
3974
|
getStrategies(target, propertyKey) {
|
|
3945
3975
|
const serverStrategies = Reflect.getMetadata(STRATEGY_KEY, this.constructor) || [];
|
|
3946
|
-
console.log("serverStrategies:", serverStrategies);
|
|
3947
3976
|
const classStrategies = Reflect.getMetadata(STRATEGY_KEY, target.constructor) || [];
|
|
3948
|
-
console.log("classStrategies:", classStrategies);
|
|
3949
3977
|
const methodStrategies = Reflect.getMetadata(
|
|
3950
3978
|
`${STRATEGY_KEY.toString()}_${propertyKey}`,
|
|
3951
3979
|
target
|
|
3952
3980
|
) || [];
|
|
3953
|
-
console.log("methodStrategies:", methodStrategies);
|
|
3954
3981
|
const allStrategies = [
|
|
3955
3982
|
...this.strategies,
|
|
3956
3983
|
...serverStrategies,
|
|
@@ -3971,7 +3998,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3971
3998
|
VERSIONING_KEY,
|
|
3972
3999
|
this.constructor
|
|
3973
4000
|
);
|
|
3974
|
-
console.log("版本管理器:", versioning);
|
|
3975
4001
|
if (!versioning) return request;
|
|
3976
4002
|
const version = Reflect.getMetadata(VERSION_KEY, target, propertyKey);
|
|
3977
4003
|
const currentVersion = version || this.version || versioning.defaultVersion;
|
|
@@ -3982,24 +4008,104 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3982
4008
|
const versionUrl = url ? `${url}/${request.url}` : request.url;
|
|
3983
4009
|
return { ...request, url: versionUrl, headers, params };
|
|
3984
4010
|
}
|
|
3985
|
-
async
|
|
4011
|
+
async getCache(request, strategies) {
|
|
3986
4012
|
var _a;
|
|
4013
|
+
if (!this.cacheStorage) return void 0;
|
|
3987
4014
|
const cacheKey = apiKey(request);
|
|
3988
|
-
|
|
3989
|
-
|
|
4015
|
+
const cached = await ((_a = this.cacheStorage) == null ? void 0 : _a.get(cacheKey));
|
|
4016
|
+
if (cached && cached.error === null) {
|
|
4017
|
+
let cacheResponse = {
|
|
4018
|
+
data: cached.data,
|
|
4019
|
+
status: 304,
|
|
4020
|
+
headers: {},
|
|
4021
|
+
statusText: "get response data from cache",
|
|
4022
|
+
config: { headers: new AxiosHeaders() }
|
|
4023
|
+
};
|
|
4024
|
+
const responseStrategies = strategies.filter(
|
|
4025
|
+
(strategy) => strategy.applyResponse
|
|
4026
|
+
);
|
|
4027
|
+
const strategyResponse = await this.applyStrategies(
|
|
4028
|
+
cacheResponse,
|
|
4029
|
+
responseStrategies,
|
|
4030
|
+
"response"
|
|
4031
|
+
);
|
|
4032
|
+
return strategyResponse;
|
|
3990
4033
|
}
|
|
3991
|
-
return void 0;
|
|
3992
4034
|
}
|
|
3993
|
-
|
|
3994
|
-
|
|
3995
|
-
|
|
4035
|
+
initCacheManage(options) {
|
|
4036
|
+
if (!this.cacheStorage && options !== void 0) {
|
|
4037
|
+
this.cacheStorage = createCache(options.type, options.ttl || 300);
|
|
4038
|
+
}
|
|
3996
4039
|
}
|
|
3997
|
-
|
|
3998
|
-
|
|
4040
|
+
getHitSource(target, propertyKey) {
|
|
4041
|
+
const methodHitSource = Reflect.getMetadata(
|
|
4042
|
+
`${CACHE_OPTIONS_KEY.toString()}_${propertyKey}`,
|
|
4043
|
+
target
|
|
4044
|
+
);
|
|
4045
|
+
if (methodHitSource !== void 0) return methodHitSource;
|
|
4046
|
+
const apiHitSource = Reflect.getMetadata(
|
|
4047
|
+
CACHE_OPTIONS_KEY,
|
|
4048
|
+
target.constructor
|
|
4049
|
+
);
|
|
4050
|
+
return apiHitSource;
|
|
4051
|
+
}
|
|
4052
|
+
setHitSource(request, hitSource) {
|
|
4053
|
+
const cacheKeys = this.sourceMap.get(hitSource);
|
|
4054
|
+
const currentCacheKey = apiKey(request);
|
|
4055
|
+
if (cacheKeys) {
|
|
4056
|
+
cacheKeys.push(currentCacheKey);
|
|
4057
|
+
this.sourceMap.set(hitSource, cacheKeys);
|
|
4058
|
+
} else {
|
|
4059
|
+
this.sourceMap.set(hitSource, [currentCacheKey]);
|
|
4060
|
+
}
|
|
4061
|
+
}
|
|
4062
|
+
async setCache(request, response) {
|
|
4063
|
+
var _a;
|
|
4064
|
+
const cacheKey = apiKey(request);
|
|
4065
|
+
await ((_a = this.cacheStorage) == null ? void 0 : _a.set(cacheKey, response.data));
|
|
4066
|
+
}
|
|
4067
|
+
async expireCache(hitSource) {
|
|
4068
|
+
const keys = this.sourceMap.get(hitSource);
|
|
4069
|
+
if (keys) {
|
|
4070
|
+
Promise.all(
|
|
4071
|
+
keys.map(async (key) => {
|
|
4072
|
+
var _a;
|
|
4073
|
+
await ((_a = this.cacheStorage) == null ? void 0 : _a.delete(key));
|
|
4074
|
+
})
|
|
4075
|
+
);
|
|
4076
|
+
}
|
|
4077
|
+
}
|
|
4078
|
+
getServerConfig() {
|
|
4079
|
+
return Reflect.getMetadata(
|
|
4080
|
+
SERVER_CONFIG_KEY,
|
|
4081
|
+
this.constructor
|
|
4082
|
+
);
|
|
4083
|
+
}
|
|
4084
|
+
getApiConfig(constructor) {
|
|
4085
|
+
return Reflect.getMetadata(API_CONFIG_KEY, constructor);
|
|
4086
|
+
}
|
|
4087
|
+
initAxios(config) {
|
|
4088
|
+
if (!this.axiosInstance) {
|
|
4089
|
+
this.axiosInstance = axios.create({
|
|
4090
|
+
baseURL: config.baseURL,
|
|
4091
|
+
timeout: config.timeout
|
|
4092
|
+
});
|
|
4093
|
+
}
|
|
4094
|
+
}
|
|
4095
|
+
generateSseUrl(baseURL, url) {
|
|
4096
|
+
if (!baseURL && !url) {
|
|
4097
|
+
return "/";
|
|
4098
|
+
}
|
|
4099
|
+
if (!baseURL && url) {
|
|
4100
|
+
return `/${url}`;
|
|
4101
|
+
}
|
|
4102
|
+
return `${baseURL}/${url}`;
|
|
4103
|
+
}
|
|
4104
|
+
handleResponse(response, hitCache) {
|
|
3999
4105
|
return {
|
|
4000
4106
|
data: response.data,
|
|
4001
4107
|
error: null,
|
|
4002
|
-
hitCache
|
|
4108
|
+
hitCache
|
|
4003
4109
|
};
|
|
4004
4110
|
}
|
|
4005
4111
|
handleError(error) {
|
|
@@ -4008,32 +4114,98 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4008
4114
|
error
|
|
4009
4115
|
};
|
|
4010
4116
|
}
|
|
4117
|
+
createSse(constructor) {
|
|
4118
|
+
const instance = new constructor();
|
|
4119
|
+
const serverConfig = this.getServerConfig();
|
|
4120
|
+
const { baseURL, enableLog } = serverConfig;
|
|
4121
|
+
const apiConfig = this.getApiConfig(constructor);
|
|
4122
|
+
enableLog && console.log("ApiConfig:", apiConfig);
|
|
4123
|
+
this.version = apiConfig.version;
|
|
4124
|
+
return new Proxy(instance, {
|
|
4125
|
+
get: (target, propertyKey) => {
|
|
4126
|
+
enableLog && console.log("proxy:", target, "|", propertyKey);
|
|
4127
|
+
if (typeof propertyKey !== "string") return;
|
|
4128
|
+
const sseOption = Reflect.getMetadata(
|
|
4129
|
+
EVENT_SOURCE_OPTION_KEY,
|
|
4130
|
+
target,
|
|
4131
|
+
propertyKey
|
|
4132
|
+
);
|
|
4133
|
+
if (sseOption) {
|
|
4134
|
+
const url = sseOption.path == "" ? apiConfig.url : apiConfig.url + `/${sseOption.path}`;
|
|
4135
|
+
const { url: versionUrl } = this.applyVersion(
|
|
4136
|
+
{ url },
|
|
4137
|
+
target,
|
|
4138
|
+
propertyKey
|
|
4139
|
+
);
|
|
4140
|
+
const sseUrl = this.generateSseUrl(baseURL, versionUrl || url);
|
|
4141
|
+
enableLog && console.log("sse-url:", sseUrl);
|
|
4142
|
+
const { eventSource, close } = this.initSse(sseUrl, sseOption);
|
|
4143
|
+
this.eventSource = eventSource;
|
|
4144
|
+
this.setSse(target);
|
|
4145
|
+
this.registerSseEvent(target);
|
|
4146
|
+
return () => {
|
|
4147
|
+
return { eventSource, close };
|
|
4148
|
+
};
|
|
4149
|
+
}
|
|
4150
|
+
}
|
|
4151
|
+
});
|
|
4152
|
+
}
|
|
4153
|
+
initSse(url, options) {
|
|
4154
|
+
const eventSource = new EventSource(url, {
|
|
4155
|
+
withCredentials: options.withCredentials ? options.withCredentials : false
|
|
4156
|
+
});
|
|
4157
|
+
console.log("EventSource:", eventSource);
|
|
4158
|
+
return {
|
|
4159
|
+
eventSource,
|
|
4160
|
+
close: eventSource.close
|
|
4161
|
+
};
|
|
4162
|
+
}
|
|
4163
|
+
setSse(target) {
|
|
4164
|
+
const onOpenFunc = Reflect.getMetadata(EVENT_SOURCE_OPEN_KEY, target);
|
|
4165
|
+
console.log("sse-proxy[open]:", onOpenFunc);
|
|
4166
|
+
if (typeof onOpenFunc == "function") {
|
|
4167
|
+
this.eventSource && (this.eventSource.onopen = onOpenFunc);
|
|
4168
|
+
}
|
|
4169
|
+
const onErrorFunc = Reflect.getMetadata(EVENT_SOURCE_ERROR_KEY, target);
|
|
4170
|
+
console.log("sse-proxy[error]:", onErrorFunc);
|
|
4171
|
+
if (typeof onErrorFunc == "function") {
|
|
4172
|
+
this.eventSource && (this.eventSource.onerror = onErrorFunc);
|
|
4173
|
+
}
|
|
4174
|
+
}
|
|
4175
|
+
registerSseEvent(target) {
|
|
4176
|
+
const eventSource = this.eventSource;
|
|
4177
|
+
if (!eventSource) return;
|
|
4178
|
+
const events = Reflect.getMetadata(
|
|
4179
|
+
EVENT_SOURCE_EVENTS_KEY,
|
|
4180
|
+
target
|
|
4181
|
+
);
|
|
4182
|
+
events.map((event) => {
|
|
4183
|
+
eventSource.addEventListener(event.eventName, event.emit, event.options);
|
|
4184
|
+
});
|
|
4185
|
+
}
|
|
4011
4186
|
}
|
|
4012
|
-
exports2.API_CONFIG_KEY = API_CONFIG_KEY;
|
|
4013
4187
|
exports2.Api = Api;
|
|
4014
|
-
exports2.CACHE_KEY = CACHE_KEY;
|
|
4015
4188
|
exports2.Cache = Cache;
|
|
4016
4189
|
exports2.CacheType = CacheType;
|
|
4017
4190
|
exports2.Data = Data;
|
|
4018
4191
|
exports2.Delete = Delete;
|
|
4019
4192
|
exports2.Get = Get;
|
|
4020
4193
|
exports2.Head = Head;
|
|
4021
|
-
exports2.
|
|
4194
|
+
exports2.OnSseError = OnSseError;
|
|
4195
|
+
exports2.OnSseOpen = OnSseOpen;
|
|
4022
4196
|
exports2.Options = Options;
|
|
4023
4197
|
exports2.Params = Params;
|
|
4024
4198
|
exports2.Patch = Patch;
|
|
4025
4199
|
exports2.Post = Post;
|
|
4026
4200
|
exports2.Put = Put;
|
|
4027
|
-
exports2.
|
|
4201
|
+
exports2.RegisterSseEvent = RegisterSseEvent;
|
|
4028
4202
|
exports2.RequestMethod = RequestMethod;
|
|
4029
|
-
exports2.SERVER_CONFIG_KEY = SERVER_CONFIG_KEY;
|
|
4030
|
-
exports2.STRATEGY_KEY = STRATEGY_KEY;
|
|
4031
4203
|
exports2.Server = Server;
|
|
4032
4204
|
exports2.Snail = Snail;
|
|
4205
|
+
exports2.Sse = Sse;
|
|
4206
|
+
exports2.SseEvent = SseEvent;
|
|
4033
4207
|
exports2.Strategy = Strategy;
|
|
4034
4208
|
exports2.UseStrategy = UseStrategy;
|
|
4035
|
-
exports2.VERSIONING_KEY = VERSIONING_KEY;
|
|
4036
|
-
exports2.VERSION_KEY = VERSION_KEY;
|
|
4037
4209
|
exports2.Version = Version;
|
|
4038
4210
|
exports2.Versioning = Versioning;
|
|
4039
4211
|
exports2.VersioningType = VersioningType;
|
|
@@ -5,7 +5,7 @@ export type ApiResponse<T> = Promise<{
|
|
|
5
5
|
export type ApiProxy<T, R extends {
|
|
6
6
|
data: any;
|
|
7
7
|
}> = {
|
|
8
|
-
[K in keyof T]: T[K] extends (...args: infer A) => any ? <D = any
|
|
8
|
+
[K in keyof T]: T[K] extends (...args: infer A) => any ? <D = any>(...args: A) => ApiResponse<R & {
|
|
9
9
|
data: D;
|
|
10
10
|
}> : T[K];
|
|
11
11
|
};
|
package/dist/typings/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { CacheManagementOption } from "./cache.management.option";
|
|
2
|
-
import { VersioningOption } from "./versioning.option";
|
|
3
2
|
export interface SnailOption {
|
|
4
3
|
baseURL?: string;
|
|
5
|
-
Versioning?: VersioningOption;
|
|
6
4
|
CacheManage?: CacheManagementOption;
|
|
7
5
|
enableLog?: boolean;
|
|
8
6
|
timeout?: number;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type SseProxy<T extends object> = {
|
|
2
|
+
[K in keyof T]: T[K] extends (...args: infer A) => any ? () => {
|
|
3
|
+
sourceEvent: EventSource;
|
|
4
|
+
close: Function;
|
|
5
|
+
} : T[K];
|
|
6
|
+
};
|
|
7
|
+
export declare class RegisterSseEvent {
|
|
8
|
+
eventName: string;
|
|
9
|
+
emit: (event: any) => any;
|
|
10
|
+
options: boolean | AddEventListenerOptions;
|
|
11
|
+
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { VersioningOption } from "../typings";
|
|
2
|
-
interface VersioningResult {
|
|
2
|
+
export interface VersioningResult {
|
|
3
3
|
url?: string;
|
|
4
4
|
headers?: Record<string, any>;
|
|
5
5
|
params?: Record<string, any>;
|
|
6
6
|
}
|
|
7
7
|
export declare function applyVersioning(version: string, versioning: VersioningOption): VersioningResult;
|
|
8
|
-
export {};
|