@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
@@ -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,12 +10,26 @@ 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);
17
+ if (response instanceof Error)
18
+ this._initErrorInstance(response);
19
+ else if (typeof response === 'string')
20
+ this._initResponse({ message: response });
21
+ else
22
+ this._initResponse(response);
23
+ if (!this.response.severity)
24
+ if (this.status >= 500)
25
+ this.response.severity = 'fatal';
26
+ else
27
+ this.response.severity = 'error';
28
+ if (this.cause instanceof Error && this.cause.stack) {
29
+ if (ApiException.stackAsDiagnostics)
30
+ this.response.diagnostics = this.cause.stack.split('\n');
31
+ this.stack = this.cause.stack;
32
+ }
19
33
  }
20
34
  setStatus(status) {
21
35
  this.status = status;
@@ -24,37 +38,20 @@ export class ApiException extends Error {
24
38
  _initName() {
25
39
  this.name = this.constructor.name;
26
40
  }
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
- if (this.cause instanceof Error && this.cause.stack) {
49
- if (ApiException.stackAsDiagnostics)
50
- this.response.diagnostics = this.cause.stack.split('\n');
51
- this.stack = this.cause.stack;
52
- }
53
- if (!this.response.severity)
54
- if (this.status >= 500)
55
- this.response.severity = 'fatal';
56
- else
57
- this.response.severity = 'error';
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
+ };
58
55
  }
