@opra/core 0.0.8 → 0.0.11

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.
Files changed (84) hide show
  1. package/cjs/exception/api-exception.js +32 -35
  2. package/cjs/exception/{errors → http-errors}/bad-request.error.js +7 -4
  3. package/cjs/exception/{errors → http-errors}/failed-dependency.error.js +7 -4
  4. package/cjs/exception/{errors → http-errors}/forbidden.error.js +7 -4
  5. package/cjs/exception/{errors → http-errors}/internal-server.error.js +10 -5
  6. package/cjs/exception/{errors → http-errors}/method-not-allowed.error.js +7 -4
  7. package/cjs/exception/{errors → http-errors}/not-found.error.js +6 -3
  8. package/cjs/exception/{errors → http-errors}/unauthorized.error.js +6 -3
  9. package/cjs/exception/{errors → http-errors}/unprocessable-entity.error.js +7 -4
  10. package/cjs/exception/index.js +9 -7
  11. package/cjs/exception/resource-errors/resource-conflict.error.js +19 -0
  12. package/cjs/exception/wrap-error.js +2 -2
  13. package/cjs/implementation/adapter/adapter.js +31 -29
  14. package/cjs/implementation/adapter/express-adapter.js +40 -9
  15. package/cjs/implementation/adapter/http-adapter.js +28 -35
  16. package/cjs/implementation/{execution-context.js → query-context.js} +18 -22
  17. package/cjs/implementation/resource/entity-resource-handler.js +16 -12
  18. package/cjs/implementation/resource/resource-handler.js +1 -1
  19. package/cjs/implementation/schema-generator.js +38 -51
  20. package/cjs/index.js +3 -4
  21. package/cjs/interfaces/{http-context.interface.js → execution-context.interface.js} +0 -0
  22. package/cjs/interfaces/{execution-query.interface.js → query.interface.js} +24 -24
  23. package/cjs/utils/create-i18n.js +21 -0
  24. package/cjs/utils/get-caller-file.util.js +19 -0
  25. package/cjs/utils/internal-data-types.js +54 -17
  26. package/esm/exception/api-exception.d.ts +3 -2
  27. package/esm/exception/api-exception.js +32 -35
  28. package/esm/exception/{errors → http-errors}/bad-request.error.d.ts +2 -1
  29. package/esm/exception/{errors → http-errors}/bad-request.error.js +7 -4
  30. package/esm/exception/{errors → http-errors}/failed-dependency.error.d.ts +2 -1
  31. package/esm/exception/{errors → http-errors}/failed-dependency.error.js +7 -4
  32. package/esm/exception/{errors → http-errors}/forbidden.error.d.ts +2 -1
  33. package/esm/exception/{errors → http-errors}/forbidden.error.js +7 -4
  34. package/esm/exception/{errors → http-errors}/internal-server.error.d.ts +2 -1
  35. package/esm/exception/{errors → http-errors}/internal-server.error.js +9 -5
  36. package/esm/exception/{errors → http-errors}/method-not-allowed.error.d.ts +2 -1
  37. package/esm/exception/{errors → http-errors}/method-not-allowed.error.js +7 -4
  38. package/esm/exception/{errors → http-errors}/not-found.error.d.ts +2 -1
  39. package/esm/exception/{errors → http-errors}/not-found.error.js +6 -3
  40. package/esm/exception/{errors → http-errors}/unauthorized.error.d.ts +2 -1
  41. package/esm/exception/{errors → http-errors}/unauthorized.error.js +6 -3
  42. package/esm/exception/{errors → http-errors}/unprocessable-entity.error.d.ts +2 -1
  43. package/esm/exception/{errors → http-errors}/unprocessable-entity.error.js +7 -4
  44. package/esm/exception/index.d.ts +9 -7
  45. package/esm/exception/index.js +9 -7
  46. package/esm/exception/resource-errors/resource-conflict.error.d.ts +4 -0
  47. package/esm/exception/resource-errors/resource-conflict.error.js +15 -0
  48. package/esm/exception/wrap-error.js +2 -2
  49. package/esm/implementation/adapter/adapter.d.ts +22 -14
  50. package/esm/implementation/adapter/adapter.js +31 -29
  51. package/esm/implementation/adapter/express-adapter.d.ts +2 -2
  52. package/esm/implementation/adapter/express-adapter.js +40 -9
  53. package/esm/implementation/adapter/http-adapter.d.ts +11 -12
  54. package/esm/implementation/adapter/http-adapter.js +28 -35
  55. package/esm/implementation/query-context.d.ts +32 -0
  56. package/esm/implementation/{execution-context.js → query-context.js} +15 -18
  57. package/esm/implementation/resource/container-resource-handler.d.ts +2 -2
  58. package/esm/implementation/resource/entity-resource-handler.d.ts +3 -3
  59. package/esm/implementation/resource/entity-resource-handler.js +16 -12
  60. package/esm/implementation/resource/resource-handler.d.ts +3 -3
  61. package/esm/implementation/resource/resource-handler.js +1 -1
  62. package/esm/implementation/schema-generator.js +38 -51
  63. package/esm/index.d.ts +3 -4
  64. package/esm/index.js +3 -4
  65. package/esm/interfaces/execution-context.interface.d.ts +39 -0
  66. package/esm/interfaces/{http-context.interface.js → execution-context.interface.js} +0 -0
  67. package/esm/interfaces/query.interface.d.ts +108 -0
  68. package/esm/interfaces/{execution-query.interface.js → query.interface.js} +23 -23
  69. package/esm/services/entity-resource-controller.d.ts +11 -11
  70. package/esm/types.d.ts +1 -3
  71. package/esm/utils/create-i18n.d.ts +3 -0
  72. package/esm/utils/create-i18n.js +16 -0
  73. package/esm/utils/get-caller-file.util.d.ts +1 -0
  74. package/esm/utils/get-caller-file.util.js +15 -0
  75. package/esm/utils/internal-data-types.d.ts +2 -1
  76. package/esm/utils/internal-data-types.js +53 -16
  77. package/i18n/en/error.json +12 -0
  78. package/package.json +10 -6
  79. package/cjs/interfaces/user-context.interface.js +0 -2
  80. package/esm/implementation/execution-context.d.ts +0 -42
  81. package/esm/interfaces/execution-query.interface.d.ts +0 -102
  82. package/esm/interfaces/http-context.interface.d.ts +0 -23
  83. package/esm/interfaces/user-context.interface.d.ts +0 -3
  84. package/esm/interfaces/user-context.interface.js +0 -1
