@ellipticltd/aml-utils 0.0.3 → 0.0.9

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.
Files changed (55) hide show
  1. package/.eslintrc +3 -0
  2. package/.nvmrc +1 -0
  3. package/README.md +8 -0
  4. package/index.js +8 -12
  5. package/lib/errors.js +53 -51
  6. package/lib/formatting.js +15 -25
  7. package/lib/middleware.js +20 -28
  8. package/lib/ormHelpers.js +17 -26
  9. package/lib/types.js +202 -170
  10. package/lib/validations.js +189 -174
  11. package/package.json +22 -7
  12. package/.idea/aml-utils.iml +0 -12
  13. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  14. package/.idea/misc.xml +0 -6
  15. package/.idea/modules.xml +0 -8
  16. package/.idea/vcs.xml +0 -6
  17. package/.idea/workspace.xml +0 -283
  18. package/dist/errors/errors.d.ts +0 -9
  19. package/dist/errors/errors.js +0 -42
  20. package/dist/errors/errors.spec.d.ts +0 -1
  21. package/dist/errors/errors.spec.js +0 -23
  22. package/dist/file-parser/__tests/file-parser.spec.d.ts +0 -1
  23. package/dist/file-parser/__tests/file-parser.spec.js +0 -118
  24. package/dist/file-parser/__tests/parse-row.spec.d.ts +0 -1
  25. package/dist/file-parser/__tests/parse-row.spec.js +0 -29
  26. package/dist/file-parser/__tests/sanitize-rows.spec.d.ts +0 -1
  27. package/dist/file-parser/__tests/sanitize-rows.spec.js +0 -78
  28. package/dist/file-parser/errors.d.ts +0 -3
  29. package/dist/file-parser/errors.js +0 -11
  30. package/dist/file-parser/file-parser.d.ts +0 -8
  31. package/dist/file-parser/file-parser.js +0 -68
  32. package/dist/file-parser/parse-row.d.ts +0 -2
  33. package/dist/file-parser/parse-row.js +0 -39
  34. package/dist/file-parser/sanitzeRows.d.ts +0 -3
  35. package/dist/file-parser/sanitzeRows.js +0 -18
  36. package/dist/formatting/formatting.d.ts +0 -2
  37. package/dist/formatting/formatting.js +0 -17
  38. package/dist/formatting/formatting.spec.d.ts +0 -1
  39. package/dist/formatting/formatting.spec.js +0 -37
  40. package/dist/index.d.ts +0 -8
  41. package/dist/index.js +0 -20
  42. package/dist/middleware/middleware.d.ts +0 -4
  43. package/dist/middleware/middleware.js +0 -22
  44. package/dist/orm-helpers/ormHelpers.d.ts +0 -1
  45. package/dist/orm-helpers/ormHelpers.js +0 -17
  46. package/dist/orm-helpers/ormHelpers.spec.d.ts +0 -1
  47. package/dist/orm-helpers/ormHelpers.spec.js +0 -38
  48. package/dist/types/types.d.ts +0 -17
  49. package/dist/types/types.js +0 -203
  50. package/dist/validations/old.validations.d.ts +0 -5
  51. package/dist/validations/old.validations.js +0 -303
  52. package/dist/validations/validations.d.ts +0 -118
  53. package/dist/validations/validations.js +0 -300
  54. package/dist/validations/validations.spec.d.ts +0 -1
  55. package/dist/validations/validations.spec.js +0 -287
