@opra/common 1.17.5 → 1.17.7

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.
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ComplexTypeBase = exports.FIELD_PATH_PATTERN = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const hash_object_1 = tslib_1.__importDefault(require("hash-object"));
5
+ const object_hash_1 = tslib_1.__importDefault(require("object-hash"));
6
6
  const ts_gems_1 = require("ts-gems");
7
7
  const valgen_1 = require("valgen");
8
8
  const index_js_1 = require("../../helpers/index.js");
@@ -241,7 +241,7 @@ class ComplexTypeBaseClass extends data_type_js_1.DataType {
241
241
  return fn;
242
242
  }
243
243
  _generateSchema(codec, context) {
244
- context.cache = context.cache || new Map();
244
+ context.fieldCache = context.fieldCache || new Map();
245
245
  context.level = context.level || 0;
246
246
  context.forwardCallbacks = context.forwardCallbacks || new Set();
247
247
  const schema = {};
@@ -279,13 +279,13 @@ class ComplexTypeBaseClass extends data_type_js_1.DataType {
279
279
  const subProjection = typeof projection === 'object'
280
280
  ? projection[fieldName]?.projection || '*'
281
281
  : projection;
282
- let cacheItem = context.cache.get(field.type);
282
+ let cacheItem = context.fieldCache.get(field);
283
283
  const cacheKey = typeof subProjection === 'string'
284
284
  ? subProjection
285
- : (0, hash_object_1.default)(subProjection || {});
285
+ : (0, object_hash_1.default)(subProjection || {});
286
286
  if (!cacheItem) {
287
287
  cacheItem = {};
288
- context.cache.set(field.type, cacheItem);
288
+ context.fieldCache.set(field, cacheItem);
289
289
  }
290
290
  let fn = cacheItem[cacheKey];
291
291
  /** If in progress (circular) */
@@ -301,17 +301,28 @@ class ComplexTypeBaseClass extends data_type_js_1.DataType {
301
301
  });
302
302
  }
303
303
  else if (!fn) {
304
- cacheItem[cacheKey] = null;
305
- fn = this._generateFieldCodec(codec, field, {
306
- ...context,
307
- partial: context.partial === 'deep' ? context.partial : undefined,
308
- projection: subProjection,
309
- currentPath: currentPath + (currentPath ? '.' : '') + fieldName,
310
- });
311
- cacheItem[cacheKey] = fn;
304
+ const defaultGenerator = () => {
305
+ cacheItem[cacheKey] = null;
306
+ const xfn = this._generateFieldCodec(codec, field, {
307
+ ...context,
308
+ partial: context.partial === 'deep' ? context.partial : undefined,
309
+ projection: subProjection,
310
+ currentPath: currentPath + (currentPath ? '.' : '') + fieldName,
311
+ });
312
+ cacheItem[cacheKey] = xfn;
313
+ return xfn;
314
+ };
315
+ if (context.fieldHook)
316
+ fn = context.fieldHook(field, context.currentPath, defaultGenerator);
317
+ else
318
+ fn = defaultGenerator();
312
319
  }
313
320
  schema[fieldName] =
314
- context.partial || !field.required ? valgen_1.vg.optional(fn) : valgen_1.vg.required(fn);
321
+ context.partial || !(field.required || fn.id === 'required')
322
+ ? valgen_1.vg.optional(fn)
323
+ : fn.id === 'required'
324
+ ? fn
325
+ : valgen_1.vg.required(fn);
315
326
  }
