@opra/common 0.32.1 → 0.32.3

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.
package/browser.js CHANGED
@@ -17,8 +17,9 @@ import "reflect-metadata";
17
17
  var DATATYPE_METADATA = Symbol("DATATYPE_METADATA");
18
18
  var RESOURCE_METADATA = Symbol("RESOURCE_METADATA");
19
19
  var DECORATOR = Symbol("DECORATOR");
20
- var NAMESPACE_PATTERN = /([A-Z$_]\w+)(?::.+)?/i;
20
+ var NAMESPACE_PATTERN = /([a-z$_]\w+)(?::.+)?/i;
21
21
  var TYPENAME_PATTERN = /^(.*)Type(\d*)$/;
22
+ var SORT_FIELD_PATTERN = /^([+-])?([a-z$_][\w.]+)$/;
22
23
 
23
24
  // ../../build/common/esm/document/resource/container.js
24
25
  import merge3 from "putil-merge";
@@ -2336,13 +2337,19 @@ var ComplexTypeClass = class extends DataType {
2336
2337
  }
2337
2338
  };
2338
2339
  }
2339
- normalizeFieldPath(fieldPaths) {
2340
- const array = (Array.isArray(fieldPaths) ? fieldPaths : [fieldPaths]).map((s) => {
2340
+ normalizeFieldNames(fieldNames, allowSortSigns) {
2341
+ const array = (Array.isArray(fieldNames) ? fieldNames : [fieldNames]).map((s) => {
2342
+ let sign = "";
2343
+ if (allowSortSigns) {
2344
+ const m = SORT_FIELD_PATTERN.exec(s);
2345
+ sign = m && m[1] || "";
2346
+ s = m && m[2] || s;
2347
+ }
2341
2348
  let curPath = "";
2342
2349
  for (const [, , p] of this.iteratePath(s)) {
2343
2350
  curPath = p;
2344
2351
  }
2345
- return curPath;
2352
+ return sign + curPath;
2346
2353
  }).flat();
2347
2354
  return array.length ? array : void 0;
2348
2355
  }
@@ -11394,17 +11401,18 @@ var CollectionClass = class extends CrudResource {
11394
11401
  return result;
11395
11402
  }
11396
11403
  }
11397
- normalizeFieldPath(path) {
11398
- return this.type.normalizeFieldPath(path);
11404
+ normalizeFieldNames(fields, allowSortSigns) {
11405
+ return this.type.normalizeFieldNames(fields, allowSortSigns);
11399
11406
  }
11400
11407
  normalizeSortFields(fields) {
11401
- const normalized = this.type.normalizeFieldPath(fields);
11408
+ const normalized = this.type.normalizeFieldNames(fields, true);
11402
11409
  if (!normalized)
11403
11410
  return;
11404
11411
  const findManyOp = this.getOperation("findMany");
11405
11412
  const sortFields = findManyOp && findManyOp.options.sortFields;
11406
11413
  (Array.isArray(normalized) ? normalized : [normalized]).forEach((field) => {
11407
- if (!sortFields?.find((x) => x === field))
11414
+ const m = SORT_FIELD_PATTERN.exec(field);
11415
+ if (m && !sortFields?.find((x) => x === m[2]))
11408
11416
  throw new BadRequestError({
11409
11417
  message: translate("error:UNACCEPTED_SORT_FIELD", { field })
11410
11418
  });
@@ -11451,7 +11459,7 @@ var CollectionClass = class extends CrudResource {
11451
11459
  return ast;
11452
11460
  }
11453
11461
  if (ast instanceof opra_filter_ns_exports.QualifiedIdentifier) {
11454
- const normalizedFieldPath = this.type.normalizeFieldPath(ast.value)?.join(".");
11462
+ const normalizedFieldPath = this.type.normalizeFieldNames(ast.value)?.join(".");
11455
11463
  ast.field = this.type.getField(normalizedFieldPath);
11456
11464
  ast.dataType = ast.field?.type || this.document.getDataType("any");
11457
11465
  ast.value = normalizedFieldPath;
@@ -11547,7 +11555,7 @@ Object.assign(CollectionDecorator, ResourceDecorator);
11547
11555
  const decorator = createOperationDecorator("deleteMany", options, list);
11548
11556
  decorator.Filter = (field, operators, notes) => {
11549
11557
  if (typeof operators === "string")
11550
- operators = operators.split(/\s*[,| ]\s*/);
11558
+ operators = operators.split(/\s*[,| ]\s*/).filter((s) => s);
11551
11559
  list.push((operationMeta) => {
11552
11560
  operationMeta.options.filters = operationMeta.options.filters || [];
11553
11561
  operationMeta.options.filters.push(omitUndefined({ field, operators, notes }));
@@ -11777,8 +11785,8 @@ var SingletonClass = class extends CrudResource {
11777
11785
  type: this.type.name || "any"
11778
11786
  };
11779
11787
  }
11780
- normalizeFieldPath(path) {
11781
- return this.type.normalizeFieldPath(path);
11788
+ normalizeFieldNames(path) {
11789
+ return this.type.normalizeFieldNames(path);
11782
11790
  }
11783
11791
  };
11784
11792
 
@@ -12277,6 +12285,7 @@ export {
12277
12285
  ResourceConflictError,
12278
12286
  ResourceNotFoundError,
12279
12287
  ResponsiveMap,
12288
+ SORT_FIELD_PATTERN,
12280
12289
  SimpleType2 as SimpleType,
12281
12290
  Singleton2 as Singleton,
12282
12291
  Storage2 as Storage,
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TYPENAME_PATTERN = exports.NAMESPACE_PATTERN = exports.DECORATOR = exports.RESOURCE_METADATA = exports.DATATYPE_METADATA = void 0;
3
+ exports.SORT_FIELD_PATTERN = exports.TYPENAME_PATTERN = exports.NAMESPACE_PATTERN = exports.DECORATOR = exports.RESOURCE_METADATA = exports.DATATYPE_METADATA = void 0;
4
4
  exports.DATATYPE_METADATA = Symbol('DATATYPE_METADATA');
5
5
  exports.RESOURCE_METADATA = Symbol('RESOURCE_METADATA');
6
6
  exports.DECORATOR = Symbol('DECORATOR');
7
- exports.NAMESPACE_PATTERN = /([A-Z$_]\w+)(?::.+)?/i;
7
+ exports.NAMESPACE_PATTERN = /([a-z$_]\w+)(?::.+)?/i;
8
8
  exports.TYPENAME_PATTERN = /^(.*)Type(\d*)$/;
9
+ exports.SORT_FIELD_PATTERN = /^([+-])?([a-z$_][\w.]+)$/;
@@ -6,6 +6,7 @@ const vg = tslib_1.__importStar(require("valgen"));
6
6
  const index_js_1 = require("../../helpers/index.js");
7
7
  const index_js_2 = require("../../i18n/index.js");
8
8
  const index_js_3 = require("../../schema/index.js");
9
+ const constants_js_1 = require("../constants.js");
9
10
  const data_type_js_1 = require("./data-type.js");
10
11
  const field_js_1 = require("./field.js");
11
12
  /**
@@ -119,14 +120,20 @@ class ComplexTypeClass extends data_type_js_1.DataType {
119
120
  }
120
121
  };
121
122
  }
122
- normalizeFieldPath(fieldPaths) {
123
- const array = (Array.isArray(fieldPaths) ? fieldPaths : [fieldPaths])
123
+ normalizeFieldNames(fieldNames, allowSortSigns) {
124
+ const array = (Array.isArray(fieldNames) ? fieldNames : [fieldNames])
124
125
  .map(s => {
126
+ let sign = '';
127
+ if (allowSortSigns) {
128
+ const m = constants_js_1.SORT_FIELD_PATTERN.exec(s);
129
+ sign = (m && m[1]) || '';
130
+ s = (m && m[2]) || s;
131
+ }
125
132
  let curPath = '';
126
133
  for (const [, , p] of this.iteratePath(s)) {
127
134
  curPath = p;
128
135
  }
129
- return curPath;
136
+ return sign + curPath;
130
137
  }).flat();
131
138
  return array.length ? array : undefined;
132
139
  }
@@ -7,6 +7,7 @@ const index_js_1 = require("../../exception/index.js");
7
7
  const index_js_2 = require("../../filter/index.js");
8
8
  const index_js_3 = require("../../i18n/index.js");
9
9
  const index_js_4 = require("../../schema/index.js");
10
+ const constants_js_1 = require("../constants.js");
10
11
  const simple_type_js_1 = require("../data-type/simple-type.js");
11
12
  const crud_resource_js_1 = require("./crud-resource.js");
12
13
  class CollectionClass extends crud_resource_js_1.CrudResource {
@@ -183,17 +184,18 @@ class CollectionClass extends crud_resource_js_1.CrudResource {
183
184
  return result;
184
185
  }
185
186
  }
186
- normalizeFieldPath(path) {
187
- return this.type.normalizeFieldPath(path);
187
+ normalizeFieldNames(fields, allowSortSigns) {
188
+ return this.type.normalizeFieldNames(fields, allowSortSigns);
188
189
  }
189
190
  normalizeSortFields(fields) {
190
- const normalized = this.type.normalizeFieldPath(fields);
191
+ const normalized = this.type.normalizeFieldNames(fields, true);
191
192
  if (!normalized)
192
193
  return;
193
194
  const findManyOp = this.getOperation('findMany');
194
195
  const sortFields = findManyOp && findManyOp.options.sortFields;
195
196
  (Array.isArray(normalized) ? normalized : [normalized]).forEach(field => {
196
- if (!sortFields?.find(x => x === field))
197
+ const m = constants_js_1.SORT_FIELD_PATTERN.exec(field);
198
+ if (m && !sortFields?.find(x => x === m[2]))
197
199
  throw new index_js_1.BadRequestError({
198
200
  message: (0, index_js_3.translate)('error:UNACCEPTED_SORT_FIELD', { field }),
199
201
  });
@@ -243,7 +245,7 @@ class CollectionClass extends crud_resource_js_1.CrudResource {
243
245
  return ast;
244
246
  }
245
247
  if (ast instanceof index_js_2.OpraFilter.QualifiedIdentifier) {
246
- const normalizedFieldPath = this.type.normalizeFieldPath(ast.value)?.join('.');
248
+ const normalizedFieldPath = this.type.normalizeFieldNames(ast.value)?.join('.');
247
249
  ast.field = this.type.getField(normalizedFieldPath);
248
250
  ast.dataType = ast.field?.type || this.document.getDataType('any');
249
251
  ast.value = normalizedFieldPath;
@@ -68,7 +68,8 @@ Object.assign(CollectionDecorator, resource_decorator_js_1.ResourceDecorator);
68
68
  const decorator = (0, crud_operation_decorator_js_1.createOperationDecorator)('deleteMany', options, list);
69
69
  decorator.Filter = (field, operators, notes) => {
70
70
  if (typeof operators === 'string')
71
- operators = operators.split(/\s*[,| ]\s*/);
71
+ operators = operators.split(/\s*[,| ]\s*/)
72
+ .filter(s => s);
72
73
  list.push(operationMeta => {
73
74
  operationMeta.options.filters = operationMeta.options.filters || [];
74
75
  operationMeta.options.filters.push((0, index_js_1.omitUndefined)({ field, operators, notes }));
@@ -76,8 +76,8 @@ class SingletonClass extends crud_resource_js_1.CrudResource {
76
76
  type: this.type.name || 'any'
77
77
  };
78
78
  }
79
- normalizeFieldPath(path) {
80
- return this.type.normalizeFieldPath(path);
79
+ normalizeFieldNames(path) {
80
+ return this.type.normalizeFieldNames(path);
81
81
  }
82
82
  }
83
83
  exports.SingletonClass = SingletonClass;
@@ -1,5 +1,6 @@
1
1
  export const DATATYPE_METADATA = Symbol('DATATYPE_METADATA');
2
2
  export const RESOURCE_METADATA = Symbol('RESOURCE_METADATA');
3
3
  export const DECORATOR = Symbol('DECORATOR');
4
- export const NAMESPACE_PATTERN = /([A-Z$_]\w+)(?::.+)?/i;
4
+ export const NAMESPACE_PATTERN = /([a-z$_]\w+)(?::.+)?/i;
5
5
  export const TYPENAME_PATTERN = /^(.*)Type(\d*)$/;
6
+ export const SORT_FIELD_PATTERN = /^([+-])?([a-z$_][\w.]+)$/;
@@ -2,6 +2,7 @@ import * as vg from 'valgen';
2
2
  import { omitUndefined, ResponsiveMap } from '../../helpers/index.js';
3
3
  import { translate } from '../../i18n/index.js';
4
4
  import { OpraSchema } from '../../schema/index.js';
5
+ import { SORT_FIELD_PATTERN } from '../constants.js';
5
6
  import { DataType } from './data-type.js';
6
7
  import { ApiField } from './field.js';
7
8
  /**
@@ -115,14 +116,20 @@ export class ComplexTypeClass extends DataType {
115
116
  }
116
117
  };
117
118
  }
118
- normalizeFieldPath(fieldPaths) {
119
- const array = (Array.isArray(fieldPaths) ? fieldPaths : [fieldPaths])
119
+ normalizeFieldNames(fieldNames, allowSortSigns) {
120
+ const array = (Array.isArray(fieldNames) ? fieldNames : [fieldNames])
120
121
  .map(s => {
122
+ let sign = '';
123
+ if (allowSortSigns) {
124
+ const m = SORT_FIELD_PATTERN.exec(s);
125
+ sign = (m && m[1]) || '';
126
+ s = (m && m[2]) || s;
127
+ }
121
128
  let curPath = '';
122
129
  for (const [, , p] of this.iteratePath(s)) {
123
130
  curPath = p;
124
131
  }
125
- return curPath;
132
+ return sign + curPath;
126
133
  }).flat();
127
134
  return array.length ? array : undefined;
128
135
  }
@@ -3,6 +3,7 @@ import { BadRequestError } from '../../exception/index.js';
3
3
  import { OpraFilter } from '../../filter/index.js';
4
4
  import { translate } from '../../i18n/index.js';
5
5
  import { OpraSchema } from '../../schema/index.js';
6
+ import { SORT_FIELD_PATTERN } from '../constants.js';
6
7
  import { SimpleType } from '../data-type/simple-type.js';
7
8
  import { CrudResource } from './crud-resource.js';
8
9
  export class CollectionClass extends CrudResource {
@@ -179,17 +180,18 @@ export class CollectionClass extends CrudResource {
179
180
  return result;
180
181
  }
181
182
  }
182
- normalizeFieldPath(path) {
183
- return this.type.normalizeFieldPath(path);
183
+ normalizeFieldNames(fields, allowSortSigns) {
184
+ return this.type.normalizeFieldNames(fields, allowSortSigns);
184
185
  }
185
186
  normalizeSortFields(fields) {
186
- const normalized = this.type.normalizeFieldPath(fields);
187
+ const normalized = this.type.normalizeFieldNames(fields, true);
187
188
  if (!normalized)
188
189
  return;
189
190
  const findManyOp = this.getOperation('findMany');
190
191
  const sortFields = findManyOp && findManyOp.options.sortFields;
191
192
  (Array.isArray(normalized) ? normalized : [normalized]).forEach(field => {
192
- if (!sortFields?.find(x => x === field))
193
+ const m = SORT_FIELD_PATTERN.exec(field);
194
+ if (m && !sortFields?.find(x => x === m[2]))
193
195
  throw new BadRequestError({
194
196
  message: translate('error:UNACCEPTED_SORT_FIELD', { field }),
195
197
  });
@@ -239,7 +241,7 @@ export class CollectionClass extends CrudResource {
239
241
  return ast;
240
242
  }
241
243
  if (ast instanceof OpraFilter.QualifiedIdentifier) {
242
- const normalizedFieldPath = this.type.normalizeFieldPath(ast.value)?.join('.');
244
+ const normalizedFieldPath = this.type.normalizeFieldNames(ast.value)?.join('.');
243
245
  ast.field = this.type.getField(normalizedFieldPath);
244
246
  ast.dataType = ast.field?.type || this.document.getDataType('any');
245
247
  ast.value = normalizedFieldPath;
@@ -64,7 +64,8 @@ Object.assign(CollectionDecorator, ResourceDecorator);
64
64
  const decorator = createOperationDecorator('deleteMany', options, list);
65
65
  decorator.Filter = (field, operators, notes) => {
66
66
  if (typeof operators === 'string')
67
- operators = operators.split(/\s*[,| ]\s*/);
67
+ operators = operators.split(/\s*[,| ]\s*/)
68
+ .filter(s => s);
68
69
  list.push(operationMeta => {
69
70
  operationMeta.options.filters = operationMeta.options.filters || [];
70
71
  operationMeta.options.filters.push(omitUndefined({ field, operators, notes }));
@@ -73,7 +73,7 @@ export class SingletonClass extends CrudResource {
73
73
  type: this.type.name || 'any'
74
74
  };
75
75
  }
76
- normalizeFieldPath(path) {
77
- return this.type.normalizeFieldPath(path);
76
+ normalizeFieldNames(path) {
77
+ return this.type.normalizeFieldNames(path);
78
78
  }
79
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/common",
3
- "version": "0.32.1",
3
+ "version": "0.32.3",
4
4
  "description": "Opra common package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -3,3 +3,4 @@ export declare const RESOURCE_METADATA: unique symbol;
3
3
  export declare const DECORATOR: unique symbol;
4
4
  export declare const NAMESPACE_PATTERN: RegExp;
5
5
  export declare const TYPENAME_PATTERN: RegExp;
6
+ export declare const SORT_FIELD_PATTERN: RegExp;
@@ -23,13 +23,13 @@ export declare class ComplexTypeClass extends DataType {
23
23
  findField(nameOrPath: string): ApiField | undefined;
24
24
  getField(nameOrPath: string): ApiField;
25
25
  iteratePath(path: string, silent?: boolean): IterableIterator<[string, ApiField | undefined, string]>;
26
- normalizeFieldPath(fieldPaths: string | string[]): string[] | undefined;
26
+ normalizeFieldNames(fieldNames: string | string[], allowSortSigns?: boolean): string[] | undefined;
27
27
  exportSchema(options?: {
28
28
  webSafe?: boolean;
29
29
  }): any;
30
30
  isTypeOf(t: Type | Function): boolean;
31
31
  extendsFrom(t: string | Type | DataType): boolean;
32
- generateCodec(codec: 'decode' | 'encode', options?: DataType.GenerateCodecOptions): vg.Validator;
32
+ generateCodec<T extends Object = any>(codec: 'decode' | 'encode', options?: DataType.GenerateCodecOptions): vg.ObjectValidator<T>;
33
33
  generateCodecSchema(codec: 'decode' | 'encode', options?: DataType.GenerateCodecOptions): vg.ObjectSchema;
34
34
  protected _generateCodecSchema(codec: 'decode' | 'encode', options?: RequiredSome<DataType.GenerateCodecOptions, 'pick' | 'omit'>): vg.ObjectSchema;
35
35
  protected _buildOverwriteFieldsTree(obj: Record<string, DataType.OverrideFieldsConfig>): Record<string, DataType.OverrideFieldsConfig>;
@@ -14,7 +14,7 @@ export declare class CollectionClass extends CrudResource {
14
14
  webSafe?: boolean;
15
15
  }): OpraSchema.Collection;
16
16
  parseKeyValue(value: any): any;
17
- normalizeFieldPath(path: string | string[]): string[] | undefined;
18
- normalizeSortFields(this: Collection, fields: string | string[]): string[] | undefined;
17
+ normalizeFieldNames(fields: string | string[], allowSortSigns?: boolean): string[] | undefined;
18
+ normalizeSortFields(fields: string | string[]): string[] | undefined;
19
19
  normalizeFilter(filter: string | OpraFilter.Expression): OpraFilter.Expression | undefined;
20
20
  }
@@ -17,5 +17,5 @@ export declare class SingletonClass extends CrudResource {
17
17
  exportSchema(options?: {
18
18
  webSafe?: boolean;
19
19
  }): OpraSchema.Singleton;
20
- normalizeFieldPath(path: string | string[]): string[] | undefined;
20
+ normalizeFieldNames(path: string | string[]): string[] | undefined;
21
21
  }