@opra/core 0.21.0 → 0.22.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.
|
@@ -58,10 +58,6 @@ class OpraHttpAdapter extends adapter_js_1.OpraAdapter {
|
|
|
58
58
|
await this.handleError(request.switchToHttp(), outgoing, errors);
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
|
-
if (request.resource instanceof common_1.Singleton || request.resource instanceof common_1.Collection) {
|
|
62
|
-
outgoing.setHeader(common_1.HttpHeaderCodes.X_Opra_Data_Type, String(request.resource.type.name));
|
|
63
|
-
outgoing.setHeader(common_1.HttpHeaderCodes.X_Opra_Operation, request.operation);
|
|
64
|
-
}
|
|
65
61
|
if (request.crud === 'create') {
|
|
66
62
|
if (!response.value)
|
|
67
63
|
throw new common_1.InternalServerError();
|
|
@@ -70,13 +66,21 @@ class OpraHttpAdapter extends adapter_js_1.OpraAdapter {
|
|
|
70
66
|
}
|
|
71
67
|
if (request.resource instanceof common_1.Collection &&
|
|
72
68
|
request.crud === 'read' && request.many && request.args.count >= 0) {
|
|
73
|
-
outgoing.setHeader(common_1.HttpHeaderCodes.
|
|
69
|
+
outgoing.setHeader(common_1.HttpHeaderCodes.X_Total_Count, String(response.count));
|
|
74
70
|
}
|
|
75
71
|
outgoing.statusCode = outgoing.statusCode || common_1.HttpStatusCodes.OK;
|
|
76
72
|
outgoing.setHeader(common_1.HttpHeaderCodes.Cache_Control, 'no-cache');
|
|
77
73
|
outgoing.setHeader(common_1.HttpHeaderCodes.Pragma, 'no-cache');
|
|
78
74
|
outgoing.setHeader(common_1.HttpHeaderCodes.Expires, '-1');
|
|
79
75
|
outgoing.setHeader(common_1.HttpHeaderCodes.X_Opra_Version, common_1.OpraSchema.SpecVersion);
|
|
76
|
+
// Expose headers if cors enabled
|
|
77
|
+
if (outgoing.getHeader(common_1.HttpHeaderCodes.Access_Control_Allow_Origin)) {
|
|
78
|
+
// Expose X-Total-Count header
|
|
79
|
+
outgoing.appendHeader(common_1.HttpHeaderCodes.Access_Control_Expose_Headers, [common_1.HttpHeaderCodes.X_Total_Count]);
|
|
80
|
+
// Expose X-Opra-* headers
|
|
81
|
+
outgoing.appendHeader(common_1.HttpHeaderCodes.Access_Control_Expose_Headers, Object.values(common_1.HttpHeaderCodes)
|
|
82
|
+
.filter(k => k.toLowerCase().startsWith('x-opra-')));
|
|
83
|
+
}
|
|
80
84
|
if (response.value) {
|
|
81
85
|
if (typeof response.value === 'object') {
|
|
82
86
|
if ((0, common_1.isReadable)(response.value) || Buffer.isBuffer(response.value))
|
|
@@ -105,9 +109,9 @@ class OpraHttpAdapter extends adapter_js_1.OpraAdapter {
|
|
|
105
109
|
return b.status - a.status;
|
|
106
110
|
return i;
|
|
107
111
|
});
|
|
108
|
-
if (!status || status < common_1.HttpStatusCodes.BAD_REQUEST) {
|
|
112
|
+
if (!status || status < Number(common_1.HttpStatusCodes.BAD_REQUEST)) {
|
|
109
113
|
status = errors[0].status;
|
|
110
|
-
if (status < common_1.HttpStatusCodes.BAD_REQUEST)
|
|
114
|
+
if (status < Number(common_1.HttpStatusCodes.BAD_REQUEST))
|
|
111
115
|
status = common_1.HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
112
116
|
}
|
|
113
117
|
const body = this.i18n.deep({
|
|
@@ -47,28 +47,28 @@ class HttpServerResponseHost {
|
|
|
47
47
|
}
|
|
48
48
|
setHeader(field, val) {
|
|
49
49
|
const setHeader = Object.getPrototypeOf(this).setHeader;
|
|
50
|
-
if (typeof field === '
|
|
51
|
-
let value = Array.isArray(val)
|
|
52
|
-
? val.map(String)
|
|
53
|
-
: (val ? String(val) : '');
|
|
54
|
-
// add charset to content-type
|
|
55
|
-
if (field.toLowerCase() === 'content-type') {
|
|
56
|
-
if (Array.isArray(value)) {
|
|
57
|
-
throw new TypeError('Content-Type cannot be set to an Array');
|
|
58
|
-
}
|
|
59
|
-
if (!charsetRegExp.test(value)) {
|
|
60
|
-
const charset = mime_types_1.default.charsets.lookup(value.split(';')[0]);
|
|
61
|
-
if (charset)
|
|
62
|
-
value += '; charset=' + charset.toLowerCase();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
setHeader.call(this, field, value);
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
50
|
+
if (typeof field === 'object') {
|
|
68
51
|
for (const [k, v] of Object.entries(field)) {
|
|
69
52
|
this.setHeader(k, v);
|
|
70
53
|
}
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
const fieldLower = field.toLowerCase();
|
|
57
|
+
let value = Array.isArray(val)
|
|
58
|
+
? val.map(String)
|
|
59
|
+
: (val ? String(val) : '');
|
|
60
|
+
// add charset to content-type
|
|
61
|
+
if (fieldLower === 'content-type') {
|
|
62
|
+
if (Array.isArray(value)) {
|
|
63
|
+
throw new TypeError('Content-Type cannot be set to an Array');
|
|
64
|
+
}
|
|
65
|
+
if (!charsetRegExp.test(value)) {
|
|
66
|
+
const charset = mime_types_1.default.charsets.lookup(value.split(';')[0]);
|
|
67
|
+
if (charset)
|
|
68
|
+
value += '; charset=' + charset.toLowerCase();
|
|
69
|
+
}
|
|
71
70
|
}
|
|
71
|
+
setHeader.call(this, field, value);
|
|
72
72
|
return this;
|
|
73
73
|
}
|
|
74
74
|
clearCookie(name, options) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Task } from 'power-tasks';
|
|
2
|
-
import { BadRequestError, Collection, HttpHeaderCodes, HttpStatusCodes, InternalServerError, isReadable, IssueSeverity, OpraException, OpraSchema,
|
|
2
|
+
import { BadRequestError, Collection, HttpHeaderCodes, HttpStatusCodes, InternalServerError, isReadable, IssueSeverity, OpraException, OpraSchema, wrapException } from '@opra/common';
|
|
3
3
|
import { OpraAdapter } from '../adapter.js';
|
|
4
4
|
import { RequestContextHost } from '../request-context.host.js';
|
|
5
5
|
import { ResponseHost } from '../response.host.js';
|
|
@@ -55,10 +55,6 @@ export class OpraHttpAdapter extends OpraAdapter {
|
|
|
55
55
|
await this.handleError(request.switchToHttp(), outgoing, errors);
|
|
56
56
|
return;
|
|
57
57
|
}
|
|
58
|
-
if (request.resource instanceof Singleton || request.resource instanceof Collection) {
|
|
59
|
-
outgoing.setHeader(HttpHeaderCodes.X_Opra_Data_Type, String(request.resource.type.name));
|
|
60
|
-
outgoing.setHeader(HttpHeaderCodes.X_Opra_Operation, request.operation);
|
|
61
|
-
}
|
|
62
58
|
if (request.crud === 'create') {
|
|
63
59
|
if (!response.value)
|
|
64
60
|
throw new InternalServerError();
|
|
@@ -67,13 +63,21 @@ export class OpraHttpAdapter extends OpraAdapter {
|
|
|
67
63
|
}
|
|
68
64
|
if (request.resource instanceof Collection &&
|
|
69
65
|
request.crud === 'read' && request.many && request.args.count >= 0) {
|
|
70
|
-
outgoing.setHeader(HttpHeaderCodes.
|
|
66
|
+
outgoing.setHeader(HttpHeaderCodes.X_Total_Count, String(response.count));
|
|
71
67
|
}
|
|
72
68
|
outgoing.statusCode = outgoing.statusCode || HttpStatusCodes.OK;
|
|
73
69
|
outgoing.setHeader(HttpHeaderCodes.Cache_Control, 'no-cache');
|
|
74
70
|
outgoing.setHeader(HttpHeaderCodes.Pragma, 'no-cache');
|
|
75
71
|
outgoing.setHeader(HttpHeaderCodes.Expires, '-1');
|
|
76
72
|
outgoing.setHeader(HttpHeaderCodes.X_Opra_Version, OpraSchema.SpecVersion);
|
|
73
|
+
// Expose headers if cors enabled
|
|
74
|
+
if (outgoing.getHeader(HttpHeaderCodes.Access_Control_Allow_Origin)) {
|
|
75
|
+
// Expose X-Total-Count header
|
|
76
|
+
outgoing.appendHeader(HttpHeaderCodes.Access_Control_Expose_Headers, [HttpHeaderCodes.X_Total_Count]);
|
|
77
|
+
// Expose X-Opra-* headers
|
|
78
|
+
outgoing.appendHeader(HttpHeaderCodes.Access_Control_Expose_Headers, Object.values(HttpHeaderCodes)
|
|
79
|
+
.filter(k => k.toLowerCase().startsWith('x-opra-')));
|
|
80
|
+
}
|
|
77
81
|
if (response.value) {
|
|
78
82
|
if (typeof response.value === 'object') {
|
|
79
83
|
if (isReadable(response.value) || Buffer.isBuffer(response.value))
|
|
@@ -102,9 +106,9 @@ export class OpraHttpAdapter extends OpraAdapter {
|
|
|
102
106
|
return b.status - a.status;
|
|
103
107
|
return i;
|
|
104
108
|
});
|
|
105
|
-
if (!status || status < HttpStatusCodes.BAD_REQUEST) {
|
|
109
|
+
if (!status || status < Number(HttpStatusCodes.BAD_REQUEST)) {
|
|
106
110
|
status = errors[0].status;
|
|
107
|
-
if (status < HttpStatusCodes.BAD_REQUEST)
|
|
111
|
+
if (status < Number(HttpStatusCodes.BAD_REQUEST))
|
|
108
112
|
status = HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
109
113
|
}
|
|
110
114
|
const body = this.i18n.deep({
|
|
@@ -43,28 +43,28 @@ class HttpServerResponseHost {
|
|
|
43
43
|
}
|
|
44
44
|
setHeader(field, val) {
|
|
45
45
|
const setHeader = Object.getPrototypeOf(this).setHeader;
|
|
46
|
-
if (typeof field === '
|
|
47
|
-
let value = Array.isArray(val)
|
|
48
|
-
? val.map(String)
|
|
49
|
-
: (val ? String(val) : '');
|
|
50
|
-
// add charset to content-type
|
|
51
|
-
if (field.toLowerCase() === 'content-type') {
|
|
52
|
-
if (Array.isArray(value)) {
|
|
53
|
-
throw new TypeError('Content-Type cannot be set to an Array');
|
|
54
|
-
}
|
|
55
|
-
if (!charsetRegExp.test(value)) {
|
|
56
|
-
const charset = mime.charsets.lookup(value.split(';')[0]);
|
|
57
|
-
if (charset)
|
|
58
|
-
value += '; charset=' + charset.toLowerCase();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
setHeader.call(this, field, value);
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
46
|
+
if (typeof field === 'object') {
|
|
64
47
|
for (const [k, v] of Object.entries(field)) {
|
|
65
48
|
this.setHeader(k, v);
|
|
66
49
|
}
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
const fieldLower = field.toLowerCase();
|
|
53
|
+
let value = Array.isArray(val)
|
|
54
|
+
? val.map(String)
|
|
55
|
+
: (val ? String(val) : '');
|
|
56
|
+
// add charset to content-type
|
|
57
|
+
if (fieldLower === 'content-type') {
|
|
58
|
+
if (Array.isArray(value)) {
|
|
59
|
+
throw new TypeError('Content-Type cannot be set to an Array');
|
|
60
|
+
}
|
|
61
|
+
if (!charsetRegExp.test(value)) {
|
|
62
|
+
const charset = mime.charsets.lookup(value.split(';')[0]);
|
|
63
|
+
if (charset)
|
|
64
|
+
value += '; charset=' + charset.toLowerCase();
|
|
65
|
+
}
|
|
67
66
|
}
|
|
67
|
+
setHeader.call(this, field, value);
|
|
68
68
|
return this;
|
|
69
69
|
}
|
|
70
70
|
clearCookie(name, options) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"description": "Opra schema package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,15 +27,15 @@
|
|
|
27
27
|
"clean:cover": "rimraf ../../coverage/core"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@opra/common": "^0.
|
|
30
|
+
"@opra/common": "^0.22.0",
|
|
31
31
|
"accepts": "^1.3.8",
|
|
32
|
-
"cookie": "^0.5.0",
|
|
33
|
-
"cookie-signature": "^1.2.1",
|
|
34
32
|
"content-disposition": "^0.5.4",
|
|
35
33
|
"content-type": "^1.0.5",
|
|
34
|
+
"cookie": "^0.5.0",
|
|
35
|
+
"cookie-signature": "^1.2.1",
|
|
36
36
|
"encodeurl": "^1.0.2",
|
|
37
|
-
"fresh": "^0.5.2",
|
|
38
37
|
"formidable": "^3.5.0",
|
|
38
|
+
"fresh": "^0.5.2",
|
|
39
39
|
"mime-types": "^2.1.35",
|
|
40
40
|
"power-tasks": "^1.7.0",
|
|
41
41
|
"putil-varhelpers": "^1.6.5",
|
|
@@ -55,9 +55,9 @@
|
|
|
55
55
|
"@types/cookie": "^0.5.1",
|
|
56
56
|
"@types/cookie-signature": "^1.1.0",
|
|
57
57
|
"@types/encodeurl": "^1.0.0",
|
|
58
|
-
"@types/fresh": "^0.5.0",
|
|
59
|
-
"@types/formidable": "^3.4.0",
|
|
60
58
|
"@types/express": "^4.17.17",
|
|
59
|
+
"@types/formidable": "^3.4.0",
|
|
60
|
+
"@types/fresh": "^0.5.0",
|
|
61
61
|
"@types/mime-types": "^2.1.1",
|
|
62
62
|
"@types/range-parser": "^1.2.4",
|
|
63
63
|
"@types/type-is": "^1.6.3",
|