@opra/common 1.0.0-beta.4 → 1.0.0-beta.6

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.
@@ -36,8 +36,6 @@ exports.HttpController = function (...args) {
36
36
  _this.ctor = initArgs.ctor;
37
37
  _this._controllerReverseMap = new WeakMap();
38
38
  _this._initialize?.(initArgs);
39
- _this.onInit = initArgs.onInit;
40
- _this.onShutdown = initArgs.onShutdown;
41
39
  };
42
40
  /**
43
41
  *
@@ -157,17 +155,3 @@ class HttpControllerClass extends document_element_js_1.DocumentElement {
157
155
  exports.HttpController.prototype = HttpControllerClass.prototype;
158
156
  Object.assign(exports.HttpController, http_controller_decorator_js_1.HttpControllerDecoratorFactory);
159
157
  exports.HttpController[constants_js_1.DECORATOR] = http_controller_decorator_js_1.HttpControllerDecoratorFactory;
160
- exports.HttpController.OnInit = function () {
161
- return (target, propertyKey) => {
162
- const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, target.constructor) || {});
163
- sourceMetadata.onInit = target[propertyKey];
164
- Reflect.defineMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, target.constructor, sourceMetadata);
165
- };
166
- };
167
- exports.HttpController.OnShutdown = function () {
168
- return (target, propertyKey) => {
169
- const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, target.constructor) || {});
170
- sourceMetadata.onShutdown = target[propertyKey];
171
- Reflect.defineMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, target.constructor, sourceMetadata);
172
- };
173
- };
@@ -32,8 +32,6 @@ exports.RpcController = function (...args) {
32
32
  _this.ctor = initArgs.ctor;
33
33
  _this._controllerReverseMap = new WeakMap();
34
34
  _this._initialize?.(initArgs);
35
- _this.onInit = initArgs.onInit;
36
- _this.onShutdown = initArgs.onShutdown;
37
35
  };
38
36
  /**
39
37
  *
@@ -100,17 +98,3 @@ class RpcControllerClass extends document_element_js_1.DocumentElement {
100
98
  exports.RpcController.prototype = RpcControllerClass.prototype;
101
99
  Object.assign(exports.RpcController, rpc_controller_decorator_js_1.RpcControllerDecoratorFactory);
102
100
  exports.RpcController[constants_js_1.DECORATOR] = rpc_controller_decorator_js_1.RpcControllerDecoratorFactory;
103
- exports.RpcController.OnInit = function () {
104
- return (target, propertyKey) => {
105
- const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.RPC_CONTROLLER_METADATA, target.constructor) || {});
106
- sourceMetadata.onInit = target[propertyKey];
107
- Reflect.defineMetadata(constants_js_1.RPC_CONTROLLER_METADATA, target.constructor, sourceMetadata);
108
- };
109
- };
110
- exports.RpcController.OnShutdown = function () {
111
- return (target, propertyKey) => {
112
- const sourceMetadata = (Reflect.getOwnMetadata(constants_js_1.RPC_CONTROLLER_METADATA, target.constructor) || {});
113
- sourceMetadata.onShutdown = target[propertyKey];
114
- Reflect.defineMetadata(constants_js_1.RPC_CONTROLLER_METADATA, target.constructor, sourceMetadata);
115
- };
116
- };
@@ -9,6 +9,7 @@ const parse_js_1 = require("./parse.js");
9
9
  class FilterRules {
10
10
  constructor(rules, options) {
11
11
  this._rules = new index_js_2.ResponsiveMap();
12
+ this._decoderCache = new WeakMap();
12
13
  Object.defineProperty(this, '_rules', {
13
14
  value: new index_js_2.ResponsiveMap(null, { caseSensitive: options?.caseSensitive }),
14
15
  enumerable: false,
@@ -28,12 +29,13 @@ class FilterRules {
28
29
  operators,
29
30
  }));
30
31
  }
31
- normalizeFilter(filter, dataType) {
32
- if (!filter)
33
- return;
32
+ normalizeFilter(filter, currentType) {
34
33
  const ast = typeof filter === 'string' ? (0, parse_js_1.parse)(filter) : filter;
34
+ return this.normalizeFilterAst(ast, currentType);
35
+ }
36
+ normalizeFilterAst(ast, currentType, left) {
35
37
  if (ast instanceof index_js_4.ComparisonExpression) {
36
- this.normalizeFilter(ast.left, dataType);
38
+ this.normalizeFilterAst(ast.left, currentType);
37
39
  if (!(ast.left instanceof index_js_4.QualifiedIdentifier && ast.left.field)) {
38
40
  throw new TypeError(`Invalid filter query. Left side should be a data field.`);
39
41
  }
@@ -60,30 +62,45 @@ class FilterRules {
60
62
  },
61
63
  });
62
64
  }
63
- this.normalizeFilter(ast.right, dataType);
65
+ this.normalizeFilterAst(ast.right, currentType, ast.left);
64
66
  return ast;
65
67
  }
66
68
  if (ast instanceof index_js_4.LogicalExpression) {
67
- ast.items.forEach(item => this.normalizeFilter(item, dataType));
69
+ ast.items.forEach(item => this.normalizeFilterAst(item, currentType, left));
68
70
  return ast;
69
71
  }
70
72
  if (ast instanceof index_js_4.ArithmeticExpression) {
71
- ast.items.forEach(item => this.normalizeFilter(item.expression, dataType));
73
+ ast.items.forEach(item => this.normalizeFilterAst(item.expression, currentType, left));
72
74
  return ast;
73
75
  }
74
76
  if (ast instanceof index_js_4.ArrayExpression) {
75
- ast.items.forEach(item => this.normalizeFilter(item, dataType));
77
+ ast.items.forEach(item => this.normalizeFilterAst(item, currentType, left));
76
78
  return ast;
77
79
  }
78
80
  if (ast instanceof index_js_4.ParenthesizedExpression) {
79
- this.normalizeFilter(ast.expression, dataType);
81
+ this.normalizeFilterAst(ast.expression, currentType, left);
80
82
  return ast;
81
83
  }
82
- if (ast instanceof index_js_4.QualifiedIdentifier && dataType) {
83
- ast.value = dataType.normalizeFieldPath(ast.value);
84
- ast.field = dataType.getField(ast.value);
84
+ if (ast instanceof index_js_4.QualifiedIdentifier && currentType) {
85
+ ast.value = currentType.normalizeFieldPath(ast.value);
86
+ ast.field = currentType.getField(ast.value);
85
87
  ast.dataType = ast.field.type;
86
88
  }
89
+ if (ast instanceof index_js_4.Literal && left instanceof index_js_4.QualifiedIdentifier && left.field) {
90
+ if (ast.value == null && !left.field.required)
91
+ return ast.value;
92
+ let decoder = this._decoderCache.get(left.field);
93
+ if (!decoder) {
94
+ decoder = left.field.type.generateCodec('decode', {
95
+ projection: '*',
96
+ ignoreWriteonlyFields: true,
97
+ ignoreHiddenFields: true,
98
+ coerce: true,
99
+ });
100
+ this._decoderCache.set(left.field, decoder);
101
+ }
102
+ ast.value = decoder(ast.value);
103
+ }
87
104
  return ast;
88
105
  }
89
106
  toJSON() {
@@ -4,7 +4,7 @@ import { omitUndefined, ResponsiveMap } from '../../helpers/index.js';
4
4
  import { OpraSchema } from '../../schema/index.js';
5
5
  import { DataTypeMap } from '../common/data-type-map.js';
6
6
  import { DocumentElement } from '../common/document-element.js';
7
- import { CLASS_NAME_PATTERN, DECORATOR, HTTP_CONTROLLER_METADATA, kDataTypeMap } from '../constants.js';
7
+ import { CLASS_NAME_PATTERN, DECORATOR, kDataTypeMap } from '../constants.js';
8
8
  import { HttpControllerDecoratorFactory } from '../decorators/http-controller.decorator.js';
9
9
  import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../utils/inspect.util.js';
10
10
  /**
@@ -32,8 +32,6 @@ export const HttpController = function (...args) {
32
32
  _this.ctor = initArgs.ctor;
33
33
  _this._controllerReverseMap = new WeakMap();
34
34
  _this._initialize?.(initArgs);
35
- _this.onInit = initArgs.onInit;
36
- _this.onShutdown = initArgs.onShutdown;
37
35
  };
38
36
  /**
39
37
  *
@@ -153,17 +151,3 @@ class HttpControllerClass extends DocumentElement {
153
151
  HttpController.prototype = HttpControllerClass.prototype;
154
152
  Object.assign(HttpController, HttpControllerDecoratorFactory);
155
153
  HttpController[DECORATOR] = HttpControllerDecoratorFactory;
156
- HttpController.OnInit = function () {
157
- return (target, propertyKey) => {
158
- const sourceMetadata = (Reflect.getOwnMetadata(HTTP_CONTROLLER_METADATA, target.constructor) || {});
159
- sourceMetadata.onInit = target[propertyKey];
160
- Reflect.defineMetadata(HTTP_CONTROLLER_METADATA, target.constructor, sourceMetadata);
161
- };
162
- };
163
- HttpController.OnShutdown = function () {
164
- return (target, propertyKey) => {
165
- const sourceMetadata = (Reflect.getOwnMetadata(HTTP_CONTROLLER_METADATA, target.constructor) || {});
166
- sourceMetadata.onShutdown = target[propertyKey];
167
- Reflect.defineMetadata(HTTP_CONTROLLER_METADATA, target.constructor, sourceMetadata);
168
- };
169
- };
@@ -3,7 +3,7 @@ import { omitUndefined, ResponsiveMap } from '../../helpers/index.js';
3
3
  import { OpraSchema } from '../../schema/index.js';
4
4
  import { DataTypeMap } from '../common/data-type-map.js';
5
5
  import { DocumentElement } from '../common/document-element.js';
6
- import { CLASS_NAME_PATTERN, DECORATOR, kDataTypeMap, RPC_CONTROLLER_METADATA } from '../constants.js';
6
+ import { CLASS_NAME_PATTERN, DECORATOR, kDataTypeMap } from '../constants.js';
7
7
  import { RpcControllerDecoratorFactory } from '../decorators/rpc-controller.decorator.js';
8
8
  import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../utils/inspect.util.js';
9
9
  /**
@@ -29,8 +29,6 @@ export const RpcController = function (...args) {
29
29
  _this.ctor = initArgs.ctor;
30
30
  _this._controllerReverseMap = new WeakMap();
31
31
  _this._initialize?.(initArgs);
32
- _this.onInit = initArgs.onInit;
33
- _this.onShutdown = initArgs.onShutdown;
34
32
  };
35
33
  /**
36
34
  *
@@ -97,17 +95,3 @@ class RpcControllerClass extends DocumentElement {
97
95
  RpcController.prototype = RpcControllerClass.prototype;
98
96
  Object.assign(RpcController, RpcControllerDecoratorFactory);
99
97
  RpcController[DECORATOR] = RpcControllerDecoratorFactory;
100
- RpcController.OnInit = function () {
101
- return (target, propertyKey) => {
102
- const sourceMetadata = (Reflect.getOwnMetadata(RPC_CONTROLLER_METADATA, target.constructor) || {});
103
- sourceMetadata.onInit = target[propertyKey];
104
- Reflect.defineMetadata(RPC_CONTROLLER_METADATA, target.constructor, sourceMetadata);
105
- };
106
- };
107
- RpcController.OnShutdown = function () {
108
- return (target, propertyKey) => {
109
- const sourceMetadata = (Reflect.getOwnMetadata(RPC_CONTROLLER_METADATA, target.constructor) || {});
110
- sourceMetadata.onShutdown = target[propertyKey];
111
- Reflect.defineMetadata(RPC_CONTROLLER_METADATA, target.constructor, sourceMetadata);
112
- };
113
- };
@@ -1,11 +1,12 @@
1
1
  import { OpraException } from '../exception/index.js';
2
2
  import { omitUndefined, ResponsiveMap } from '../helpers/index.js';
3
3
  import { translate } from '../i18n/index.js';
4
- import { ArithmeticExpression, ArrayExpression, ComparisonExpression, LogicalExpression, ParenthesizedExpression, QualifiedIdentifier, } from './ast/index.js';
4
+ import { ArithmeticExpression, ArrayExpression, ComparisonExpression, Literal, LogicalExpression, ParenthesizedExpression, QualifiedIdentifier, } from './ast/index.js';
5
5
  import { parse } from './parse.js';
6
6
  export class FilterRules {
7
7
  constructor(rules, options) {
8
8
  this._rules = new ResponsiveMap();
9
+ this._decoderCache = new WeakMap();
9
10
  Object.defineProperty(this, '_rules', {
10
11
  value: new ResponsiveMap(null, { caseSensitive: options?.caseSensitive }),
11
12
  enumerable: false,
@@ -25,12 +26,13 @@ export class FilterRules {
25
26
  operators,
26
27
  }));
27
28
  }
28
- normalizeFilter(filter, dataType) {
29
- if (!filter)
30
- return;
29
+ normalizeFilter(filter, currentType) {
31
30
  const ast = typeof filter === 'string' ? parse(filter) : filter;
31
+ return this.normalizeFilterAst(ast, currentType);
32
+ }
33
+ normalizeFilterAst(ast, currentType, left) {
32
34
  if (ast instanceof ComparisonExpression) {
33
- this.normalizeFilter(ast.left, dataType);
35
+ this.normalizeFilterAst(ast.left, currentType);
34
36
  if (!(ast.left instanceof QualifiedIdentifier && ast.left.field)) {
35
37
  throw new TypeError(`Invalid filter query. Left side should be a data field.`);
36
38
  }
@@ -57,30 +59,45 @@ export class FilterRules {
57
59
  },
58
60
  });
59
61
  }
60
- this.normalizeFilter(ast.right, dataType);
62
+ this.normalizeFilterAst(ast.right, currentType, ast.left);
61
63
  return ast;
62
64
  }
63
65
  if (ast instanceof LogicalExpression) {
64
- ast.items.forEach(item => this.normalizeFilter(item, dataType));
66
+ ast.items.forEach(item => this.normalizeFilterAst(item, currentType, left));
65
67
  return ast;
66
68
  }
67
69
  if (ast instanceof ArithmeticExpression) {
68
- ast.items.forEach(item => this.normalizeFilter(item.expression, dataType));
70
+ ast.items.forEach(item => this.normalizeFilterAst(item.expression, currentType, left));
69
71
  return ast;
70
72
  }
71
73
  if (ast instanceof ArrayExpression) {
72
- ast.items.forEach(item => this.normalizeFilter(item, dataType));
74
+ ast.items.forEach(item => this.normalizeFilterAst(item, currentType, left));
73
75
  return ast;
74
76
  }
75
77
  if (ast instanceof ParenthesizedExpression) {
76
- this.normalizeFilter(ast.expression, dataType);
78
+ this.normalizeFilterAst(ast.expression, currentType, left);
77
79
  return ast;
78
80
  }
79
- if (ast instanceof QualifiedIdentifier && dataType) {
80
- ast.value = dataType.normalizeFieldPath(ast.value);
81
- ast.field = dataType.getField(ast.value);
81
+ if (ast instanceof QualifiedIdentifier && currentType) {
82
+ ast.value = currentType.normalizeFieldPath(ast.value);
83
+ ast.field = currentType.getField(ast.value);
82
84
  ast.dataType = ast.field.type;
83
85
  }
86
+ if (ast instanceof Literal && left instanceof QualifiedIdentifier && left.field) {
87
+ if (ast.value == null && !left.field.required)
88
+ return ast.value;
89
+ let decoder = this._decoderCache.get(left.field);
90
+ if (!decoder) {
91
+ decoder = left.field.type.generateCodec('decode', {
92
+ projection: '*',
93
+ ignoreWriteonlyFields: true,
94
+ ignoreHiddenFields: true,
95
+ coerce: true,
96
+ });
97
+ this._decoderCache.set(left.field, decoder);
98
+ }
99
+ ast.value = decoder(ast.value);
100
+ }
84
101
  return ast;
85
102
  }
86
103
  toJSON() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/common",
3
- "version": "1.0.0-beta.4",
3
+ "version": "1.0.0-beta.6",
4
4
  "description": "Opra common package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -19,8 +19,6 @@ export declare namespace HttpController {
19
19
  types?: ThunkAsync<Type | EnumType.EnumObject | EnumType.EnumArray>[];
20
20
  operations?: Record<string, HttpOperation.Metadata>;
21
21
  parameters?: HttpParameter.Metadata[];
22
- onInit?: (resource: HttpController) => void;
23
- onShutdown?: (resource: HttpController) => void | Promise<void>;
24
22
  }
25
23
  interface Options extends Partial<Pick<OpraSchema.HttpController, 'description' | 'path'>> {
26
24
  name?: string;
@@ -29,7 +27,7 @@ export declare namespace HttpController {
29
27
  interface InitArguments extends Combine<{
30
28
  instance?: object;
31
29
  ctor?: Type;
32
- }, Pick<Metadata, 'name' | 'description' | 'path' | 'onInit' | 'onShutdown'>> {
30
+ }, Pick<Metadata, 'name' | 'description' | 'path'>> {
33
31
  }
34
32
  }
35
33
  /**
@@ -44,8 +42,6 @@ export interface HttpControllerStatic extends HttpControllerDecoratorFactory {
44
42
  */
