anote-server-libs 0.12.2 → 0.12.4

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,65 +1,65 @@
1
- import {Request, Response} from 'express';
2
- import Ajv, {_} from 'ajv';
3
-
4
- export const ajv = new Ajv();
5
- ajv.addKeyword({
6
- keyword: 'maxDigits',
7
- type: 'number',
8
- schemaType: 'number',
9
- validate: (schema: number, data: any) => typeof data === 'number' && Math.round(data * 10 ** schema) / (10 ** schema) === data
10
- });
11
-
12
- export function WithBody(schema: any, collapseTopLevelNulls = true) {
13
- if(schema.type === 'object') {
14
- const required: string[] = [];
15
- const keys = Object.getOwnPropertyNames(schema.properties);
16
- keys.forEach(key => {
17
- if(schema.properties[key].required === true) {
18
- required.push(key);
19
- }
20
- if(schema.properties[key].required === true || schema.properties[key].required === false) {
21
- delete schema.properties[key].required;
22
- }
23
- });
24
- schema.required = required;
25
- }
26
- const validate = ajv.compile(schema);
27
- return function(_: any, __: string, descriptor: PropertyDescriptor) {
28
- if(typeof descriptor.value === 'function') {
29
- const previousMethod = descriptor.value;
30
- descriptor.value = function(this: any, req: Request, res: Response) {
31
- if(req.method.toUpperCase() !== 'POST' && req.method.toUpperCase() !== 'PUT') {
32
- return previousMethod.call(this, req, res);
33
- }
34
- if(!req.body) {
35
- res.status(400).json({
36
- error: {
37
- errorKey: 'client.body.missing',
38
- additionalInfo: 'client.extended.badPayload'
39
- }
40
- });
41
- } else {
42
- if(collapseTopLevelNulls) {
43
- Object.keys(req.body).forEach(k => {
44
- if(req.body[k] === null) delete req.body[k];
45
- });
46
- }
47
- const valid = validate(req.body);
48
- if(valid) {
49
- return previousMethod.call(this, req, res);
50
- } else {
51
- res.status(400).json({
52
- error: {
53
- errorKey: 'client.body.missing',
54
- additionalInfo: 'client.extended.badPayload',
55
- detailedInfo: validate.errors
56
- }
57
- });
58
- }
59
- }
60
- };
61
- return descriptor;
62
- }
63
- return undefined;
64
- };
65
- }
1
+ import {Request, Response} from 'express';
2
+ import Ajv, {_} from 'ajv';
3
+
4
+ export const ajv = new Ajv();
5
+ ajv.addKeyword({
6
+ keyword: 'maxDigits',
7
+ type: 'number',
8
+ schemaType: 'number',
9
+ validate: (schema: number, data: any) => typeof data === 'number' && Math.round(data * 10 ** schema) / (10 ** schema) === data
10
+ });
11
+
12
+ export function WithBody(schema: any, collapseTopLevelNulls = true) {
13
+ if(schema.type === 'object') {
14
+ const required: string[] = [];
15
+ const keys = Object.getOwnPropertyNames(schema.properties);
16
+ keys.forEach(key => {
17
+ if(schema.properties[key].required === true) {
18
+ required.push(key);
19
+ }
20
+ if(schema.properties[key].required === true || schema.properties[key].required === false) {
21
+ delete schema.properties[key].required;
22
+ }
23
+ });
24
+ schema.required = required;
25
+ }
26
+ const validate = ajv.compile(schema);
27
+ return function(_: any, __: string, descriptor: PropertyDescriptor) {
28
+ if(typeof descriptor.value === 'function') {
29
+ const previousMethod = descriptor.value;
30
+ descriptor.value = function(this: any, req: Request, res: Response) {
31
+ if(req.method.toUpperCase() !== 'POST' && req.method.toUpperCase() !== 'PUT') {
32
+ return previousMethod.call(this, req, res);
33
+ }
34
+ if(!req.body) {
35
+ res.status(400).json({
36
+ error: {
37
+ errorKey: 'client.body.missing',
38
+ additionalInfo: 'client.extended.badPayload'
39
+ }
40
+ });
41
+ } else {
42
+ if(collapseTopLevelNulls) {
43
+ Object.keys(req.body).forEach(k => {
44
+ if(req.body[k] === null) delete req.body[k];
45
+ });
46
+ }
47
+ const valid = validate(req.body);
48
+ if(valid) {
49
+ return previousMethod.call(this, req, res);
50
+ } else {
51
+ res.status(400).json({
52
+ error: {
53
+ errorKey: 'client.body.missing',
54
+ additionalInfo: 'client.extended.badPayload',
55
+ detailedInfo: validate.errors
56
+ }
57
+ });
58
+ }
59
+ }
60
+ };
61
+ return descriptor;
62
+ }
63
+ return undefined;
64
+ };
65
+ }
@@ -59,7 +59,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
59
59
  return new Promise((resolve, reject) => {
60
60
  exports.withTransactionConfig.gateQueue.push({ resolve, reject });
61
61
  }).then(() => {
62
- return withTransaction(repo, logger, previousMethod, lock, commitIfLost)(req, res, next);
62
+ return withTransaction(repo, logger, previousMethod, lock, commitIfLost, idleInTransactionTimeout).call(this, req, res, next);
63
63
  });
