@snail-js/api 0.1.19 → 0.1.21
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 +20 -0
- package/dist/core/snailServer.d.ts +3 -1
- package/dist/core/snailSse.d.ts +4 -4
- package/dist/snail-api.js +68 -28
- package/dist/snail-api.umd.cjs +68 -28
- package/dist/typings/snail.option.d.ts +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -171,6 +171,13 @@ const data = await send();
|
|
|
171
171
|
<td>Get</td>
|
|
172
172
|
<td>要启用缓存的方法,默认仅开启Get缓存</td>
|
|
173
173
|
</tr>
|
|
174
|
+
<tr>
|
|
175
|
+
<td>serverStatusCodeRule</td>
|
|
176
|
+
<td><a href="#SnailServerStatusCodeRuleOptions">SnailServerStatusCodeRuleOptions</a></td>
|
|
177
|
+
<td>否</td>
|
|
178
|
+
<td>undefined</td>
|
|
179
|
+
<td>服务端状态码规则,若配置此项,rule函数返回false时会触发错误</td>
|
|
180
|
+
</tr>
|
|
174
181
|
<tr>
|
|
175
182
|
<td>enableLog</td>
|
|
176
183
|
<td>boolean</td>
|
|
@@ -426,6 +433,19 @@ export type VersioningOption =
|
|
|
426
433
|
| VersioningCustomOption;
|
|
427
434
|
```
|
|
428
435
|
|
|
436
|
+
### <a id="SnailServerStatusCodeRuleOptions">SnailServerStatusCodeRuleOptions</a> 类型
|
|
437
|
+
|
|
438
|
+
```typescript
|
|
439
|
+
export class SnailServerStatusCodeRuleOptions {
|
|
440
|
+
// 通常服务端会返回一个状态码,当状态码不符合预期时,会抛出错误
|
|
441
|
+
// 可以使用此选项来定义服务端状态码的规则
|
|
442
|
+
// 当rule函数返回false时,会触发错误
|
|
443
|
+
rule: (statusCode: number) => boolean;
|
|
444
|
+
// response.data中服务端状态码的key,默认为code
|
|
445
|
+
key?: string;
|
|
446
|
+
}
|
|
447
|
+
```
|
|
448
|
+
|
|
429
449
|
### 临时版本修改器`@Version`
|
|
430
450
|
|
|
431
451
|
- 临时改变方法请求的版本
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
import { AxiosInstance } from "axios";
|
|
3
|
-
import { Strategy, ApiInstanceOptions, VersioningOption, CacheStorage, ApiProxy, ResponseData, StandardResponseData, CacheForType } from "../typings";
|
|
3
|
+
import { Strategy, ApiInstanceOptions, VersioningOption, CacheStorage, ApiProxy, ResponseData, StandardResponseData, CacheForType, SnailServerStatusCodeRuleOptions } from "../typings";
|
|
4
4
|
import { SnailApi } from "./snailApi";
|
|
5
5
|
import { SnailSse } from "./snailSse";
|
|
6
6
|
export declare const CacheStorageMap: Map<string, CacheStorage>;
|
|
@@ -10,6 +10,7 @@ export declare const ExpireSourceMap: Map<string, Set<string>>;
|
|
|
10
10
|
export declare const AxiosInstanceMap: Map<string, AxiosInstance>;
|
|
11
11
|
export declare const StrategyMap: Map<string, (new () => Strategy)[]>;
|
|
12
12
|
export declare const VersioningMap: Map<string, VersioningOption>;
|
|
13
|
+
export declare const ServerStatusCodeRuleMap: Map<string, SnailServerStatusCodeRuleOptions>;
|
|
13
14
|
export declare class SnailServer<RT extends ResponseData = Omit<StandardResponseData, "data">, DK extends string = "data"> {
|
|
14
15
|
private Name;
|
|
15
16
|
private BaseURL;
|
|
@@ -17,6 +18,7 @@ export declare class SnailServer<RT extends ResponseData = Omit<StandardResponse
|
|
|
17
18
|
private EnableLog;
|
|
18
19
|
constructor();
|
|
19
20
|
private init;
|
|
21
|
+
private initServerStatusCodeRule;
|
|
20
22
|
private initLog;
|
|
21
23
|
private initStrategy;
|
|
22
24
|
private initVersioning;
|
package/dist/core/snailSse.d.ts
CHANGED
|
@@ -9,12 +9,12 @@ export declare class SnailSse {
|
|
|
9
9
|
constructor(server: SnailServer);
|
|
10
10
|
private getSseOptions;
|
|
11
11
|
private initVersion;
|
|
12
|
-
open()
|
|
13
|
-
close()
|
|
12
|
+
open: () => void;
|
|
13
|
+
close: () => void;
|
|
14
14
|
private registerSseEvent;
|
|
15
15
|
private setEvent;
|
|
16
|
-
on(eventName: string, callback: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions)
|
|
17
|
-
off(eventName: string, callback: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions)
|
|
16
|
+
on: (eventName: string, callback: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) => void;
|
|
17
|
+
off: (eventName: string, callback: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions) => void;
|
|
18
18
|
get url(): string;
|
|
19
19
|
get eventSource(): EventSource | null;
|
|
20
20
|
}
|
package/dist/snail-api.js
CHANGED
|
@@ -3727,6 +3727,12 @@ var RequestMethodEnum = /* @__PURE__ */ ((RequestMethodEnum2) => {
|
|
|
3727
3727
|
RequestMethodEnum2["OPTIONS"] = "Options";
|
|
3728
3728
|
return RequestMethodEnum2;
|
|
3729
3729
|
})(RequestMethodEnum || {});
|
|
3730
|
+
class SnailServerStatusCodeRuleOptions {
|
|
3731
|
+
constructor() {
|
|
3732
|
+
__publicField(this, "key");
|
|
3733
|
+
__publicField(this, "rule");
|
|
3734
|
+
}
|
|
3735
|
+
}
|
|
3730
3736
|
var VersioningType = /* @__PURE__ */ ((VersioningType2) => {
|
|
3731
3737
|
VersioningType2[VersioningType2["Uri"] = 0] = "Uri";
|
|
3732
3738
|
VersioningType2[VersioningType2["Header"] = 1] = "Header";
|
|
@@ -4036,6 +4042,7 @@ class SnailMethod {
|
|
|
4036
4042
|
const [serverName] = this.apiInstance.name.split(".");
|
|
4037
4043
|
const axios2 = AxiosInstanceMap.get(serverName);
|
|
4038
4044
|
if (!axios2) throw new Error("AxiosInstance not created");
|
|
4045
|
+
const serverStatusCodeRule = ServerStatusCodeRuleMap.get(serverName);
|
|
4039
4046
|
const strategies = this.getStrategies();
|
|
4040
4047
|
this.Request = await applyStrategies(this.Request, strategies, "request");
|
|
4041
4048
|
this.Request = this.applyProgress(this.Request);
|
|
@@ -4044,7 +4051,7 @@ class SnailMethod {
|
|
|
4044
4051
|
this.propertyKey,
|
|
4045
4052
|
this.Args
|
|
4046
4053
|
);
|
|
4047
|
-
console.log("
|
|
4054
|
+
this.enableLog() && console.log("methodUrl:", this.Request.baseURL, this.Request.url);
|
|
4048
4055
|
const newUrl = replacePlaceholders(this.Url, params);
|
|
4049
4056
|
this.Request = {
|
|
4050
4057
|
...this.Request,
|
|
@@ -4083,11 +4090,23 @@ class SnailMethod {
|
|
|
4083
4090
|
}
|
|
4084
4091
|
this.Response = response;
|
|
4085
4092
|
this.applyHitSource(serverName);
|
|
4086
|
-
this.emit("success", response.data);
|
|
4087
|
-
this.emit("finish", response.data);
|
|
4088
4093
|
isSpecial && console.log("isSpecial:", isSpecial);
|
|
4089
|
-
if (isSpecial)
|
|
4094
|
+
if (isSpecial) {
|
|
4095
|
+
this.emit("success", response.data);
|
|
4096
|
+
this.emit("finish", response.data);
|
|
4097
|
+
return response;
|
|
4098
|
+
}
|
|
4090
4099
|
!isSpecial && console.log("isSpecial:", isSpecial);
|
|
4100
|
+
if (serverStatusCodeRule) {
|
|
4101
|
+
const { key, rule } = serverStatusCodeRule;
|
|
4102
|
+
const statuCodeKey = key ?? "code";
|
|
4103
|
+
const data2 = response.data;
|
|
4104
|
+
if (rule(data2[statuCodeKey])) {
|
|
4105
|
+
this.emit("success", response.data);
|
|
4106
|
+
} else {
|
|
4107
|
+
this.emit("error", response.data);
|
|
4108
|
+
}
|
|
4109
|
+
}
|
|
4091
4110
|
return response.data;
|
|
4092
4111
|
} catch (error) {
|
|
4093
4112
|
this.emit("error", error);
|
|
@@ -4157,7 +4176,7 @@ class SnailMethod {
|
|
|
4157
4176
|
this.propertyKey = propertyKey.toString();
|
|
4158
4177
|
this.Args = args ?? [];
|
|
4159
4178
|
this.init();
|
|
4160
|
-
console.log("url:", this.Url);
|
|
4179
|
+
this.enableLog() && console.log("url:", this.Url);
|
|
4161
4180
|
this.eventMap = /* @__PURE__ */ new Map();
|
|
4162
4181
|
this.onceWrapperMap = /* @__PURE__ */ new Map();
|
|
4163
4182
|
this.eventMap.set("success", /* @__PURE__ */ new Set());
|
|
@@ -4179,8 +4198,11 @@ class SnailMethod {
|
|
|
4179
4198
|
this.initVersion();
|
|
4180
4199
|
}
|
|
4181
4200
|
initUrl() {
|
|
4182
|
-
|
|
4183
|
-
|
|
4201
|
+
if (this.Path === "") {
|
|
4202
|
+
this.Url = this.apiInstance.url || "";
|
|
4203
|
+
return;
|
|
4204
|
+
}
|
|
4205
|
+
this.Url = `${this.apiInstance.url || ""}${resolveUrl(this.Path)}`;
|
|
4184
4206
|
}
|
|
4185
4207
|
initVersion() {
|
|
4186
4208
|
const version = Reflect.getMetadata(
|
|
@@ -4326,7 +4348,10 @@ class SnailMethod {
|
|
|
4326
4348
|
this.propertyKey
|
|
4327
4349
|
);
|
|
4328
4350
|
const isApiNoCache = this.apiInstance.noCache;
|
|
4351
|
+
this.enableLog() && console.log("isApiNoCache:", isApiNoCache);
|
|
4352
|
+
this.enableLog() && console.log("isMethodNoCache:", isMethodNoCache);
|
|
4329
4353
|
const noCacheFlag = isMethodNoCache ? true : isApiNoCache ? true : false;
|
|
4354
|
+
this.enableLog() && console.log("noCacheFlag:", noCacheFlag);
|
|
4330
4355
|
return flag ? noCacheFlag : true;
|
|
4331
4356
|
}
|
|
4332
4357
|
enableLog() {
|
|
@@ -4372,6 +4397,7 @@ const ExpireSourceMap = /* @__PURE__ */ new Map();
|
|
|
4372
4397
|
const AxiosInstanceMap = /* @__PURE__ */ new Map();
|
|
4373
4398
|
const StrategyMap = /* @__PURE__ */ new Map();
|
|
4374
4399
|
const VersioningMap = /* @__PURE__ */ new Map();
|
|
4400
|
+
const ServerStatusCodeRuleMap = /* @__PURE__ */ new Map();
|
|
4375
4401
|
const defaultCacheManageOptions = {
|
|
4376
4402
|
type: CacheType.Memory,
|
|
4377
4403
|
ttl: 500
|
|
@@ -4400,9 +4426,19 @@ class SnailServer {
|
|
|
4400
4426
|
}
|
|
4401
4427
|
init() {
|
|
4402
4428
|
const options = this.getServerOptions();
|
|
4403
|
-
const {
|
|
4429
|
+
const {
|
|
4430
|
+
baseURL,
|
|
4431
|
+
timeout,
|
|
4432
|
+
cacheManage,
|
|
4433
|
+
enableLog,
|
|
4434
|
+
cacheFor,
|
|
4435
|
+
serverStatusCodeRule
|
|
4436
|
+
} = options;
|
|
4404
4437
|
this.BaseURL = baseURL ?? resolveUrl("");
|
|
4405
4438
|
this.initAxios({ baseURL, timeout });
|
|
4439
|
+
if (serverStatusCodeRule) {
|
|
4440
|
+
this.initServerStatusCodeRule(serverStatusCodeRule);
|
|
4441
|
+
}
|
|
4406
4442
|
this.initStrategy();
|
|
4407
4443
|
this.initCacheManage(cacheManage, cacheFor);
|
|
4408
4444
|
this.initVersioning();
|
|
@@ -4410,6 +4446,9 @@ class SnailServer {
|
|
|
4410
4446
|
this.EnableLog = enableLog ?? false;
|
|
4411
4447
|
this.initLog(enableLog);
|
|
4412
4448
|
}
|
|
4449
|
+
initServerStatusCodeRule(ruleOptions) {
|
|
4450
|
+
ServerStatusCodeRuleMap.set(this.name, ruleOptions);
|
|
4451
|
+
}
|
|
4413
4452
|
initLog(enableLog) {
|
|
4414
4453
|
if (enableLog) {
|
|
4415
4454
|
const axiosInstance = AxiosInstanceMap.get(this.Name);
|
|
@@ -4681,6 +4720,26 @@ class SnailSse {
|
|
|
4681
4720
|
__publicField(this, "_version");
|
|
4682
4721
|
__publicField(this, "_withCredentials");
|
|
4683
4722
|
__publicField(this, "_eventSource");
|
|
4723
|
+
__publicField(this, "open", () => {
|
|
4724
|
+
const url = `${this._baseUrl}${this._url}`;
|
|
4725
|
+
const eventSource = new EventSource(url, {
|
|
4726
|
+
withCredentials: this._withCredentials
|
|
4727
|
+
});
|
|
4728
|
+
this._eventSource = eventSource;
|
|
4729
|
+
this.setEvent();
|
|
4730
|
+
this.registerSseEvent();
|
|
4731
|
+
});
|
|
4732
|
+
__publicField(this, "close", () => {
|
|
4733
|
+
const eventSource = this.eventSource;
|
|
4734
|
+
if (!eventSource) return;
|
|
4735
|
+
eventSource.close();
|
|
4736
|
+
});
|
|
4737
|
+
__publicField(this, "on", (eventName, callback, options) => {
|
|
4738
|
+
this._eventSource && this._eventSource.addEventListener(eventName, callback, options);
|
|
4739
|
+
});
|
|
4740
|
+
__publicField(this, "off", (eventName, callback, options) => {
|
|
4741
|
+
this._eventSource && this._eventSource.removeEventListener(eventName, callback, options);
|
|
4742
|
+
});
|
|
4684
4743
|
this._serverInstance = server;
|
|
4685
4744
|
this._baseUrl = server.baseUrl;
|
|
4686
4745
|
const options = this.getSseOptions();
|
|
@@ -4721,20 +4780,6 @@ class SnailSse {
|
|
|
4721
4780
|
return;
|
|
4722
4781
|
}
|
|
4723
4782
|
}
|
|
4724
|
-
open() {
|
|
4725
|
-
const url = `${this._baseUrl}${this._url}`;
|
|
4726
|
-
const eventSource = new EventSource(url, {
|
|
4727
|
-
withCredentials: this._withCredentials
|
|
4728
|
-
});
|
|
4729
|
-
this._eventSource = eventSource;
|
|
4730
|
-
this.setEvent();
|
|
4731
|
-
this.registerSseEvent();
|
|
4732
|
-
}
|
|
4733
|
-
close() {
|
|
4734
|
-
const eventSource = this.eventSource;
|
|
4735
|
-
if (!eventSource) return;
|
|
4736
|
-
eventSource.close();
|
|
4737
|
-
}
|
|
4738
4783
|
registerSseEvent() {
|
|
4739
4784
|
const eventSource = this._eventSource;
|
|
4740
4785
|
if (!eventSource) return;
|
|
@@ -4762,12 +4807,6 @@ class SnailSse {
|
|
|
4762
4807
|
this._eventSource && (this._eventSource.onerror = onErrorFunc);
|
|
4763
4808
|
}
|
|
4764
4809
|
}
|
|
4765
|
-
on(eventName, callback, options) {
|
|
4766
|
-
this._eventSource && this._eventSource.addEventListener(eventName, callback, options);
|
|
4767
|
-
}
|
|
4768
|
-
off(eventName, callback, options) {
|
|
4769
|
-
this._eventSource && this._eventSource.removeEventListener(eventName, callback, options);
|
|
4770
|
-
}
|
|
4771
4810
|
get url() {
|
|
4772
4811
|
return this._url;
|
|
4773
4812
|
}
|
|
@@ -4800,6 +4839,7 @@ export {
|
|
|
4800
4839
|
SnailMethod,
|
|
4801
4840
|
SnailPass,
|
|
4802
4841
|
SnailServer,
|
|
4842
|
+
SnailServerStatusCodeRuleOptions,
|
|
4803
4843
|
SnailSse,
|
|
4804
4844
|
Sse,
|
|
4805
4845
|
SseEvent,
|
package/dist/snail-api.umd.cjs
CHANGED
|
@@ -3731,6 +3731,12 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3731
3731
|
RequestMethodEnum2["OPTIONS"] = "Options";
|
|
3732
3732
|
return RequestMethodEnum2;
|
|
3733
3733
|
})(RequestMethodEnum || {});
|
|
3734
|
+
class SnailServerStatusCodeRuleOptions {
|
|
3735
|
+
constructor() {
|
|
3736
|
+
__publicField(this, "key");
|
|
3737
|
+
__publicField(this, "rule");
|
|
3738
|
+
}
|
|
3739
|
+
}
|
|
3734
3740
|
var VersioningType = /* @__PURE__ */ ((VersioningType2) => {
|
|
3735
3741
|
VersioningType2[VersioningType2["Uri"] = 0] = "Uri";
|
|
3736
3742
|
VersioningType2[VersioningType2["Header"] = 1] = "Header";
|
|
@@ -4040,6 +4046,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4040
4046
|
const [serverName] = this.apiInstance.name.split(".");
|
|
4041
4047
|
const axios2 = AxiosInstanceMap.get(serverName);
|
|
4042
4048
|
if (!axios2) throw new Error("AxiosInstance not created");
|
|
4049
|
+
const serverStatusCodeRule = ServerStatusCodeRuleMap.get(serverName);
|
|
4043
4050
|
const strategies = this.getStrategies();
|
|
4044
4051
|
this.Request = await applyStrategies(this.Request, strategies, "request");
|
|
4045
4052
|
this.Request = this.applyProgress(this.Request);
|
|
@@ -4048,7 +4055,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4048
4055
|
this.propertyKey,
|
|
4049
4056
|
this.Args
|
|
4050
4057
|
);
|
|
4051
|
-
console.log("
|
|
4058
|
+
this.enableLog() && console.log("methodUrl:", this.Request.baseURL, this.Request.url);
|
|
4052
4059
|
const newUrl = replacePlaceholders(this.Url, params);
|
|
4053
4060
|
this.Request = {
|
|
4054
4061
|
...this.Request,
|
|
@@ -4087,11 +4094,23 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4087
4094
|
}
|
|
4088
4095
|
this.Response = response;
|
|
4089
4096
|
this.applyHitSource(serverName);
|
|
4090
|
-
this.emit("success", response.data);
|
|
4091
|
-
this.emit("finish", response.data);
|
|
4092
4097
|
isSpecial && console.log("isSpecial:", isSpecial);
|
|
4093
|
-
if (isSpecial)
|
|
4098
|
+
if (isSpecial) {
|
|
4099
|
+
this.emit("success", response.data);
|
|
4100
|
+
this.emit("finish", response.data);
|
|
4101
|
+
return response;
|
|
4102
|
+
}
|
|
4094
4103
|
!isSpecial && console.log("isSpecial:", isSpecial);
|
|
4104
|
+
if (serverStatusCodeRule) {
|
|
4105
|
+
const { key, rule } = serverStatusCodeRule;
|
|
4106
|
+
const statuCodeKey = key ?? "code";
|
|
4107
|
+
const data2 = response.data;
|
|
4108
|
+
if (rule(data2[statuCodeKey])) {
|
|
4109
|
+
this.emit("success", response.data);
|
|
4110
|
+
} else {
|
|
4111
|
+
this.emit("error", response.data);
|
|
4112
|
+
}
|
|
4113
|
+
}
|
|
4095
4114
|
return response.data;
|
|
4096
4115
|
} catch (error) {
|
|
4097
4116
|
this.emit("error", error);
|
|
@@ -4161,7 +4180,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4161
4180
|
this.propertyKey = propertyKey.toString();
|
|
4162
4181
|
this.Args = args ?? [];
|
|
4163
4182
|
this.init();
|
|
4164
|
-
console.log("url:", this.Url);
|
|
4183
|
+
this.enableLog() && console.log("url:", this.Url);
|
|
4165
4184
|
this.eventMap = /* @__PURE__ */ new Map();
|
|
4166
4185
|
this.onceWrapperMap = /* @__PURE__ */ new Map();
|
|
4167
4186
|
this.eventMap.set("success", /* @__PURE__ */ new Set());
|
|
@@ -4183,8 +4202,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4183
4202
|
this.initVersion();
|
|
4184
4203
|
}
|
|
4185
4204
|
initUrl() {
|
|
4186
|
-
|
|
4187
|
-
|
|
4205
|
+
if (this.Path === "") {
|
|
4206
|
+
this.Url = this.apiInstance.url || "";
|
|
4207
|
+
return;
|
|
4208
|
+
}
|
|
4209
|
+
this.Url = `${this.apiInstance.url || ""}${resolveUrl(this.Path)}`;
|
|
4188
4210
|
}
|
|
4189
4211
|
initVersion() {
|
|
4190
4212
|
const version = Reflect.getMetadata(
|
|
@@ -4330,7 +4352,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4330
4352
|
this.propertyKey
|
|
4331
4353
|
);
|
|
4332
4354
|
const isApiNoCache = this.apiInstance.noCache;
|
|
4355
|
+
this.enableLog() && console.log("isApiNoCache:", isApiNoCache);
|
|
4356
|
+
this.enableLog() && console.log("isMethodNoCache:", isMethodNoCache);
|
|
4333
4357
|
const noCacheFlag = isMethodNoCache ? true : isApiNoCache ? true : false;
|
|
4358
|
+
this.enableLog() && console.log("noCacheFlag:", noCacheFlag);
|
|
4334
4359
|
return flag ? noCacheFlag : true;
|
|
4335
4360
|
}
|
|
4336
4361
|
enableLog() {
|
|
@@ -4376,6 +4401,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4376
4401
|
const AxiosInstanceMap = /* @__PURE__ */ new Map();
|
|
4377
4402
|
const StrategyMap = /* @__PURE__ */ new Map();
|
|
4378
4403
|
const VersioningMap = /* @__PURE__ */ new Map();
|
|
4404
|
+
const ServerStatusCodeRuleMap = /* @__PURE__ */ new Map();
|
|
4379
4405
|
const defaultCacheManageOptions = {
|
|
4380
4406
|
type: CacheType.Memory,
|
|
4381
4407
|
ttl: 500
|
|
@@ -4404,9 +4430,19 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4404
4430
|
}
|
|
4405
4431
|
init() {
|
|
4406
4432
|
const options = this.getServerOptions();
|
|
4407
|
-
const {
|
|
4433
|
+
const {
|
|
4434
|
+
baseURL,
|
|
4435
|
+
timeout,
|
|
4436
|
+
cacheManage,
|
|
4437
|
+
enableLog,
|
|
4438
|
+
cacheFor,
|
|
4439
|
+
serverStatusCodeRule
|
|
4440
|
+
} = options;
|
|
4408
4441
|
this.BaseURL = baseURL ?? resolveUrl("");
|
|
4409
4442
|
this.initAxios({ baseURL, timeout });
|
|
4443
|
+
if (serverStatusCodeRule) {
|
|
4444
|
+
this.initServerStatusCodeRule(serverStatusCodeRule);
|
|
4445
|
+
}
|
|
4410
4446
|
this.initStrategy();
|
|
4411
4447
|
this.initCacheManage(cacheManage, cacheFor);
|
|
4412
4448
|
this.initVersioning();
|
|
@@ -4414,6 +4450,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4414
4450
|
this.EnableLog = enableLog ?? false;
|
|
4415
4451
|
this.initLog(enableLog);
|
|
4416
4452
|
}
|
|
4453
|
+
initServerStatusCodeRule(ruleOptions) {
|
|
4454
|
+
ServerStatusCodeRuleMap.set(this.name, ruleOptions);
|
|
4455
|
+
}
|
|
4417
4456
|
initLog(enableLog) {
|
|
4418
4457
|
if (enableLog) {
|
|
4419
4458
|
const axiosInstance = AxiosInstanceMap.get(this.Name);
|
|
@@ -4685,6 +4724,26 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4685
4724
|
__publicField(this, "_version");
|
|
4686
4725
|
__publicField(this, "_withCredentials");
|
|
4687
4726
|
__publicField(this, "_eventSource");
|
|
4727
|
+
__publicField(this, "open", () => {
|
|
4728
|
+
const url = `${this._baseUrl}${this._url}`;
|
|
4729
|
+
const eventSource = new EventSource(url, {
|
|
4730
|
+
withCredentials: this._withCredentials
|
|
4731
|
+
});
|
|
4732
|
+
this._eventSource = eventSource;
|
|
4733
|
+
this.setEvent();
|
|
4734
|
+
this.registerSseEvent();
|
|
4735
|
+
});
|
|
4736
|
+
__publicField(this, "close", () => {
|
|
4737
|
+
const eventSource = this.eventSource;
|
|
4738
|
+
if (!eventSource) return;
|
|
4739
|
+
eventSource.close();
|
|
4740
|
+
});
|
|
4741
|
+
__publicField(this, "on", (eventName, callback, options) => {
|
|
4742
|
+
this._eventSource && this._eventSource.addEventListener(eventName, callback, options);
|
|
4743
|
+
});
|
|
4744
|
+
__publicField(this, "off", (eventName, callback, options) => {
|
|
4745
|
+
this._eventSource && this._eventSource.removeEventListener(eventName, callback, options);
|
|
4746
|
+
});
|
|
4688
4747
|
this._serverInstance = server;
|
|
4689
4748
|
this._baseUrl = server.baseUrl;
|
|
4690
4749
|
const options = this.getSseOptions();
|
|
@@ -4725,20 +4784,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4725
4784
|
return;
|
|
4726
4785
|
}
|
|
4727
4786
|
}
|
|
4728
|
-
open() {
|
|
4729
|
-
const url = `${this._baseUrl}${this._url}`;
|
|
4730
|
-
const eventSource = new EventSource(url, {
|
|
4731
|
-
withCredentials: this._withCredentials
|
|
4732
|
-
});
|
|
4733
|
-
this._eventSource = eventSource;
|
|
4734
|
-
this.setEvent();
|
|
4735
|
-
this.registerSseEvent();
|
|
4736
|
-
}
|
|
4737
|
-
close() {
|
|
4738
|
-
const eventSource = this.eventSource;
|
|
4739
|
-
if (!eventSource) return;
|
|
4740
|
-
eventSource.close();
|
|
4741
|
-
}
|
|
4742
4787
|
registerSseEvent() {
|
|
4743
4788
|
const eventSource = this._eventSource;
|
|
4744
4789
|
if (!eventSource) return;
|
|
@@ -4766,12 +4811,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4766
4811
|
this._eventSource && (this._eventSource.onerror = onErrorFunc);
|
|
4767
4812
|
}
|
|
4768
4813
|
}
|
|
4769
|
-
on(eventName, callback, options) {
|
|
4770
|
-
this._eventSource && this._eventSource.addEventListener(eventName, callback, options);
|
|
4771
|
-
}
|
|
4772
|
-
off(eventName, callback, options) {
|
|
4773
|
-
this._eventSource && this._eventSource.removeEventListener(eventName, callback, options);
|
|
4774
|
-
}
|
|
4775
4814
|
get url() {
|
|
4776
4815
|
return this._url;
|
|
4777
4816
|
}
|
|
@@ -4803,6 +4842,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4803
4842
|
exports2.SnailMethod = SnailMethod;
|
|
4804
4843
|
exports2.SnailPass = SnailPass;
|
|
4805
4844
|
exports2.SnailServer = SnailServer;
|
|
4845
|
+
exports2.SnailServerStatusCodeRuleOptions = SnailServerStatusCodeRuleOptions;
|
|
4806
4846
|
exports2.SnailSse = SnailSse;
|
|
4807
4847
|
exports2.Sse = Sse;
|
|
4808
4848
|
exports2.SseEvent = SseEvent;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { CacheManagementOption } from "./cache.management.option";
|
|
2
2
|
import { RequestMethod } from "./request.method";
|
|
3
3
|
export type CacheForType = "All" | "all" | RequestMethod | RequestMethod[];
|
|
4
|
+
export declare class SnailServerStatusCodeRuleOptions {
|
|
5
|
+
key?: string;
|
|
6
|
+
rule: (status: number) => boolean;
|
|
7
|
+
}
|
|
4
8
|
export interface SnailOption {
|
|
5
9
|
name?: string;
|
|
6
10
|
baseURL?: string;
|
|
@@ -8,4 +12,5 @@ export interface SnailOption {
|
|
|
8
12
|
cacheFor?: CacheForType;
|
|
9
13
|
enableLog?: boolean;
|
|
10
14
|
timeout?: number;
|
|
15
|
+
serverStatusCodeRule?: SnailServerStatusCodeRuleOptions;
|
|
11
16
|
}
|