anote-server-libs 0.2.2 → 0.2.3

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.
@@ -1,60 +1,60 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WithBody = void 0;
4
- const jsonschema_1 = require("jsonschema");
5
- function WithBody(schema) {
6
- const validator = new jsonschema_1.Validator();
7
- validator.attributes.maxDigits = (instance, sc) => {
8
- if (typeof instance !== 'number')
9
- return undefined;
10
- if (typeof sc.maxDigits !== 'number' || Math.floor(sc.maxDigits) !== sc.maxDigits) {
11
- throw new jsonschema_1.SchemaError('"maxDigits" expects an integer', sc);
12
- }
13
- if (Math.round(instance * 10 ** sc.maxDigits) / (10 ** sc.maxDigits) !== instance) {
14
- return 'has more precision than ' + sc.maxDigits + ' digits';
15
- }
16
- return undefined;
17
- };
18
- return function (_, __, descriptor) {
19
- if (typeof descriptor.value === 'function') {
20
- const previousMethod = descriptor.value;
21
- descriptor.value = function (req, res) {
22
- const keys = Object.getOwnPropertyNames(schema.properties);
23
- keys.forEach(key => {
24
- if (typeof schema.properties[key] === 'string') {
25
- schema.properties[key] = this.config.app.endpointsSchemas[schema.properties[key]];
26
- }
27
- });
28
- if (req.method.toUpperCase() !== 'POST' && req.method.toUpperCase() !== 'PUT') {
29
- return previousMethod.call(this, req, res);
30
- }
31
- if (!req.body) {
32
- res.status(400).json({
33
- error: {
34
- errorKey: 'client.body.missing',
35
- additionalInfo: 'client.extended.badPayload'
36
- }
37
- });
38
- }
39
- else {
40
- const result = validator.validate(req.body, schema);
41
- if (result.valid) {
42
- return previousMethod.call(this, req, res);
43
- }
44
- else {
45
- res.status(400).json({
46
- error: {
47
- errorKey: 'client.body.missing',
48
- additionalInfo: 'client.extended.badPayload',
49
- detailedInfo: result.errors
50
- }
51
- });
52
- }
53
- }
54
- };
55
- return descriptor;
56
- }
57
- return undefined;
58
- };
59
- }
60
- exports.WithBody = WithBody;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WithBody = void 0;
4
+ const jsonschema_1 = require("jsonschema");
5
+ function WithBody(schema) {
6
+ const validator = new jsonschema_1.Validator();
7
+ validator.attributes.maxDigits = (instance, sc) => {
8
+ if (typeof instance !== 'number')
9
+ return undefined;
10
+ if (typeof sc.maxDigits !== 'number' || Math.floor(sc.maxDigits) !== sc.maxDigits) {
11
+ throw new jsonschema_1.SchemaError('"maxDigits" expects an integer', sc);
12
+ }
13
+ if (Math.round(instance * 10 ** sc.maxDigits) / (10 ** sc.maxDigits) !== instance) {
14
+ return 'has more precision than ' + sc.maxDigits + ' digits';
15
+ }
16
+ return undefined;
17
+ };
18
+ return function (_, __, descriptor) {
19
+ if (typeof descriptor.value === 'function') {
20
+ const previousMethod = descriptor.value;
21
+ descriptor.value = function (req, res) {
22
+ const keys = Object.getOwnPropertyNames(schema.properties);
23
+ keys.forEach(key => {
24
+ if (typeof schema.properties[key] === 'string') {
25
+ schema.properties[key] = this.config.app.endpointsSchemas[schema.properties[key]];
26
+ }
27
+ });
28
+ if (req.method.toUpperCase() !== 'POST' && req.method.toUpperCase() !== 'PUT') {
29
+ return previousMethod.call(this, req, res);
30
+ }
31
+ if (!req.body) {
32
+ res.status(400).json({
33
+ error: {
34
+ errorKey: 'client.body.missing',
35
+ additionalInfo: 'client.extended.badPayload'
36
+ }
37
+ });
38
+ }
39
+ else {
40
+ const result = validator.validate(req.body, schema);
41
+ if (result.valid) {
42
+ return previousMethod.call(this, req, res);
43
+ }
44
+ else {
45
+ res.status(400).json({
46
+ error: {
47
+ errorKey: 'client.body.missing',
48
+ additionalInfo: 'client.extended.badPayload',
49
+ detailedInfo: result.errors
50
+ }
51
+ });
52
+ }
53
+ }
54
+ };
55
+ return descriptor;
56
+ }
57
+ return undefined;
58
+ };
59
+ }
60
+ exports.WithBody = WithBody;
@@ -1,56 +1,56 @@
1
- import {Request, Response} from 'express';
2
- import {SchemaError, Validator} from 'jsonschema';
3
-
4
- export function WithBody(schema: any) {
5
- const validator = new Validator();
6
- validator.attributes.maxDigits = (instance, sc: any) => {
7
- if(typeof instance !== 'number') return undefined;
8
- if(typeof sc.maxDigits !== 'number' || Math.floor(sc.maxDigits) !== sc.maxDigits) {
9
- throw new SchemaError('"maxDigits" expects an integer', sc);
10
- }
11
- if(Math.round(instance * 10 ** sc.maxDigits) / (10 ** sc.maxDigits) !== instance) {
12
- return 'has more precision than ' + sc.maxDigits + ' digits';
13
- }
14
- return undefined;
15
- };
16
-
17
- return function(_: any, __: string, descriptor: PropertyDescriptor) {
18
- if(typeof descriptor.value === 'function') {
19
- const previousMethod = descriptor.value;
20
- descriptor.value = function(this: any, req: Request, res: Response) {
21
- const keys = Object.getOwnPropertyNames(schema.properties);
22
- keys.forEach(key => {
23
- if(typeof schema.properties[key] === 'string') {
24
- schema.properties[key] = this.config.app.endpointsSchemas[schema.properties[key]];
25
- }
26
- });
27
- if(req.method.toUpperCase() !== 'POST' && req.method.toUpperCase() !== 'PUT') {
28
- return previousMethod.call(this, req, res);
29
- }
30
- if(!req.body) {
31
- res.status(400).json({
32
- error: {
33
- errorKey: 'client.body.missing',
34
- additionalInfo: 'client.extended.badPayload'
35
- }
36
- });
37
- } else {
38
- const result = validator.validate(req.body, schema);
39
- if(result.valid) {
40
- return previousMethod.call(this, req, res);
41
- } else {
42
- res.status(400).json({
43
- error: {
44
- errorKey: 'client.body.missing',
45
- additionalInfo: 'client.extended.badPayload',
46
- detailedInfo: result.errors
47
- }
48
- });
49
- }
50
- }
51
- };
52
- return descriptor;
53
- }
54
- return undefined;
55
- };
56
- }
1
+ import {Request, Response} from 'express';
2
+ import {SchemaError, Validator} from 'jsonschema';
3
+
4
+ export function WithBody(schema: any) {
5
+ const validator = new Validator();
6
+ validator.attributes.maxDigits = (instance, sc: any) => {
7
+ if(typeof instance !== 'number') return undefined;
8
+ if(typeof sc.maxDigits !== 'number' || Math.floor(sc.maxDigits) !== sc.maxDigits) {
9
+ throw new SchemaError('"maxDigits" expects an integer', sc);
10
+ }
11
+ if(Math.round(instance * 10 ** sc.maxDigits) / (10 ** sc.maxDigits) !== instance) {
12
+ return 'has more precision than ' + sc.maxDigits + ' digits';
13
+ }
14
+ return undefined;
15
+ };
16
+
17
+ return function(_: any, __: string, descriptor: PropertyDescriptor) {
18
+ if(typeof descriptor.value === 'function') {
19
+ const previousMethod = descriptor.value;
20
+ descriptor.value = function(this: any, req: Request, res: Response) {
21
+ const keys = Object.getOwnPropertyNames(schema.properties);
22
+ keys.forEach(key => {
23
+ if(typeof schema.properties[key] === 'string') {
24
+ schema.properties[key] = this.config.app.endpointsSchemas[schema.properties[key]];
25
+ }
26
+ });
27
+ if(req.method.toUpperCase() !== 'POST' && req.method.toUpperCase() !== 'PUT') {
28
+ return previousMethod.call(this, req, res);
29
+ }
30
+ if(!req.body) {
31
+ res.status(400).json({
32
+ error: {
33
+ errorKey: 'client.body.missing',
34
+ additionalInfo: 'client.extended.badPayload'
35
+ }
36
+ });
37
+ } else {
38
+ const result = validator.validate(req.body, schema);
39
+ if(result.valid) {
40
+ return previousMethod.call(this, req, res);
41
+ } else {
42
+ res.status(400).json({
43
+ error: {
44
+ errorKey: 'client.body.missing',
45
+ additionalInfo: 'client.extended.badPayload',
46
+ detailedInfo: result.errors
47
+ }
48
+ });
49
+ }
50
+ }
51
+ };
52
+ return descriptor;
53
+ }
54
+ return undefined;
55
+ };
56
+ }
@@ -1,137 +1,137 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.withTransaction = void 0;
4
- const utils_1 = require("./utils");
5
- function withTransaction(repo, logger, previousMethod, lock) {
6
- return function (req, res, next) {
7
- const endTerminator = res.end.bind(res);
8
- const jsonTerminator = (obj) => {
9
- res.write((0, utils_1.jsonStringify)(obj) || '{}');
10
- endTerminator();
11
- };
12
- const connectTimeoutHandler = setTimeout(() => {
13
- logger.error('Error timed out getting a client, exiting...');
14
- process.exit(22);
15
- }, 3000);
16
- Promise.all([
17
- repo.db ? repo.db.connect() : Promise.resolve(undefined),
18
- repo.dbMssql ? Promise.resolve(repo.dbMssql.transaction()) : Promise.resolve(undefined)
19
- ]).then(([c1, c2]) => {
20
- const dbClient = c1 || c2;
21
- clearTimeout(connectTimeoutHandler);
22
- utils_1.utils.logger = logger;
23
- dbClient.removeListener('error', utils_1.utils.clientErrorHandler);
24
- dbClient.on('error', utils_1.utils.clientErrorHandler);
25
- res.locals.dbClient = dbClient;
26
- res.locals.dbClientCommited = false;
27
- res.locals.dbClientCommit = (cb) => {
28
- if (!res.locals.dbClientCommited) {
29
- res.locals.dbClientCommited = true;
30
- (repo.db ? dbClient.query('COMMIT') : dbClient.commit()).catch((err) => err).then((err) => {
31
- if (repo.db)
32
- dbClient.release();
33
- if (!(err instanceof Error)) {
34
- for (let i = 0; i < res.locals.dbClientOnCommit.length; i++) {
35
- res.locals.dbClientOnCommit[i]();
36
- }
37
- }
38
- cb(err instanceof Error ? err : undefined);
39
- });
40
- }
41
- else {
42
- cb(undefined);
43
- }
44
- };
45
- res.locals.dbClientOnCommit = [];
46
- return (repo.db ? dbClient.query('BEGIN') : dbClient.begin()).then(() => {
47
- const finish = () => {
48
- res.json = (obj) => {
49
- if (res.statusCode > 303 && res.statusCode !== 412) {
50
- if (logger && res.statusCode > 499) {
51
- logger.error('Uncaught 500: %j', obj.error.additionalInfo);
52
- }
53
- (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj.error.additionalInfo2 = { message: err.message }).then(() => {
54
- if (repo.db)
55
- dbClient.release();
56
- jsonTerminator(obj);
57
- });
58
- }
59
- else {
60
- res.locals.dbClientCommit((err) => {
61
- if (err) {
62
- res.status(500);
63
- jsonTerminator({
64
- error: {
65
- errorKey: 'internal.db',
66
- additionalInfo: { message: err.message }
67
- }
68
- });
69
- }
70
- else
71
- jsonTerminator(obj);
72
- });
73
- }
74
- return res;
75
- };
76
- res.end = () => {
77
- if (res.statusCode > 303 && res.statusCode !== 412) {
78
- if (logger && res.statusCode > 499) {
79
- logger.error('Uncaught 500 with no details...');
80
- }
81
- (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch(() => undefined).then(() => {
82
- if (repo.db)
83
- dbClient.release();
84
- endTerminator();
85
- });
86
- }
87
- else {
88
- res.locals.dbClientCommit((err) => {
89
- if (err) {
90
- res.status(500);
91
- jsonTerminator({
92
- error: {
93
- errorKey: 'internal.db',
94
- additionalInfo: { message: err.message }
95
- }
96
- });
97
- }
98
- else
99
- endTerminator();
100
- });
101
- }
102
- return res;
103
- };
104
- return previousMethod.call(this, req, res, next);
105
- };
106
- if (lock) {
107
- dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err) => {
108
- res.status(500).json({
109
- error: {
110
- errorKey: 'internal.db',
111
- additionalInfo: { message: err.message }
112
- }
113
- });
114
- dbClient.release();
115
- });
116
- }
117
- else {
118
- finish();
119
- }
120
- }).catch((err) => {
121
- if (repo.db)
122
- dbClient.release();
123
- else
124
- dbClient.rollback();
125
- throw err;
126
- });
127
- }).catch(err => {
128
- res.status(500).json({
129
- error: {
130
- errorKey: 'internal.db',
131
- additionalInfo: { message: err.message }
132
- }
133
- });
134
- });
135
- };
136
- }
137
- exports.withTransaction = withTransaction;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withTransaction = void 0;
4
+ const utils_1 = require("./utils");
5
+ function withTransaction(repo, logger, previousMethod, lock) {
6
+ return function (req, res, next) {
7
+ const endTerminator = res.end.bind(res);
8
+ const jsonTerminator = (obj) => {
9
+ res.write((0, utils_1.jsonStringify)(obj) || '{}');
10
+ endTerminator();
11
+ };
12
+ const connectTimeoutHandler = setTimeout(() => {
13
+ logger.error('Error timed out getting a client, exiting...');
14
+ process.exit(22);
15
+ }, 3000);
16
+ Promise.all([
17
+ repo.db ? repo.db.connect() : Promise.resolve(undefined),
18
+ repo.dbMssql ? Promise.resolve(repo.dbMssql.transaction()) : Promise.resolve(undefined)
19
+ ]).then(([c1, c2]) => {
20
+ const dbClient = c1 || c2;
21
+ clearTimeout(connectTimeoutHandler);
22
+ utils_1.utils.logger = logger;
23
+ dbClient.removeListener('error', utils_1.utils.clientErrorHandler);
24
+ dbClient.on('error', utils_1.utils.clientErrorHandler);
25
+ res.locals.dbClient = dbClient;
26
+ res.locals.dbClientCommited = false;
27
+ res.locals.dbClientCommit = (cb) => {
28
+ if (!res.locals.dbClientCommited) {
29
+ res.locals.dbClientCommited = true;
30
+ (repo.db ? dbClient.query('COMMIT') : dbClient.commit()).catch((err) => err).then((err) => {
31
+ if (repo.db)
32
+ dbClient.release();
33
+ if (!(err instanceof Error)) {
34
+ for (let i = 0; i < res.locals.dbClientOnCommit.length; i++) {
35
+ res.locals.dbClientOnCommit[i]();
36
+ }
37
+ }
38
+ cb(err instanceof Error ? err : undefined);
39
+ });
40
+ }
41
+ else {
42
+ cb(undefined);
43
+ }
44
+ };
45
+ res.locals.dbClientOnCommit = [];
46
+ return (repo.db ? dbClient.query('BEGIN') : dbClient.begin()).then(() => {
47
+ const finish = () => {
48
+ res.json = (obj) => {
49
+ if (res.statusCode > 303 && res.statusCode !== 412) {
50
+ if (logger && res.statusCode > 499) {
51
+ logger.error('Uncaught 500: %j', obj.error.additionalInfo);
52
+ }
53
+ (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj.error.additionalInfo2 = { message: err.message }).then(() => {
54
+ if (repo.db)
55
+ dbClient.release();
56
+ jsonTerminator(obj);
57
+ });
58
+ }
59
+ else {
60
+ res.locals.dbClientCommit((err) => {
61
+ if (err) {
62
+ res.status(500);
63
+ jsonTerminator({
64
+ error: {
65
+ errorKey: 'internal.db',
66
+ additionalInfo: { message: err.message }
67
+ }
68
+ });
69
+ }
70
+ else
71
+ jsonTerminator(obj);
72
+ });
73
+ }
74
+ return res;
75
+ };
76
+ res.end = () => {
77
+ if (res.statusCode > 303 && res.statusCode !== 412) {
78
+ if (logger && res.statusCode > 499) {
79
+ logger.error('Uncaught 500 with no details...');
80
+ }
81
+ (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch(() => undefined).then(() => {
82
+ if (repo.db)
83
+ dbClient.release();
84
+ endTerminator();
85
+ });
86
+ }
87
+ else {
88
+ res.locals.dbClientCommit((err) => {
89
+ if (err) {
90
+ res.status(500);
91
+ jsonTerminator({
92
+ error: {
93
+ errorKey: 'internal.db',
94
+ additionalInfo: { message: err.message }
95
+ }
96
+ });
97
+ }
98
+ else
99
+ endTerminator();
100
+ });
101
+ }
102
+ return res;
103
+ };
104
+ return previousMethod.call(this, req, res, next);
105
+ };
106
+ if (lock) {
107
+ dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err) => {
108
+ res.status(500).json({
109
+ error: {
110
+ errorKey: 'internal.db',
111
+ additionalInfo: { message: err.message }
112
+ }
113
+ });
114
+ dbClient.release();
115
+ });
116
+ }
117
+ else {
118
+ finish();
119
+ }
120
+ }).catch((err) => {
121
+ if (repo.db)
122
+ dbClient.release();
123
+ else
124
+ dbClient.rollback();
125
+ throw err;
126
+ });
127
+ }).catch(err => {
128
+ res.status(500).json({
129
+ error: {
130
+ errorKey: 'internal.db',
131
+ additionalInfo: { message: err.message }
132
+ }
133
+ });
134
+ });
135
+ };
136
+ }
137
+ exports.withTransaction = withTransaction;