64
64
  }
65
65
  let commit = true;
@@ -141,10 +141,10 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
141
141
  if (res.statusCode > 303 && res.statusCode !== 412) {
142
142
  if (logger) {
143
143
  if (res.statusCode > 499) {
144
- logger.error('Uncaught 500 at %s %s: %j', req.method, req.originalUrl, { body: obj, stack: new Error().stack });
144
+ logger.error('Uncaught 500 at %s %s: %j', req?.method, req?.originalUrl, { body: obj, stack: new Error().stack });
145
145
  }
146
146
  else {
147
- logger.warn('Client error 4XX at %s %s: %j', req.method, req.originalUrl, { body: obj, stack: new Error().stack });
147
+ logger.warn('Client error 4XX at %s %s: %j', req?.method, req?.originalUrl, { body: obj, stack: new Error().stack });
148
148
  }
149
149
  }
150
150
  (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj && obj.error && (obj.error.additionalInfo2 = { message: err.message })).then(() => {
@@ -172,7 +172,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
172
172
  res.end = () => {
173
173
  if (res.statusCode > 303 && res.statusCode !== 412) {
174
174
  if (logger && res.statusCode > 499) {
175
- logger.error('Uncaught 500 with no details at %s %s: %j', req.method, req.originalUrl, { stack: new Error().stack });
175
+ logger.error('Uncaught 500 with no details at %s %s: %j', req?.method, req?.originalUrl, { stack: new Error().stack });
176
176
  }
177
177
  (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch(() => undefined).then(() => {
178
178
  releaseAndDecrement(repo, dbClient);
@@ -200,7 +200,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
200
200
  };
201
201
  if (lock) {
202
202
  dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err) => {
203
- logger.error('Error acquiring advisory lock %d at %s %s: %j', lock, req.method, req.originalUrl, { err, stack: new Error().stack });
203
+ logger.error('Error acquiring advisory lock %d at %s %s: %j', lock, req?.method, req?.originalUrl, { err, stack: new Error().stack });
204
204
  res.status(500).json({
205
205
  error: {
206
206
  errorKey: 'internal.db',
@@ -215,7 +215,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
215
215
  }
216
216
  }).catch((err) => {
217
217
  releaseAndDecrement(repo, dbClient);
218
- logger.error('Error beginning transaction at %s %s: %j', req.method, req.originalUrl, { err, stack: new Error().stack });
218
+ logger.error('Error beginning transaction at %s %s: %j', req?.method, req?.originalUrl, { err, stack: new Error().stack });
219
219
  res.status(500).json({
220
220
  error: {
221
221
  errorKey: 'internal.db',
@@ -225,7 +225,7 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
225
225
  });
226
226
  }).catch(err => {
227
227
  clearTimeout(connectTimeoutHandler);
228
- logger.error('Error connecting to database at %s %s: %j', req.method, req.originalUrl, { err, stack: new Error().stack });
228
+ logger.error('Error connecting to database at %s %s: %j', req?.method, req?.originalUrl, { err, stack: new Error().stack });
229
229
  exports.withTransactionConfig.activeCount = Math.max(0, exports.withTransactionConfig.activeCount - 1);
230
230
  res.status(500).json({
231
231
  error: {