@opra/core 0.0.8 → 0.0.9

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 (69) hide show
  1. package/cjs/exception/api-exception.js +30 -33
  2. package/cjs/exception/errors/bad-request.error.js +6 -3
  3. package/cjs/exception/errors/failed-dependency.error.js +6 -3
  4. package/cjs/exception/errors/forbidden.error.js +6 -3
  5. package/cjs/exception/errors/internal-server.error.js +9 -4
  6. package/cjs/exception/errors/method-not-allowed.error.js +6 -3
  7. package/cjs/exception/errors/not-found.error.js +6 -3
  8. package/cjs/exception/errors/unauthorized.error.js +6 -3
  9. package/cjs/exception/errors/unprocessable-entity.error.js +6 -3
  10. package/cjs/exception/wrap-error.js +1 -1
  11. package/cjs/implementation/adapter/adapter.js +29 -26
  12. package/cjs/implementation/adapter/express-adapter.js +40 -9
  13. package/cjs/implementation/adapter/http-adapter.js +27 -34
  14. package/cjs/implementation/{execution-context.js → query-context.js} +18 -22
  15. package/cjs/implementation/resource/entity-resource-handler.js +16 -12
  16. package/cjs/implementation/resource/resource-handler.js +1 -1
  17. package/cjs/implementation/schema-generator.js +38 -51
  18. package/cjs/index.js +3 -4
  19. package/cjs/interfaces/{http-context.interface.js → execution-context.interface.js} +0 -0
  20. package/cjs/interfaces/{execution-query.interface.js → query.interface.js} +16 -16
  21. package/cjs/utils/internal-data-types.js +54 -17
  22. package/esm/exception/api-exception.d.ts +3 -2
  23. package/esm/exception/api-exception.js +30 -33
  24. package/esm/exception/errors/bad-request.error.d.ts +2 -1
  25. package/esm/exception/errors/bad-request.error.js +6 -3
  26. package/esm/exception/errors/failed-dependency.error.d.ts +2 -1
  27. package/esm/exception/errors/failed-dependency.error.js +6 -3
  28. package/esm/exception/errors/forbidden.error.d.ts +2 -1
  29. package/esm/exception/errors/forbidden.error.js +6 -3
  30. package/esm/exception/errors/internal-server.error.d.ts +2 -1
  31. package/esm/exception/errors/internal-server.error.js +8 -4
  32. package/esm/exception/errors/method-not-allowed.error.d.ts +2 -1
  33. package/esm/exception/errors/method-not-allowed.error.js +6 -3
  34. package/esm/exception/errors/not-found.error.d.ts +2 -1
  35. package/esm/exception/errors/not-found.error.js +6 -3
  36. package/esm/exception/errors/unauthorized.error.d.ts +2 -1
  37. package/esm/exception/errors/unauthorized.error.js +6 -3
  38. package/esm/exception/errors/unprocessable-entity.error.d.ts +2 -1
  39. package/esm/exception/errors/unprocessable-entity.error.js +6 -3
  40. package/esm/exception/wrap-error.js +1 -1
  41. package/esm/implementation/adapter/adapter.d.ts +22 -14
  42. package/esm/implementation/adapter/adapter.js +29 -26
  43. package/esm/implementation/adapter/express-adapter.d.ts +2 -2
  44. package/esm/implementation/adapter/express-adapter.js +40 -9
  45. package/esm/implementation/adapter/http-adapter.d.ts +11 -12
  46. package/esm/implementation/adapter/http-adapter.js +27 -34
  47. package/esm/implementation/query-context.d.ts +32 -0
  48. package/esm/implementation/{execution-context.js → query-context.js} +15 -18
  49. package/esm/implementation/resource/container-resource-handler.d.ts +2 -2
  50. package/esm/implementation/resource/entity-resource-handler.d.ts +3 -3
  51. package/esm/implementation/resource/entity-resource-handler.js +16 -12
  52. package/esm/implementation/resource/resource-handler.d.ts +3 -3
  53. package/esm/implementation/resource/resource-handler.js +1 -1
  54. package/esm/implementation/schema-generator.js +38 -51
  55. package/esm/index.d.ts +3 -4
  56. package/esm/index.js +3 -4
  57. package/esm/interfaces/execution-context.interface.d.ts +39 -0
  58. package/esm/interfaces/{http-context.interface.js → execution-context.interface.js} +0 -0
  59. package/esm/interfaces/{execution-query.interface.d.ts → query.interface.d.ts} +31 -25
  60. package/esm/interfaces/{execution-query.interface.js → query.interface.js} +15 -15
  61. package/esm/services/entity-resource-controller.d.ts +11 -11
  62. package/esm/utils/internal-data-types.d.ts +2 -1
  63. package/esm/utils/internal-data-types.js +53 -16
  64. package/package.json +6 -5
  65. package/cjs/interfaces/user-context.interface.js +0 -2
  66. package/esm/implementation/execution-context.d.ts +0 -42
  67. package/esm/interfaces/http-context.interface.d.ts +0 -23
  68. package/esm/interfaces/user-context.interface.d.ts +0 -3
  69. package/esm/interfaces/user-context.interface.js +0 -1
