@jayfong/x-server 2.21.0 → 2.21.1

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.
@@ -23,6 +23,7 @@ class CaptchaService {
23
23
  };
24
24
  }
25
25
  async generate(payload) {
26
+ var _payload$retryCount;
26
27
  const chars = this.charsetMap[payload.charset];
27
28
  let code = '';
28
29
  for (let i = 0; i < payload.size; i++) {
@@ -31,6 +32,7 @@ class CaptchaService {
31
32
  const token = await _x.x.cache.save({
32
33
  code: code,
33
34
  scene: payload.scene,
35
+ retryCount: (_payload$retryCount = payload.retryCount) != null ? _payload$retryCount : 0,
34
36
  extra: payload.extra
35
37
  }, payload.ttl);
36
38
  return {
@@ -49,6 +51,7 @@ class CaptchaService {
49
51
  const token = await _x.x.cache.save({
50
52
  code: code,
51
53
  scene: payload.scene,
54
+ retryCount: payload.retryCount || 0,
52
55
  extra: payload.extra
53
56
  }, payload.ttl);
54
57
  const dataUrl = (0, _miniSvgDataUri.default)(svg);
@@ -63,7 +66,13 @@ class CaptchaService {
63
66
  if (expected == null) {
64
67
  return false;
65
68
  }
66
- await _x.x.cache.remove(options.token);
69
+ if (expected.retryCount === 0) {
70
+ await _x.x.cache.remove(options.token);
71
+ } else {
72
+ expected.retryCount--;
73
+ const ttl = await _x.x.cache.ttl(options.token);
74
+ await _x.x.cache.set(options.token, expected, ttl);
75
+ }
67
76
  return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase() && (options.extra ? options.extra(expected.extra) : true);
68
77
  }
69
78
  }
@@ -20,6 +20,12 @@ export interface CaptchaServiceGeneratePayload<T> {
20
20
  * 字符集
21
21
  */
22
22
  charset: CaptchaServiceGenerateCharset;
23
+ /**
24
+ * 可重试次数
25
+ *
26
+ * @default 0
27
+ */
28
+ retryCount?: number;
23
29
  /**
24
30
  * 其他信息
25
31
  */
@@ -18,6 +18,7 @@ export class CaptchaService {
18
18
  };
19
19
  }
20
20
  async generate(payload) {
21
+ var _payload$retryCount;
21
22
  const chars = this.charsetMap[payload.charset];
22
23
  let code = '';
23
24
  for (let i = 0; i < payload.size; i++) {
@@ -26,6 +27,7 @@ export class CaptchaService {
26
27
  const token = await x.cache.save({
27
28
  code: code,
28
29
  scene: payload.scene,
30
+ retryCount: (_payload$retryCount = payload.retryCount) != null ? _payload$retryCount : 0,
29
31
  extra: payload.extra
30
32
  }, payload.ttl);
31
33
  return {
@@ -44,6 +46,7 @@ export class CaptchaService {
44
46
  const token = await x.cache.save({
45
47
  code: code,
46
48
  scene: payload.scene,
49
+ retryCount: payload.retryCount || 0,
47
50
  extra: payload.extra
48
51
  }, payload.ttl);
49
52
  const dataUrl = svgToDataUrl(svg);
@@ -58,7 +61,13 @@ export class CaptchaService {
58
61
  if (expected == null) {
59
62
  return false;
60
63
  }
61
- await x.cache.remove(options.token);
64
+ if (expected.retryCount === 0) {
65
+ await x.cache.remove(options.token);
66
+ } else {
67
+ expected.retryCount--;
68
+ const ttl = await x.cache.ttl(options.token);
69
+ await x.cache.set(options.token, expected, ttl);
70
+ }
62
71
  return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase() && (options.extra ? options.extra(expected.extra) : true);
63
72
  }
64
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.21.0",
3
+ "version": "2.21.1",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",