@opra/core 0.31.5 → 0.31.7
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.
|
@@ -597,8 +597,8 @@ class HttpAdapterHost extends platform_adapter_host_js_1.PlatformAdapterHost {
|
|
|
597
597
|
const incoming = context.switchToHttp().incoming;
|
|
598
598
|
const apiUrl = new common_1.OpraURL(incoming.baseUrl, incoming.protocol + '://' + incoming.get('host')).toString();
|
|
599
599
|
const body = new common_1.OperationResult({
|
|
600
|
-
context:
|
|
601
|
-
contextUrl:
|
|
600
|
+
context: '',
|
|
601
|
+
contextUrl: ''
|
|
602
602
|
});
|
|
603
603
|
const operationName = endpoint.kind === 'operation' ? endpoint.name : '';
|
|
604
604
|
if (operationName === 'delete' || operationName === 'deleteMany' ||
|
|
@@ -628,9 +628,16 @@ class HttpAdapterHost extends platform_adapter_host_js_1.PlatformAdapterHost {
|
|
|
628
628
|
}
|
|
629
629
|
else
|
|
630
630
|
body.typeUrl = body.contextUrl + '/type';
|
|
631
|
-
|
|
631
|
+
if (response.value instanceof common_1.OperationResult) {
|
|
632
|
+
Object.assign(body, response.value);
|
|
633
|
+
}
|
|
634
|
+
else
|
|
635
|
+
body.payload = response.value;
|
|
636
|
+
body.payload = this.i18n.deep(body.payload);
|
|
632
637
|
}
|
|
633
638
|
}
|
|
639
|
+
body.context = endpoint.getFullPath(false);
|
|
640
|
+
body.contextUrl = apiUrl + '/#' + endpoint.getFullPath(true);
|
|
634
641
|
outgoing.setHeader(common_1.HttpHeaderCodes.Content_Type, 'application/opra+json; charset=utf-8');
|
|
635
642
|
outgoing.send(JSON.stringify(body));
|
|
636
643
|
outgoing.end();
|
|
@@ -1,18 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ApiService = void 0;
|
|
4
|
+
const cachedServices = Symbol('cachedServices');
|
|
4
5
|
class ApiService {
|
|
5
6
|
get context() {
|
|
6
7
|
if (!this._context)
|
|
7
8
|
throw new Error(`No context assigned for ${Object.getPrototypeOf(this).constructor.name}`);
|
|
8
9
|
return this._context;
|
|
9
10
|
}
|
|
10
|
-
forContext(
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
forContext(arg0, attributes) {
|
|
12
|
+
let context;
|
|
13
|
+
if (arg0 instanceof ApiService) {
|
|
14
|
+
context = arg0.context;
|
|
15
|
+
attributes = arg0;
|
|
16
|
+
}
|
|
17
|
+
else
|
|
18
|
+
context = arg0;
|
|
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
|
+
const newInstance = !this._context || this._context !== context;
|
|
24
|
+
if (!newInstance) {
|
|
25
|
+
const instance = cachedInstances
|
|
26
|
+
.find(service => this._cacheMatch(service, context, attributes));
|
|
27
|
+
if (instance)
|
|
28
|
+
return instance;
|
|
29
|
+
}
|
|
13
30
|
const instance = { context };
|
|
14
31
|
Object.setPrototypeOf(instance, this);
|
|
32
|
+
cachedInstances.push(instance);
|
|
15
33
|
return instance;
|
|
16
34
|
}
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
36
|
+
_cacheMatch(service, context, attributes) {
|
|
37
|
+
return service.context === context;
|
|
38
|
+
}
|
|
17
39
|
}
|
|
18
40
|
exports.ApiService = ApiService;
|
|
@@ -593,8 +593,8 @@ export class HttpAdapterHost extends PlatformAdapterHost {
|
|
|
593
593
|
const incoming = context.switchToHttp().incoming;
|
|
594
594
|
const apiUrl = new OpraURL(incoming.baseUrl, incoming.protocol + '://' + incoming.get('host')).toString();
|
|
595
595
|
const body = new OperationResult({
|
|
596
|
-
context:
|
|
597
|
-
contextUrl:
|
|
596
|
+
context: '',
|
|
597
|
+
contextUrl: ''
|
|
598
598
|
});
|
|
599
599
|
const operationName = endpoint.kind === 'operation' ? endpoint.name : '';
|
|
600
600
|
if (operationName === 'delete' || operationName === 'deleteMany' ||
|
|
@@ -624,9 +624,16 @@ export class HttpAdapterHost extends PlatformAdapterHost {
|
|
|
624
624
|
}
|
|
625
625
|
else
|
|
626
626
|
body.typeUrl = body.contextUrl + '/type';
|
|
627
|
-
|
|
627
|
+
if (response.value instanceof OperationResult) {
|
|
628
|
+
Object.assign(body, response.value);
|
|
629
|
+
}
|
|
630
|
+
else
|
|
631
|
+
body.payload = response.value;
|
|
632
|
+
body.payload = this.i18n.deep(body.payload);
|
|
628
633
|
}
|
|
629
634
|
}
|
|
635
|
+
body.context = endpoint.getFullPath(false);
|
|
636
|
+
body.contextUrl = apiUrl + '/#' + endpoint.getFullPath(true);
|
|
630
637
|
outgoing.setHeader(HttpHeaderCodes.Content_Type, 'application/opra+json; charset=utf-8');
|
|
631
638
|
outgoing.send(JSON.stringify(body));
|
|
632
639
|
outgoing.end();
|
|
@@ -1,14 +1,36 @@
|
|
|
1
|
+
const cachedServices = Symbol('cachedServices');
|
|
1
2
|
export class ApiService {
|
|
2
3
|
get context() {
|
|
3
4
|
if (!this._context)
|
|
4
5
|
throw new Error(`No context assigned for ${Object.getPrototypeOf(this).constructor.name}`);
|
|
5
6
|
return this._context;
|
|
6
7
|
}
|
|
7
|
-
forContext(
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
forContext(arg0, attributes) {
|
|
9
|
+
let context;
|
|
10
|
+
if (arg0 instanceof ApiService) {
|
|
11
|
+
context = arg0.context;
|
|
12
|
+
attributes = arg0;
|
|
13
|
+
}
|
|
14
|
+
else
|
|
15
|
+
context = arg0;
|
|
16
|
+
const cacheMap = context[cachedServices] = context[cachedServices] || new WeakMap();
|
|
17
|
+
const ctor = Object.getPrototypeOf(this).constructor;
|
|
18
|
+
const cachedInstances = cacheMap.get(ctor) || [];
|
|
19
|
+
cacheMap.set(ctor, cachedInstances);
|
|
20
|
+
const newInstance = !this._context || this._context !== context;
|
|
21
|
+
if (!newInstance) {
|
|
22
|
+
const instance = cachedInstances
|
|
23
|
+
.find(service => this._cacheMatch(service, context, attributes));
|
|
24
|
+
if (instance)
|
|
25
|
+
return instance;
|
|
26
|
+
}
|
|
10
27
|
const instance = { context };
|
|
11
28
|
Object.setPrototypeOf(instance, this);
|
|
29
|
+
cachedInstances.push(instance);
|
|
12
30
|
return instance;
|
|
13
31
|
}
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
33
|
+
_cacheMatch(service, context, attributes) {
|
|
34
|
+
return service.context === context;
|
|
35
|
+
}
|
|
14
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/core",
|
|
3
|
-
"version": "0.31.
|
|
3
|
+
"version": "0.31.7",
|
|
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.31.
|
|
32
|
+
"@opra/common": "^0.31.7",
|
|
33
33
|
"accepts": "^1.3.8",
|
|
34
34
|
"content-disposition": "^0.5.4",
|
|
35
35
|
"content-type": "^1.0.5",
|
|
@@ -2,7 +2,7 @@ import { RequestContext } from '../request-context.js';
|
|
|
2
2
|
export declare abstract class ApiService {
|
|
3
3
|
protected _context: RequestContext;
|
|
4
4
|
get context(): RequestContext;
|
|
5
|
-
forContext(
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
forContext(source: ApiService): this;
|
|
6
|
+
forContext(context: RequestContext, attributes?: any): this;
|
|
7
|
+
protected _cacheMatch(service: ApiService, context: RequestContext, attributes?: any): boolean;
|
|
8
8
|
}
|