316
327
  if (context.allowPatchOperators) {
317
328
  schema._$pull = valgen_1.vg.optional(valgen_1.vg.isAny());
@@ -2,12 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OmitType = OmitType;
4
4
  const create_mapped_class_js_1 = require("./utils/create-mapped-class.js");
5
- /**
6
- * Create a new MappedType that omits given fields from base type
7
- * @param baseType
8
- * @param keys
9
- * @param options
10
- */
11
5
  /**
12
6
  *
13
7
  */
@@ -2,12 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PartialType = PartialType;
4
4
  const create_mapped_class_js_1 = require("./utils/create-mapped-class.js");
5
- /**
6
- * Create a new MappedType that marks given or all fields as optional
7
- * @param baseType
8
- * @param keys
9
- * @param options
10
- */
11
5
  /**
12
6
  *
13
7
  */
@@ -1,4 +1,4 @@
1
- import hashObject from 'hash-object';
1
+ import hashObject from 'object-hash';
2
2
  import { asMutable } from 'ts-gems';
3
3
  import { validator, vg } from 'valgen';
4
4
  import { parseFieldsProjection, ResponsiveMap, } from '../../helpers/index.js';
@@ -237,7 +237,7 @@ class ComplexTypeBaseClass extends DataType {
237
237
  return fn;
238
238
  }
239
239
  _generateSchema(codec, context) {
240
- context.cache = context.cache || new Map();
240
+ context.fieldCache = context.fieldCache || new Map();
241
241
  context.level = context.level || 0;
242
242
  context.forwardCallbacks = context.forwardCallbacks || new Set();
243
243
  const schema = {};
@@ -275,13 +275,13 @@ class ComplexTypeBaseClass extends DataType {
275
275
  const subProjection = typeof projection === 'object'
276
276
  ? projection[fieldName]?.projection || '*'
277
277
  : projection;
278
- let cacheItem = context.cache.get(field.type);
278
+ let cacheItem = context.fieldCache.get(field);
279
279
  const cacheKey = typeof subProjection === 'string'
280
280
  ? subProjection
281
281
  : hashObject(subProjection || {});
282
282
  if (!cacheItem) {
283
283
  cacheItem = {};
284
- context.cache.set(field.type, cacheItem);
284
+ context.fieldCache.set(field, cacheItem);
285
285
  }
286
286
  let fn = cacheItem[cacheKey];
287
287
  /** If in progress (circular) */
@@ -297,17 +297,28 @@ class ComplexTypeBaseClass extends DataType {
297
297
  });
298
298
  }
299
299
  else if (!fn) {
300
- cacheItem[cacheKey] = null;
301
- fn = this._generateFieldCodec(codec, field, {
302
- ...context,
303
- partial: context.partial === 'deep' ? context.partial : undefined,
304
- projection: subProjection,
305
- currentPath: currentPath + (currentPath ? '.' : '') + fieldName,
306
- });
307
- cacheItem[cacheKey] = fn;
300
+ const defaultGenerator = () => {
301
+ cacheItem[cacheKey] = null;
302
+ const xfn = this._generateFieldCodec(codec, field, {
303
+ ...context,
304
+ partial: context.partial === 'deep' ? context.partial : undefined,
305
+ projection: subProjection,
306
+ currentPath: currentPath + (currentPath ? '.' : '') + fieldName,
307
+ });
308
+ cacheItem[cacheKey] = xfn;
309
+ return xfn;
310
+ };
311
+ if (context.fieldHook)
312
+ fn = context.fieldHook(field, context.currentPath, defaultGenerator);
313
+ else
314
+ fn = defaultGenerator();
308
315
  }
309
316
  schema[fieldName] =
310
- context.partial || !field.required ? vg.optional(fn) : vg.required(fn);
317
+ context.partial || !(field.required || fn.id === 'required')
318
+ ? vg.optional(fn)
319
+ : fn.id === 'required'
320
+ ? fn
321
+ : vg.required(fn);
311
322
  }
