@opra/common 1.27.4 → 1.28.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.
@@ -1,5 +1,5 @@
1
1
  import type { Combine, StrictOmit, Type, TypeThunkAsync } from 'ts-gems';
2
- import type { Validator } from 'valgen';
2
+ import { type Validator } from 'valgen';
3
3
  import { OpraSchema } from '../../schema/index.js';
4
4
  import type { ApiDocument } from '../api-document.js';
5
5
  import { DocumentElement } from '../common/document-element.js';
@@ -1,5 +1,6 @@
1
1
  import { omitUndefined } from '@jsopen/objects';
2
2
  import { asMutable } from 'ts-gems';
3
+ import { vg } from 'valgen';
3
4
  import { OpraSchema } from '../../schema/index.js';
4
5
  import { DocumentElement } from '../common/document-element.js';
5
6
  import { DECORATOR } from '../constants.js';
@@ -11,7 +12,7 @@ import { ComplexTypeBase } from './complex-type-base.js';
11
12
  */
12
13
  export const ApiField = function (...args) {
13
14
  // Decorator
14
- if (!this) {
15
+ if (!(this instanceof ApiField)) {
15
16
  const [options] = args;
16
17
  return ApiField[DECORATOR](options);
17
18
  }
@@ -112,10 +113,20 @@ class ApiFieldClass extends DocumentElement {
112
113
  });
113
114
  }
114
115
  generateCodec(codec, options, properties) {
115
- return this.type.generateCodec(codec, options, {
116
+ if (this.fixed)
117
+ return vg.isEqual(this.fixed);
118
+ let fn = this.type?.generateCodec(codec, options, {
116
119
  ...properties,
117
120
  designType: this.designType,
118
- });
121
+ }) || vg.isAny();
122
+ if (this.default !== undefined) {
123
+ fn = vg.required(fn, {
124
+ default: this.default,
125
+ });
126
+ }
127
+ if (this.isArray && !(this.type.kind === OpraSchema.ArrayType.Kind))
128
+ fn = vg.isArray(fn);
129
+ return fn;
119
130
  }
120
131
  }
121
132
  ApiField.prototype = ApiFieldClass.prototype;
@@ -74,7 +74,6 @@ declare abstract class ComplexTypeBaseClass extends DataType {
74
74
  */
75
75
  generateCodec(codec: 'encode' | 'decode', options?: DataType.GenerateCodecOptions): Validator;
76
76
  protected _generateSchema(codec: 'encode' | 'decode', context: GenerateCodecContext): vg.isObject.Schema;
77
- protected _generateFieldCodec(codec: 'encode' | 'decode', field: ApiField, context: GenerateCodecContext): Validator;
78
77
  }
79
78
  type GenerateCodecContext = StrictOmit<DataType.GenerateCodecOptions, 'projection'> & {
80
79
  currentPath: string;
@@ -4,7 +4,6 @@ import { validator, vg } from 'valgen';
4
4
  import { FieldsProjection, parseFieldsProjection, ResponsiveMap, } from '../../helpers/index.js';
5
5
  import { OpraSchema } from '../../schema/index.js';
6
6
  import { DocumentInitContext } from '../common/document-init-context.js';
7
- import { ArrayType } from './array-type.js';
8
7
  import { DataType } from './data-type.js';
9
8
  export const FIELD_PATH_PATTERN = /^([+-])?([a-z$_][\w.]*)$/i;
10
9
  /**
@@ -305,11 +304,12 @@ class ComplexTypeBaseClass extends DataType {
305
304
  else if (!fn) {
306
305
  const defaultGenerator = () => {
307
306
  cacheItem[cacheKey] = null;
308
- const xfn = this._generateFieldCodec(codec, field, {
307
+ const xfn = field.generateCodec(codec, {
309
308
  ...context,
310
309
  partial: context.partial === 'deep' ? context.partial : undefined,
311
310
  projection: subProjection,
312
311
  currentPath: currentPath + (currentPath ? '.' : '') + fieldName,
312
+ level: context.level + 1,
313
313
  });
314
314
  cacheItem[cacheKey] = xfn;
315
315
  return xfn;
@@ -332,16 +332,5 @@ class ComplexTypeBaseClass extends DataType {
332
332
  }
333
333
  return schema;
334
334
  }
335
- _generateFieldCodec(codec, field, context) {
336
- let fn = field.generateCodec(codec, {
337
- ...context,
338
- level: context.level + 1,
339
- });
340
- if (field.fixed)
341
- fn = vg.isEqual(field.fixed);
342
- if (field.isArray && !(field.type instanceof ArrayType))
343
- fn = vg.isArray(fn);
344
- return fn;
345
- }
346
335
  }
347
336
  ComplexTypeBase.prototype = ComplexTypeBaseClass.prototype;
@@ -50,6 +50,7 @@ declare class HttpParameterClass extends Value {
50
50
  keyParam?: boolean;
51
51
  deprecated?: boolean | string;
52
52
  required?: boolean;
53
+ readonly default?: any;
53
54
  arraySeparator?: string;
54
55
  parser?: (v: any) => any;
55
56
  designType?: Type;
@@ -26,6 +26,7 @@ export const HttpParameter = function (owner, initArgs) {
26
26
  _this.required = initArgs.required;
27
27
  if (_this.required == null && initArgs.location === 'path')
28
28
  _this.required = true;
29
+ _this.default = initArgs.default;
29
30
  _this.arraySeparator = initArgs.arraySeparator;
30
31
  _this.keyParam = initArgs.keyParam;
31
32
  _this.parser = initArgs.parser;
@@ -43,14 +44,19 @@ class HttpParameterClass extends Value {
43
44
  arraySeparator: this.arraySeparator,
44
45
  keyParam: this.keyParam,
45
46
  required: this.required,
47
+ default: this.default,
46
48
  deprecated: this.deprecated,
47
49
  });
48
50
  }
49
51
  generateCodec(codec, options, properties) {
50
- return (this.type?.generateCodec(codec, options, {
52
+ const fn = this.type?.generateCodec(codec, options, {
51
53
  ...properties,
52
54
  designType: this.designType,
53
- }) || vg.isAny());
55
+ }) || vg.isAny();
56
+ if (this.default !== undefined) {
57
+ return vg.optional(fn, this.default);
58
+ }
59
+ return fn;
54
60
  }
55
61
  }
56
62
  HttpParameter.prototype = HttpParameterClass.prototype;
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import 'reflect-metadata';
1
2
  export * from './document/index.js';
2
3
  export * from './enums/index.js';
3
4
  export * from './exception/index.js';
package/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import 'reflect-metadata';
1
2
  export * from './document/index.js';
2
3
  export * from './enums/index.js';
3
4
  export * from './exception/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/common",
3
- "version": "1.27.4",
3
+ "version": "1.28.1",
4
4
  "description": "Opra common package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -19,7 +19,7 @@
19
19
  "super-fast-md5": "^1.0.3",
20
20
  "tslib": "^2.8.1",
21
21
  "uid": "^2.0.2",
22
- "valgen": "^6.0.3"
22
+ "valgen": "^6.1.0"
23
23
  },
24
24
  "type": "module",
25
25
  "module": "./index.js",
@@ -17,6 +17,10 @@ export interface HttpParameter extends Value {
17
17
  * Determines if parameter is key
18
18
  */
19
19
  keyParam?: boolean;
20
+ /**
21
+ * Default value
22
+ */
23
+ default?: any;
20
24
  /**
21
25
  * Defines array separator
22
26
  */