anote-server-libs 0.4.8 → 0.5.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.
@@ -1,63 +1,63 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WithBody = exports.ajv = void 0;
4
- const ajv_1 = require("ajv");
5
- exports.ajv = new ajv_1.default();
6
- exports.ajv.addKeyword({
7
- keyword: 'maxDigits',
8
- type: 'number',
9
- schemaType: 'number',
10
- validate: (schema, data) => typeof data === 'number' && Math.round(data * 10 ** schema) / (10 ** schema) === data
11
- });
12
- function WithBody(schema) {
13
- if (schema.type === 'object') {
14
- const required = [];
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 = exports.ajv.compile(schema);
27
- return function (_, __, descriptor) {
28
- if (typeof descriptor.value === 'function') {
29
- const previousMethod = descriptor.value;
30
- descriptor.value = function (req, res) {
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
- }
42
- else {
43
- const valid = validate(req.body);
44
- if (valid) {
45
- return previousMethod.call(this, req, res);
46
- }
47
- else {
48
- res.status(400).json({
49
- error: {
50
- errorKey: 'client.body.missing',
51
- additionalInfo: 'client.extended.badPayload',
52
- detailedInfo: validate.errors
53
- }
54
- });
55
- }
56
- }
57
- };
58
- return descriptor;
59
- }
60
- return undefined;
61
- };
62
- }
63
- exports.WithBody = WithBody;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WithBody = exports.ajv = void 0;
4
+ const ajv_1 = require("ajv");
5
+ exports.ajv = new ajv_1.default();
6
+ exports.ajv.addKeyword({
7
+ keyword: 'maxDigits',
8
+ type: 'number',
9
+ schemaType: 'number',
10
+ validate: (schema, data) => typeof data === 'number' && Math.round(data * 10 ** schema) / (10 ** schema) === data
11
+ });
12
+ function WithBody(schema) {
13
+ if (schema.type === 'object') {
14
+ const required = [];
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 = exports.ajv.compile(schema);
27
+ return function (_, __, descriptor) {
28
+ if (typeof descriptor.value === 'function') {
29
+ const previousMethod = descriptor.value;
30
+ descriptor.value = function (req, res) {
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
+ }
42
+ else {
43
+ const valid = validate(req.body);
44
+ if (valid) {
45
+ return previousMethod.call(this, req, res);
46
+ }
47
+ else {
48
+ res.status(400).json({
49
+ error: {
50
+ errorKey: 'client.body.missing',
51
+ additionalInfo: 'client.extended.badPayload',
52
+ detailedInfo: validate.errors
53
+ }
54
+ });
55
+ }
56
+ }
57
+ };
58
+ return descriptor;
59
+ }
60
+ return undefined;
61
+ };
62
+ }
63
+ exports.WithBody = WithBody;
@@ -1,60 +1,60 @@
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) {
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
- const valid = validate(req.body);
43
- if(valid) {
44
- return previousMethod.call(this, req, res);
45
- } else {
46
- res.status(400).json({
47
- error: {
48
- errorKey: 'client.body.missing',
49
- additionalInfo: 'client.extended.badPayload',
50
- detailedInfo: validate.errors
51
- }
52
- });
53
- }
54
- }
55
- };
56
- return descriptor;
57
- }
58
- return undefined;
59
- };
60
- }
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) {
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
+ const valid = validate(req.body);
43
+ if(valid) {
44
+ return previousMethod.call(this, req, res);
45
+ } else {
46
+ res.status(400).json({
47
+ error: {
48
+ errorKey: 'client.body.missing',
49
+ additionalInfo: 'client.extended.badPayload',
50
+ detailedInfo: validate.errors
51
+ }
52
+ });
53
+ }
54
+ }
55
+ };
56
+ return descriptor;
57
+ }
58
+ return undefined;
59
+ };
60
+ }
@@ -1,136 +1,136 @@
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
- }, 15000);
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) {
51
- if (res.statusCode > 499) {
52
- logger.error('Uncaught 500: %j', obj.error.additionalInfo);
53
- }
54
- else {
55
- logger.warn('Client error 4XX: %j', obj.error);
56
- }
57
- }
58
- (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj.error.additionalInfo2 = { message: err.message }).then(() => {
59
- if (repo.db)
60
- dbClient.release();
61
- jsonTerminator(obj);
62
- });
63
- }
64
- else {
65
- res.locals.dbClientCommit((err) => {
66
- if (err) {
67
- res.status(500);
68
- jsonTerminator({
69
- error: {
70
- errorKey: 'internal.db',
71
- additionalInfo: { message: err.message }
72
- }
73
- });
74
- }
75
- else
76
- jsonTerminator(obj);
77
- });
78
- }
79
- return res;
80
- };
81
- res.end = () => {
82
- if (res.statusCode > 303 && res.statusCode !== 412) {
83
- if (logger && res.statusCode > 499) {
84
- logger.error('Uncaught 500 with no details...');
85
- }
86
- (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch(() => undefined).then(() => {
87
- if (repo.db)
88
- dbClient.release();
89
- endTerminator();
90
- });
91
- }
92
- else {
93
- res.locals.dbClientCommit((err) => {
94
- if (err) {
95
- res.status(500);
96
- jsonTerminator({
97
- error: {
98
- errorKey: 'internal.db',
99
- additionalInfo: { message: err.message }
100
- }
101
- });
102
- }
103
- else
104
- endTerminator();
105
- });
106
- }
107
- return res;
108
- };
109
- return previousMethod.call(this, req, res, next);
110
- };
111
- if (lock) {
112
- dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err) => {
113
- res.status(500).json({
114
- error: {
115
- errorKey: 'internal.db',
116
- additionalInfo: { message: err.message }
117
- }
118
- });
119
- dbClient.release();
120
- });
121
- }
122
- else {
123
- finish();
124
- }
125
- });
126
- }).catch(err => {
127
- res.status(500).json({
128
- error: {
129
- errorKey: 'internal.db',
130
- additionalInfo: { message: err.message }
131
- }
132
- });
133
- });
134
- };
135
- }
136
- 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
+ }, 15000);
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) {
51
+ if (res.statusCode > 499) {
52
+ logger.error('Uncaught 500: %j', obj.error.additionalInfo);
53
+ }
54
+ else {
55
+ logger.warn('Client error 4XX: %j', obj.error);
56
+ }
57
+ }
58
+ (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj.error.additionalInfo2 = { message: err.message }).then(() => {
59
+ if (repo.db)
60
+ dbClient.release();
61
+ jsonTerminator(obj);
62
+ });
63
+ }
64
+ else {
65
+ res.locals.dbClientCommit((err) => {
66
+ if (err) {
67
+ res.status(500);
68
+ jsonTerminator({
69
+ error: {
70
+ errorKey: 'internal.db',
71
+ additionalInfo: { message: err.message }
72
+ }
73
+ });
74
+ }
75
+ else
76
+ jsonTerminator(obj);
77
+ });
78
+ }
79
+ return res;
80
+ };
81
+ res.end = () => {
82
+ if (res.statusCode > 303 && res.statusCode !== 412) {
83
+ if (logger && res.statusCode > 499) {
84
+ logger.error('Uncaught 500 with no details...');
85
+ }
86
+ (repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch(() => undefined).then(() => {
87
+ if (repo.db)
88
+ dbClient.release();
89
+ endTerminator();
90
+ });
91
+ }
92
+ else {
93
+ res.locals.dbClientCommit((err) => {
94
+ if (err) {
95
+ res.status(500);
96
+ jsonTerminator({
97
+ error: {
98
+ errorKey: 'internal.db',
99
+ additionalInfo: { message: err.message }
100
+ }
101
+ });
102
+ }
103
+ else
104
+ endTerminator();
105
+ });
106
+ }
107
+ return res;
108
+ };
109
+ return previousMethod.call(this, req, res, next);
110
+ };
111
+ if (lock) {
112
+ dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err) => {
113
+ res.status(500).json({
114
+ error: {
115
+ errorKey: 'internal.db',
116
+ additionalInfo: { message: err.message }
117
+ }
118
+ });
119
+ dbClient.release();
120
+ });
121
+ }
122
+ else {
123
+ finish();
124
+ }
125
+ });
126
+ }).catch(err => {
127
+ res.status(500).json({
128
+ error: {
129
+ errorKey: 'internal.db',
130
+ additionalInfo: { message: err.message }
131
+ }
132
+ });
133
+ });
134
+ };
135
+ }
136
+ exports.withTransaction = withTransaction;