@opra/core 0.32.6 → 0.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/http/http-adapter-host.js +3 -3
- package/cjs/services/api-service.js +15 -31
- package/esm/http/http-adapter-host.js +4 -4
- package/esm/services/api-service.js +15 -31
- package/i18n/en/error.json +1 -1
- package/i18n/i18n/en/error.json +1 -1
- package/package.json +3 -3
- package/types/services/api-service.d.ts +5 -9
|
@@ -4,7 +4,7 @@ exports.HttpAdapterHost = void 0;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const promises_1 = tslib_1.__importDefault(require("fs/promises"));
|
|
6
6
|
const os_1 = tslib_1.__importDefault(require("os"));
|
|
7
|
-
const
|
|
7
|
+
const valgen_1 = require("valgen");
|
|
8
8
|
const type_is_1 = tslib_1.__importDefault(require("@browsery/type-is"));
|
|
9
9
|
const common_1 = require("@opra/common");
|
|
10
10
|
const execution_context_host_js_1 = require("../execution-context.host.js");
|
|
@@ -108,7 +108,7 @@ class HttpAdapterHost extends platform_adapter_host_js_1.PlatformAdapterHost {
|
|
|
108
108
|
catch (e) {
|
|
109
109
|
if (e instanceof common_1.OpraException)
|
|
110
110
|
throw e;
|
|
111
|
-
if (e instanceof
|
|
111
|
+
if (e instanceof valgen_1.ValidationError) {
|
|
112
112
|
throw new common_1.InternalServerError({
|
|
113
113
|
message: (0, common_1.translate)('error:RESPONSE_VALIDATION,'),
|
|
114
114
|
code: 'RESPONSE_VALIDATION',
|
|
@@ -559,7 +559,7 @@ class HttpAdapterHost extends platform_adapter_host_js_1.PlatformAdapterHost {
|
|
|
559
559
|
return;
|
|
560
560
|
// "get" and "update" endpoints must return the entity instance, otherwise it means resource not found
|
|
561
561
|
if (value == null && (operationName === 'get' || operationName === 'update'))
|
|
562
|
-
throw new common_1.
|
|
562
|
+
throw new common_1.ResourceNotAvailableError(resource.name, request.key);
|
|
563
563
|
// "findMany" endpoint should return array of entity instances
|
|
564
564
|
if (operationName === 'findMany')
|
|
565
565
|
value = (value == null ? [] : Array.isArray(value) ? value : [value]);
|
|
@@ -1,45 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ApiService = void 0;
|
|
4
|
-
const request_context_js_1 = require("../request-context.js");
|
|
5
|
-
const cachedServices = Symbol('cachedServices');
|
|
6
4
|
class ApiService {
|
|
7
5
|
get context() {
|
|
8
6
|
if (!this._context)
|
|
9
7
|
throw new Error(`No context assigned for ${Object.getPrototypeOf(this).constructor.name}`);
|
|
10
8
|
return this._context;
|
|
11
9
|
}
|
|
12
|
-
for(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
attributes = attributes || arg0;
|
|
21
|
-
// Create cache map and instance array
|
|
22
|
-
const cacheMap = context[cachedServices] = context[cachedServices] || new WeakMap();
|
|
23
|
-
const ctor = Object.getPrototypeOf(this).constructor;
|
|
24
|
-
const cachedInstances = cacheMap.get(ctor) || [];
|
|
25
|
-
cacheMap.set(ctor, cachedInstances);
|
|
26
|
-
// Find cached instance that matches given arguments
|
|
27
|
-
let instance = cachedInstances.find((service) => this._instanceCompare(service, context, attributes));
|
|
28
|
-
if (instance)
|
|
29
|
-
return instance;
|
|
30
|
-
// Create new instance
|
|
31
|
-
instance = { context };
|
|
32
|
-
if (attributes)
|
|
33
|
-
Object.assign(instance, attributes);
|
|
10
|
+
for(context, overwriteProperties, overwriteContext) {
|
|
11
|
+
// Create new context instance
|
|
12
|
+
const ctx = {};
|
|
13
|
+
Object.setPrototypeOf(ctx, context);
|
|
14
|
+
if (overwriteContext)
|
|
15
|
+
Object.assign(ctx, overwriteContext);
|
|
16
|
+
// Create new service instance
|
|
17
|
+
const instance = { _context: ctx };
|
|
34
18
|
Object.setPrototypeOf(instance, this);
|
|
35
|
-
|
|
19
|
+
if (overwriteProperties)
|
|
20
|
+
Object.assign(instance, overwriteProperties);
|
|
21
|
+
if (this[ApiService.extendSymbol])
|
|
22
|
+
this[ApiService.extendSymbol](instance);
|
|
36
23
|
return instance;
|
|
37
24
|
}
|
|
38
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
39
|
-
_instanceCompare(service, context, attributes) {
|
|
40
|
-
if (attributes && Object.keys(attributes).find(k => attributes[k] !== service[k]))
|
|
41
|
-
return false;
|
|
42
|
-
return service.context === context;
|
|
43
|
-
}
|
|
44
25
|
}
|
|
45
26
|
exports.ApiService = ApiService;
|
|
27
|
+
(function (ApiService) {
|
|
28
|
+
ApiService.extendSymbol = Symbol('extend');
|
|
29
|
+
})(ApiService || (exports.ApiService = ApiService = {}));
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import fs from 'fs/promises';
|
|
2
2
|
import os from 'os';
|
|
3
|
-
import
|
|
3
|
+
import { ValidationError } from 'valgen';
|
|
4
4
|
import typeIs from '@browsery/type-is';
|
|
5
|
-
import { BadRequestError, Collection, Container, HttpHeaderCodes, HttpStatusCodes, InternalServerError, isReadable, IssueSeverity, MethodNotAllowedError, OperationResult, OpraException, OpraSchema, OpraURL,
|
|
5
|
+
import { BadRequestError, Collection, Container, HttpHeaderCodes, HttpStatusCodes, InternalServerError, isReadable, IssueSeverity, MethodNotAllowedError, OperationResult, OpraException, OpraSchema, OpraURL, ResourceNotAvailableError, Singleton, Storage, translate, uid, wrapException } from '@opra/common';
|
|
6
6
|
import { ExecutionContextHost } from '../execution-context.host.js';
|
|
7
7
|
import { PlatformAdapterHost } from '../platform-adapter.host.js';
|
|
8
8
|
import { RequestHost } from '../request.host.js';
|
|
@@ -104,7 +104,7 @@ export class HttpAdapterHost extends PlatformAdapterHost {
|
|
|
104
104
|
catch (e) {
|
|
105
105
|
if (e instanceof OpraException)
|
|
106
106
|
throw e;
|
|
107
|
-
if (e instanceof
|
|
107
|
+
if (e instanceof ValidationError) {
|
|
108
108
|
throw new InternalServerError({
|
|
109
109
|
message: translate('error:RESPONSE_VALIDATION,'),
|
|
110
110
|
code: 'RESPONSE_VALIDATION',
|
|
@@ -555,7 +555,7 @@ export class HttpAdapterHost extends PlatformAdapterHost {
|
|
|
555
555
|
return;
|
|
556
556
|
// "get" and "update" endpoints must return the entity instance, otherwise it means resource not found
|
|
557
557
|
if (value == null && (operationName === 'get' || operationName === 'update'))
|
|
558
|
-
throw new
|
|
558
|
+
throw new ResourceNotAvailableError(resource.name, request.key);
|
|
559
559
|
// "findMany" endpoint should return array of entity instances
|
|
560
560
|
if (operationName === 'findMany')
|
|
561
561
|
value = (value == null ? [] : Array.isArray(value) ? value : [value]);
|
|
@@ -1,41 +1,25 @@
|
|
|
1
|
-
import { RequestContext } from '../request-context.js';
|
|
2
|
-
const cachedServices = Symbol('cachedServices');
|
|
3
1
|
export class ApiService {
|
|
4
2
|
get context() {
|
|
5
3
|
if (!this._context)
|
|
6
4
|
throw new Error(`No context assigned for ${Object.getPrototypeOf(this).constructor.name}`);
|
|
7
5
|
return this._context;
|
|
8
6
|
}
|
|
9
|
-
for(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
attributes = attributes || arg0;
|
|
18
|
-
// Create cache map and instance array
|
|
19
|
-
const cacheMap = context[cachedServices] = context[cachedServices] || new WeakMap();
|
|
20
|
-
const ctor = Object.getPrototypeOf(this).constructor;
|
|
21
|
-
const cachedInstances = cacheMap.get(ctor) || [];
|
|
22
|
-
cacheMap.set(ctor, cachedInstances);
|
|
23
|
-
// Find cached instance that matches given arguments
|
|
24
|
-
let instance = cachedInstances.find((service) => this._instanceCompare(service, context, attributes));
|
|
25
|
-
if (instance)
|
|
26
|
-
return instance;
|
|
27
|
-
// Create new instance
|
|
28
|
-
instance = { context };
|
|
29
|
-
if (attributes)
|
|
30
|
-
Object.assign(instance, attributes);
|
|
7
|
+
for(context, overwriteProperties, overwriteContext) {
|
|
8
|
+
// Create new context instance
|
|
9
|
+
const ctx = {};
|
|
10
|
+
Object.setPrototypeOf(ctx, context);
|
|
11
|
+
if (overwriteContext)
|
|
12
|
+
Object.assign(ctx, overwriteContext);
|
|
13
|
+
// Create new service instance
|
|
14
|
+
const instance = { _context: ctx };
|
|
31
15
|
Object.setPrototypeOf(instance, this);
|
|
32
|
-
|
|
16
|
+
if (overwriteProperties)
|
|
17
|
+
Object.assign(instance, overwriteProperties);
|
|
18
|
+
if (this[ApiService.extendSymbol])
|
|
19
|
+
this[ApiService.extendSymbol](instance);
|
|
33
20
|
return instance;
|
|
34
21
|
}
|
|
35
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
36
|
-
_instanceCompare(service, context, attributes) {
|
|
37
|
-
if (attributes && Object.keys(attributes).find(k => attributes[k] !== service[k]))
|
|
38
|
-
return false;
|
|
39
|
-
return service.context === context;
|
|
40
|
-
}
|
|
41
22
|
}
|
|
23
|
+
(function (ApiService) {
|
|
24
|
+
ApiService.extendSymbol = Symbol('extend');
|
|
25
|
+
})(ApiService || (ApiService = {}));
|
package/i18n/en/error.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"UNPROCESSABLE_ENTITY": "Unprocessable entity",
|
|
11
11
|
"REQUEST_VALIDATION": "Request validation failed",
|
|
12
12
|
"RESPONSE_VALIDATION": "Response validation failed",
|
|
13
|
-
"
|
|
13
|
+
"RESOURCE_NOT_AVAILABLE": "Resource is not available or you dont have access",
|
|
14
14
|
"RESOURCE_CONFLICT": "There is already an other {{resource}} resource with same field values ({{fields}})",
|
|
15
15
|
"OPERATION_FORBIDDEN": "The {{resource}} resource does not accept '{{operation}}' operations",
|
|
16
16
|
"ACTION_NOT_FOUND": "The {{resource}} resource doesn't have an action named '{{action}}'",
|
package/i18n/i18n/en/error.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"UNPROCESSABLE_ENTITY": "Unprocessable entity",
|
|
11
11
|
"REQUEST_VALIDATION": "Request validation failed",
|
|
12
12
|
"RESPONSE_VALIDATION": "Response validation failed",
|
|
13
|
-
"
|
|
13
|
+
"RESOURCE_NOT_AVAILABLE": "Resource is not available or you dont have access",
|
|
14
14
|
"RESOURCE_CONFLICT": "There is already an other {{resource}} resource with same field values ({{fields}})",
|
|
15
15
|
"OPERATION_FORBIDDEN": "The {{resource}} resource does not accept '{{operation}}' operations",
|
|
16
16
|
"ACTION_NOT_FOUND": "The {{resource}} resource doesn't have an action named '{{action}}'",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.33.0",
|
|
4
4
|
"description": "Opra schema package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@browsery/type-is": "^0.6.3",
|
|
32
|
-
"@opra/common": "^0.
|
|
32
|
+
"@opra/common": "^0.33.0",
|
|
33
33
|
"accepts": "^1.3.8",
|
|
34
34
|
"content-disposition": "^0.5.4",
|
|
35
35
|
"content-type": "^1.0.5",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"@types/vary": "^1.1.3",
|
|
67
67
|
"crypto-browserify": "^3.12.0",
|
|
68
68
|
"path-browserify": "^1.0.1",
|
|
69
|
-
"ts-gems": "^
|
|
69
|
+
"ts-gems": "^3.1.0"
|
|
70
70
|
},
|
|
71
71
|
"type": "module",
|
|
72
72
|
"module": "./esm/index.js",
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
+
import { Nullish } from 'ts-gems';
|
|
1
2
|
import { RequestContext } from '../request-context.js';
|
|
2
|
-
export declare namespace ApiService {
|
|
3
|
-
interface ExtendOptions {
|
|
4
|
-
[key: string | symbol]: any;
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
3
|
export declare abstract class ApiService {
|
|
8
4
|
protected _context: RequestContext;
|
|
9
5
|
get context(): RequestContext;
|
|
10
|
-
for(context:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
for<C extends RequestContext, P extends Partial<this>>(context: C, overwriteProperties?: Nullish<P>, overwriteContext?: Partial<C>): this & Required<P>;
|
|
7
|
+
}
|
|
8
|
+
export declare namespace ApiService {
|
|
9
|
+
const extendSymbol: unique symbol;
|
|
14
10
|
}
|