@jayfong/x-server 2.109.1 → 2.110.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.
@@ -42,16 +42,34 @@ class DbValueService {
42
42
  }
43
43
  };
44
44
  this.JsonObject = class {
45
- constructor() {
45
+ constructor(options) {
46
+ this.options = void 0;
47
+ this.hasTransformAdd = false;
48
+ this.hasTransformGet = false;
49
+ this.hasTransformUpdate = false;
46
50
  this.add = (value, defaultValue = {}) => {
51
+ if (value && this.hasTransformAdd) {
52
+ value = this.options.transform.add(value);
53
+ }
47
54
  return JSON.stringify(!value || !(0, _vtils.isPlainObject)(value) ? defaultValue : value);
48
55
  };
49
56
  this.get = value => {
50
- return !value ? {} : JSON.parse(value);
57
+ let result = !value ? {} : JSON.parse(value);
58
+ if (result && this.hasTransformGet) {
59
+ result = this.options.transform.get(result);
60
+ }
61
+ return result;
51
62
  };
52
63
  this.update = value => {
64
+ if (value && this.hasTransformUpdate) {
65
+ value = this.options.transform.update(value);
66
+ }
53
67
  return !value || !(0, _vtils.isPlainObject)(value) ? undefined : JSON.stringify(value);
54
68
  };
69
+ this.options = options;
70
+ this.hasTransformAdd = !!options.transform?.add;
71
+ this.hasTransformGet = !!options.transform?.get;
72
+ this.hasTransformUpdate = !!options.transform?.update;
55
73
  }
56
74
  };
57
75
  }
@@ -1,4 +1,4 @@
1
- import { BaseService } from './base';
1
+ import type { BaseService } from './base';
2
2
  export type DbValueServiceSimpleArrayOptions = {
3
3
  /**
4
4
  * 分隔符
@@ -15,6 +15,13 @@ export type DbValueServiceSimpleArrayOptions = {
15
15
  */
16
16
  number?: boolean;
17
17
  };
18
+ export type DbValueServiceJsonObjectOptions<T> = {
19
+ transform?: {
20
+ add?: (value: T) => T;
21
+ get?: (value: T) => T;
22
+ update?: (value: T) => T;
23
+ };
24
+ };
18
25
  export declare class DbValueService implements BaseService {
19
26
  serviceName: string;
20
27
  SimpleArray: {
@@ -35,7 +42,11 @@ export declare class DbValueService implements BaseService {
35
42
  };
36
43
  };
37
44
  JsonObject: {
38
- new <T>(): {
45
+ new <T>(options: DbValueServiceJsonObjectOptions<T>): {
46
+ options: DbValueServiceJsonObjectOptions<T>;
47
+ hasTransformAdd: boolean;
48
+ hasTransformGet: boolean;
49
+ hasTransformUpdate: boolean;
39
50
  add: (value?: T, defaultValue?: T) => string;
40
51
  get: (value?: string) => T;
41
52
  update: (value?: T) => string | undefined;
@@ -38,16 +38,34 @@ export class DbValueService {
38
38
  }
39
39
  };
40
40
  this.JsonObject = class {
41
- constructor() {
41
+ constructor(options) {
42
+ this.options = void 0;
43
+ this.hasTransformAdd = false;
44
+ this.hasTransformGet = false;
45
+ this.hasTransformUpdate = false;
42
46
  this.add = (value, defaultValue = {}) => {
47
+ if (value && this.hasTransformAdd) {
48
+ value = this.options.transform.add(value);
49
+ }
43
50
  return JSON.stringify(!value || !isPlainObject(value) ? defaultValue : value);
44
51
  };
45
52
  this.get = value => {
46
- return !value ? {} : JSON.parse(value);
53
+ let result = !value ? {} : JSON.parse(value);
54
+ if (result && this.hasTransformGet) {
55
+ result = this.options.transform.get(result);
56
+ }
57
+ return result;
47
58
  };
48
59
  this.update = value => {
60
+ if (value && this.hasTransformUpdate) {
61
+ value = this.options.transform.update(value);
62
+ }
49
63
  return !value || !isPlainObject(value) ? undefined : JSON.stringify(value);
50
64
  };
65
+ this.options = options;
66
+ this.hasTransformAdd = !!options.transform?.add;
67
+ this.hasTransformGet = !!options.transform?.get;
68
+ this.hasTransformUpdate = !!options.transform?.update;
51
69
  }
52
70
  };
53
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.109.1",
3
+ "version": "2.110.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",