@@ -3,6 +3,7 @@ import { DATATYPE_METADATA, DATATYPE_PROPERTIES, OpraSchema } from '@opra/schema
3
3
  import { RESOURCE_METADATA } from '../constants.js';
4
4
  import { isConstructor, resolveClassAsync } from '../utils/class-utils.js';
5
5
  import { builtinClassMap, internalDataTypes, primitiveDataTypeNames } from '../utils/internal-data-types.js';
6
+ var isDataType = OpraSchema.isDataType;
6
7
  const entityMethods = ['search', 'count', 'create', 'get', 'update', 'updateMany', 'delete', 'deleteMany'];
7
8
  export class SchemaGenerator {
8
9
  _dataTypes = {};
@@ -11,24 +12,16 @@ export class SchemaGenerator {
11
12
  //
12
13
  }
13
14
  async addDataType(thunk) {
14
- let schema;
15
15
  thunk = isPromise(thunk) ? await thunk : thunk;
16
- if (typeof thunk === 'function' && !isConstructor(thunk))
17
- thunk = await thunk();
18
- if (!isConstructor(thunk) || OpraSchema.isDataType(thunk))
19
- throw new TypeError(`Function must return a type class or type schema`);
20
- if (isConstructor(thunk)) {
16
+ if (typeof thunk === 'function') {
17
+ if (!isConstructor(thunk))
18
+ return this.addDataType(await thunk());
19
+ if (builtinClassMap.has(thunk))
20
+ return this.addDataType(builtinClassMap.get(thunk));
21
21
  const ctor = thunk;
22
22
  const metadata = Reflect.getOwnMetadata(DATATYPE_METADATA, ctor);
23
23
  if (!metadata)
24
24
  throw new TypeError(`Class "${ctor}" has no type metadata`);
25
- schema = this._dataTypes[metadata.name];
26
- if (schema) {
27
- if (schema.kind !== metadata.kind ||
28
- (OpraSchema.isComplexType(schema) && schema.ctor !== ctor))
29
- throw new Error(`An other instance of "${schema.name}" data type previously defined`);
30
- return schema;
31
- }
32
25
  // Add base data type
33
26
  let base;
34
27
  let baseCtor = Object.getPrototypeOf(ctor);
@@ -39,49 +32,43 @@ export class SchemaGenerator {
39
32
  const baseSchema = await this.addDataType(baseCtor);
40
33
  base = baseSchema.name;
41
34
  }
42
- if (OpraSchema.isComplexType(metadata) || OpraSchema.isEntityType(metadata)) {
43
- schema = {
44
- ...metadata,
45
- ctor,
46
- base
47
- };
48
- const properties = Reflect.getMetadata(DATATYPE_PROPERTIES, ctor.prototype);
49
- if (properties) {
50
- for (const [k, p] of Object.entries(properties)) {
51
- let type;
52
- if (isConstructor(p.type) && builtinClassMap.has(p.type))
53
- type = builtinClassMap.get(p.type);
54
- else if (typeof p.type === 'function' || typeof p.type === 'object') {
55
- const t = await this.addDataType(p.type);
56
- type = t.name;
57
- }
58
- else
59
- type = p.type || 'string';
60
- if (internalDataTypes.has(type) && !this._dataTypes[type])
61
- this._dataTypes[type] = internalDataTypes.get(type.toLowerCase());
62
- schema.properties = schema.properties || {};
63
- schema.properties[k] = { ...p, type };
35
+ const schema = {
36
+ ...metadata,
37
+ ctor,
38
+ base
39
+ };
40
+ const properties = Reflect.getMetadata(DATATYPE_PROPERTIES, ctor.prototype);
41
+ if (properties) {
42
+ for (const [k, p] of Object.entries(properties)) {
43
+ let type = p.type || 'string';
44
+ if (typeof type !== 'string') {
45
+ const propSchema = await this.addDataType(type);
46
+ type = propSchema.name;
64
47
  }
48
+ else {
49
+ if (internalDataTypes.has(type))
50
+ await this.addDataType(internalDataTypes.get(type));
51
+ }
52
+ schema.properties = schema.properties || {};
53
+ schema.properties[k] = { ...p, type };
65
54
  }
66
55
  }
67
- else if (OpraSchema.isSimpleType(metadata)) {
68
- if (!primitiveDataTypeNames.includes(metadata.type))
69
- throw new Error(`"type" of SimpleType schema must be one of enumerated value (${primitiveDataTypeNames})`);
70
- schema = {
71
- ...metadata
72
- };
73
- }
74
- else
75
- /* istanbul ignore next */
76
- throw new TypeError(`Invalid metadata`);
56
+ return this.addDataType(schema);
77
57
  }
78
- else if (OpraSchema.isDataType(thunk)) {
79
- schema = thunk;
80
- }
81
- else
58
+ if (!isDataType(thunk))
82
59
  throw new TypeError(`Invalid data type schema`);
83
- this._dataTypes[schema.name] = schema;
84
- return schema;
60
+ // Check if datatype previously added
61
+ const currentSchema = this._dataTypes[thunk.name];
62
+ if (currentSchema) {
63
+ if (!(currentSchema.kind === thunk.kind && currentSchema.ctor && currentSchema.ctor === thunk.ctor))
64
+ throw new Error(`An other instance of "${currentSchema.name}" data type previously defined`);
65
+ return currentSchema;
66
+ }
67
+ if (OpraSchema.isSimpleType(thunk) && !primitiveDataTypeNames.includes(thunk.type))
68
+ throw new Error(`"type" of SimpleType schema must be one of enumerated value (${primitiveDataTypeNames})`);
69
+ return this._dataTypes[thunk.name] = {
70
+ ...thunk
71
+ };
85
72
  }
86
73
  async addResource(instance) {
87
74
  if (isConstructor(instance))
package/esm/index.d.ts CHANGED
@@ -4,11 +4,10 @@ export * from './types.js';
4
4
  export * from './enums/index.js';
5
5
  export * from './exception/index.js';
6
6
  export * from './decorators/entity-resource.decorator.js';
7
- export * from './interfaces/http-context.interface.js';
8
- export * from './interfaces/execution-query.interface.js';
7
+ export * from './interfaces/execution-context.interface.js';
8
+ export * from './interfaces/query.interface.js';
9
9
  export * from './interfaces/resource-container.interface.js';
10
- export * from './interfaces/user-context.interface.js';
11
- export * from './implementation/execution-context.js';
10
+ export * from './implementation/query-context.js';
12
11
  export * from './implementation/opra-document.js';
13
12
  export * from './implementation/opra-service.js';
14
13
  export * from './implementation/schema-generator.js';
package/esm/index.js CHANGED
@@ -4,11 +4,10 @@ export * from './types.js';
4
4
  export * from './enums/index.js';
5
5
  export * from './exception/index.js';
6
6
  export * from './decorators/entity-resource.decorator.js';
7
- export * from './interfaces/http-context.interface.js';
8
- export * from './interfaces/execution-query.interface.js';
7
+ export * from './interfaces/execution-context.interface.js';
8
+ export * from './interfaces/query.interface.js';
9
9
  export * from './interfaces/resource-container.interface.js';
10
- export * from './interfaces/user-context.interface.js';
11
- export * from './implementation/execution-context.js';
10
+ export * from './implementation/query-context.js';
12
11
  export * from './implementation/opra-document.js';
13
12
  export * from './implementation/opra-service.js';
14
13
  export * from './implementation/schema-generator.js';
@@ -0,0 +1,39 @@
1
+ export declare type ContextType = 'http' | 'ws' | 'rpc';
2
+ export declare type PlatformType = 'express';
3
+ export declare namespace IExecutionContext {
4
+ type OnFinishArgs = {
5
+ userContext: any;
6
+ failed: boolean;
7
+ };
8
+ }
9
+ export interface IExecutionContext {
10
+ getType(): ContextType;
11
+ getPlatform(): PlatformType;
12
+ switchToHttp(): IHttpExecutionContext;
13
+ onFinish(fn: (args: IExecutionContext.OnFinishArgs) => void | Promise<void>): any;
14
+ }
15
+ export interface IHttpExecutionContext extends IExecutionContext {
16
+ getRequest(): any;
17
+ getResponse(): any;
18
+ getRequestWrapper(): IHttpRequestWrapper;
19
+ getResponseWrapper(): IHttpResponseWrapper;
20
+ }
21
+ export interface IHttpRequestWrapper {
22
+ getInstance(): any;
23
+ getUrl(): string;
24
+ getMethod(): string;
25
+ getHeaderNames(): string[];
26
+ getHeader(name: string): string | undefined;
27
+ getHeaders(): Record<string, any>;
28
+ getBody(): any;
29
+ }
30
+ export interface IHttpResponseWrapper {
31
+ getInstance(): any;
32
+ getStatus(): number | undefined;
33
+ setStatus(value: number): this;
34
+ getHeaderNames(name: string): string[];
35
+ getHeader(name: string): string | undefined;
36
+ setHeader(name: string, value: string): this;
37
+ send(body: any): this;
38
+ end(): this;
39
+ }
@@ -0,0 +1,108 @@
1
+ import { StrictOmit } from 'ts-gems';
2
+ import { OpraSchema } from '@opra/schema';
3
+ import { Expression } from '@opra/url';
4
+ import { EntityResourceHandler } from '../implementation/resource/entity-resource-handler.js';
5
+ import { KeyValue, OperationType, QueryScope, QueryType } from '../types.js';
6
+ export declare type OpraQuery = OpraCreateQuery | OpraGetQuery | OpraSearchQuery | OpraUpdateQuery | OpraUpdateManyQuery | OpraDeleteQuery | OpraDeleteManyQuery;
7
+ interface BaseOpraQuery {
8
+ queryType: QueryType;
9
+ scope: QueryScope;
10
+ operation: OperationType;
11
+ }
12
+ export interface OpraCreateQuery extends BaseOpraQuery {
13
+ queryType: 'create';
14
+ scope: 'collection';
15
+ operation: 'create';
16
+ resource: EntityResourceHandler;
17
+ data: {};
18
+ pick?: string[];
19
+ omit?: string[];
20
+ include?: string[];
21
+ }
22
+ export interface OpraGetQuery extends BaseOpraQuery {
23
+ queryType: 'get';
24
+ scope: 'instance';
25
+ operation: 'read';
26
+ resource: EntityResourceHandler;
27
+ keyValue: KeyValue;
28
+ pick?: string[];
29
+ omit?: string[];
30
+ include?: string[];
31
+ nested?: OpraPropertyQuery;
32
+ }
33
+ export interface OpraPropertyQuery extends BaseOpraQuery {
34
+ queryType: 'get';
35
+ scope: 'property';
36
+ operation: 'read';
37
+ property: OpraSchema.Property;
38
+ nested?: OpraPropertyQuery;
39
+ }
40
+ export interface OpraUpdateQuery extends BaseOpraQuery {
41
+ queryType: 'update';
42
+ scope: 'instance';
43
+ operation: 'update';
44
+ resource: EntityResourceHandler;
45
+ keyValue: KeyValue;
46
+ data: {};
47
+ pick?: string[];
48
+ omit?: string[];
49
+ include?: string[];
50
+ }
51
+ export interface OpraUpdateManyQuery extends BaseOpraQuery {
52
+ queryType: 'updateMany';
53
+ scope: 'collection';
54
+ operation: 'update';
55
+ resource: EntityResourceHandler;
56
+ filter?: string | Expression;
57
+ data: {};
58
+ }
59
+ export interface OpraDeleteQuery extends BaseOpraQuery {
60
+ queryType: 'delete';
61
+ scope: 'instance';
62
+ operation: 'delete';
63
+ resource: EntityResourceHandler;
64
+ keyValue: KeyValue;
65
+ }
66
+ export interface OpraDeleteManyQuery extends BaseOpraQuery {
67
+ queryType: 'deleteMany';
68
+ scope: 'collection';
69
+ operation: 'delete';
70
+ resource: EntityResourceHandler;
71
+ filter?: string | Expression;
72
+ }
73
+ export interface OpraSearchQuery extends BaseOpraQuery {
74
+ queryType: 'search';
75
+ scope: 'collection';
76
+ operation: 'read';
77
+ resource: EntityResourceHandler;
78
+ pick?: string[];
79
+ omit?: string[];
80
+ include?: string[];
81
+ filter?: string | Expression;
82
+ limit?: number;
83
+ skip?: number;
84
+ distinct?: boolean;
85
+ count?: boolean;
86
+ sort?: string[];
87
+ }
88
+ export declare type CreateQueryOptions = StrictOmit<OpraCreateQuery, 'queryType' | 'scope' | 'operation' | 'resource' | 'data'>;
89
+ export declare type GetQueryOptions = StrictOmit<OpraGetQuery, 'queryType' | 'scope' | 'operation' | 'resource' | 'keyValue'>;
90
+ export declare type SearchQueryOptions = StrictOmit<OpraSearchQuery, 'queryType' | 'scope' | 'operation' | 'resource'>;
91
+ export declare type UpdateQueryOptions = StrictOmit<OpraUpdateQuery, 'queryType' | 'scope' | 'operation' | 'resource' | 'keyValue' | 'data'>;
92
+ export declare type UpdateManyQueryOptions = StrictOmit<OpraUpdateManyQuery, 'queryType' | 'scope' | 'operation' | 'resource' | 'data'>;
93
+ export declare type DeleteManyQueryOption = StrictOmit<OpraDeleteManyQuery, 'queryType' | 'scope' | 'operation' | 'resource'>;
94
+ export declare namespace OpraQuery {
95
+ function forCreate(resource: EntityResourceHandler, values: {}, options?: CreateQueryOptions): OpraCreateQuery;
96
+ function forGet(resource: EntityResourceHandler, key: KeyValue, options?: GetQueryOptions): OpraGetQuery;
97
+ function forSearch(resource: EntityResourceHandler, options?: SearchQueryOptions): OpraSearchQuery;
98
+ function forGetProperty(property: OpraSchema.Property, options?: StrictOmit<OpraPropertyQuery, 'queryType' | 'scope' | 'operation' | 'property'>): OpraPropertyQuery;
99
+ function forUpdate(resource: EntityResourceHandler, keyValue: KeyValue, values: any, options?: UpdateQueryOptions): OpraUpdateQuery;
100
+ function forUpdateMany(resource: EntityResourceHandler, values: any, options?: UpdateManyQueryOptions): OpraUpdateManyQuery;
101
+ function forDelete(resource: EntityResourceHandler, key: KeyValue): OpraDeleteQuery;
102
+ function forDeleteMany(resource: EntityResourceHandler, options?: DeleteManyQueryOption): OpraDeleteManyQuery;
103
+ function isCreateQuery(q: any): q is OpraCreateQuery;
104
+ function isSearchQuery(q: any): q is OpraSearchQuery;
105
+ function isReadQuery(q: any): q is OpraGetQuery;
106
+ function isDeleteQuery(q: any): q is OpraDeleteQuery;
107
+ }
108
+ export {};
@@ -1,8 +1,8 @@
1
1
  import _ from 'lodash';
2
2
  import { ComplexType } from '../implementation/data-type/complex-type.js';
3
3
  import { stringPathToObjectTree } from '../utils/string-path-to-object-tree.js';
4
- export var ExecutionQuery;
5
- (function (ExecutionQuery) {
4
+ export var OpraQuery;
5
+ (function (OpraQuery) {
6
6
  function forCreate(resource, values, options) {
7
7
  if (options?.pick)
8
8
  options.pick = normalizePick(resource, options.pick);
@@ -13,14 +13,14 @@ export var ExecutionQuery;
13
13
  const out = {
14
14
  queryType: 'create',
15
15
  scope: 'collection',
16
- operationType: 'create',
16
+ operation: 'create',
17
17
  resource,
18
18
  data: values
19
19
  };
20
20
  Object.assign(out, _.omit(options, Object.keys(out)));
21
21
  return out;
22
22
  }
23
- ExecutionQuery.forCreate = forCreate;
23
+ OpraQuery.forCreate = forCreate;
24
24
  function forGet(resource, key, options) {
25
25
  if (options?.pick)
26
26
  options.pick = normalizePick(resource, options.pick);
@@ -32,14 +32,14 @@ export var ExecutionQuery;
32
32
  const out = {
33
33
  queryType: 'get',
34
34
  scope: 'instance',
35
- operationType: 'read',
35
+ operation: 'read',
36
36
  resource,
37
37
  keyValue: key
38
38
  };
39
39
  Object.assign(out, _.omit(options, Object.keys(out)));
40
40
  return out;
41
41
  }
42
- ExecutionQuery.forGet = forGet;
42
+ OpraQuery.forGet = forGet;
43
43
  function forSearch(resource, options) {
44
44
  if (options?.pick)
45
45
  options.pick = normalizePick(resource, options.pick);
@@ -52,24 +52,24 @@ export var ExecutionQuery;
52
52
  const out = {
53
53
  queryType: 'search',
54
54
  scope: 'collection',
55
- operationType: 'read',
55
+ operation: 'read',
56
56
  resource
57
57
  };
58
58
  Object.assign(out, _.omit(options, Object.keys(out)));
59
59
  return out;
60
60
  }
61
- ExecutionQuery.forSearch = forSearch;
61
+ OpraQuery.forSearch = forSearch;
62
62
  function forGetProperty(property, options) {
63
63
  const out = {
64
64
  queryType: 'get',
65
65
  scope: 'property',
66
- operationType: 'read',
66
+ operation: 'read',
67
67
  property
68
68
  };
69
69
  Object.assign(out, _.omit(options, Object.keys(out)));
70
70
  return out;
71
71
  }
72
- ExecutionQuery.forGetProperty = forGetProperty;
72
+ OpraQuery.forGetProperty = forGetProperty;
73
73
  function forUpdate(resource, keyValue, values, options) {
74
74
  if (options?.pick)
75
75
  options.pick = normalizePick(resource, options.pick);
@@ -81,7 +81,7 @@ export var ExecutionQuery;
81
81
  const out = {
82
82
  queryType: 'update',
83
83
  scope: 'instance',
84
- operationType: 'update',
84
+ operation: 'update',
85
85
  resource,
86
86
  keyValue,
87
87
  data: values
@@ -89,58 +89,58 @@ export var ExecutionQuery;
89
89
  Object.assign(out, _.omit(options, Object.keys(out)));
90
90
  return out;
91
91
  }
92
- ExecutionQuery.forUpdate = forUpdate;
92
+ OpraQuery.forUpdate = forUpdate;
93
93
  function forUpdateMany(resource, values, options) {
94
94
  const out = {
95
95
  queryType: 'updateMany',
96
96
  scope: 'collection',
97
- operationType: 'update',
97
+ operation: 'update',
98
98
  resource,
99
99
  data: values
100
100
  };
101
101
  Object.assign(out, _.omit(options, Object.keys(out)));
102
102
  return out;
103
103
  }
104
- ExecutionQuery.forUpdateMany = forUpdateMany;
104
+ OpraQuery.forUpdateMany = forUpdateMany;
105
105
  function forDelete(resource, key) {
106
106
  checkKeyFields(resource, key);
107
107
  return {
108
108
  queryType: 'delete',
109
109
  scope: 'instance',
110
- operationType: 'delete',
110
+ operation: 'delete',
111
111
  resource,
112
112
  keyValue: key
113
113
  };
114
114
  }
115
- ExecutionQuery.forDelete = forDelete;
115
+ OpraQuery.forDelete = forDelete;
116
116
  function forDeleteMany(resource, options) {
117
117
  const out = {
118
118
  queryType: 'deleteMany',
119
119
  scope: 'collection',
120
- operationType: 'delete',
120
+ operation: 'delete',
121
121
  resource,
122
122
  };
123
123
  Object.assign(out, _.omit(options, Object.keys(out)));
124
124
  return out;
125
125
  }
126
- ExecutionQuery.forDeleteMany = forDeleteMany;
126
+ OpraQuery.forDeleteMany = forDeleteMany;
127
127
  function isCreateQuery(q) {
128
128
  return q && typeof q === 'object' && q.scope === 'collection' && q.queryType === 'create';
129
129
  }
130
- ExecutionQuery.isCreateQuery = isCreateQuery;
130
+ OpraQuery.isCreateQuery = isCreateQuery;
131
131
  function isSearchQuery(q) {
132
132
  return q && typeof q === 'object' && q.scope === 'collection' && q.queryType === 'search';
133
133
  }
134
- ExecutionQuery.isSearchQuery = isSearchQuery;
134
+ OpraQuery.isSearchQuery = isSearchQuery;
135
135
  function isReadQuery(q) {
136
136
  return q && typeof q === 'object' && q.scope === 'instance' && q.queryType === 'read';
137
137
  }
138
- ExecutionQuery.isReadQuery = isReadQuery;
138
+ OpraQuery.isReadQuery = isReadQuery;
139
139
  function isDeleteQuery(q) {
140
140
  return q && typeof q === 'object' && q.scope === 'instance' && q.queryType === 'delete';
141
141
  }
142
- ExecutionQuery.isDeleteQuery = isDeleteQuery;
143
- })(ExecutionQuery || (ExecutionQuery = {}));
142
+ OpraQuery.isDeleteQuery = isDeleteQuery;
143
+ })(OpraQuery || (OpraQuery = {}));
144
144
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
145
145
  function checkKeyFields(resource, key) {
146
146
  if (!resource.dataType.primaryKey)
@@ -1,17 +1,17 @@
1
1
  import { Maybe } from 'ts-gems';
2
2
  import { EntityOutput } from '@sqb/connect';
3
- import { ExecutionContext } from '../implementation/execution-context.js';
3
+ import { QueryContext } from '../implementation/query-context.js';
4
4
  export interface IEntityService {
5
- processRequest(ctx: ExecutionContext): any;
5
+ processRequest(ctx: QueryContext): any;
6
6
  }
7
7
  export declare abstract class EntityResourceController<T> {
8
- search(ctx: ExecutionContext): Promise<Maybe<EntityOutput<T>>[]>;
9
- get(ctx: ExecutionContext): Promise<Maybe<EntityOutput<T>>>;
10
- count(ctx: ExecutionContext): Promise<Maybe<number>>;
11
- create(ctx: ExecutionContext): Promise<Maybe<EntityOutput<T>>>;
12
- update(ctx: ExecutionContext): Promise<Maybe<EntityOutput<T>>>;
13
- updateMany(ctx: ExecutionContext): Promise<Maybe<number>>;
14
- delete(ctx: ExecutionContext): Promise<Maybe<boolean | number>>;
15
- deleteMany(ctx: ExecutionContext): Promise<Maybe<number>>;
16
- abstract getService(ctx: ExecutionContext): IEntityService | Promise<IEntityService>;
8
+ search(ctx: QueryContext): Promise<Maybe<EntityOutput<T>>[]>;
9
+ get(ctx: QueryContext): Promise<Maybe<EntityOutput<T>>>;
10
+ count(ctx: QueryContext): Promise<Maybe<number>>;
11
+ create(ctx: QueryContext): Promise<Maybe<EntityOutput<T>>>;
12
+ update(ctx: QueryContext): Promise<Maybe<EntityOutput<T>>>;
13
+ updateMany(ctx: QueryContext): Promise<Maybe<number>>;
14
+ delete(ctx: QueryContext): Promise<Maybe<boolean | number>>;
15
+ deleteMany(ctx: QueryContext): Promise<Maybe<number>>;
16
+ abstract getService(ctx: QueryContext): IEntityService | Promise<IEntityService>;
17
17
  }
package/esm/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { DeepPartial, DeepPickJson, DeepPickWritable, Type } from 'ts-gems';
1
+ import type { Type } from 'ts-gems';
2
2
  export declare type Thunk<T> = T | (() => T);
3
3
  export declare type ThunkAsync<T> = T | Promise<T> | (() => T) | (() => Promise<T>);
4
4
  export declare type TypeThunk<T = any> = Thunk<Type<T>>;
@@ -7,5 +7,3 @@ export declare type QueryScope = 'collection' | 'instance' | 'property';
7
7
  export declare type QueryType = 'create' | 'get' | 'update' | 'patch' | 'delete' | 'execute' | 'search' | 'updateMany' | 'patchMany' | 'deleteMany';
8
8
  export declare type OperationType = 'create' | 'read' | 'update' | 'patch' | 'delete' | 'execute';
9
9
  export declare type KeyValue = string | number | boolean | object;
10
- export declare type PartialInput<T> = DeepPartial<DeepPickWritable<DeepPickJson<T>>>;
11
- export declare type PartialOutput<T> = DeepPartial<DeepPickJson<T>>;
@@ -0,0 +1,3 @@
1
+ import { I18n } from '@opra/i18n';
2
+ import { OpraAdapter } from '../implementation/adapter/adapter.js';
3
+ export declare function createI18n(options?: OpraAdapter.I18nOptions): Promise<I18n>;
@@ -0,0 +1,16 @@
1
+ import path from 'path';
2
+ import { I18n } from '@opra/i18n';
3
+ import { getCallerFile } from './get-caller-file.util.js';
4
+ export async function createI18n(options) {
5
+ const opts = {
6
+ ...options,
7
+ resourceDirs: undefined
8
+ };
9
+ const instance = I18n.createInstance(opts);
10
+ await instance.init();
11
+ await instance.loadResourceDir(path.resolve(getCallerFile(), '../../../i18n'));
12
+ if (options?.resourceDirs)
13
+ for (const dir of options.resourceDirs)
14
+ await instance.loadResourceDir(dir);
15
+ return instance;
16
+ }
@@ -0,0 +1 @@
1
+ export declare function getCallerFile(position?: number): any;
@@ -0,0 +1,15 @@
1
+ export function getCallerFile(position = 1) {
2
+ if (position >= Error.stackTraceLimit) {
3
+ throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' +
4
+ position + '` and Error.stackTraceLimit was: `' + Error.stackTraceLimit + '`');
5
+ }
6
+ const oldPrepareStackTrace = Error.prepareStackTrace;
7
+ Error.prepareStackTrace = (_, stack) => stack;
8
+ const stack = new Error().stack;
9
+ Error.prepareStackTrace = oldPrepareStackTrace;
10
+ if (stack !== null && typeof stack === 'object') {
11
+ // stack[0] holds this file
12
+ // stack[1] holds where this function was called
13
+ return stack[position] ? stack[position].getFileName() : undefined;
14
+ }
15
+ }
@@ -1,4 +1,5 @@
1
+ import { Type } from 'ts-gems';
1
2
  import { OpraSchema } from '@opra/schema';
2
- export declare const builtinClassMap: Map<any, any>;
3
3
  export declare const primitiveDataTypeNames: string[];
4
+ export declare const builtinClassMap: Map<Type<any>, OpraSchema.DataType>;
4
5
  export declare const internalDataTypes: Map<string, OpraSchema.DataType>;
@@ -1,41 +1,78 @@
1
+ export const primitiveDataTypeNames = ['boolean', 'number', 'string', 'null'];
1
2
  export const builtinClassMap = new Map();
2
- builtinClassMap.set(Boolean, 'boolean');
3
- builtinClassMap.set(Number, 'number');
4
- builtinClassMap.set(String, 'string');
5
- builtinClassMap.set(Buffer, 'buffer');
6
- export const primitiveDataTypeNames = ['boolean', 'number', 'integer', 'string'];
7
3
  export const internalDataTypes = new Map();
8
4
  const internalDataTypeArray = [
9
5
  {
10
6
  kind: 'SimpleType',
11
7
  name: 'boolean',
12
8
  type: 'boolean',
13
- description: 'Simple true/false value'
9
+ description: 'Simple true/false value',
10
+ ctor: Boolean
11
+ },
12
+ {
13
+ kind: 'SimpleType',
14
+ name: 'string',
15
+ type: 'string',
16
+ description: 'A sequence of characters',
17
+ ctor: String
14
18
  },
15
19
  {
16
20
  kind: 'SimpleType',
17
21
  name: 'number',
18
22
  type: 'number',
19
- description: 'Both Integer as well as Floating-Point numbers'
23
+ description: 'Both Integer as well as Floating-Point numbers',
24
+ ctor: Number
20
25
  },
21
26
  {
22
27
  kind: 'SimpleType',
23
- name: 'string',
24
- type: 'string',
25
- description: 'A sequence of characters'
28
+ name: 'integer',
29
+ type: 'number',
30
+ base: 'number',
31
+ description: 'Integer number',
32
+ ctor: Number
26
33
  },
27
34
  {
28
35
  kind: 'ComplexType',
29
36
  name: 'object',
30
37
  description: 'Object type with additional properties',
31
- additionalProperties: true
38
+ additionalProperties: true,
39
+ ctor: Object
32
40
  },
33
41
  {
34
42
  kind: 'SimpleType',
35
- name: 'integer',
36
- type: 'number',
37
- base: 'number',
38
- description: 'Integer number'
43
+ name: 'date',
44
+ type: 'string',
45
+ description: 'Date',
46
+ format: 'date',
47
+ ctor: Date
48
+ },
49
+ {
50
+ kind: 'SimpleType',
51
+ name: 'date-time',
52
+ type: 'string',
53
+ description: 'Date time value',
54
+ format: 'date-time',
55
+ ctor: Date
56
+ },
57
+ {
58
+ kind: 'SimpleType',
59
+ name: 'time',
60
+ type: 'string',
61
+ description: 'Time value',
62
+ format: 'time',
63
+ ctor: String
64
+ },
65
+ {
66
+ kind: 'SimpleType',
67
+ name: 'buffer',
68
+ type: 'string',
69
+ description: 'Buffer value',
70
+ format: 'base64',
71
+ ctor: Buffer
39
72
  }
40
73
  ];
41
- internalDataTypeArray.forEach(sch => internalDataTypes.set(sch.name, sch));
74
+ internalDataTypeArray.forEach(sch => {
75
+ internalDataTypes.set(sch.name, sch);
76
+ if (sch.ctor && !builtinClassMap.has(sch.ctor))
77
+ builtinClassMap.set(sch.ctor, sch);
78
+ });
@@ -0,0 +1,12 @@
1
+ {
2
+ "BAD_REQUEST": "Bad request",
3
+ "FAILED_DEPENDENCY": "The request failed due to failure of a previous request",
4
+ "FORBIDDEN": "You are not authorized to perform this action",
5
+ "INTERNAL_SERVER_ERROR": "Internal server error",
6
+ "METHOD_NOT_ALLOWED": "Method Not Allowed",
7
+ "NOT_FOUND": "Not found",
8
+ "UNAUTHORIZED": "You have not been authenticated to perform this action",
9
+ "UNPROCESSABLE_ENTITY": "Unprocessable entity",
10
+
11
+ "RESOURCE_CONFLICT": "There is already an other {{resource}} resource with same field values ({{fields}})"
12
+ }