45
43
  new (owner: HttpApi | HttpController, args: HttpController.InitArguments): HttpController;
46
44
  prototype: HttpController;
47
- OnInit(): PropertyDecorator;
48
- OnShutdown(): PropertyDecorator;
49
45
  }
50
46
  /**
51
47
  * Type definition of HttpController prototype
@@ -73,8 +69,6 @@ declare class HttpControllerClass extends DocumentElement {
73
69
  operations: ResponsiveMap<HttpOperation>;
74
70
  controllers: ResponsiveMap<HttpController>;
75
71
  types: DataTypeMap;
76
- onInit?: (resource: HttpController) => void;
77
- onShutdown?: (resource: HttpController) => void | Promise<void>;
78
72
  /**
79
73
  * @property isRoot
80
74
  */
@@ -18,8 +18,6 @@ export declare namespace RpcController {
18
18
  types?: ThunkAsync<Type | EnumType.EnumObject | EnumType.EnumArray>[];
19
19
  operations?: Record<string, RpcOperation.Metadata>;
20
20
  headers?: RpcHeader.Metadata[];
21
- onInit?: (resource: RpcController) => void;
22
- onShutdown?: (resource: RpcController) => void | Promise<void>;
23
21
  }
24
22
  interface Options extends Partial<Pick<OpraSchema.RpcController, 'description'>> {
25
23
  name?: string;
@@ -27,7 +25,7 @@ export declare namespace RpcController {
27
25
  interface InitArguments extends Combine<{
28
26
  instance?: object;
29
27
  ctor?: Type;
30
- }, Pick<Metadata, 'name' | 'description' | 'onInit' | 'onShutdown'>> {
28
+ }, Pick<Metadata, 'name' | 'description'>> {
31
29
  }
32
30
  }
33
31
  /**
@@ -42,8 +40,6 @@ export interface RpcControllerStatic extends RpcControllerDecoratorFactory {
42
40
  */
43
41
  new (owner: RpcApi | RpcController, args: RpcController.InitArguments): RpcController;
44
42
  prototype: RpcController;
45
- OnInit(): PropertyDecorator;
46
- OnShutdown(): PropertyDecorator;
47
43
  }
