@jayfong/x-server 2.20.0 → 2.21.0

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.
@@ -30,7 +30,8 @@ class CaptchaService {
30
30
  }
31
31
  const token = await _x.x.cache.save({
32
32
  code: code,
33
- scene: payload.scene
33
+ scene: payload.scene,
34
+ extra: payload.extra
34
35
  }, payload.ttl);
35
36
  return {
36
37
  token: token,
@@ -46,8 +47,9 @@ class CaptchaService {
46
47
  charPreset: this.charsetMap[payload.charset].join('')
47
48
  });
48
49
  const token = await _x.x.cache.save({
49
- code,
50
- scene: payload.scene
50
+ code: code,
51
+ scene: payload.scene,
52
+ extra: payload.extra
51
53
  }, payload.ttl);
52
54
  const dataUrl = (0, _miniSvgDataUri.default)(svg);
53
55
  return {
@@ -62,7 +64,7 @@ class CaptchaService {
62
64
  return false;
63
65
  }
64
66
  await _x.x.cache.remove(options.token);
65
- return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase();
67
+ return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase() && (options.extra ? options.extra(expected.extra) : true);
66
68
  }
67
69
  }
68
70
  exports.CaptchaService = CaptchaService;
@@ -1,9 +1,11 @@
1
1
  import { type MsValue } from 'vtils';
2
2
  import { BaseService } from './base';
3
3
  export type CaptchaServiceGenerateCharset = '09' | '19' | 'az' | 'AZ' | 'aZ' | '0z' | '0Z' | '1z' | '1Z';
4
- export interface CaptchaServiceGeneratePayload {
4
+ export interface CaptchaServiceGeneratePayload<T> {
5
5
  /**
6
6
  * 场景值
7
+ *
8
+ * 比如:防止后台验证码图片登录用其他场景下的短信验证码
7
9
  */
8
10
  scene: string;
9
11
  /**
@@ -18,6 +20,10 @@ export interface CaptchaServiceGeneratePayload {
18
20
  * 字符集
19
21
  */
20
22
  charset: CaptchaServiceGenerateCharset;
23
+ /**
24
+ * 其他信息
25
+ */
26
+ extra?: T;
21
27
  }
22
28
  export interface CaptchaServiceGenerateResult {
23
29
  token: string;
@@ -33,17 +39,18 @@ export interface CaptchaServiceGenerateDataUrlResult {
33
39
  code: string;
34
40
  dataUrl: string;
35
41
  }
36
- export interface CaptchaServiceVerifyOptions {
42
+ export interface CaptchaServiceVerifyOptions<T> {
37
43
  token: string;
38
44
  code: string;
39
45
  scene: string;
46
+ extra?: (extra: T) => boolean;
40
47
  }
41
48
  export declare class CaptchaService implements BaseService {
42
49
  serviceName: string;
43
50
  private charsetMap;
44
- generate(payload: CaptchaServiceGeneratePayload): Promise<CaptchaServiceGenerateResult>;
45
- generateDataUrl(payload: CaptchaServiceGeneratePayload): Promise<CaptchaServiceGenerateDataUrlResult>;
46
- verify(options: CaptchaServiceVerifyOptions): Promise<boolean>;
51
+ generate<T extends Record<string, any>>(payload: CaptchaServiceGeneratePayload<T>): Promise<CaptchaServiceGenerateResult>;
52
+ generateDataUrl<T extends Record<string, any>>(payload: CaptchaServiceGeneratePayload<T>): Promise<CaptchaServiceGenerateDataUrlResult>;
53
+ verify<T extends Record<string, any>>(options: CaptchaServiceVerifyOptions<T>): Promise<boolean>;
47
54
  }
48
55
  declare module '../x' {
49
56
  interface X {
@@ -25,7 +25,8 @@ export class CaptchaService {
25
25
  }
26
26
  const token = await x.cache.save({
27
27
  code: code,
28
- scene: payload.scene
28
+ scene: payload.scene,
29
+ extra: payload.extra
29
30
  }, payload.ttl);
30
31
  return {
31
32
  token: token,
@@ -41,8 +42,9 @@ export class CaptchaService {
41
42
  charPreset: this.charsetMap[payload.charset].join('')
42
43
  });
43
44
  const token = await x.cache.save({
44
- code,
45
- scene: payload.scene
45
+ code: code,
46
+ scene: payload.scene,
47
+ extra: payload.extra
46
48
  }, payload.ttl);
47
49
  const dataUrl = svgToDataUrl(svg);
48
50
  return {
@@ -57,6 +59,6 @@ export class CaptchaService {
57
59
  return false;
58
60
  }
59
61
  await x.cache.remove(options.token);
60
- return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase();
62
+ return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase() && (options.extra ? options.extra(expected.extra) : true);
61
63
  }
62
64
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.20.0",
3
+ "version": "2.21.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",