@thzero/library_server 0.16.9 → 0.16.11

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 (2) hide show
  1. package/package.json +3 -3
  2. package/repository/index.js +61 -38
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@thzero/library_server",
3
3
  "type": "module",
4
- "version": "0.16.9",
4
+ "version": "0.16.11",
5
5
  "version_major": 0,
6
6
  "version_minor": 16,
7
- "version_patch": 9,
8
- "version_date": "12/21/2022",
7
+ "version_patch": 11,
8
+ "version_date": "01/02/2023",
9
9
  "description": "An opinionated library of common functionality to bootstrap an API application using MongoDb and Firebase. Currently either Fastify or Koa can be used as the web server.",
10
10
  "author": "thZero",
11
11
  "license": "MIT",
@@ -12,10 +12,13 @@ class Repository {
12
12
  return this._injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG)
13
13
  }
14
14
 
15
- _enforce(clazz, method, value, name, correlationId) {
15
+ _enforce(clazz, method, value, name, correlationId, message) {
16
16
  if (!value) {
17
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
18
- const error = Error(`Invalid ${name}`, true);
17
+ if (!String.isNullOrEmpty(message))
18
+ message = `${name} is invalid.`;
19
+
20
+ this._logger.error(clazz, method, message, null, correlationId);
21
+ const error = Error(message, true);
19
22
  error.correlationId = correlationId;
20
23
  throw error;
21
24
  }
@@ -23,71 +26,91 @@ class Repository {
23
26
 
24
27
  _enforceNotEmpty(clazz, method, value, name, correlationId) {
25
28
  if (String.isNullOrEmpty(value)) {
26
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
27
- const error = Error(`Invalid ${name}`, true);
29
+ this._logger.error(clazz, method, `${name} is empty.`, null, correlationId);
30
+ const error = Error(`${name} is empty.`, true);
28
31
  error.correlationId = correlationId;
29
32
  throw error;
30
33
  }
31
34
  }
32
-
33
- _enforceNotNull(clazz, method, value, name, correlationId) {
34
- if (!value) {
35
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
36
- const error = Error(`Invalid ${name}`, true);
35
+
36
+ _enforceNotEmptyEither(clazz, method, value1, value2, name1, name2, correlationId) {
37
+ if (String.isNullOrEmpty(value1) && String.isNullOrEmpty(value2)) {
38
+ this._logger.error(clazz, method, `Either ${name1} or ${name2} is empty.`, null, correlationId);
39
+ const error = Error(`Either ${name1} or ${name2} is empty.`, true);
37
40
  error.correlationId = correlationId;
38
41
  throw error;
39
42
  }
40
43
  }
41
44
 
42
- _enforce(clazz, method, value, name, correlationId) {
43
- if (!value) {
44
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
45
- return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
45
+ _enforceNotEmptyMultiple(clazz, method, values, names, correlationId) {
46
+ let valid = true;
47
+ for (const value of values)
48
+ valid &= String.isNullOrEmpty(value);
49
+ if (!valid) {
50
+ names = names.join(', ');
51
+ this._logger.error(clazz, method, `None of the fields are not null: ${names}`, null, correlationId);
52
+ const error = Error(`None of the fields are not null: ${names}`, true);
53
+ error.correlationId = correlationId;
54
+ throw error;
46
55
  }
47
-
48
- return this._success(correlationId);
49
56
  }
50
57
 
51
58
  _enforceNotEmptyResponse(clazz, method, value, name, correlationId) {
52
59
  if (String.isNullOrEmpty(value)) {
53
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
54
- return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
60
+ this._logger.error(clazz, method, `${name} is empty.`, null, correlationId);
61
+ return Response.error(clazz, method, `${name} is empty.`, null, null, null, correlationId);
55
62
  }
56
63
 
57
- return this._success(correlationId);
64
+ return this._successResponse(value, correlationId);
58
65
  }
59
66
 
60
- _enforceNotNullResponse(clazz, method, value, name, correlationId) {
61
- if (!value) {
62
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
63
- return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
67
+ _enforceNotNull(clazz, method, value, name, correlationId) {
68
+ if (!value || value === undefined) {
69
+ this._logger.error(clazz, method, `${name} is null.`, null, correlationId);
70
+ const error = Error(`${name} is null.`, true);
71
+ error.correlationId = correlationId;
72
+ throw error;
64
73
  }
65
-
66
- return this._success(correlationId);
67
74
  }
68
75
 
69
- _enforceNotEmptyAsResponse(clazz, method, value, name, correlationId) {
70
- if (String.isNullOrEmpty(value)) {
71
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
72
- return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
76
+ _enforceNotNullEither(clazz, method, value1, value2, name1, name2, correlationId) {
77
+ if ((!value1 || value1 === undefined) && (!value2 || value2 == undefined)) {
78
+ this._logger.error(clazz, method, `Either ${name1} or ${name2} is null.`, null, correlationId);
79
+ const error = Error(`Either ${name1} or ${name2} is null.`, true);
80
+ error.correlationId = correlationId;
81
+ throw error;
73
82
  }
83
+ }
74
84
 
75
- return this._successResponse(null, correlationId);
85
+ _enforceNotNullMultiple(clazz, method, values, names, correlationId) {
86
+ let valid = true;
87
+ for (const value of values)
88
+ valid &= values;
89
+ if (!valid) {
90
+ names = names.join(', ');
91
+ this._logger.error(clazz, method, `None of the fields are not null: ${names}`, null, correlationId);
92
+ const error = Error(`None of the fields are not null: ${names}`, true);
93
+ error.correlationId = correlationId;
94
+ throw error;
95
+ }
76
96
  }
77
97
 
78
- _enforceNotNullAsResponse(clazz, method, value, name, correlationId) {
79
- if (!value) {
80
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
81
- return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
98
+ _enforceNotNullResponse(clazz, method, value, name, correlationId) {
99
+ if (!value || value === undefined) {
100
+ this._logger.error(clazz, method, `${name} is null.`, null, correlationId);
101
+ return Response.error(clazz, method, `${name} is null.`, null, null, null, correlationId);
82
102
  }
83
103
 
84
- return this._successResponse(null, correlationId);
104
+ return this._successResponse(value, correlationId);
85
105
  }
86
106
 
87
- _enforceResponse(clazz, method, response, name, correlationId) {
107
+ _enforceResponse(clazz, method, response, name, correlationId, message) {
88
108
  if (!response || (response && !response.success)) {
89
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
90
- const error = Error(`Unsuccessful response for ${name}`, true);
109
+ if (!String.isNullOrEmpty(message))
110
+ message = `Unsuccessful response for ${name}.`;
111
+
112
+ this._logger.error(clazz, method, message, null, correlationId);
113
+ const error = Error(message, true);
91
114
  error.correlationId = correlationId;
92
115
  throw error;
93
116
  }