package/.eslintrc ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": ["@ellipticltd"]
3
+ }
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 10.1.0
package/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # Elliptic aml-utils
2
+
3
+ This is a library that contains a set of utilities used by aml-api. In particular:
4
+
5
+ - Custom errors used when returning a response from aml-api, e.g. BadRequest, Forbidden etc.
6
+ - Formatting functions (sql escaping)
7
+ - Validator functions, also used by swagger node middleware. E.g. Bitcoin and Ethereum addresses validators
8
+ - Express middlewares to validate params types
package/index.js CHANGED
@@ -1,12 +1,8 @@
1
- // Generated by CoffeeScript 1.10.0
2
- (function() {
3
- module.exports = {
4
- types: require('./lib/types'),
5
- validations: require('./lib/validations'),
6
- errors: require('./lib/errors'),
7
- formatting: require('./lib/formatting'),
8
- middleware: require('./lib/middleware'),
9
- ormHelpers: require('./lib/ormHelpers')
10
- };
11
-
12
- }).call(this);
1
+ module.exports = {
2
+ types: require('./lib/types'),
3
+ validations: require('./lib/validations'),
4
+ errors: require('./lib/errors'),
5
+ formatting: require('./lib/formatting'),
6
+ middleware: require('./lib/middleware'),
7
+ ormHelpers: require('./lib/ormHelpers'),
8
+ };
package/lib/errors.js CHANGED
@@ -1,51 +1,53 @@
1
- // Generated by CoffeeScript 1.10.0
2
- (function() {
3
- var AppError, BadRequest, Forbidden, InvalidArguments, NotFound, RequestError, ServerError, Unauthorized, create;
4
-
5
- create = require('create-error');
6
-
7
- AppError = create('AppError');
8
-
9
- RequestError = create(AppError, 'RequestError');
10
-
11
- RequestError.toJSON = function() {
12
- return {
13
- message: this.message
14
- };
15
- };
16
-
17
- Forbidden = create(RequestError, 'ForbiddenError', {
18
- status: 403
19
- });
20
-
21
- Unauthorized = create(RequestError, 'UnauthorizedError', {
22
- status: 401
23
- });
24
-
25
- BadRequest = create(RequestError, 'BadRequestError', {
26
- status: 400
27
- });
28
-
29
- NotFound = create(RequestError, 'NotFoundError', {
30
- status: 404
31
- });
32
-
33
- ServerError = create(RequestError, 'ServerError', {
34
- status: 500
35
- });
36
-
37
- InvalidArguments = create(RequestError, 'InvalidArgumentsError', {
38
- status: 500
39
- });
40
-
41
- module.exports = {
42
- RequestError: RequestError,
43
- ServerError: ServerError,
44
- Forbidden: Forbidden,
45
- Unauthorized: Unauthorized,
46
- BadRequest: BadRequest,
47
- NotFound: NotFound,
48
- InvalidArguments: InvalidArguments
49
- };
50
-
51
- }).call(this);
1
+ const create = require('create-error');
2
+
3
+ const AppError = create('AppError');
4
+
5
+ const RequestError = create(AppError, 'RequestError');
6
+
7
+ RequestError.toJSON = () => ({
8
+ message: this.message,
9
+ });
10
+
11
+ const Forbidden = create(RequestError, 'ForbiddenError', {
12
+ status: 403,
13
+ });
14
+
15
+ const Unauthorized = create(RequestError, 'UnauthorizedError', {
16
+ status: 401,
17
+ });
18
+
19
+ const BadRequest = create(RequestError, 'BadRequestError', {
20
+ status: 400,
21
+ });
22
+
23
+ const NotFound = create(RequestError, 'NotFoundError', {
24
+ status: 404,
25
+ });
26
+
27
+ const ConflictError = create(RequestError, 'ConflictError', {
28
+ status: 409,
29
+ });
30
+
31
+ const ServerError = create(RequestError, 'ServerError', {
32
+ status: 500,
33
+ });
34
+
35
+ const InvalidArguments = create(RequestError, 'InvalidArgumentsError', {
36
+ status: 500,
37
+ });
38
+
39
+ const ServerTimeout = create(RequestError, 'ServerTimeoutError', {
40
+ status: 503,
41
+ });
42
+
43
+ module.exports = {
44
+ RequestError,
45
+ ServerError,
46
+ Forbidden,
47
+ Unauthorized,
48
+ BadRequest,
49
+ NotFound,
50
+ ConflictError,
51
+ InvalidArguments,
52
+ ServerTimeout,
53
+ };
package/lib/formatting.js CHANGED
@@ -1,28 +1,18 @@
1
- // Generated by CoffeeScript 1.10.0
2
- (function() {
3
- var _, rethrowError, sqlEscapeWildcard;
1
+ const _ = require('lodash');
4
2
 
5
- _ = require('lodash');
3
+ const rethrowError = (fnName, e) => {
4
+ let formattedError;
5
+ if (_.isPlainObject(e.error) || _.isArray(e.error)) {
6
+ formattedError = `\n${JSON.stringify(e.error, null, 2)}`;
7
+ } else {
8
+ formattedError = e.error;
9
+ }
10
+ return `${fnName} - ${e.name}: ${e.statusCode} - ${formattedError}`;
11
+ };
6
12
 
7
- rethrowError = function(fnName, e) {
8
- var formattedError;
9
- if (_.isPlainObject(e.error) || _.isArray(e.error)) {
10
- formattedError = "\n" + (JSON.stringify(e.error, null, 2));
11
- } else {
12
- formattedError = e.error;
13
- }
14
- return fnName + " - " + e.name + ": " + e.statusCode + " - " + formattedError;
15
- };
13
+ const sqlEscapeWildcard = str => str.replace(/_|%|\\/g, x => `\\${x}`);
16
14
 
17
- sqlEscapeWildcard = function(str) {
18
- return str.replace(/_|%|\\/g, function(x) {
19
- return "\\" + x;
20
- });
21
- };
22
-
23
- module.exports = {
24
- rethrowError: rethrowError,
25
- sqlEscapeWildcard: sqlEscapeWildcard
26
- };
27
-
28
- }).call(this);
15
+ module.exports = {
16
+ rethrowError,
17
+ sqlEscapeWildcard,
18
+ };
package/lib/middleware.js CHANGED
@@ -1,33 +1,25 @@
1
- // Generated by CoffeeScript 1.10.0
2
- (function() {
3
- var T, ensureHex32, ensureInteger, ensureType, ensureUUID;
1
+ const T = require('./types');
4
2
 
5
- T = require('./types');
3
+ const ensureType = type => (req, res, next, id, name) => {
4
+ let error;
5
+ try {
6
+ T.ensureType(type, id, `Invalid ${name}`);
7
+ return next();
8
+ } catch (error1) {
9
+ error = error1;
10
+ return next(error);
11
+ }
12
+ };
6
13
 
7
- ensureType = function(type) {
8
- return function(req, res, next, id, name) {
9
- var error, error1;
10
- try {
11
- T.ensureType(type, id, "Invalid " + name);
12
- return next();
13
- } catch (error1) {
14
- error = error1;
15
- return next(error);
16
- }
17
- };
18
- };
14
+ const ensureUUID = ensureType('UUID');
19
15
 
20
- ensureUUID = ensureType('UUID');
16
+ const ensureHex32 = ensureType('Hex32');
21
17
 
22
- ensureHex32 = ensureType('Hex32');
18
+ const ensureInteger = ensureType('IntString');
23
19
 
24
- ensureInteger = ensureType('IntString');
25
-
26
- module.exports = {
27
- ensureType: ensureType,
28
- ensureUUID: ensureUUID,
29
- ensureHex32: ensureHex32,
30
- ensureInteger: ensureInteger
31
- };
32
-
33
- }).call(this);
20
+ module.exports = {
21
+ ensureType,
22
+ ensureUUID,
23
+ ensureHex32,
24
+ ensureInteger,
25
+ };
package/lib/ormHelpers.js CHANGED
@@ -1,28 +1,19 @@
1
- // Generated by CoffeeScript 1.10.0
2
- (function() {
3
- var _, includeNested;
1
+ const _ = require('lodash');
4
2
 
5
- _ = require('lodash');
3
+ const includeNested = (orm, models) =>
4
+ _.map(_.toPairs(models), (arg) => {
5
+ const [modelName, inclusion] = arg;
6
+ const include = includeNested(orm, inclusion);
7
+ const model = orm[modelName];
8
+ if (_.isEmpty(include)) {
9
+ return model;
10
+ }
11
+ return {
12
+ model,
13
+ include,
14
+ };
15
+ });
6
16
 
7
- includeNested = function(orm, models) {
8
- return _.map(_.toPairs(models), function(arg) {
9
- var include, inclusion, model, modelName;
10
- modelName = arg[0], inclusion = arg[1];
11
- include = includeNested(orm, inclusion);
12
- model = orm[modelName];
13
- if (_.isEmpty(include)) {
14
- return model;
15
- } else {
16
- return {
17
- model: model,
18
- include: include
19
- };
20
- }
21
- });
22
- };
23
-
24
- module.exports = {
25
- includeNested: includeNested
26
- };
27
-
28
- }).call(this);
17
+ module.exports = {
18
+ includeNested,
19
+ };
package/lib/types.js CHANGED
@@ -1,179 +1,211 @@
1
- // Generated by CoffeeScript 1.10.0
2
- (function() {
3
- var E, TC, V, _, customTypes;
1
+ const TC = require('type-check');
4
2
 
5
- TC = require('type-check');
3
+ const E = require('./errors');
6
4
 
7
- E = require('./errors');
5
+ const V = require('./validations');
8
6
 
9
- V = require('./validations');
7
+ const _ = require('lodash');
10
8
 
11
- _ = require('lodash');
9
+ const customTypes = {
10
+ Integer: {
11
+ typeOf: 'Number',
12
+ validate(x) {
13
+ return x % 1 === 0;
14
+ },
15
+ },
16
+ IntString: {
17
+ typeOf: 'String',
18
+ validate: V.isInt,
19
+ },
20
+ BoolString: {
21
+ typeOf: 'String',
22
+ validate: V.isBoolean,
23
+ },
24
+ Hex32: {
25
+ typeOf: 'String',
26
+ validate(x) {
27
+ return V.isHexadecimal(x) && x.length === 32;
28
+ },
29
+ },
30
+ Email: {
31
+ typeOf: 'String',
32
+ validate: V.isEmail,
33
+ },
34
+ UUID: {
35
+ typeOf: 'String',
36
+ validate: V.isUUID,
37
+ },
38
+ UUIDv4: {
39
+ typeOf: 'String',
40
+ validate(x) {
41
+ return V.isUUID(x, 4);
42
+ },
43
+ },
44
+ JSON: {
45
+ typeOf: 'String',
46
+ validate: V.isJSON,
47
+ },
48
+ Url: {
49
+ typeOf: 'String',
50
+ validate: V.isURL,
51
+ },
52
+ DateString: {
53
+ typeOf: 'String',
54
+ validate: V.isDate,
55
+ },
56
+ NonEmptyArray: {
57
+ typeOf: 'Array',
58
+ validate: V.nonEmpty,
59
+ },
60
+ NonNullString: {
61
+ typeOf: 'String',
62
+ validate: V.nonEmpty,
63
+ },
64
+ CustomerReference: {
65
+ typeOf: 'String',
66
+ validate: V.isCustomerReference,
67
+ },
68
+ CustomerLabelName: {
69
+ typeOf: 'String',
70
+ validate: V.isCustomerLabelName,
71
+ },
72
+ BitcoinAddress: {
73
+ typeOf: 'String',
74
+ validate: V.bitcoin.isAddress,
75
+ },
76
+ EthereumAddress: {
77
+ typeOf: 'String',
78
+ validate: V.ethereum.isAddress,
79
+ },
80
+ EthereumTx: {
81
+ typeOf: 'String',
82
+ validate: V.ethereum.isTxHash,
83
+ },
84
+ EthereumBlockHash: {
85
+ typeOf: 'String',
86
+ validate: V.ethereum.isBlockHash,
87
+ },
88
+ EthereumWeiAmount: {
89
+ typeOf: 'String',
90
+ validate: V.ethereum.isValidWeiAmount,
91
+ },
92
+ EthereumAddressCode: {
93
+ typeOf: 'String',
94
+ validate: V.ethereum.isAddressCode,
95
+ },
96
+ MineId: {
97
+ typeOf: 'String',
98
+ validate(x) {
99
+ return x === 'mine';
100
+ },
101
+ },
102
+ BitcoinTxHash: {
103
+ typeOf: 'String',
104
+ validate: V.bitcoin.isTxHash,
105
+ },
106
+ BitcoinAddressArray: {
107
+ typeOf: 'Array',
108
+ validate(as) {
109
+ return _.every(as, V.bitcoin.isAddress);
110
+ },
111
+ },
112
+ BitcoinTxHashArray: {
113
+ typeOf: 'Array',
114
+ validate(as) {
115
+ return _.every(as, V.bitcoin.isTxHash);
116
+ },
117
+ },
118
+ BitcoinTxHex: {
119
+ typeOf: 'String',
120
+ validate: V.bitcoin.isTxHex,
121
+ },
122
+ BitcoinScriptHex: {
123
+ typeOf: 'String',
124
+ validate: V.bitcoin.isScriptHex,
125
+ },
126
+ BitcoinHDPath: {
127
+ typeOf: 'String',
128
+ validate: V.bitcoin.isHDPath,
129
+ },
130
+ BitcoinPublicKey: {
131
+ typeOf: 'String',
132
+ validate: V.bitcoin.isPublicKey,
133
+ },
134
+ BitcoinXpub: {
135
+ typeOf: 'String',
136
+ validate: V.bitcoin.isHDPublicKey,
137
+ },
138
+ BitcoinTxSignature: {
139
+ typeOf: 'String',
140
+ validate: V.bitcoin.isTxSignature,
141
+ },
142
+ BitcoinBlockHeight: {
143
+ typeOf: 'Number',
144
+ validate: V.bitcoin.isBlockHeight,
145
+ },
146
+ };
12
147
 
13
- customTypes = {
14
- Integer: {
15
- typeOf: 'Number',
16
- validate: function(x) {
17
- return x % 1 === 0;
18
- }
19
- },
20
- IntString: {
21
- typeOf: 'String',
22
- validate: V.isInt
23
- },
24
- BoolString: {
25
- typeOf: 'String',
26
- validate: V.isBoolean
27
- },
28
- Hex32: {
29
- typeOf: 'String',
30
- validate: function(x) {
31
- return V.isHexadecimal(x) && (x.length === 32);
32
- }
33
- },
34
- Email: {
35
- typeOf: 'String',
36
- validate: V.isEmail
37
- },
38
- UUID: {
39
- typeOf: 'String',
40
- validate: V.isUUID
41
- },
42
- UUIDv4: {
43
- typeOf: 'String',
44
- validate: function(x) {
45
- return V.isUUID(x, 4);
46
- }
47
- },
48
- JSON: {
49
- typeOf: 'String',
50
- validate: V.isJSON
51
- },
52
- Url: {
53
- typeOf: 'String',
54
- validate: V.isURL
55
- },
56
- DateString: {
57
- typeOf: 'String',
58
- validate: V.isDate
59
- },
60
- NonEmptyArray: {
61
- typeOf: 'Array',
62
- validate: V.nonEmpty
63
- },
64
- NonNullString: {
65
- typeOf: 'String',
66
- validate: V.nonEmpty
67
- },
68
- CustomerReference: {
69
- typeOf: 'String',
70
- validate: V.isCustomerReference
71
- },
72
- CustomerLabelName: {
73
- typeOf: 'String',
74
- validate: V.isCustomerLabelName
75
- },
76
- BitcoinAddress: {
77
- typeOf: 'String',
78
- validate: V.bitcoin.isAddress
79
- },
80
- MineId: {
81
- typeOf: 'String',
82
- validate: function(x) {
83
- return x === 'mine';
84
- }
85
- },
86
- BitcoinTxHash: {
87
- typeOf: 'String',
88
- validate: V.bitcoin.isTxHash
89
- },
90
- BitcoinAddressArray: {
91
- typeOf: 'Array',
92
- validate: function(as) {
93
- return _.every(as, V.bitcoin.isAddress);
94
- }
95
- },
96
- BitcoinTxHashArray: {
97
- typeOf: 'Array',
98
- validate: function(as) {
99
- return _.every(as, V.bitcoin.isTxHash);
100
- }
101
- },
102
- BitcoinTxHex: {
103
- typeOf: 'String',
104
- validate: V.bitcoin.isTxHex
105
- },
106
- BitcoinScriptHex: {
107
- typeOf: 'String',
108
- validate: V.bitcoin.isScriptHex
109
- },
110
- BitcoinHDPath: {
111
- typeOf: 'String',
112
- validate: V.bitcoin.isHDPath
113
- },
114
- BitcoinPublicKey: {
115
- typeOf: 'String',
116
- validate: V.bitcoin.isPublicKey
117
- },
118
- BitcoinXpub: {
119
- typeOf: 'String',
120
- validate: V.bitcoin.isHDPublicKey
121
- },
122
- BitcoinTxSignature: {
123
- typeOf: 'String',
124
- validate: V.bitcoin.isTxSignature
125
- },
126
- BitcoinBlockHeight: {
127
- typeOf: 'Number',
128
- validate: V.bitcoin.isBlockHeight
148
+ // add support for nullable properties
149
+ const withNullable = Object.keys(customTypes).reduce((acc, _type) => {
150
+ acc[_type] = customTypes[_type];
151
+ const { typeOf, validate } = customTypes[_type];
152
+
153
+ // for every type, add Nullable+type. A nullable string can also be empty
154
+ acc[`Nullable${_type}`] = Object.assign({}, customTypes[_type], {
155
+ validate: x =>
156
+ x == null || (typeOf === 'String' && x === '') || validate(x),
157
+ });
158
+
159
+ return acc;
160
+ }, {});
161
+
162
+ module.exports = {
163
+ parseType(str) {
164
+ return TC.parseType(str);
165
+ },
166
+ parsedTypeCheck(type, obj) {
167
+ return TC.parsedTypeCheck(type, obj, this.opts);
168
+ },
169
+ typeCheck(type, obj) {
170
+ return TC.typeCheck(type, obj, this.opts);
171
+ },
172
+ opts: {
173
+ customTypes: withNullable,
174
+ },
175
+ ensureType(type, obj, msg) {
176
+ return V.ensure(this.typeCheck(type, obj), msg);
177
+ },
178
+ checkArg(obj, prop, type, msg) {
179
+ if (msg == null) {
180
+ msg = `Invalid ${prop}`;
129
181
  }
130
- };
182
+ this.ensureType(type, obj[prop], msg);
183
+ return obj[prop];
184
+ },
185
+ checkArgs(input, types) {
186
+ const inputKeys = {};
187
+ const [typ] = types;
188
+ const fields = typ.of;
189
+ let numInputKeys = 0;
190
+ let numKeys;
131
191
 
132
- module.exports = {
133
- parseType: function(str) {
134
- return TC.parseType(str);
135
- },
136
- parsedTypeCheck: function(type, obj) {
137
- return TC.parsedTypeCheck(type, obj, this.opts);
138
- },
139
- typeCheck: function(type, obj) {
140
- return TC.typeCheck(type, obj, this.opts);
141
- },
142
- opts: {
143
- customTypes: customTypes
144
- },
145
- ensureType: function(type, obj, msg) {
146
- return V.ensure(this.typeCheck(type, obj), msg);
147
- },
148
- checkArg: function(obj, prop, type, msg) {
149
- if (msg == null) {
150
- msg = "Invalid " + prop;
151
- }
152
- this.ensureType(type, obj[prop], msg);
153
- return obj[prop];
154
- },
155
- checkArgs: function(input, types) {
156
- var fields, inputKeys, k, key, numInputKeys, numKeys, typ;
157
- typ = types[0];
158
- numInputKeys = 0;
159
- inputKeys = {};
160
- for (k in input) {
161
- inputKeys[k] = true;
162
- numInputKeys++;
163
- }
164
- fields = typ.of;
165
- numKeys = 0;
166
- for (key in fields) {
167
- types = fields[key];
168
- V.ensure(this.parsedTypeCheck(types, input[key]), "Invalid " + key);
169
- if (inputKeys[key]) {
170
- numKeys++;
171
- }
172
- }
173
- if (!(typ.subset || numInputKeys === numKeys)) {
174
- throw new E.BadRequest('invalid extra arguments are present');
192
+ Object.keys(input).forEach((k) => {
193
+ inputKeys[k] = true;
194
+ numInputKeys += 1;
195
+ });
196
+
197
+ numKeys = 0;
198
+
199
+ Object.keys(fields).forEach((key) => {
200
+ types = fields[key];
201
+ V.ensure(this.parsedTypeCheck(types, input[key]), `Invalid ${key}`);
202
+ if (inputKeys[key]) {
203
+ numKeys += 1;
175
204
  }
176
- }
177
- };
205
+ });
178
206
 
179
- }).call(this);
207
+ if (!(typ.subset || numInputKeys === numKeys)) {
208
+ throw new E.BadRequest('invalid extra arguments are present');
209
+ }
210
+ },
211
+ };