59
56
  static wrap(response) {
60
57
  if (response instanceof ApiException)
@@ -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({
12
- message: translate('error:BAD_REQUEST', 'Bad request'),
11
+ super(response, cause);
12
+ this.status = HttpStatus.BAD_REQUEST;
13
+ }
14
+ _initResponse(response) {
15
+ super._initResponse({
16
+ message: translate('error: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({
11
- message: translate('error:FAILED_DEPENDENCY', 'The request failed due to failure of a previous request.'),
10
+ super(response, cause);
11
+ this.status = HttpStatus.FAILED_DEPENDENCY;
12
+ }
13
+ _initResponse(response) {
14
+ super._initResponse({
15
+ message: translate('error:FAILED_DEPENDENCY'),
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({
13
- message: translate('error:FORBIDDEN', 'Forbidden'),
12
+ super(response, cause);
13
+ this.status = HttpStatus.FORBIDDEN;
14
+ }
15
+ _initResponse(response) {
16
+ super._initResponse({
17
+ message: translate('error:FORBIDDEN'),
14
18
  severity: 'error',
15
19
  code: 'FORBIDDEN',
16
20
  ...response
17
- }, cause);
18
- this.status = HttpStatus.FORBIDDEN;
21
+ });
19
22
  }
20
23
  }
@@ -4,5 +4,6 @@ import { ApiException, ErrorResponse } from '../api-exception.js';
4
4
  * The server has encountered a situation it does not know how to handle.
5
5
  */
6
6
  export declare class InternalServerError 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
  }
@@ -1,3 +1,4 @@
1
+ import _ from 'lodash';
1
2
  import { translate } from '@opra/i18n';
2
3
  import { HttpStatus } from '../../enums/index.js';
3
4
  import { ApiException } from '../api-exception.js';
@@ -7,12 +8,15 @@ import { ApiException } from '../api-exception.js';
7
8
  */
8
9
  export class InternalServerError extends ApiException {
9
10
  constructor(response, cause) {
10
- super({
11
- message: translate('error:INTERNAL_SERVER_ERROR', 'Internal server error'),
11
+ super(response, cause);
12
+ this.status = HttpStatus.INTERNAL_SERVER_ERROR;
13
+ }
14
+ _initResponse(response) {
15
+ super._initResponse({
16
+ message: translate('error:INTERNAL_SERVER_ERROR'),
12
17
  severity: 'error',
13
18
  code: 'INTERNAL_SERVER_ERROR',
14
- ...response
15
- }, cause);
16
- this.status = HttpStatus.INTERNAL_SERVER_ERROR;
19
+ ..._.omitBy(response, _.isNil)
20
+ });
17
21
  }
18
22
  }
@@ -5,5 +5,6 @@ import { ApiException, ErrorResponse } from '../api-exception.js';
5
5
  * For example, an API may not allow calling DELETE to remove a resource.
6
6
  */
7
7
  export declare class MethodNotAllowedError 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 MethodNotAllowedError extends ApiException {
10
10
  constructor(response, cause) {
11
- super({
12
- message: translate('error:METHOD_NOT_ALLOWED', 'Method Not Allowed'),
11
+ super(response, cause);
12
+ this.status = HttpStatus.METHOD_NOT_ALLOWED;
13
+ }
14
+ _initResponse(response) {
15
+ super._initResponse({
16
+ message: translate('error:METHOD_NOT_ALLOWED'),
13
17
  severity: 'error',
14
18
  code: 'METHOD_NOT_ALLOWED',
15
19
  ...response
16
- }, cause);
17
- this.status = HttpStatus.METHOD_NOT_ALLOWED;
20
+ });
18
21
  }
19
22
  }
@@ -8,5 +8,6 @@ import { ApiException, ErrorResponse } from '../api-exception.js';
8
8
  * frequent occurrence on the web.
9
9
  */
10
10
  export declare class NotFoundError extends ApiException {
11
- constructor(response?: ErrorResponse, cause?: Error);
11
+ constructor(response?: string | ErrorResponse | Error, cause?: Error);
12
+ protected _initResponse(response: Partial<ErrorResponse>): void;
12
13
  }
@@ -11,12 +11,15 @@ import { ApiException } from '../api-exception.js';
11
11
  */
12
12
  export class NotFoundError extends ApiException {
13
13
  constructor(response, cause) {
14
- super({
14
+ super(response, cause);
15
+ this.status = HttpStatus.NOT_FOUND;
16
+ }
17
+ _initResponse(response) {
18
+ super._initResponse({
15
19
  message: translate('error:NOT_FOUND', 'Not found'),
16
20
  severity: 'error',
17
21
  code: 'NOT_FOUND',
18
22
  ...response
19
- }, cause);
20
- this.status = HttpStatus.NOT_FOUND;
23
+ });
21
24
  }
22
25
  }
@@ -5,5 +5,6 @@ import { ApiException, ErrorResponse } from '../api-exception.js';
5
5
  * That is, the client must authenticate itself to get the requested response.
6
6
  */
7
7
  export declare class UnauthorizedError 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 UnauthorizedError extends ApiException {
10
10
  constructor(response, cause) {
11
- super({
11
+ super(response, cause);
12
+ this.status = HttpStatus.UNAUTHORIZED;
13
+ }
14
+ _initResponse(response) {
15
+ super._initResponse({
12
16
  message: translate('error:UNAUTHORIZED', 'Unauthorized'),
13
17
  severity: 'error',
14
18
  code: 'UNAUTHORIZED',
15
19
  ...response
16
- }, cause);
17
- this.status = HttpStatus.UNAUTHORIZED;
20
+ });
18
21
  }
19
22
  }
@@ -4,5 +4,6 @@ import { ApiException, ErrorResponse } from '../api-exception.js';
4
4
  * The request was well-formed but was unable to be followed due to semantic errors.
5
5
  */
6
6
  export declare class UnprocessableEntityError 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 UnprocessableEntityError extends ApiException {
9
9
  constructor(response, cause) {
10
- super({
11
- message: translate('error:UNPROCESSABLE_ENTITY', 'Unprocessable entity'),
10
+ super(response, cause);
11
+ this.status = HttpStatus.UNPROCESSABLE_ENTITY;
12
+ }
13
+ _initResponse(response) {
14
+ super._initResponse({
15
+ message: translate('error:UNPROCESSABLE_ENTITY'),
12
16
  severity: 'error',
13
17
  code: 'UNPROCESSABLE_ENTITY',
14
18
  ...response
15
- }, cause);
16
- this.status = HttpStatus.UNPROCESSABLE_ENTITY;
19
+ });
17
20
  }
18
21
  }
@@ -1,8 +1,10 @@
1
1
  export * from './api-exception.js';
2
- export * from './errors/bad-request.error.js';
3
- export * from './errors/failed-dependency.error.js';
4
- export * from './errors/internal-server.error.js';
5
- export * from './errors/method-not-allowed.error.js';
6
- export * from './errors/not-found.error.js';
7
- export * from './errors/unauthorized.error.js';
8
- export * from './errors/unprocessable-entity.error.js';
2
+ export * from './http-errors/bad-request.error.js';
3
+ export * from './http-errors/failed-dependency.error.js';
4
+ export * from './http-errors/forbidden.error.js';
5
+ export * from './http-errors/internal-server.error.js';
6
+ export * from './http-errors/method-not-allowed.error.js';
7
+ export * from './http-errors/not-found.error.js';
8
+ export * from './http-errors/unauthorized.error.js';
9
+ export * from './http-errors/unprocessable-entity.error.js';
10
+ export * from './resource-errors/resource-conflict.error.js';
@@ -1,8 +1,10 @@
1
1
  export * from './api-exception.js';
2
- export * from './errors/bad-request.error.js';
3
- export * from './errors/failed-dependency.error.js';
4
- export * from './errors/internal-server.error.js';
5
- export * from './errors/method-not-allowed.error.js';
6
- export * from './errors/not-found.error.js';
7
- export * from './errors/unauthorized.error.js';
8
- export * from './errors/unprocessable-entity.error.js';
2
+ export * from './http-errors/bad-request.error.js';
3
+ export * from './http-errors/failed-dependency.error.js';
4
+ export * from './http-errors/forbidden.error.js';
5
+ export * from './http-errors/internal-server.error.js';
6
+ export * from './http-errors/method-not-allowed.error.js';
7
+ export * from './http-errors/not-found.error.js';
8
+ export * from './http-errors/unauthorized.error.js';
9
+ export * from './http-errors/unprocessable-entity.error.js';
10
+ export * from './resource-errors/resource-conflict.error.js';
@@ -0,0 +1,4 @@
1
+ import { ApiException } from '../api-exception.js';
2
+ export declare class ResourceConflictError extends ApiException {
3
+ constructor(resource: string, fields: string | string[], cause?: Error);
4
+ }
@@ -0,0 +1,15 @@
1
+ import { translate } from '@opra/i18n';
2
+ import { HttpStatus } from '../../enums/index.js';
3
+ import { ApiException } from '../api-exception.js';
4
+ export class ResourceConflictError extends ApiException {
5
+ constructor(resource, fields, cause) {
6
+ super({
7
+ message: translate(`error:RESOURCE_CONFLICT`, { resource, fields }),
8
+ severity: 'error',
9
+ code: 'RESOURCE_CONFLICT',
10
+ subject: resource,
11
+ location: fields
12
+ }, cause);
13
+ this.status = HttpStatus.CONFLICT;
14
+ }
15
+ }
@@ -1,5 +1,5 @@
1
1
  import { ApiException } from './api-exception.js';
2
- import { InternalServerError } from './errors/internal-server.error.js';
2
+ import { InternalServerError } from './http-errors/internal-server.error.js';
3
3
  export function wrapError(response) {
4
4
  if (response instanceof ApiException)
5
5
  return response;
@@ -7,7 +7,7 @@ export function wrapError(response) {
7
7
  const x = response;
8
8
  if (typeof x.status === 'number' || typeof x.getStatus === 'function')
9
9
  return new ApiException(response);
10
- return new InternalServerError(undefined, response);
10
+ return new InternalServerError(response);
11
11
  }
12
12
  return new InternalServerError();
13
13
  }
@@ -1,14 +1,21 @@
1
1
  import { FallbackLng, I18n, LanguageResource } from '@opra/i18n';
2
2
  import { ApiException } from '../../exception/index.js';
3
- import { ExecutionContext, ExecutionRequest } from '../execution-context.js';
3
+ import { IExecutionContext } from '../../interfaces/execution-context.interface.js';
4
4
  import { OpraService } from '../opra-service.js';
5
+ import { QueryContext } from '../query-context.js';
5
6
  export declare namespace OpraAdapter {
7
+ type UserContextResolver = (args: {
8
+ executionContext: IExecutionContext;
9
+ isBatch: boolean;
10
+ }) => object | Promise<object>;
11
+ type RequestFinishEvent = (args: {
12
+ executionContext: IExecutionContext;
13
+ userContext?: any;
14
+ failed: boolean;
15
+ }) => Promise<void>;
6
16
  interface Options {
7
17
  i18n?: I18n | I18nOptions | (() => Promise<I18n>);
8
- userContext?: (request: any, options: {
9
- platform: string;
10
- isBatch: boolean;
11
- }) => object | Promise<object>;
18
+ userContext?: UserContextResolver;
12
19
  }
13
20
  interface I18nOptions {
14
21
  /**
@@ -37,17 +44,18 @@ export declare namespace OpraAdapter {
37
44
  */
38
45
  resourceDirs?: string[];
39
46
  }
40
- type UserContextResolver = (isBatch: boolean) => any | Promise<any>;
41
47
  }
42
- export declare abstract class OpraAdapter<TAdapterContext = any> {
48
+ export declare abstract class OpraAdapter<TExecutionContext extends IExecutionContext> {
43
49
  readonly service: OpraService;
44
50
  readonly i18n: I18n;
45
- constructor(service: OpraService, i18n?: I18n);
46
- protected abstract prepareRequests(adapterContext: TAdapterContext): ExecutionRequest[];
47
- protected abstract createExecutionContext(adapterContext: any, request: ExecutionRequest): ExecutionContext;
48
- protected abstract sendResponse(adapterContext: TAdapterContext, executionContexts: ExecutionContext[]): Promise<void>;
49
- protected abstract sendError(adapterContext: TAdapterContext, error: ApiException): Promise<void>;
50
- protected abstract isBatch(adapterContext: TAdapterContext): boolean;
51
- protected handler(adapterContext: TAdapterContext, userContextResolver?: OpraAdapter.UserContextResolver): Promise<void>;
51
+ readonly userContextResolver?: OpraAdapter.UserContextResolver;
52
+ constructor(service: OpraService, options?: Omit<OpraAdapter.Options, 'i18n'> & {
53
+ i18n: I18n;
54
+ });
55
+ protected abstract prepareRequests(executionContext: TExecutionContext): QueryContext[];
56
+ protected abstract sendResponse(executionContext: TExecutionContext, queryContexts: QueryContext[]): Promise<void>;
57
+ protected abstract sendError(executionContext: TExecutionContext, error: ApiException): Promise<void>;
58
+ protected abstract isBatch(executionContext: TExecutionContext): boolean;
59
+ protected handler(executionContext: TExecutionContext): Promise<void>;
52
60
  protected static initI18n(options?: OpraAdapter.Options): Promise<I18n>;
53
61
  }