48
44
  /**
49
45
  * Type definition of RpcController prototype
@@ -70,8 +66,6 @@ declare class RpcControllerClass extends DocumentElement {
70
66
  headers: RpcHeader[];
71
67
  operations: ResponsiveMap<RpcOperation>;
72
68
  types: DataTypeMap;
73
- onInit?: (resource: RpcController) => void;
74
- onShutdown?: (resource: RpcController) => void | Promise<void>;
75
69
  findHeader(paramName: string, location?: OpraSchema.HttpParameterLocation): RpcHeader | undefined;
76
70
  /**
77
71
  *
@@ -1,4 +1,5 @@
1
1
  import type { StrictOmit } from 'ts-gems';
2
+ import { Validator } from 'valgen';
2
3
  import type { ComplexType } from '../document/index.js';
3
4
  import { ResponsiveMap } from '../helpers/index.js';
4
5
  import { OpraSchema } from '../schema/index.js';
@@ -14,11 +15,13 @@ export declare namespace FilterRules {
14
15
  }
15
16
  export declare class FilterRules {
16
17
  protected _rules: ResponsiveMap<FilterRules.Rule>;
18
+ protected _decoderCache: WeakMap<any, Validator<any, any, import("valgen").ExecutionOptions>>;
17
19
  constructor(rules?: Record<string, FilterRules.Rule>, options?: FilterRules.Options);
18
20
  set(fieldName: string, options?: Partial<StrictOmit<FilterRules.Rule, 'operators'>> & {
19
21
  operators?: ComparisonOperator[] | string;
20
22
  }): void;
21
- normalizeFilter(filter: OpraSchema.Field.QualifiedName | Expression, dataType?: ComplexType): Expression | undefined;
23
+ normalizeFilter(filter: OpraSchema.Field.QualifiedName | Expression, currentType?: ComplexType): Expression | undefined;
24
+ protected normalizeFilterAst(ast: Expression, currentType?: ComplexType, left?: Expression): Expression | undefined;
22
25
  toJSON(): Record<string, FilterRules.Rule>;
23
26
  [Symbol.iterator](): IterableIterator<[string, FilterRules.Rule]>;
24
27
  }