@@ -1,41 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ExecutionResponse = exports.ExecutionRequest = exports.ExecutionContext = void 0;
3
+ exports.QueryResponse = exports.QueryContext = void 0;
4
4
  const url_1 = require("@opra/url");
5
5
  const headers_js_1 = require("../utils/headers.js");
6
- class ExecutionContext {
7
- type;
6
+ class QueryContext {
8
7
  service;
9
- request;
10
- response;
11
- adapterContext;
12
- userContext;
13
- continueOnError;
14
- constructor(args) {
15
- Object.assign(this, args);
16
- }
17
- switchToHttp() {
18
- if (this.type !== 'http')
19
- throw new Error(`You can't access http context within an ${this.type} context`);
20
- return this.adapterContext;
21
- }
22
- }
23
- exports.ExecutionContext = ExecutionContext;
24
- class ExecutionRequest {
8
+ executionContext;
25
9
  query;
26
10
  params;
27
11
  headers;
28
12
  parentValue;
29
13
  resultPath;
14
+ response;
15
+ userContext;
16
+ continueOnError;
30
17
  constructor(args) {
31
18
  Object.assign(this, args);
19
+ this.response = new QueryResponse();
32
20
  this.params = this.params || new url_1.OpraURLSearchParams();
33
21
  this.headers = this.headers || headers_js_1.Headers.create();
34
22
  this.resultPath = this.resultPath || '';
35
23
  }
24
+ get type() {
25
+ return this.executionContext.getType();
26
+ }
27
+ switchToHttp() {
28
+ if (this.type !== 'http')
29
+ throw new Error(`You can't access http context within an ${this.type} context`);
30
+ return this.executionContext;
31
+ }
36
32
  }
37
- exports.ExecutionRequest = ExecutionRequest;
38
- class ExecutionResponse {
33
+ exports.QueryContext = QueryContext;
34
+ class QueryResponse {
39
35
  headers = headers_js_1.Headers.create();
40
36
  errors = [];
41
37
  status;
@@ -46,4 +42,4 @@ class ExecutionResponse {
46
42
  Object.assign(this, args);
47
43
  }
48
44
  }
49
- exports.ExecutionResponse = ExecutionResponse;
45
+ exports.QueryResponse = QueryResponse;
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EntityResourceHandler = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
6
- const execution_query_interface_js_1 = require("../../interfaces/execution-query.interface.js");
6
+ const query_interface_js_1 = require("../../interfaces/query.interface.js");
7
7
  const entity_type_js_1 = require("../data-type/entity-type.js");
8
8
  const resource_handler_js_1 = require("./resource-handler.js");
9
- var isSearchQuery = execution_query_interface_js_1.ExecutionQuery.isSearchQuery;
9
+ var isSearchQuery = query_interface_js_1.OpraQuery.isSearchQuery;
10
+ const index_js_1 = require("../../exception/index.js");
10
11
  class EntityResourceHandler extends resource_handler_js_1.ResourceHandler {
11
12
  service;
12
13
  dataType;
@@ -22,7 +23,7 @@ class EntityResourceHandler extends resource_handler_js_1.ResourceHandler {
22
23
  throw new TypeError(`You should provide an EntityType for EntityResourceController`);
23
24
  }
24
25
  async execute(ctx) {
25
- const { query } = ctx.request;
26
+ const { query } = ctx;
26
27
  if (isSearchQuery(query)) {
27
28
  const promises = [];
28
29
  let search;
@@ -61,16 +62,19 @@ class EntityResourceHandler extends resource_handler_js_1.ResourceHandler {
61
62
  if (typeof result === 'object')
62
63
  affectedRecords = result.affectedRows || result.affectedRecords;
63
64
  return { affectedRecords };
64
- default:
65
- result = Array.isArray(result) ? result[0] : result;
66
- if (result && ctx.request.resultPath) {
67
- const pathArray = ctx.request.resultPath.split('.');
68
- for (const property of pathArray) {
69
- result = result && typeof result === 'object' && result[property];
70
- }
71
- }
72
- return result;
73
65
  }
66
+ result = Array.isArray(result) ? result[0] : result;
67
+ if (!result)
68
+ throw new index_js_1.UnprocessableEntityError();
69
+ if (ctx.resultPath) {
70
+ const pathArray = ctx.resultPath.split('.');
71
+ for (const property of pathArray) {
72
+ result = result && typeof result === 'object' && result[property];
73
+ }
74
+ }
75
+ if (fnName === 'create')
76
+ ctx.response.status = 201;
77
+ return result;
74
78
  }
75
79
  }
76
80
  exports.EntityResourceHandler = EntityResourceHandler;
@@ -17,7 +17,7 @@ class ResourceHandler {
17
17
  return `[${Object.getPrototypeOf(this).constructor.name} ${this.name}]`;
18
18
  }
19
19
  async prepare(ctx) {
20
- const { query } = ctx.request;
20
+ const { query } = ctx;
21
21
  const fn = this._args['pre_' + query.queryType];
22
22
  if (fn && typeof fn === 'function') {
23
23
  await fn(ctx);
@@ -6,6 +6,7 @@ const schema_1 = require("@opra/schema");
6
6
  const constants_js_1 = require("../constants.js");
7
7
  const class_utils_js_1 = require("../utils/class-utils.js");
8
8
  const internal_data_types_js_1 = require("../utils/internal-data-types.js");
9
+ var isDataType = schema_1.OpraSchema.isDataType;
9
10
  const entityMethods = ['search', 'count', 'create', 'get', 'update', 'updateMany', 'delete', 'deleteMany'];
10
11
  class SchemaGenerator {
11
12
  _dataTypes = {};
@@ -14,24 +15,16 @@ class SchemaGenerator {
14
15
  //
15
16
  }
16
17
  async addDataType(thunk) {
17
- let schema;
18
18
  thunk = (0, types_1.isPromise)(thunk) ? await thunk : thunk;
19
- if (typeof thunk === 'function' && !(0, class_utils_js_1.isConstructor)(thunk))
20
- thunk = await thunk();
21
- if (!(0, class_utils_js_1.isConstructor)(thunk) || schema_1.OpraSchema.isDataType(thunk))
22
- throw new TypeError(`Function must return a type class or type schema`);
23
- if ((0, class_utils_js_1.isConstructor)(thunk)) {
19
+ if (typeof thunk === 'function') {
20
+ if (!(0, class_utils_js_1.isConstructor)(thunk))
21
+ return this.addDataType(await thunk());
22
+ if (internal_data_types_js_1.builtinClassMap.has(thunk))
23
+ return this.addDataType(internal_data_types_js_1.builtinClassMap.get(thunk));
24
24
  const ctor = thunk;
25
25
  const metadata = Reflect.getOwnMetadata(schema_1.DATATYPE_METADATA, ctor);
26
26
  if (!metadata)
27
27
  throw new TypeError(`Class "${ctor}" has no type metadata`);
28
- schema = this._dataTypes[metadata.name];
29
- if (schema) {
30
- if (schema.kind !== metadata.kind ||
31
- (schema_1.OpraSchema.isComplexType(schema) && schema.ctor !== ctor))
32
- throw new Error(`An other instance of "${schema.name}" data type previously defined`);
33
- return schema;
34
- }
35
28
  // Add base data type
36
29
  let base;
37
30
  let baseCtor = Object.getPrototypeOf(ctor);
@@ -42,49 +35,43 @@ class SchemaGenerator {
42
35
  const baseSchema = await this.addDataType(baseCtor);
43
36
  base = baseSchema.name;
44
37
  }
45
- if (schema_1.OpraSchema.isComplexType(metadata) || schema_1.OpraSchema.isEntityType(metadata)) {
46
- schema = {
47
- ...metadata,
48
- ctor,
49
- base
50
- };
51
- const properties = Reflect.getMetadata(schema_1.DATATYPE_PROPERTIES, ctor.prototype);
52
- if (properties) {
53
- for (const [k, p] of Object.entries(properties)) {
54
- let type;
55
- if ((0, class_utils_js_1.isConstructor)(p.type) && internal_data_types_js_1.builtinClassMap.has(p.type))
56
- type = internal_data_types_js_1.builtinClassMap.get(p.type);
57
- else if (typeof p.type === 'function' || typeof p.type === 'object') {
58
- const t = await this.addDataType(p.type);
59
- type = t.name;
60
- }
61
- else
62
- type = p.type || 'string';
63
- if (internal_data_types_js_1.internalDataTypes.has(type) && !this._dataTypes[type])
64
- this._dataTypes[type] = internal_data_types_js_1.internalDataTypes.get(type.toLowerCase());
65
- schema.properties = schema.properties || {};
66
- schema.properties[k] = { ...p, type };
38
+ const schema = {
39
+ ...metadata,
40
+ ctor,
41
+ base
42
+ };
43
+ const properties = Reflect.getMetadata(schema_1.DATATYPE_PROPERTIES, ctor.prototype);
44
+ if (properties) {
45
+ for (const [k, p] of Object.entries(properties)) {
46
+ let type = p.type || 'string';
47
+ if (typeof type !== 'string') {
48
+ const propSchema = await this.addDataType(type);
49
+ type = propSchema.name;
67
50
  }
51
+ else {
52
+ if (internal_data_types_js_1.internalDataTypes.has(type))
53
+ await this.addDataType(internal_data_types_js_1.internalDataTypes.get(type));
54
+ }
55
+ schema.properties = schema.properties || {};
56
+ schema.properties[k] = { ...p, type };
68
57
  }
69
58
  }
70
- else if (schema_1.OpraSchema.isSimpleType(metadata)) {
71
- if (!internal_data_types_js_1.primitiveDataTypeNames.includes(metadata.type))
72
- throw new Error(`"type" of SimpleType schema must be one of enumerated value (${internal_data_types_js_1.primitiveDataTypeNames})`);
73
- schema = {
74
- ...metadata
75
- };
76
- }
77
- else
78
- /* istanbul ignore next */
79
- throw new TypeError(`Invalid metadata`);
59
+ return this.addDataType(schema);
80
60
  }
81
- else if (schema_1.OpraSchema.isDataType(thunk)) {
82
- schema = thunk;
83
- }
84
- else
61
+ if (!isDataType(thunk))
85
62
  throw new TypeError(`Invalid data type schema`);
86
- this._dataTypes[schema.name] = schema;
87
- return schema;
63
+ // Check if datatype previously added
64
+ const currentSchema = this._dataTypes[thunk.name];
65
+ if (currentSchema) {
66
+ if (!(currentSchema.kind === thunk.kind && currentSchema.ctor && currentSchema.ctor === thunk.ctor))
67
+ throw new Error(`An other instance of "${currentSchema.name}" data type previously defined`);
68
+ return currentSchema;
69
+ }
70
+ if (schema_1.OpraSchema.isSimpleType(thunk) && !internal_data_types_js_1.primitiveDataTypeNames.includes(thunk.type))
71
+ throw new Error(`"type" of SimpleType schema must be one of enumerated value (${internal_data_types_js_1.primitiveDataTypeNames})`);
72
+ return this._dataTypes[thunk.name] = {
73
+ ...thunk
74
+ };
88
75
  }
89
76
  async addResource(instance) {
90
77
  if ((0, class_utils_js_1.isConstructor)(instance))
package/cjs/index.js CHANGED
@@ -7,11 +7,10 @@ tslib_1.__exportStar(require("./types.js"), exports);
7
7
  tslib_1.__exportStar(require("./enums/index.js"), exports);
8
8
  tslib_1.__exportStar(require("./exception/index.js"), exports);
9
9
  tslib_1.__exportStar(require("./decorators/entity-resource.decorator.js"), exports);
10
- tslib_1.__exportStar(require("./interfaces/http-context.interface.js"), exports);
11
- tslib_1.__exportStar(require("./interfaces/execution-query.interface.js"), exports);
10
+ tslib_1.__exportStar(require("./interfaces/execution-context.interface.js"), exports);
11
+ tslib_1.__exportStar(require("./interfaces/query.interface.js"), exports);
12
12
  tslib_1.__exportStar(require("./interfaces/resource-container.interface.js"), exports);
13
- tslib_1.__exportStar(require("./interfaces/user-context.interface.js"), exports);
14
- tslib_1.__exportStar(require("./implementation/execution-context.js"), exports);
13
+ tslib_1.__exportStar(require("./implementation/query-context.js"), exports);
15
14
  tslib_1.__exportStar(require("./implementation/opra-document.js"), exports);
16
15
  tslib_1.__exportStar(require("./implementation/opra-service.js"), exports);
17
16
  tslib_1.__exportStar(require("./implementation/schema-generator.js"), exports);
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ExecutionQuery = void 0;
3
+ exports.OpraQuery = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
6
6
  const complex_type_js_1 = require("../implementation/data-type/complex-type.js");
7
7
  const string_path_to_object_tree_js_1 = require("../utils/string-path-to-object-tree.js");
8
- var ExecutionQuery;
9
- (function (ExecutionQuery) {
8
+ var OpraQuery;
9
+ (function (OpraQuery) {
10
10
  function forCreate(resource, values, options) {
11
11
  if (options?.pick)
12
12
  options.pick = normalizePick(resource, options.pick);
@@ -24,7 +24,7 @@ var ExecutionQuery;
24
24
  Object.assign(out, lodash_1.default.omit(options, Object.keys(out)));
25
25
  return out;
26
26
  }
27
- ExecutionQuery.forCreate = forCreate;
27
+ OpraQuery.forCreate = forCreate;
28
28
  function forGet(resource, key, options) {
29
29
  if (options?.pick)
30
30
  options.pick = normalizePick(resource, options.pick);
@@ -43,7 +43,7 @@ var ExecutionQuery;
43
43
  Object.assign(out, lodash_1.default.omit(options, Object.keys(out)));
44
44
  return out;
45
45
  }
46
- ExecutionQuery.forGet = forGet;
46
+ OpraQuery.forGet = forGet;
47
47
  function forSearch(resource, options) {
48
48
  if (options?.pick)
49
49
  options.pick = normalizePick(resource, options.pick);
@@ -62,7 +62,7 @@ var ExecutionQuery;
62
62
  Object.assign(out, lodash_1.default.omit(options, Object.keys(out)));
63
63
  return out;
64
64
  }
65
- ExecutionQuery.forSearch = forSearch;
65
+ OpraQuery.forSearch = forSearch;
66
66
  function forGetProperty(property, options) {
67
67
  const out = {
68
68
  queryType: 'get',
@@ -73,7 +73,7 @@ var ExecutionQuery;
73
73
  Object.assign(out, lodash_1.default.omit(options, Object.keys(out)));
74
74
  return out;
75
75
  }
76
- ExecutionQuery.forGetProperty = forGetProperty;
76
+ OpraQuery.forGetProperty = forGetProperty;
77
77
  function forUpdate(resource, keyValue, values, options) {
78
78
  if (options?.pick)
79
79
  options.pick = normalizePick(resource, options.pick);
@@ -93,7 +93,7 @@ var ExecutionQuery;
93
93
  Object.assign(out, lodash_1.default.omit(options, Object.keys(out)));
94
94
  return out;
95
95
  }
96
- ExecutionQuery.forUpdate = forUpdate;
96
+ OpraQuery.forUpdate = forUpdate;
97
97
  function forUpdateMany(resource, values, options) {
98
98
  const out = {
99
99
  queryType: 'updateMany',
@@ -105,7 +105,7 @@ var ExecutionQuery;
105
105
  Object.assign(out, lodash_1.default.omit(options, Object.keys(out)));
106
106
  return out;
107
107
  }
108
- ExecutionQuery.forUpdateMany = forUpdateMany;
108
+ OpraQuery.forUpdateMany = forUpdateMany;
109
109
  function forDelete(resource, key) {
110
110
  checkKeyFields(resource, key);
111
111
  return {
@@ -116,7 +116,7 @@ var ExecutionQuery;
116
116
  keyValue: key
117
117
  };
118
118
  }
119
- ExecutionQuery.forDelete = forDelete;
119
+ OpraQuery.forDelete = forDelete;
120
120
  function forDeleteMany(resource, options) {
121
121
  const out = {
122
122
  queryType: 'deleteMany',
@@ -127,24 +127,24 @@ var ExecutionQuery;
127
127
  Object.assign(out, lodash_1.default.omit(options, Object.keys(out)));
128
128
  return out;
129
129
  }
130
- ExecutionQuery.forDeleteMany = forDeleteMany;
130
+ OpraQuery.forDeleteMany = forDeleteMany;
131
131
  function isCreateQuery(q) {
132
132
  return q && typeof q === 'object' && q.scope === 'collection' && q.queryType === 'create';
133
133
  }
134
- ExecutionQuery.isCreateQuery = isCreateQuery;
134
+ OpraQuery.isCreateQuery = isCreateQuery;
135
135
  function isSearchQuery(q) {
136
136
  return q && typeof q === 'object' && q.scope === 'collection' && q.queryType === 'search';
137
137
  }
138
- ExecutionQuery.isSearchQuery = isSearchQuery;
138
+ OpraQuery.isSearchQuery = isSearchQuery;
139
139
  function isReadQuery(q) {
140
140
  return q && typeof q === 'object' && q.scope === 'instance' && q.queryType === 'read';
141
141
  }
142
- ExecutionQuery.isReadQuery = isReadQuery;
142
+ OpraQuery.isReadQuery = isReadQuery;
143
143
  function isDeleteQuery(q) {
144
144
  return q && typeof q === 'object' && q.scope === 'instance' && q.queryType === 'delete';
145
145
  }
146
- ExecutionQuery.isDeleteQuery = isDeleteQuery;
147
- })(ExecutionQuery = exports.ExecutionQuery || (exports.ExecutionQuery = {}));
146
+ OpraQuery.isDeleteQuery = isDeleteQuery;
147
+ })(OpraQuery = exports.OpraQuery || (exports.OpraQuery = {}));
148
148
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
149
149
  function checkKeyFields(resource, key) {
150
150
  if (!resource.dataType.primaryKey)
@@ -1,44 +1,81 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.internalDataTypes = exports.primitiveDataTypeNames = exports.builtinClassMap = void 0;
3
+ exports.internalDataTypes = exports.builtinClassMap = exports.primitiveDataTypeNames = void 0;
4
+ exports.primitiveDataTypeNames = ['boolean', 'number', 'string', 'null'];
4
5
  exports.builtinClassMap = new Map();
5
- exports.builtinClassMap.set(Boolean, 'boolean');
6
- exports.builtinClassMap.set(Number, 'number');
7
- exports.builtinClassMap.set(String, 'string');
8
- exports.builtinClassMap.set(Buffer, 'buffer');
9
- exports.primitiveDataTypeNames = ['boolean', 'number', 'integer', 'string'];
10
6
  exports.internalDataTypes = new Map();
11
7
  const internalDataTypeArray = [
12
8
  {
13
9
  kind: 'SimpleType',
14
10
  name: 'boolean',
15
11
  type: 'boolean',
16
- description: 'Simple true/false value'
12
+ description: 'Simple true/false value',
13
+ ctor: Boolean
14
+ },
15
+ {
16
+ kind: 'SimpleType',
17
+ name: 'string',
18
+ type: 'string',
19
+ description: 'A sequence of characters',
20
+ ctor: String
17
21
  },
18
22
  {
19
23
  kind: 'SimpleType',
20
24
  name: 'number',
21
25
  type: 'number',
22
- description: 'Both Integer as well as Floating-Point numbers'
26
+ description: 'Both Integer as well as Floating-Point numbers',
27
+ ctor: Number
23
28
  },
24
29
  {
25
30
  kind: 'SimpleType',
26
- name: 'string',
27
- type: 'string',
28
- description: 'A sequence of characters'
31
+ name: 'integer',
32
+ type: 'number',
33
+ base: 'number',
34
+ description: 'Integer number',
35
+ ctor: Number
29
36
  },
30
37
  {
31
38
  kind: 'ComplexType',
32
39
  name: 'object',
33
40
  description: 'Object type with additional properties',
34
- additionalProperties: true
41
+ additionalProperties: true,
42
+ ctor: Object
35
43
  },
36
44
  {
37
45
  kind: 'SimpleType',
38
- name: 'integer',
39
- type: 'number',
40
- base: 'number',
41
- description: 'Integer number'
46
+ name: 'date',
47
+ type: 'string',
48
+ description: 'Date',
49
+ format: 'date',
50
+ ctor: Date
51
+ },
52
+ {
53
+ kind: 'SimpleType',
54
+ name: 'date-time',
55
+ type: 'string',
56
+ description: 'Date time value',
57
+ format: 'date-time',
58
+ ctor: Date
59
+ },
60
+ {
61
+ kind: 'SimpleType',
62
+ name: 'time',
63
+ type: 'string',
64
+ description: 'Time value',
65
+ format: 'time',
66
+ ctor: String
67
+ },
68
+ {
69
+ kind: 'SimpleType',
70
+ name: 'buffer',
71
+ type: 'string',
72
+ description: 'Buffer value',
73
+ format: 'base64',
74
+ ctor: Buffer
42
75
  }
43
76
  ];
44
- internalDataTypeArray.forEach(sch => exports.internalDataTypes.set(sch.name, sch));
77
+ internalDataTypeArray.forEach(sch => {
78
+ exports.internalDataTypes.set(sch.name, sch);
79
+ if (sch.ctor && !exports.builtinClassMap.has(sch.ctor))
80
+ exports.builtinClassMap.set(sch.ctor, sch);
81
+ });
@@ -31,9 +31,10 @@ export declare class ApiException extends Error {
31
31
  response: ErrorResponse;
32
32
  status: number;
33
33
  cause?: Error;
34
- constructor(response: string | ErrorResponse | Error, cause?: Error);
34
+ constructor(response?: string | ErrorResponse | Error, cause?: Error);
35
35
  setStatus(status: number | HttpStatus): this;
36
36
  protected _initName(): void;
37
- protected _initResponse(init: string | ErrorResponse | Error): void;
37
+ protected _initErrorInstance(init: Error): void;
38
+ protected _initResponse(response?: Partial<ErrorResponse>): void;
38
39
  static wrap(response: string | ErrorResponse | Error): ApiException;
39
40
  }
@@ -10,52 +10,49 @@ export class ApiException extends Error {
10
10
  constructor(response, cause) {
11
11
  super('');
12
12
  this._initName();
13
+ this.status = HttpStatus.INTERNAL_SERVER_ERROR;
14
+ cause = cause || (response instanceof Error ? response : undefined);
13
15
  if (cause)
14
16
  Object.defineProperty(this, 'cause', { enumerable: false, configurable: true, writable: true, value: cause });
15
- else if (response instanceof Error)
16
- Object.defineProperty(this, 'cause', { enumerable: false, configurable: true, writable: true, value: response });
17
- this.status = HttpStatus.INTERNAL_SERVER_ERROR;
18
- this._initResponse(response);
19
- }
20
- setStatus(status) {
21
- this.status = status;
22
- return this;
23
- }
24
- _initName() {
25
- this.name = this.constructor.name;
26
- }
27
- _initResponse(init) {
28
- if (init && typeof init === 'object') {
29
- const x = init;
30
- if (typeof x.status === 'number')
31
- this.setStatus(x.status);
32
- else if (typeof x.getStatus === 'function')
33
- this.setStatus(x.getStatus());
34
- }
35
- if (typeof init === 'object') {
36
- const x = init;
37
- this.response = {
38
- message: x.message || x.details || ('' + init),
39
- };
40
- if (!(init instanceof Error))
41
- Object.assign(this.response, init);
42
- }
43
- else {
44
- this.response = {
45
- message: '' + init,
46
- };
47
- }
48
17
  if (this.cause instanceof Error && this.cause.stack) {
49
18
  if (ApiException.stackAsDiagnostics)
50
19
  this.response.diagnostics = this.cause.stack.split('\n');
51
20
  this.stack = this.cause.stack;
52
21
  }
22
+ if (response instanceof Error)
23
+ this._initErrorInstance(response);
24
+ else if (typeof response === 'string')
25
+ this._initResponse({ message: response });
26
+ else
27
+ this._initResponse(response);
53
28
  if (!this.response.severity)
54
29
  if (this.status >= 500)
55
30
  this.response.severity = 'fatal';
56
31
  else
57
32
  this.response.severity = 'error';
58
33
  }
34
+ setStatus(status) {
35
+ this.status = status;
36
+ return this;
37
+ }
38
+ _initName() {
39
+ this.name = this.constructor.name;
40
+ }
41
+ _initErrorInstance(init) {
42
+ this._initResponse({
43
+ message: init.message
44
+ });
45
+ if (typeof init.status === 'number')
46
+ this.setStatus(init.status);
47
+ else if (typeof init.getStatus === 'function')
48
+ this.setStatus(init.getStatus());
49
+ }
50
+ _initResponse(response) {
51
+ this.response = {
52
+ message: 'Unknown error',
53
+ ...response
54
+ };
55
+ }
59
56
  static wrap(response) {
60
57
  if (response instanceof ApiException)
61
58
  return response;
@@ -5,5 +5,6 @@ import { ApiException, ErrorResponse } from '../api-exception.js';
5
5
  * (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
6
6
  */
7
7
  export declare class BadRequestError extends ApiException {
8
- constructor(response?: ErrorResponse, cause?: Error);
8
+ constructor(response?: string | ErrorResponse | Error, cause?: Error);
9
+ protected _initResponse(response: Partial<ErrorResponse>): void;
9
10
  }
@@ -8,12 +8,15 @@ import { ApiException } from '../api-exception.js';
8
8
  */
9
9
  export class BadRequestError extends ApiException {
10
10
  constructor(response, cause) {
11
- super({
11
+ super(response, cause);
12
+ this.status = HttpStatus.BAD_REQUEST;
13
+ }
14
+ _initResponse(response) {
15
+ super._initResponse({
12
16
  message: translate('error:BAD_REQUEST', 'Bad request'),
13
17
  severity: 'error',
14
18
  code: 'BAD_REQUEST',
15
19
  ...response
16
- }, cause);
17
- this.status = HttpStatus.BAD_REQUEST;
20
+ });
18
21
  }
19
22
  }
@@ -4,5 +4,6 @@ import { ApiException, ErrorResponse } from '../api-exception.js';
4
4
  * The request failed due to failure of a previous request.
5
5
  */
6
6
  export declare class FailedDependencyError extends ApiException {
7
- constructor(response?: ErrorResponse, cause?: Error);
7
+ constructor(response?: string | ErrorResponse | Error, cause?: Error);
8
+ protected _initResponse(response: Partial<ErrorResponse>): void;
8
9
  }
@@ -7,12 +7,15 @@ import { ApiException } from '../api-exception.js';
7
7
  */
8
8
  export class FailedDependencyError extends ApiException {
9
9
  constructor(response, cause) {
10
- super({
10
+ super(response, cause);
11
+ this.status = HttpStatus.FAILED_DEPENDENCY;
12
+ }
13
+ _initResponse(response) {
14
+ super._initResponse({
11
15
  message: translate('error:FAILED_DEPENDENCY', 'The request failed due to failure of a previous request.'),
12
16
  severity: 'error',
13
17
  code: 'FAILED_DEPENDENCY',
14
18
  ...response
15
- }, cause);
16
- this.status = HttpStatus.FAILED_DEPENDENCY;
19
+ });
17
20
  }
18
21
  }
@@ -6,5 +6,6 @@ import { ApiException, ErrorResponse } from '../api-exception.js';
6
6
  * the client's identity is known to the server.
7
7
  */
8
8
  export declare class ForbiddenError extends ApiException {
9
- constructor(response?: ErrorResponse, cause?: Error);
9
+ constructor(response?: string | ErrorResponse | Error, cause?: Error);
10
+ protected _initResponse(response: Partial<ErrorResponse>): void;
10
11
  }
@@ -9,12 +9,15 @@ import { ApiException } from '../api-exception.js';
9
9
  */
10
10
  export class ForbiddenError extends ApiException {
11
11
  constructor(response, cause) {
12
- super({
12
+ super(response, cause);
13
+ this.status = HttpStatus.FORBIDDEN;
14
+ }
15
+ _initResponse(response) {
16
+ super._initResponse({
13
17
  message: translate('error:FORBIDDEN', 'Forbidden'),
14
18
  severity: 'error',
15
19
  code: 'FORBIDDEN',
16
20
  ...response
17
- }, cause);
18
- this.status = HttpStatus.FORBIDDEN;
21
+ });
19
22
  }
20
23
  }