312
323
  if (context.allowPatchOperators) {
313
324
  schema._$pull = vg.optional(vg.isAny());
@@ -1,10 +1,4 @@
1
1
  import { createMappedClass } from './utils/create-mapped-class.js';
2
- /**
3
- * Create a new MappedType that omits given fields from base type
4
- * @param baseType
5
- * @param keys
6
- * @param options
7
- */
8
2
  /**
9
3
  *
10
4
  */
@@ -1,10 +1,4 @@
1
1
  import { createMappedClass } from './utils/create-mapped-class.js';
2
- /**
3
- * Create a new MappedType that marks given or all fields as optional
4
- * @param baseType
5
- * @param keys
6
- * @param options
7
- */
8
2
  /**
9
3
  *
10
4
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/common",
3
- "version": "1.17.5",
3
+ "version": "1.17.7",
4
4
  "description": "Opra common package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -12,7 +12,7 @@
12
12
  "@browsery/type-is": "^1.6.18-r8",
13
13
  "@jsopen/objects": "^1.6.2",
14
14
  "fast-tokenizer": "^1.7.0",
15
- "hash-object": "^5.0.1",
15
+ "object-hash": "^3.0.0",
16
16
  "putil-promisify": "^1.10.1",
17
17
  "putil-varhelpers": "^1.6.5",
18
18
  "reflect-metadata": "^0.2.2",
@@ -80,7 +80,7 @@ type GenerateCodecContext = StrictOmit<DataType.GenerateCodecOptions, 'projectio
80
80
  currentPath: string;
81
81
  projection?: FieldsProjection | '*';
82
82
  level?: number;
83
- cache?: Map<DataType, Record<string, Validator | null>>;
83
+ fieldCache?: Map<ApiField, Record<string, Validator | null>>;
84
84
  forwardCallbacks?: Set<Function>;
85
85
  };
86
86
  export {};
@@ -7,6 +7,7 @@ import type { ApiDocument } from '../api-document.js';
7
7
  import { DocumentElement } from '../common/document-element.js';
8
8
  import { DocumentInitContext } from '../common/document-init-context.js';
9
9
  import { nodeInspectCustom } from '../utils/inspect.util.js';
10
+ import type { ApiField } from './api-field.js';
10
11
  /**
11
12
  * @namespace DataType
12
13
  */
@@ -30,6 +31,7 @@ export declare namespace DataType {
30
31
  ignoreReadonlyFields?: boolean;
31
32
  ignoreWriteonlyFields?: boolean;
32
33
  allowPatchOperators?: boolean;
34
+ fieldHook?: (field: ApiField, currentPath: string, defaultGenerator: () => Validator) => Validator;
33
35
  }
34
36
  }
35
37
  interface DataTypeStatic {
@@ -1,9 +1,16 @@
1
1
  import type { Class, Type } from 'ts-gems';
2
2
  import type { DataType } from './data-type.js';
3
- export declare function OmitType(baseType: string | Type, keys: string[], options?: DataType.Options): Type;
4
3
  /**
5
4
  * Create a new MappedType that omits given fields from base type
6
5
  * @param baseType
7
6
  * @param keys
8
7
  * @param options
9
- */ export declare function OmitType<Args extends any[], Instance, Static, K extends keyof Instance>(baseType: Class<Args, Instance, Static>, keys: readonly K[], options?: DataType.Options): Class<Args, Omit<Instance, K>> & Omit<Pick<Static, keyof typeof baseType>, 'prototype' | 'constructor'>;
8
+ */
9
+ export declare function OmitType<Args extends any[], Instance, Static, K extends keyof Instance>(baseType: Class<Args, Instance, Static>, keys: readonly K[], options?: DataType.Options): Class<Args, Omit<Instance, K>> & Omit<Pick<Static, keyof typeof baseType>, 'prototype' | 'constructor'>;
10
+ /**
11
+ * Create a new MappedType that omits given fields from base type
12
+ * @param baseType
13
+ * @param keys
14
+ * @param options
15
+ */
16
+ export declare function OmitType(baseType: string | Type, keys: string[], options?: DataType.Options): Type;
@@ -1,6 +1,5 @@
1
1
  import type { Class, PartialSome, Type } from 'ts-gems';
2
2
  import type { DataType } from './data-type.js';
3
- export declare function PartialType(baseType: string | Type, keys: string[], options?: DataType.Options): Type;
4
3
  /**
5
4
  * Create a new MappedType that marks given or all fields as optional
6
5
  * @param baseType
@@ -20,3 +19,10 @@ export declare function PartialType<Args extends any[], Instance, Static, K exte
20
19
  * @param options
21
20
  */
22
21
  export declare function PartialType<Args extends any[], Instance, Static>(baseType: Class<Args, Instance, Static>, options?: DataType.Options): Class<Args, Partial<Instance>> & Omit<Pick<Static, keyof typeof baseType>, 'prototype' | 'constructor'>;
22
+ /**
23
+ * Create a new MappedType that marks given or all fields as optional
24
+ * @param baseType
25
+ * @param keys
26
+ * @param options
27
+ */
28
+ export declare function PartialType(baseType: string | Type, keys: string[], options?: DataType.Options): Type;