anote-server-libs 0.4.0 → 0.4.2
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/package.json +4 -4
- package/services/WithBody.js +25 -12
- package/services/WithBody.ts +26 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "anote-server-libs",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Helpers for express-TS servers",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
"author": "Mathonet Gregoire",
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"dependencies": {
|
|
19
|
+
"@types/memcached": "2.2.7",
|
|
20
|
+
"@types/mssql": "8.1.2",
|
|
21
|
+
"@types/pg": "8.6.6",
|
|
19
22
|
"ajv": "8.12.0",
|
|
20
23
|
"memcached": "2.2.2",
|
|
21
24
|
"mssql": "9.1.1",
|
|
@@ -27,10 +30,7 @@
|
|
|
27
30
|
},
|
|
28
31
|
"devDependencies": {
|
|
29
32
|
"@types/express": "4.17.16",
|
|
30
|
-
"@types/memcached": "2.2.7",
|
|
31
|
-
"@types/mssql": "8.1.2",
|
|
32
33
|
"@types/node": "14.18.2",
|
|
33
|
-
"@types/pg": "8.6.6",
|
|
34
34
|
"typescript": "4.9.4"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/services/WithBody.js
CHANGED
|
@@ -2,19 +2,32 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WithBody = void 0;
|
|
4
4
|
const ajv_1 = require("ajv");
|
|
5
|
+
const ajv = new ajv_1.default();
|
|
6
|
+
ajv.addKeyword({
|
|
7
|
+
keyword: 'maxDigits',
|
|
8
|
+
type: 'number',
|
|
9
|
+
schemaType: 'integer',
|
|
10
|
+
code(cxt) {
|
|
11
|
+
const { data, schema } = cxt;
|
|
12
|
+
const op = (0, ajv_1._) `!==`;
|
|
13
|
+
const roundedData = Math.round(data * 10 ** schema) / (10 ** schema);
|
|
14
|
+
cxt.fail((0, ajv_1._) `${roundedData} ${op} ${data}`);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
5
17
|
function WithBody(schema) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
+
if (schema.type === 'object') {
|
|
19
|
+
const required = [];
|
|
20
|
+
const keys = Object.getOwnPropertyNames(schema.properties);
|
|
21
|
+
keys.forEach(key => {
|
|
22
|
+
if (schema.properties[key].required === true) {
|
|
23
|
+
required.push(key);
|
|
24
|
+
}
|
|
25
|
+
if (schema.properties[key].required === true || schema.properties[key].required === false) {
|
|
26
|
+
delete schema.properties[key].required;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
schema.required = required;
|
|
30
|
+
}
|
|
18
31
|
let validate = ajv.compile(schema);
|
|
19
32
|
return function (_, __, descriptor) {
|
|
20
33
|
if (typeof descriptor.value === 'function') {
|
package/services/WithBody.ts
CHANGED
|
@@ -1,21 +1,34 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
2
|
import Ajv, {_} from 'ajv';
|
|
3
3
|
|
|
4
|
+
const ajv = new Ajv();
|
|
5
|
+
ajv.addKeyword({
|
|
6
|
+
keyword: 'maxDigits',
|
|
7
|
+
type: 'number',
|
|
8
|
+
schemaType: 'integer',
|
|
9
|
+
code(cxt: any) {
|
|
10
|
+
const {data, schema} = cxt;
|
|
11
|
+
const op = _`!==`;
|
|
12
|
+
const roundedData = Math.round(data * 10 ** schema) / (10 ** schema);
|
|
13
|
+
cxt.fail(_`${roundedData} ${op} ${data}`);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
4
17
|
export function WithBody(schema: any) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
18
|
+
if(schema.type === 'object') {
|
|
19
|
+
const required: string[] = [];
|
|
20
|
+
const keys = Object.getOwnPropertyNames(schema.properties);
|
|
21
|
+
keys.forEach(key => {
|
|
22
|
+
if(schema.properties[key].required === true) {
|
|
23
|
+
required.push(key);
|
|
24
|
+
}
|
|
25
|
+
if(schema.properties[key].required === true || schema.properties[key].required === false) {
|
|
26
|
+
delete schema.properties[key].required;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
schema.required = required;
|
|
30
|
+
}
|
|
17
31
|
let validate = ajv.compile(schema);
|
|
18
|
-
|
|
19
32
|
return function(_: any, __: string, descriptor: PropertyDescriptor) {
|
|
20
33
|
if(typeof descriptor.value === 'function') {
|
|
21
34
|
const previousMethod = descriptor.value;
|