@snail-js/api 0.1.20 → 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/snail-api.js +38 -4
- package/dist/snail-api.umd.cjs +38 -4
- 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/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);
|
|
@@ -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);
|
|
@@ -4378,6 +4397,7 @@ const ExpireSourceMap = /* @__PURE__ */ new Map();
|
|
|
4378
4397
|
const AxiosInstanceMap = /* @__PURE__ */ new Map();
|
|
4379
4398
|
const StrategyMap = /* @__PURE__ */ new Map();
|
|
4380
4399
|
const VersioningMap = /* @__PURE__ */ new Map();
|
|
4400
|
+
const ServerStatusCodeRuleMap = /* @__PURE__ */ new Map();
|
|
4381
4401
|
const defaultCacheManageOptions = {
|
|
4382
4402
|
type: CacheType.Memory,
|
|
4383
4403
|
ttl: 500
|
|
@@ -4406,9 +4426,19 @@ class SnailServer {
|
|
|
4406
4426
|
}
|
|
4407
4427
|
init() {
|
|
4408
4428
|
const options = this.getServerOptions();
|
|
4409
|
-
const {
|
|
4429
|
+
const {
|
|
4430
|
+
baseURL,
|
|
4431
|
+
timeout,
|
|
4432
|
+
cacheManage,
|
|
4433
|
+
enableLog,
|
|
4434
|
+
cacheFor,
|
|
4435
|
+
serverStatusCodeRule
|
|
4436
|
+
} = options;
|
|
4410
4437
|
this.BaseURL = baseURL ?? resolveUrl("");
|
|
4411
4438
|
this.initAxios({ baseURL, timeout });
|
|
4439
|
+
if (serverStatusCodeRule) {
|
|
4440
|
+
this.initServerStatusCodeRule(serverStatusCodeRule);
|
|
4441
|
+
}
|
|
4412
4442
|
this.initStrategy();
|
|
4413
4443
|
this.initCacheManage(cacheManage, cacheFor);
|
|
4414
4444
|
this.initVersioning();
|
|
@@ -4416,6 +4446,9 @@ class SnailServer {
|
|
|
4416
4446
|
this.EnableLog = enableLog ?? false;
|
|
4417
4447
|
this.initLog(enableLog);
|
|
4418
4448
|
}
|
|
4449
|
+
initServerStatusCodeRule(ruleOptions) {
|
|
4450
|
+
ServerStatusCodeRuleMap.set(this.name, ruleOptions);
|
|
4451
|
+
}
|
|
4419
4452
|
initLog(enableLog) {
|
|
4420
4453
|
if (enableLog) {
|
|
4421
4454
|
const axiosInstance = AxiosInstanceMap.get(this.Name);
|
|
@@ -4806,6 +4839,7 @@ export {
|
|
|
4806
4839
|
SnailMethod,
|
|
4807
4840
|
SnailPass,
|
|
4808
4841
|
SnailServer,
|
|
4842
|
+
SnailServerStatusCodeRuleOptions,
|
|
4809
4843
|
SnailSse,
|
|
4810
4844
|
Sse,
|
|
4811
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);
|
|
@@ -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);
|
|
@@ -4382,6 +4401,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4382
4401
|
const AxiosInstanceMap = /* @__PURE__ */ new Map();
|
|
4383
4402
|
const StrategyMap = /* @__PURE__ */ new Map();
|
|
4384
4403
|
const VersioningMap = /* @__PURE__ */ new Map();
|
|
4404
|
+
const ServerStatusCodeRuleMap = /* @__PURE__ */ new Map();
|
|
4385
4405
|
const defaultCacheManageOptions = {
|
|
4386
4406
|
type: CacheType.Memory,
|
|
4387
4407
|
ttl: 500
|
|
@@ -4410,9 +4430,19 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4410
4430
|
}
|
|
4411
4431
|
init() {
|
|
4412
4432
|
const options = this.getServerOptions();
|
|
4413
|
-
const {
|
|
4433
|
+
const {
|
|
4434
|
+
baseURL,
|
|
4435
|
+
timeout,
|
|
4436
|
+
cacheManage,
|
|
4437
|
+
enableLog,
|
|
4438
|
+
cacheFor,
|
|
4439
|
+
serverStatusCodeRule
|
|
4440
|
+
} = options;
|
|
4414
4441
|
this.BaseURL = baseURL ?? resolveUrl("");
|
|
4415
4442
|
this.initAxios({ baseURL, timeout });
|
|
4443
|
+
if (serverStatusCodeRule) {
|
|
4444
|
+
this.initServerStatusCodeRule(serverStatusCodeRule);
|
|
4445
|
+
}
|
|
4416
4446
|
this.initStrategy();
|
|
4417
4447
|
this.initCacheManage(cacheManage, cacheFor);
|
|
4418
4448
|
this.initVersioning();
|
|
@@ -4420,6 +4450,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4420
4450
|
this.EnableLog = enableLog ?? false;
|
|
4421
4451
|
this.initLog(enableLog);
|
|
4422
4452
|
}
|
|
4453
|
+
initServerStatusCodeRule(ruleOptions) {
|
|
4454
|
+
ServerStatusCodeRuleMap.set(this.name, ruleOptions);
|
|
4455
|
+
}
|
|
4423
4456
|
initLog(enableLog) {
|
|
4424
4457
|
if (enableLog) {
|
|
4425
4458
|
const axiosInstance = AxiosInstanceMap.get(this.Name);
|
|
@@ -4809,6 +4842,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4809
4842
|
exports2.SnailMethod = SnailMethod;
|
|
4810
4843
|
exports2.SnailPass = SnailPass;
|
|
4811
4844
|
exports2.SnailServer = SnailServer;
|
|
4845
|
+
exports2.SnailServerStatusCodeRuleOptions = SnailServerStatusCodeRuleOptions;
|
|
4812
4846
|
exports2.SnailSse = SnailSse;
|
|
4813
4847
|
exports2.Sse = Sse;
|
|
4814
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
|
}
|