my-q-format-response-aws-lambda 1.0.1 → 1.0.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.
- package/.vscode/settings.json +10 -0
- package/index.js +208 -53
- package/index.ts +340 -68
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -4,9 +4,11 @@ exports.controlResponseNull = exports.normaliseMongoPaginate = exports.normalise
|
|
|
4
4
|
class StatusResult {
|
|
5
5
|
}
|
|
6
6
|
exports.StatusResult = StatusResult;
|
|
7
|
-
StatusResult.ok =
|
|
8
|
-
StatusResult.error =
|
|
9
|
-
StatusResult.notFound =
|
|
7
|
+
StatusResult.ok = "Ok";
|
|
8
|
+
StatusResult.error = "Error";
|
|
9
|
+
StatusResult.notFound = "NotFound";
|
|
10
|
+
StatusResult.unauthorized = "Unauthorized";
|
|
11
|
+
StatusResult.needRedirect = "NeedRedirect";
|
|
10
12
|
class StatusCode {
|
|
11
13
|
}
|
|
12
14
|
exports.StatusCode = StatusCode;
|
|
@@ -21,13 +23,14 @@ StatusCode.NoContent = 204;
|
|
|
21
23
|
// static ResetContent = 205;
|
|
22
24
|
// static PartialContent = 206;
|
|
23
25
|
// static MultiStatus = 207;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
StatusCode.MultipleChoices = 300;
|
|
27
|
+
StatusCode.MovedPermanently = 301;
|
|
28
|
+
StatusCode.MovedTemporarily = 302;
|
|
29
|
+
StatusCode.SeeOther = 303;
|
|
28
30
|
// static UseProxy = 305;
|
|
29
31
|
// static NotModified = 304;
|
|
30
|
-
|
|
32
|
+
StatusCode.TemporaryRedirect = 307;
|
|
33
|
+
// static PermanentRedirect = 308;
|
|
31
34
|
StatusCode.BadRequest = 400;
|
|
32
35
|
StatusCode.Unauthorized = 401;
|
|
33
36
|
StatusCode.PaymentRequired = 402;
|
|
@@ -61,28 +64,30 @@ StatusCode.BadGateway = 502;
|
|
|
61
64
|
class ResponseBodyVO {
|
|
62
65
|
constructor() {
|
|
63
66
|
this.statusResult = StatusResult.ok;
|
|
64
|
-
this.message =
|
|
67
|
+
this.message = "";
|
|
65
68
|
this.data = null;
|
|
66
69
|
this.count = null;
|
|
67
70
|
this.error = null;
|
|
71
|
+
this.redirectTo = undefined;
|
|
68
72
|
}
|
|
69
73
|
}
|
|
70
74
|
exports.ResponseBodyVO = ResponseBodyVO;
|
|
71
75
|
class ResponseVO {
|
|
72
76
|
constructor() {
|
|
73
77
|
this.statusCode = StatusCode.OK;
|
|
74
|
-
this.body =
|
|
78
|
+
this.body = "";
|
|
75
79
|
}
|
|
76
80
|
}
|
|
77
81
|
exports.ResponseVO = ResponseVO;
|
|
78
82
|
class Result {
|
|
79
|
-
constructor({ statusCode = StatusCode.OK, statusResult = StatusResult.ok, message, data = null, count = null, error = null, bodyWrap = true, }) {
|
|
83
|
+
constructor({ statusCode = StatusCode.OK, statusResult = StatusResult.ok, message, data = null, count = null, error = null, redirectTo = undefined, bodyWrap = true, }) {
|
|
80
84
|
this.statusCode = statusCode;
|
|
81
85
|
this.statusResult = statusResult;
|
|
82
|
-
this.message = !message ?
|
|
86
|
+
this.message = !message ? "" : message;
|
|
83
87
|
this.count = count;
|
|
84
88
|
this.data = data;
|
|
85
89
|
this.error = error;
|
|
90
|
+
this.redirectTo = redirectTo;
|
|
86
91
|
this.bodyWrap = bodyWrap;
|
|
87
92
|
}
|
|
88
93
|
/**
|
|
@@ -90,12 +95,23 @@ class Result {
|
|
|
90
95
|
* If use to AWS Appsync need response value without body wrap
|
|
91
96
|
*/
|
|
92
97
|
bodyToString() {
|
|
93
|
-
let _err = this.error && this.error.message
|
|
98
|
+
let _err = this.error && this.error.message
|
|
99
|
+
? this.error.message
|
|
100
|
+
: !this.error
|
|
101
|
+
? null
|
|
102
|
+
: JSON.stringify(this.error);
|
|
94
103
|
const valueBody = {
|
|
95
|
-
statusResult: this.statusResult,
|
|
104
|
+
statusResult: this.statusResult,
|
|
105
|
+
message: this.message,
|
|
106
|
+
data: this.data,
|
|
107
|
+
count: this.count,
|
|
108
|
+
error: _err,
|
|
96
109
|
};
|
|
110
|
+
if (this.redirectTo)
|
|
111
|
+
valueBody.redirectTo = this.redirectTo;
|
|
97
112
|
const valueBodyWrap = {
|
|
98
|
-
statusCode: this.statusCode,
|
|
113
|
+
statusCode: this.statusCode,
|
|
114
|
+
body: JSON.stringify(valueBody),
|
|
99
115
|
};
|
|
100
116
|
return this.bodyWrap ? valueBodyWrap : valueBody;
|
|
101
117
|
}
|
|
@@ -108,9 +124,14 @@ class CreateResponse {
|
|
|
108
124
|
* @param message
|
|
109
125
|
* @param bodyWrap
|
|
110
126
|
*/
|
|
111
|
-
static success({ data = null, count = null, message =
|
|
127
|
+
static success({ data = null, count = null, message = "success", bodyWrap = true, }) {
|
|
112
128
|
const result = new Result({
|
|
113
|
-
statusCode: StatusCode.OK,
|
|
129
|
+
statusCode: StatusCode.OK,
|
|
130
|
+
statusResult: StatusResult.ok,
|
|
131
|
+
message,
|
|
132
|
+
data,
|
|
133
|
+
count,
|
|
134
|
+
bodyWrap,
|
|
114
135
|
});
|
|
115
136
|
return result.bodyToString();
|
|
116
137
|
}
|
|
@@ -120,9 +141,13 @@ class CreateResponse {
|
|
|
120
141
|
* @param message
|
|
121
142
|
* @param bodyWrap
|
|
122
143
|
*/
|
|
123
|
-
static created({ data, message =
|
|
144
|
+
static created({ data, message = "created", bodyWrap = true, }) {
|
|
124
145
|
const result = new Result({
|
|
125
|
-
statusCode: StatusCode.Created,
|
|
146
|
+
statusCode: StatusCode.Created,
|
|
147
|
+
statusResult: StatusResult.ok,
|
|
148
|
+
message,
|
|
149
|
+
data,
|
|
150
|
+
bodyWrap,
|
|
126
151
|
});
|
|
127
152
|
return result.bodyToString();
|
|
128
153
|
}
|
|
@@ -132,9 +157,29 @@ class CreateResponse {
|
|
|
132
157
|
* @param message
|
|
133
158
|
* @param bodyWrap
|
|
134
159
|
*/
|
|
135
|
-
static updated({ data, message =
|
|
160
|
+
static updated({ data, message = "updated", bodyWrap = true, }) {
|
|
136
161
|
const result = new Result({
|
|
137
|
-
statusCode: StatusCode.OK,
|
|
162
|
+
statusCode: StatusCode.OK,
|
|
163
|
+
statusResult: StatusResult.ok,
|
|
164
|
+
message,
|
|
165
|
+
data,
|
|
166
|
+
bodyWrap,
|
|
167
|
+
});
|
|
168
|
+
return result.bodyToString();
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Update or Create
|
|
172
|
+
* @param data
|
|
173
|
+
* @param message
|
|
174
|
+
* @param bodyWrap
|
|
175
|
+
*/
|
|
176
|
+
static updateOrCreate({ data, message = "update_or_create", bodyWrap = true, }) {
|
|
177
|
+
const result = new Result({
|
|
178
|
+
statusCode: StatusCode.OK,
|
|
179
|
+
statusResult: StatusResult.ok,
|
|
180
|
+
message,
|
|
181
|
+
data,
|
|
182
|
+
bodyWrap,
|
|
138
183
|
});
|
|
139
184
|
return result.bodyToString();
|
|
140
185
|
}
|
|
@@ -144,9 +189,14 @@ class CreateResponse {
|
|
|
144
189
|
* @param message
|
|
145
190
|
* @param bodyWrap
|
|
146
191
|
*/
|
|
147
|
-
static notFound({ error = null, message =
|
|
192
|
+
static notFound({ error = null, message = "", bodyWrap = true, }) {
|
|
148
193
|
const result = new Result({
|
|
149
|
-
statusCode: StatusCode.NotFound,
|
|
194
|
+
statusCode: StatusCode.NotFound,
|
|
195
|
+
statusResult: StatusResult.notFound,
|
|
196
|
+
message,
|
|
197
|
+
data: null,
|
|
198
|
+
error,
|
|
199
|
+
bodyWrap,
|
|
150
200
|
});
|
|
151
201
|
return result.bodyToString();
|
|
152
202
|
}
|
|
@@ -157,9 +207,51 @@ class CreateResponse {
|
|
|
157
207
|
* @param message
|
|
158
208
|
* @param bodyWrap
|
|
159
209
|
*/
|
|
160
|
-
static error({ error = null, statusCode = StatusCode.BadRequest, message =
|
|
210
|
+
static error({ error = null, statusCode = StatusCode.BadRequest, message = "Error", bodyWrap = true, }) {
|
|
211
|
+
const result = new Result({
|
|
212
|
+
statusCode,
|
|
213
|
+
statusResult: StatusResult.error,
|
|
214
|
+
data: null,
|
|
215
|
+
error,
|
|
216
|
+
message,
|
|
217
|
+
bodyWrap,
|
|
218
|
+
});
|
|
219
|
+
return result.bodyToString();
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Unauthorized
|
|
223
|
+
* @param error
|
|
224
|
+
* @param statusCode
|
|
225
|
+
* @param message
|
|
226
|
+
* @param bodyWrap
|
|
227
|
+
*/
|
|
228
|
+
static unauthorized({ error = null, statusCode = StatusCode.Unauthorized, message = "Unauthorized", bodyWrap = true, }) {
|
|
161
229
|
const result = new Result({
|
|
162
|
-
statusCode,
|
|
230
|
+
statusCode,
|
|
231
|
+
statusResult: StatusResult.unauthorized,
|
|
232
|
+
data: null,
|
|
233
|
+
error,
|
|
234
|
+
message,
|
|
235
|
+
bodyWrap,
|
|
236
|
+
});
|
|
237
|
+
return result.bodyToString();
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Redirect
|
|
241
|
+
* @param error
|
|
242
|
+
* @param statusCode
|
|
243
|
+
* @param message
|
|
244
|
+
* @param bodyWrap
|
|
245
|
+
*/
|
|
246
|
+
static redirect({ statusCode = StatusCode.MovedTemporarily, message = "", bodyWrap = true, redirectTo = "", }) {
|
|
247
|
+
const result = new Result({
|
|
248
|
+
statusCode,
|
|
249
|
+
statusResult: StatusResult.needRedirect,
|
|
250
|
+
data: null,
|
|
251
|
+
error: null,
|
|
252
|
+
message,
|
|
253
|
+
redirectTo,
|
|
254
|
+
bodyWrap,
|
|
163
255
|
});
|
|
164
256
|
return result.bodyToString();
|
|
165
257
|
}
|
|
@@ -173,26 +265,49 @@ class CreateResponse {
|
|
|
173
265
|
* @param count
|
|
174
266
|
* @param bodyWrap
|
|
175
267
|
*/
|
|
176
|
-
static custom({ statusCode = StatusCode.OK, statusResult = StatusResult.ok, message =
|
|
268
|
+
static custom({ statusCode = StatusCode.OK, statusResult = StatusResult.ok, message = "", error = null, data = null, count = null, bodyWrap = true, }) {
|
|
177
269
|
const result = new Result({
|
|
178
|
-
statusCode,
|
|
270
|
+
statusCode,
|
|
271
|
+
statusResult,
|
|
272
|
+
message,
|
|
273
|
+
error,
|
|
274
|
+
data,
|
|
275
|
+
count,
|
|
276
|
+
bodyWrap,
|
|
179
277
|
});
|
|
180
278
|
return result.bodyToString();
|
|
181
279
|
}
|
|
182
280
|
}
|
|
183
281
|
exports.CreateResponse = CreateResponse;
|
|
184
|
-
const messagesREST = (prefix, suffix =
|
|
282
|
+
const messagesREST = (prefix, suffix = "") => {
|
|
185
283
|
return {
|
|
186
284
|
TOTAL: `${prefix}_TOTAL${suffix}`,
|
|
285
|
+
NOT_FOUND: `${prefix}_NOT_FOUND${suffix}`,
|
|
286
|
+
TOKEN_EXPIRED_ERROR: `${prefix}_TOKEN_EXPIRED_ERROR${suffix}`,
|
|
287
|
+
AUTHORISED: `${prefix}_AUTHORISED${suffix}`,
|
|
288
|
+
UNAUTHORISED: `${prefix}_UNAUTHORISED${suffix}`,
|
|
289
|
+
ERROR_AUTHORISED: `${prefix}_ERROR_AUTHORISED${suffix}`,
|
|
290
|
+
EXIST: `${prefix}_EXIST${suffix}`,
|
|
291
|
+
NOT_EXIST: `${prefix}_NOT_EXIST${suffix}`,
|
|
292
|
+
ERROR_EXIST: `${prefix}_ERROR_EXIST${suffix}`,
|
|
293
|
+
IDENTIFIER: `${prefix}_IDENTIFIER${suffix}`,
|
|
294
|
+
NOT_IDENTIFIER: `${prefix}_NOT_IDENTIFIER${suffix}`,
|
|
295
|
+
ERROR_IDENTIFIER: `${prefix}_ERROR_IDENTIFIER${suffix}`,
|
|
187
296
|
CREATE: `${prefix}_ITEM_CREATE${suffix}`,
|
|
188
297
|
NOT_CREATE: `${prefix}_ITEM_NOT_CREATE${suffix}`,
|
|
189
298
|
ERROR_CREATE: `${prefix}_ITEM_ERROR_CREATE${suffix}`,
|
|
190
299
|
UPDATE: `${prefix}_ITEM_UPDATE${suffix}`,
|
|
191
|
-
|
|
300
|
+
ERROR_UPDATE: `${prefix}_ITEM_ERROR_UPDATE${suffix}`,
|
|
192
301
|
NOT_UPDATE: `${prefix}_ITEM_NOT_UPDATE${suffix}`,
|
|
302
|
+
UPDATE_OR_CREATE: `${prefix}_ITEM_UPDATE_OR_CREATE${suffix}`,
|
|
303
|
+
ERROR_UPDATE_OR_CREATE: `${prefix}_ITEM_UPDATE_OR_CREATE${suffix}`,
|
|
304
|
+
NOT_UPDATE_OR_CREATE: `${prefix}_ITEM_NOT_UPDATE_OR_CREATE${suffix}`,
|
|
305
|
+
UPDATE_MANY: `${prefix}_ITEM_UPDATE_MANY${suffix}`,
|
|
193
306
|
NOT_UPDATE_MANY: `${prefix}_ITEM_NOT_UPDATE_MANY${suffix}`,
|
|
194
|
-
ERROR_UPDATE: `${prefix}_ITEM_ERROR_UPDATE${suffix}`,
|
|
195
307
|
ERROR_UPDATE_MANY: `${prefix}_ITEM_ERROR_UPDATE_MANY${suffix}`,
|
|
308
|
+
GET_ONE_AND_CHILDREN: `${prefix}_GET_ONE_AND_CHILDREN${suffix}`,
|
|
309
|
+
NOT_GET_ONE_AND_CHILDREN: `${prefix}_NOT_GET_ONE_AND_CHILDREN${suffix}`,
|
|
310
|
+
ERROR_GET_ONE_AND_CHILDREN: `${prefix}_ERROR_GET_ONE_AND_CHILDREN${suffix}`,
|
|
196
311
|
GET: `${prefix}_ITEM_GET${suffix}`,
|
|
197
312
|
NOT_GET: `${prefix}_ITEM_NOT_GET${suffix}`,
|
|
198
313
|
ERROR_GET: `${prefix}_ITEM_ERROR_GET${suffix}`,
|
|
@@ -211,7 +326,6 @@ const messagesREST = (prefix, suffix = '') => {
|
|
|
211
326
|
DELETE_MANY: `${prefix}_DELETE_MANY${suffix}`,
|
|
212
327
|
NOT_DELETE_MANY: `${prefix}_NOT_DELETE_MANY${suffix}`,
|
|
213
328
|
ERROR_DELETE_MANY: `${prefix}_ERROR_DELETE_MANY${suffix}`,
|
|
214
|
-
NOT_FOUND: `${prefix}_NOT_FOUND${suffix}`,
|
|
215
329
|
INITIALISE: `${prefix}_INITIALISE${suffix}`,
|
|
216
330
|
NOT_INITIALISE: `${prefix}_NOT_INITIALISE${suffix}`,
|
|
217
331
|
ERROR_INITIALISE: `${prefix}_ERROR_INITIALISE${suffix}`,
|
|
@@ -229,16 +343,29 @@ const messagesREST = (prefix, suffix = '') => {
|
|
|
229
343
|
ERROR_COUNTER_MONTH: `${prefix}_ERROR_COUNTER_MONTH${suffix}`,
|
|
230
344
|
COUNTER_YEAR: `${prefix}_COUNTER_YEAR${suffix}`,
|
|
231
345
|
NOT_COUNTER_YEAR: `${prefix}_NOT_COUNTER_YEAR${suffix}`,
|
|
346
|
+
ERROR_COUNTER_YEAR: `${prefix}_ERROR_COUNTER_YEAR${suffix}`,
|
|
232
347
|
TEST: `${prefix}_TEST${suffix}`,
|
|
233
348
|
NOT_TEST: `${prefix}_NOT_TEST${suffix}`,
|
|
234
349
|
ERROR_TEST: `${prefix}_ERROR_TEST${suffix}`,
|
|
235
350
|
AGGREGATION: `${prefix}_AGGREGATION${suffix}`,
|
|
236
351
|
NOT_AGGREGATION: `${prefix}_NOT_AGGREGATION${suffix}`,
|
|
237
352
|
ERROR_AGGREGATION: `${prefix}_ERROR_AGGREGATION${suffix}`,
|
|
353
|
+
USER_REGISTRATION: `${prefix}_USER_REGISTRATION${suffix}`,
|
|
354
|
+
NOT_USER_REGISTRATION: `${prefix}_NOT_USER_REGISTRATION${suffix}`,
|
|
355
|
+
ERROR_USER_REGISTRATION: `${prefix}_ERROR_USER_REGISTRATION${suffix}`,
|
|
356
|
+
USER_LOGIN: `${prefix}_USER_LOGIN${suffix}`,
|
|
357
|
+
NOT_USER_LOGIN: `${prefix}_NOT_USER_LOGIN${suffix}`,
|
|
358
|
+
ERROR_USER_LOGIN: `${prefix}_ERROR_USER_LOGIN${suffix}`,
|
|
359
|
+
USER_LOGOUT: `${prefix}_USER_LOGOUT${suffix}`,
|
|
360
|
+
NOT_USER_LOGOUT: `${prefix}_NOT_USER_LOGOUT${suffix}`,
|
|
361
|
+
ERROR_USER_LOGOUT: `${prefix}_ERROR_USER_LOGOUT${suffix}`,
|
|
362
|
+
USER_AUTHENTICATION_REFRESH: `${prefix}_USER_AUTHENTICATION_REFRESH${suffix}`,
|
|
363
|
+
NOT_USER_AUTHENTICATION_REFRESH: `${prefix}_NOT_USER_AUTHENTICATION_REFRESH${suffix}`,
|
|
364
|
+
ERROR_USER_AUTHENTICATION_REFRESH: `${prefix}_ERROR_USER_AUTHENTICATION_REFRESH${suffix}`,
|
|
238
365
|
};
|
|
239
366
|
};
|
|
240
367
|
exports.messagesREST = messagesREST;
|
|
241
|
-
exports.optionsPaginationParams = [
|
|
368
|
+
exports.optionsPaginationParams = ["limit", "skip", "count"];
|
|
242
369
|
/**
|
|
243
370
|
* Normalise filter for mongoose
|
|
244
371
|
* @param regexFields
|
|
@@ -247,13 +374,19 @@ exports.optionsPaginationParams = ['limit', 'skip', 'count'];
|
|
|
247
374
|
*/
|
|
248
375
|
const normaliseMongoFilter = (filter, regexFields, excludeFields) => {
|
|
249
376
|
const _filter = {};
|
|
250
|
-
const excludeParams = excludeFields && Array.isArray(excludeFields) && excludeFields.length > 0
|
|
377
|
+
const excludeParams = excludeFields && Array.isArray(excludeFields) && excludeFields.length > 0
|
|
378
|
+
? excludeFields
|
|
379
|
+
: exports.optionsPaginationParams;
|
|
251
380
|
Object.keys(filter).forEach((f) => {
|
|
252
381
|
const v = filter[f];
|
|
253
|
-
if (!(v === null ||
|
|
382
|
+
if (!(v === null ||
|
|
383
|
+
(typeof v === "number" && isNaN(v)) ||
|
|
384
|
+
v === Infinity ||
|
|
385
|
+
v === undefined ||
|
|
386
|
+
excludeParams.includes(f))) {
|
|
254
387
|
_filter[f] = filter[f];
|
|
255
388
|
if (regexFields.includes(f))
|
|
256
|
-
_filter[f] = { $regex:
|
|
389
|
+
_filter[f] = { $regex: new RegExp(_filter[f], "gi") };
|
|
257
390
|
}
|
|
258
391
|
});
|
|
259
392
|
return _filter;
|
|
@@ -265,7 +398,8 @@ exports.normaliseMongoFilter = normaliseMongoFilter;
|
|
|
265
398
|
*/
|
|
266
399
|
const normaliseMongoPaginate = (filter) => {
|
|
267
400
|
let res = {
|
|
268
|
-
skip: 0,
|
|
401
|
+
skip: 0,
|
|
402
|
+
limit: 50,
|
|
269
403
|
};
|
|
270
404
|
res.skip = filter && filter.skip ? parseInt(filter.skip, 10) || 0 : 0;
|
|
271
405
|
res.limit = filter && filter.limit ? parseInt(filter.limit, 10) || 50 : 50;
|
|
@@ -275,46 +409,67 @@ exports.normaliseMongoPaginate = normaliseMongoPaginate;
|
|
|
275
409
|
const controlResponseNull = (data, okResultOf, prefix, bodyWrap = true) => {
|
|
276
410
|
let result;
|
|
277
411
|
if (data) {
|
|
278
|
-
if (okResultOf ===
|
|
412
|
+
if (okResultOf === "create") {
|
|
279
413
|
result = CreateResponse.created({
|
|
280
|
-
data,
|
|
414
|
+
data,
|
|
415
|
+
message: (0, exports.messagesREST)(prefix).CREATE,
|
|
416
|
+
bodyWrap,
|
|
281
417
|
});
|
|
282
418
|
}
|
|
283
|
-
if (okResultOf ===
|
|
419
|
+
if (okResultOf === "update") {
|
|
284
420
|
result = CreateResponse.updated({
|
|
285
|
-
data,
|
|
421
|
+
data,
|
|
422
|
+
message: (0, exports.messagesREST)(prefix).UPDATE,
|
|
423
|
+
bodyWrap,
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
if (okResultOf === "update_or_create") {
|
|
427
|
+
result = CreateResponse.updateOrCreate({
|
|
428
|
+
data,
|
|
429
|
+
message: (0, exports.messagesREST)(prefix).UPDATE_OR_CREATE,
|
|
430
|
+
bodyWrap,
|
|
286
431
|
});
|
|
287
432
|
}
|
|
288
|
-
if (okResultOf ===
|
|
433
|
+
if (okResultOf === "update_many") {
|
|
289
434
|
result = CreateResponse.updated({
|
|
290
|
-
data,
|
|
435
|
+
data,
|
|
436
|
+
message: (0, exports.messagesREST)(prefix).UPDATE_MANY,
|
|
437
|
+
bodyWrap,
|
|
291
438
|
});
|
|
292
439
|
}
|
|
293
|
-
if (okResultOf ===
|
|
440
|
+
if (okResultOf === "increment") {
|
|
294
441
|
result = CreateResponse.updated({
|
|
295
|
-
data,
|
|
442
|
+
data,
|
|
443
|
+
message: (0, exports.messagesREST)(prefix).INCREMENT,
|
|
444
|
+
bodyWrap,
|
|
296
445
|
});
|
|
297
446
|
}
|
|
298
|
-
if (okResultOf ===
|
|
447
|
+
if (okResultOf === "decrement") {
|
|
299
448
|
result = CreateResponse.updated({
|
|
300
|
-
data,
|
|
449
|
+
data,
|
|
450
|
+
message: (0, exports.messagesREST)(prefix).DECREMENT,
|
|
451
|
+
bodyWrap,
|
|
301
452
|
});
|
|
302
453
|
}
|
|
303
454
|
}
|
|
304
455
|
else {
|
|
305
|
-
let messageErr =
|
|
306
|
-
if (okResultOf ===
|
|
456
|
+
let messageErr = "";
|
|
457
|
+
if (okResultOf === "create")
|
|
307
458
|
messageErr = (0, exports.messagesREST)(prefix).NOT_CREATE;
|
|
308
|
-
if (okResultOf ===
|
|
459
|
+
if (okResultOf === "update")
|
|
309
460
|
messageErr = (0, exports.messagesREST)(prefix).NOT_UPDATE;
|
|
310
|
-
if (okResultOf ===
|
|
461
|
+
if (okResultOf === "update_or_create")
|
|
462
|
+
messageErr = (0, exports.messagesREST)(prefix).NOT_UPDATE_OR_CREATE;
|
|
463
|
+
if (okResultOf === "update_many")
|
|
311
464
|
messageErr = (0, exports.messagesREST)(prefix).NOT_UPDATE_MANY;
|
|
312
|
-
if (okResultOf ===
|
|
465
|
+
if (okResultOf === "increment")
|
|
313
466
|
messageErr = (0, exports.messagesREST)(prefix).NOT_INCREMENT;
|
|
314
|
-
if (okResultOf ===
|
|
467
|
+
if (okResultOf === "decrement")
|
|
315
468
|
messageErr = (0, exports.messagesREST)(prefix).NOT_DECREMENT;
|
|
316
469
|
result = CreateResponse.error({
|
|
317
|
-
data: data,
|
|
470
|
+
data: data,
|
|
471
|
+
message: messageErr,
|
|
472
|
+
bodyWrap,
|
|
318
473
|
});
|
|
319
474
|
}
|
|
320
475
|
return result;
|
package/index.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export class StatusResult {
|
|
2
|
-
static ok =
|
|
3
|
-
static error =
|
|
4
|
-
static notFound =
|
|
2
|
+
static ok = "Ok";
|
|
3
|
+
static error = "Error";
|
|
4
|
+
static notFound = "NotFound";
|
|
5
|
+
static unauthorized = "Unauthorized";
|
|
6
|
+
static needRedirect = "NeedRedirect";
|
|
5
7
|
}
|
|
6
8
|
|
|
7
9
|
export class StatusCode {
|
|
@@ -18,15 +20,14 @@ export class StatusCode {
|
|
|
18
20
|
// static PartialContent = 206;
|
|
19
21
|
// static MultiStatus = 207;
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// static SeeOther = 303;
|
|
23
|
+
static MultipleChoices = 300;
|
|
24
|
+
static MovedPermanently = 301;
|
|
25
|
+
static MovedTemporarily = 302;
|
|
26
|
+
static SeeOther = 303;
|
|
26
27
|
// static UseProxy = 305;
|
|
27
28
|
// static NotModified = 304;
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
static TemporaryRedirect = 307;
|
|
30
|
+
// static PermanentRedirect = 308;
|
|
30
31
|
|
|
31
32
|
static BadRequest = 400;
|
|
32
33
|
static Unauthorized = 401;
|
|
@@ -72,6 +73,7 @@ export class StatusCode {
|
|
|
72
73
|
type TData = object | boolean | string | null;
|
|
73
74
|
type TCount = number | null;
|
|
74
75
|
type TError = any | null;
|
|
76
|
+
type TRedirectTo = string | undefined;
|
|
75
77
|
|
|
76
78
|
interface TResultIn {
|
|
77
79
|
statusCode?: StatusCode;
|
|
@@ -80,34 +82,36 @@ interface TResultIn {
|
|
|
80
82
|
data?: TData;
|
|
81
83
|
count?: TCount;
|
|
82
84
|
error?: TError;
|
|
85
|
+
redirectTo?: TRedirectTo;
|
|
83
86
|
bodyWrap: boolean;
|
|
84
87
|
}
|
|
85
88
|
|
|
86
89
|
interface TFuncParams {
|
|
87
|
-
statusCode?: StatusCode
|
|
88
|
-
statusResult?: StatusResult
|
|
89
|
-
message?: string
|
|
90
|
-
error?: TError
|
|
91
|
-
data?: TData
|
|
92
|
-
count?: TCount
|
|
90
|
+
statusCode?: StatusCode;
|
|
91
|
+
statusResult?: StatusResult;
|
|
92
|
+
message?: string;
|
|
93
|
+
error?: TError;
|
|
94
|
+
data?: TData;
|
|
95
|
+
count?: TCount;
|
|
96
|
+
redirectTo?: TRedirectTo;
|
|
93
97
|
bodyWrap?: boolean;
|
|
94
98
|
}
|
|
95
99
|
|
|
96
100
|
export class ResponseBodyVO {
|
|
97
101
|
statusResult: StatusResult = StatusResult.ok;
|
|
98
|
-
message: string =
|
|
102
|
+
message: string = "";
|
|
99
103
|
data: TData = null;
|
|
100
104
|
count: TCount = null;
|
|
101
105
|
error: TError = null;
|
|
106
|
+
redirectTo?: TRedirectTo = undefined;
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
export class ResponseVO {
|
|
105
110
|
statusCode: StatusCode = StatusCode.OK;
|
|
106
|
-
body: string =
|
|
111
|
+
body: string = "";
|
|
107
112
|
}
|
|
108
113
|
|
|
109
|
-
export type ResponseVoAWS = ResponseVO | ResponseBodyVO
|
|
110
|
-
|
|
114
|
+
export type ResponseVoAWS = ResponseVO | ResponseBodyVO;
|
|
111
115
|
|
|
112
116
|
class Result {
|
|
113
117
|
private statusCode: StatusCode;
|
|
@@ -116,6 +120,7 @@ class Result {
|
|
|
116
120
|
private data: TData;
|
|
117
121
|
private count: TCount;
|
|
118
122
|
private error: any;
|
|
123
|
+
private redirectTo: TRedirectTo;
|
|
119
124
|
private bodyWrap: boolean;
|
|
120
125
|
|
|
121
126
|
constructor({
|
|
@@ -125,14 +130,16 @@ class Result {
|
|
|
125
130
|
data = null,
|
|
126
131
|
count = null,
|
|
127
132
|
error = null,
|
|
133
|
+
redirectTo = undefined,
|
|
128
134
|
bodyWrap = true,
|
|
129
135
|
}: TResultIn) {
|
|
130
136
|
this.statusCode = statusCode;
|
|
131
137
|
this.statusResult = statusResult;
|
|
132
|
-
this.message = !message ?
|
|
138
|
+
this.message = !message ? "" : message;
|
|
133
139
|
this.count = count;
|
|
134
140
|
this.data = data;
|
|
135
141
|
this.error = error;
|
|
142
|
+
this.redirectTo = redirectTo;
|
|
136
143
|
this.bodyWrap = bodyWrap;
|
|
137
144
|
}
|
|
138
145
|
|
|
@@ -141,13 +148,24 @@ class Result {
|
|
|
141
148
|
* If use to AWS Appsync need response value without body wrap
|
|
142
149
|
*/
|
|
143
150
|
bodyToString(): ResponseVoAWS {
|
|
144
|
-
let _err =
|
|
151
|
+
let _err =
|
|
152
|
+
this.error && this.error.message
|
|
153
|
+
? this.error.message
|
|
154
|
+
: !this.error
|
|
155
|
+
? null
|
|
156
|
+
: JSON.stringify(this.error);
|
|
145
157
|
|
|
146
158
|
const valueBody: ResponseBodyVO = {
|
|
147
|
-
statusResult: this.statusResult,
|
|
159
|
+
statusResult: this.statusResult,
|
|
160
|
+
message: this.message,
|
|
161
|
+
data: this.data,
|
|
162
|
+
count: this.count,
|
|
163
|
+
error: _err,
|
|
148
164
|
};
|
|
165
|
+
if (this.redirectTo) valueBody.redirectTo = this.redirectTo;
|
|
149
166
|
const valueBodyWrap: ResponseVO = {
|
|
150
|
-
statusCode: this.statusCode,
|
|
167
|
+
statusCode: this.statusCode,
|
|
168
|
+
body: JSON.stringify(valueBody),
|
|
151
169
|
};
|
|
152
170
|
|
|
153
171
|
return this.bodyWrap ? valueBodyWrap : valueBody;
|
|
@@ -155,7 +173,6 @@ class Result {
|
|
|
155
173
|
}
|
|
156
174
|
|
|
157
175
|
export class CreateResponse {
|
|
158
|
-
|
|
159
176
|
/**
|
|
160
177
|
* Success
|
|
161
178
|
* @param data
|
|
@@ -163,9 +180,19 @@ export class CreateResponse {
|
|
|
163
180
|
* @param message
|
|
164
181
|
* @param bodyWrap
|
|
165
182
|
*/
|
|
166
|
-
static success({
|
|
183
|
+
static success({
|
|
184
|
+
data = null,
|
|
185
|
+
count = null,
|
|
186
|
+
message = "success",
|
|
187
|
+
bodyWrap = true,
|
|
188
|
+
}: TFuncParams): ResponseVoAWS {
|
|
167
189
|
const result = new Result({
|
|
168
|
-
statusCode: StatusCode.OK,
|
|
190
|
+
statusCode: StatusCode.OK,
|
|
191
|
+
statusResult: StatusResult.ok,
|
|
192
|
+
message,
|
|
193
|
+
data,
|
|
194
|
+
count,
|
|
195
|
+
bodyWrap,
|
|
169
196
|
});
|
|
170
197
|
return result.bodyToString();
|
|
171
198
|
}
|
|
@@ -176,9 +203,17 @@ export class CreateResponse {
|
|
|
176
203
|
* @param message
|
|
177
204
|
* @param bodyWrap
|
|
178
205
|
*/
|
|
179
|
-
static created({
|
|
206
|
+
static created({
|
|
207
|
+
data,
|
|
208
|
+
message = "created",
|
|
209
|
+
bodyWrap = true,
|
|
210
|
+
}: TFuncParams): ResponseVoAWS {
|
|
180
211
|
const result = new Result({
|
|
181
|
-
statusCode: StatusCode.Created,
|
|
212
|
+
statusCode: StatusCode.Created,
|
|
213
|
+
statusResult: StatusResult.ok,
|
|
214
|
+
message,
|
|
215
|
+
data,
|
|
216
|
+
bodyWrap,
|
|
182
217
|
});
|
|
183
218
|
return result.bodyToString();
|
|
184
219
|
}
|
|
@@ -189,9 +224,38 @@ export class CreateResponse {
|
|
|
189
224
|
* @param message
|
|
190
225
|
* @param bodyWrap
|
|
191
226
|
*/
|
|
192
|
-
static updated({
|
|
227
|
+
static updated({
|
|
228
|
+
data,
|
|
229
|
+
message = "updated",
|
|
230
|
+
bodyWrap = true,
|
|
231
|
+
}: TFuncParams): ResponseVoAWS {
|
|
193
232
|
const result = new Result({
|
|
194
|
-
statusCode: StatusCode.OK,
|
|
233
|
+
statusCode: StatusCode.OK,
|
|
234
|
+
statusResult: StatusResult.ok,
|
|
235
|
+
message,
|
|
236
|
+
data,
|
|
237
|
+
bodyWrap,
|
|
238
|
+
});
|
|
239
|
+
return result.bodyToString();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Update or Create
|
|
244
|
+
* @param data
|
|
245
|
+
* @param message
|
|
246
|
+
* @param bodyWrap
|
|
247
|
+
*/
|
|
248
|
+
static updateOrCreate({
|
|
249
|
+
data,
|
|
250
|
+
message = "update_or_create",
|
|
251
|
+
bodyWrap = true,
|
|
252
|
+
}: TFuncParams): ResponseVoAWS {
|
|
253
|
+
const result = new Result({
|
|
254
|
+
statusCode: StatusCode.OK,
|
|
255
|
+
statusResult: StatusResult.ok,
|
|
256
|
+
message,
|
|
257
|
+
data,
|
|
258
|
+
bodyWrap,
|
|
195
259
|
});
|
|
196
260
|
return result.bodyToString();
|
|
197
261
|
}
|
|
@@ -202,9 +266,18 @@ export class CreateResponse {
|
|
|
202
266
|
* @param message
|
|
203
267
|
* @param bodyWrap
|
|
204
268
|
*/
|
|
205
|
-
static notFound({
|
|
269
|
+
static notFound({
|
|
270
|
+
error = null,
|
|
271
|
+
message = "",
|
|
272
|
+
bodyWrap = true,
|
|
273
|
+
}: TFuncParams): ResponseVoAWS {
|
|
206
274
|
const result = new Result({
|
|
207
|
-
statusCode: StatusCode.NotFound,
|
|
275
|
+
statusCode: StatusCode.NotFound,
|
|
276
|
+
statusResult: StatusResult.notFound,
|
|
277
|
+
message,
|
|
278
|
+
data: null,
|
|
279
|
+
error,
|
|
280
|
+
bodyWrap,
|
|
208
281
|
});
|
|
209
282
|
return result.bodyToString();
|
|
210
283
|
}
|
|
@@ -217,10 +290,67 @@ export class CreateResponse {
|
|
|
217
290
|
* @param bodyWrap
|
|
218
291
|
*/
|
|
219
292
|
static error({
|
|
220
|
-
error = null,
|
|
293
|
+
error = null,
|
|
294
|
+
statusCode = StatusCode.BadRequest,
|
|
295
|
+
message = "Error",
|
|
296
|
+
bodyWrap = true,
|
|
297
|
+
}: TFuncParams): ResponseVoAWS {
|
|
298
|
+
const result = new Result({
|
|
299
|
+
statusCode,
|
|
300
|
+
statusResult: StatusResult.error,
|
|
301
|
+
data: null,
|
|
302
|
+
error,
|
|
303
|
+
message,
|
|
304
|
+
bodyWrap,
|
|
305
|
+
});
|
|
306
|
+
return result.bodyToString();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Unauthorized
|
|
311
|
+
* @param error
|
|
312
|
+
* @param statusCode
|
|
313
|
+
* @param message
|
|
314
|
+
* @param bodyWrap
|
|
315
|
+
*/
|
|
316
|
+
static unauthorized({
|
|
317
|
+
error = null,
|
|
318
|
+
statusCode = StatusCode.Unauthorized,
|
|
319
|
+
message = "Unauthorized",
|
|
320
|
+
bodyWrap = true,
|
|
321
|
+
}: TFuncParams): ResponseVoAWS {
|
|
322
|
+
const result = new Result({
|
|
323
|
+
statusCode,
|
|
324
|
+
statusResult: StatusResult.unauthorized,
|
|
325
|
+
data: null,
|
|
326
|
+
error,
|
|
327
|
+
message,
|
|
328
|
+
bodyWrap,
|
|
329
|
+
});
|
|
330
|
+
return result.bodyToString();
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Redirect
|
|
335
|
+
* @param error
|
|
336
|
+
* @param statusCode
|
|
337
|
+
* @param message
|
|
338
|
+
* @param bodyWrap
|
|
339
|
+
*/
|
|
340
|
+
static redirect({
|
|
341
|
+
statusCode = StatusCode.MovedTemporarily,
|
|
342
|
+
message = "",
|
|
343
|
+
bodyWrap = true,
|
|
344
|
+
redirectTo = "",
|
|
221
345
|
}: TFuncParams): ResponseVoAWS {
|
|
222
346
|
const result = new Result({
|
|
223
|
-
statusCode,
|
|
347
|
+
statusCode,
|
|
348
|
+
statusResult: StatusResult.needRedirect,
|
|
349
|
+
data: null,
|
|
350
|
+
error: null,
|
|
351
|
+
message,
|
|
352
|
+
redirectTo,
|
|
353
|
+
bodyWrap,
|
|
224
354
|
});
|
|
225
355
|
return result.bodyToString();
|
|
226
356
|
}
|
|
@@ -238,79 +368,142 @@ export class CreateResponse {
|
|
|
238
368
|
static custom({
|
|
239
369
|
statusCode = StatusCode.OK,
|
|
240
370
|
statusResult = StatusResult.ok,
|
|
241
|
-
message =
|
|
371
|
+
message = "",
|
|
242
372
|
error = null,
|
|
243
373
|
data = null,
|
|
244
374
|
count = null,
|
|
245
375
|
bodyWrap = true,
|
|
246
376
|
}: TFuncParams): ResponseVoAWS {
|
|
247
377
|
const result = new Result({
|
|
248
|
-
statusCode,
|
|
378
|
+
statusCode,
|
|
379
|
+
statusResult,
|
|
380
|
+
message,
|
|
381
|
+
error,
|
|
382
|
+
data,
|
|
383
|
+
count,
|
|
384
|
+
bodyWrap,
|
|
249
385
|
});
|
|
250
386
|
return result.bodyToString();
|
|
251
387
|
}
|
|
252
388
|
}
|
|
253
389
|
|
|
254
|
-
export const messagesREST = (prefix: string, suffix: string =
|
|
390
|
+
export const messagesREST = (prefix: string, suffix: string = "") => {
|
|
255
391
|
return {
|
|
256
392
|
TOTAL: `${prefix}_TOTAL${suffix}`,
|
|
393
|
+
|
|
394
|
+
NOT_FOUND: `${prefix}_NOT_FOUND${suffix}`,
|
|
395
|
+
|
|
396
|
+
TOKEN_EXPIRED_ERROR: `${prefix}_TOKEN_EXPIRED_ERROR${suffix}`,
|
|
397
|
+
|
|
398
|
+
AUTHORISED: `${prefix}_AUTHORISED${suffix}`,
|
|
399
|
+
UNAUTHORISED: `${prefix}_UNAUTHORISED${suffix}`,
|
|
400
|
+
ERROR_AUTHORISED: `${prefix}_ERROR_AUTHORISED${suffix}`,
|
|
401
|
+
|
|
402
|
+
EXIST: `${prefix}_EXIST${suffix}`,
|
|
403
|
+
NOT_EXIST: `${prefix}_NOT_EXIST${suffix}`,
|
|
404
|
+
ERROR_EXIST: `${prefix}_ERROR_EXIST${suffix}`,
|
|
405
|
+
|
|
406
|
+
IDENTIFIER: `${prefix}_IDENTIFIER${suffix}`,
|
|
407
|
+
NOT_IDENTIFIER: `${prefix}_NOT_IDENTIFIER${suffix}`,
|
|
408
|
+
ERROR_IDENTIFIER: `${prefix}_ERROR_IDENTIFIER${suffix}`,
|
|
409
|
+
|
|
257
410
|
CREATE: `${prefix}_ITEM_CREATE${suffix}`,
|
|
258
411
|
NOT_CREATE: `${prefix}_ITEM_NOT_CREATE${suffix}`,
|
|
259
412
|
ERROR_CREATE: `${prefix}_ITEM_ERROR_CREATE${suffix}`,
|
|
413
|
+
|
|
260
414
|
UPDATE: `${prefix}_ITEM_UPDATE${suffix}`,
|
|
261
|
-
|
|
415
|
+
ERROR_UPDATE: `${prefix}_ITEM_ERROR_UPDATE${suffix}`,
|
|
262
416
|
NOT_UPDATE: `${prefix}_ITEM_NOT_UPDATE${suffix}`,
|
|
417
|
+
|
|
418
|
+
UPDATE_OR_CREATE: `${prefix}_ITEM_UPDATE_OR_CREATE${suffix}`,
|
|
419
|
+
ERROR_UPDATE_OR_CREATE: `${prefix}_ITEM_UPDATE_OR_CREATE${suffix}`,
|
|
420
|
+
NOT_UPDATE_OR_CREATE: `${prefix}_ITEM_NOT_UPDATE_OR_CREATE${suffix}`,
|
|
421
|
+
|
|
422
|
+
UPDATE_MANY: `${prefix}_ITEM_UPDATE_MANY${suffix}`,
|
|
263
423
|
NOT_UPDATE_MANY: `${prefix}_ITEM_NOT_UPDATE_MANY${suffix}`,
|
|
264
|
-
ERROR_UPDATE: `${prefix}_ITEM_ERROR_UPDATE${suffix}`,
|
|
265
424
|
ERROR_UPDATE_MANY: `${prefix}_ITEM_ERROR_UPDATE_MANY${suffix}`,
|
|
425
|
+
|
|
426
|
+
GET_ONE_AND_CHILDREN: `${prefix}_GET_ONE_AND_CHILDREN${suffix}`,
|
|
427
|
+
NOT_GET_ONE_AND_CHILDREN: `${prefix}_NOT_GET_ONE_AND_CHILDREN${suffix}`,
|
|
428
|
+
ERROR_GET_ONE_AND_CHILDREN: `${prefix}_ERROR_GET_ONE_AND_CHILDREN${suffix}`,
|
|
429
|
+
|
|
266
430
|
GET: `${prefix}_ITEM_GET${suffix}`,
|
|
267
431
|
NOT_GET: `${prefix}_ITEM_NOT_GET${suffix}`,
|
|
268
432
|
ERROR_GET: `${prefix}_ITEM_ERROR_GET${suffix}`,
|
|
433
|
+
|
|
269
434
|
GET_MANY: `${prefix}_GET_MANY${suffix}`,
|
|
270
435
|
NOT_GET_MANY: `${prefix}_NOT_GET_MANY${suffix}`,
|
|
271
436
|
ERROR_GET_MANY: `${prefix}_ERROR_GET_MANY${suffix}`,
|
|
437
|
+
|
|
272
438
|
GET_MANY_AND_COUNT: `${prefix}_GET_MANY_AND_COUNT${suffix}`,
|
|
273
439
|
NOT_GET_MANY_AND_COUNT: `${prefix}_NOT_GET_MANY_AND_COUNT${suffix}`,
|
|
274
440
|
ERROR_GET_MANY_AND_COUNT: `${prefix}_ERROR_GET_MANY_AND_COUNT${suffix}`,
|
|
441
|
+
|
|
275
442
|
GET_COUNT: `${prefix}_GET_COUNT${suffix}`,
|
|
276
443
|
NOT_GET_COUNT: `${prefix}_NOT_GET_COUNT${suffix}`,
|
|
277
444
|
ERROR_GET_COUNT: `${prefix}_ERROR_GET_COUNT${suffix}`,
|
|
445
|
+
|
|
278
446
|
DELETE: `${prefix}_ITEM_DELETE${suffix}`,
|
|
279
447
|
NOT_DELETE: `${prefix}_ITEM_NOT_DELETE${suffix}`,
|
|
280
448
|
ERROR_DELETE: `${prefix}_ITEM_ERROR_DELETE${suffix}`,
|
|
449
|
+
|
|
281
450
|
DELETE_MANY: `${prefix}_DELETE_MANY${suffix}`,
|
|
282
451
|
NOT_DELETE_MANY: `${prefix}_NOT_DELETE_MANY${suffix}`,
|
|
283
452
|
ERROR_DELETE_MANY: `${prefix}_ERROR_DELETE_MANY${suffix}`,
|
|
284
|
-
|
|
453
|
+
|
|
285
454
|
INITIALISE: `${prefix}_INITIALISE${suffix}`,
|
|
286
455
|
NOT_INITIALISE: `${prefix}_NOT_INITIALISE${suffix}`,
|
|
287
456
|
ERROR_INITIALISE: `${prefix}_ERROR_INITIALISE${suffix}`,
|
|
457
|
+
|
|
288
458
|
INCREMENT: `${prefix}_INCREMENT${suffix}`,
|
|
289
459
|
NOT_INCREMENT: `${prefix}_NOT_INCREMENT${suffix}`,
|
|
290
460
|
ERROR_INCREMENT: `${prefix}_ERROR_INCREMENT${suffix}`,
|
|
461
|
+
|
|
291
462
|
DECREMENT: `${prefix}_DECREMENT${suffix}`,
|
|
292
463
|
NOT_DECREMENT: `${prefix}_NOT_DECREMENT${suffix}`,
|
|
293
464
|
ERROR_DECREMENT: `${prefix}_ERROR_DECREMENT${suffix}`,
|
|
465
|
+
|
|
294
466
|
COUNTER_DAY: `${prefix}_COUNTER_DAY${suffix}`,
|
|
295
467
|
NOT_COUNTER_DAY: `${prefix}_NOT_COUNTER_DAY${suffix}`,
|
|
296
468
|
ERROR_COUNTER_DAY: `${prefix}_ERROR_COUNTER_DAY${suffix}`,
|
|
469
|
+
|
|
297
470
|
COUNTER_MONTH: `${prefix}_COUNTER_MONTH${suffix}`,
|
|
298
471
|
NOT_COUNTER_MONTH: `${prefix}_NOT_COUNTER_MONTH${suffix}`,
|
|
299
472
|
ERROR_COUNTER_MONTH: `${prefix}_ERROR_COUNTER_MONTH${suffix}`,
|
|
473
|
+
|
|
300
474
|
COUNTER_YEAR: `${prefix}_COUNTER_YEAR${suffix}`,
|
|
301
475
|
NOT_COUNTER_YEAR: `${prefix}_NOT_COUNTER_YEAR${suffix}`,
|
|
476
|
+
ERROR_COUNTER_YEAR: `${prefix}_ERROR_COUNTER_YEAR${suffix}`,
|
|
477
|
+
|
|
302
478
|
TEST: `${prefix}_TEST${suffix}`,
|
|
303
479
|
NOT_TEST: `${prefix}_NOT_TEST${suffix}`,
|
|
304
480
|
ERROR_TEST: `${prefix}_ERROR_TEST${suffix}`,
|
|
481
|
+
|
|
305
482
|
AGGREGATION: `${prefix}_AGGREGATION${suffix}`,
|
|
306
483
|
NOT_AGGREGATION: `${prefix}_NOT_AGGREGATION${suffix}`,
|
|
307
484
|
ERROR_AGGREGATION: `${prefix}_ERROR_AGGREGATION${suffix}`,
|
|
485
|
+
|
|
486
|
+
USER_REGISTRATION: `${prefix}_USER_REGISTRATION${suffix}`,
|
|
487
|
+
NOT_USER_REGISTRATION: `${prefix}_NOT_USER_REGISTRATION${suffix}`,
|
|
488
|
+
ERROR_USER_REGISTRATION: `${prefix}_ERROR_USER_REGISTRATION${suffix}`,
|
|
489
|
+
|
|
490
|
+
USER_LOGIN: `${prefix}_USER_LOGIN${suffix}`,
|
|
491
|
+
NOT_USER_LOGIN: `${prefix}_NOT_USER_LOGIN${suffix}`,
|
|
492
|
+
ERROR_USER_LOGIN: `${prefix}_ERROR_USER_LOGIN${suffix}`,
|
|
493
|
+
|
|
494
|
+
USER_LOGOUT: `${prefix}_USER_LOGOUT${suffix}`,
|
|
495
|
+
NOT_USER_LOGOUT: `${prefix}_NOT_USER_LOGOUT${suffix}`,
|
|
496
|
+
ERROR_USER_LOGOUT: `${prefix}_ERROR_USER_LOGOUT${suffix}`,
|
|
497
|
+
|
|
498
|
+
USER_AUTHENTICATION_REFRESH: `${prefix}_USER_AUTHENTICATION_REFRESH${suffix}`,
|
|
499
|
+
NOT_USER_AUTHENTICATION_REFRESH: `${prefix}_NOT_USER_AUTHENTICATION_REFRESH${suffix}`,
|
|
500
|
+
ERROR_USER_AUTHENTICATION_REFRESH: `${prefix}_ERROR_USER_AUTHENTICATION_REFRESH${suffix}`,
|
|
308
501
|
};
|
|
309
502
|
};
|
|
310
503
|
|
|
311
|
-
export const optionsPaginationParams = [
|
|
504
|
+
export const optionsPaginationParams = ["limit", "skip", "count"];
|
|
312
505
|
|
|
313
|
-
export type TMongoFilterNormalise = {[fieldName: string]: any}
|
|
506
|
+
export type TMongoFilterNormalise = { [fieldName: string]: any };
|
|
314
507
|
|
|
315
508
|
/**
|
|
316
509
|
* Normalise filter for mongoose
|
|
@@ -318,16 +511,32 @@ export type TMongoFilterNormalise = {[fieldName: string]: any}
|
|
|
318
511
|
* @param filter
|
|
319
512
|
* @param excludeFields
|
|
320
513
|
*/
|
|
321
|
-
export const normaliseMongoFilter = (
|
|
514
|
+
export const normaliseMongoFilter = (
|
|
515
|
+
filter: TMongoFilterNormalise,
|
|
516
|
+
regexFields: string[],
|
|
517
|
+
excludeFields?: string[]
|
|
518
|
+
) => {
|
|
322
519
|
const _filter: TMongoFilterNormalise = {};
|
|
323
|
-
const excludeParams =
|
|
520
|
+
const excludeParams =
|
|
521
|
+
excludeFields && Array.isArray(excludeFields) && excludeFields.length > 0
|
|
522
|
+
? excludeFields
|
|
523
|
+
: optionsPaginationParams;
|
|
324
524
|
|
|
325
525
|
Object.keys(filter).forEach((f) => {
|
|
326
526
|
const v = filter[f];
|
|
327
|
-
if (
|
|
527
|
+
if (
|
|
528
|
+
!(
|
|
529
|
+
v === null ||
|
|
530
|
+
(typeof v === "number" && isNaN(v)) ||
|
|
531
|
+
v === Infinity ||
|
|
532
|
+
v === undefined ||
|
|
533
|
+
excludeParams.includes(f)
|
|
534
|
+
)
|
|
535
|
+
) {
|
|
328
536
|
_filter[f] = filter[f];
|
|
329
537
|
|
|
330
|
-
if (regexFields.includes(f))
|
|
538
|
+
if (regexFields.includes(f))
|
|
539
|
+
_filter[f] = { $regex: new RegExp(_filter[f], "gi") };
|
|
331
540
|
}
|
|
332
541
|
});
|
|
333
542
|
|
|
@@ -339,13 +548,41 @@ export interface TMongoPaginate {
|
|
|
339
548
|
limit: number;
|
|
340
549
|
}
|
|
341
550
|
|
|
551
|
+
export type TFieldsGQL =
|
|
552
|
+
| "create"
|
|
553
|
+
| "count"
|
|
554
|
+
|
|
555
|
+
| "find"
|
|
556
|
+
| "findOne"
|
|
557
|
+
| "findMany"
|
|
558
|
+
|
|
559
|
+
| "findOneByID"
|
|
560
|
+
| "findManyByIDs"
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
| "update"
|
|
564
|
+
| "updateOneByID"
|
|
565
|
+
| "updateManyByIDs"
|
|
566
|
+
|
|
567
|
+
| "deleteOne"
|
|
568
|
+
| "deleteOneByID"
|
|
569
|
+
| "deleteManyByIDs"
|
|
570
|
+
|
|
571
|
+
| "activeMarkByIDs"
|
|
572
|
+
| "deleteMarkByIDs"
|
|
573
|
+
| "systemMarkByIDs"
|
|
574
|
+
| "init";
|
|
575
|
+
|
|
342
576
|
/**
|
|
343
577
|
* Normalise Mongo Paginate params
|
|
344
578
|
* @param filter
|
|
345
579
|
*/
|
|
346
|
-
export const normaliseMongoPaginate = (
|
|
580
|
+
export const normaliseMongoPaginate = (
|
|
581
|
+
filter: TMongoFilterNormalise
|
|
582
|
+
): TMongoPaginate => {
|
|
347
583
|
let res: TMongoPaginate = {
|
|
348
|
-
skip: 0,
|
|
584
|
+
skip: 0,
|
|
585
|
+
limit: 50,
|
|
349
586
|
};
|
|
350
587
|
|
|
351
588
|
res.skip = filter && filter.skip ? parseInt(filter.skip, 10) || 0 : 0;
|
|
@@ -354,50 +591,85 @@ export const normaliseMongoPaginate = (filter: TMongoFilterNormalise): TMongoPag
|
|
|
354
591
|
return res;
|
|
355
592
|
};
|
|
356
593
|
|
|
357
|
-
export const controlResponseNull = (
|
|
594
|
+
export const controlResponseNull = (
|
|
595
|
+
data: object,
|
|
596
|
+
okResultOf:
|
|
597
|
+
| "create"
|
|
598
|
+
| "update"
|
|
599
|
+
| "update_or_create"
|
|
600
|
+
| "update_many"
|
|
601
|
+
| "increment"
|
|
602
|
+
| "decrement",
|
|
603
|
+
prefix: string,
|
|
604
|
+
bodyWrap: boolean = true
|
|
605
|
+
) => {
|
|
358
606
|
let result;
|
|
359
607
|
|
|
360
608
|
if (data) {
|
|
361
|
-
if (okResultOf ===
|
|
609
|
+
if (okResultOf === "create") {
|
|
362
610
|
result = CreateResponse.created({
|
|
363
|
-
data,
|
|
611
|
+
data,
|
|
612
|
+
message: messagesREST(prefix).CREATE,
|
|
613
|
+
bodyWrap,
|
|
364
614
|
});
|
|
365
615
|
}
|
|
366
616
|
|
|
367
|
-
if (okResultOf ===
|
|
617
|
+
if (okResultOf === "update") {
|
|
368
618
|
result = CreateResponse.updated({
|
|
369
|
-
data,
|
|
619
|
+
data,
|
|
620
|
+
message: messagesREST(prefix).UPDATE,
|
|
621
|
+
bodyWrap,
|
|
370
622
|
});
|
|
371
623
|
}
|
|
372
624
|
|
|
373
|
-
if (okResultOf ===
|
|
374
|
-
result = CreateResponse.
|
|
375
|
-
data,
|
|
625
|
+
if (okResultOf === "update_or_create") {
|
|
626
|
+
result = CreateResponse.updateOrCreate({
|
|
627
|
+
data,
|
|
628
|
+
message: messagesREST(prefix).UPDATE_OR_CREATE,
|
|
629
|
+
bodyWrap,
|
|
376
630
|
});
|
|
377
631
|
}
|
|
378
632
|
|
|
379
|
-
if (okResultOf ===
|
|
633
|
+
if (okResultOf === "update_many") {
|
|
380
634
|
result = CreateResponse.updated({
|
|
381
|
-
data,
|
|
635
|
+
data,
|
|
636
|
+
message: messagesREST(prefix).UPDATE_MANY,
|
|
637
|
+
bodyWrap,
|
|
382
638
|
});
|
|
383
639
|
}
|
|
384
640
|
|
|
385
|
-
if (okResultOf ===
|
|
641
|
+
if (okResultOf === "increment") {
|
|
386
642
|
result = CreateResponse.updated({
|
|
387
|
-
data,
|
|
643
|
+
data,
|
|
644
|
+
message: messagesREST(prefix).INCREMENT,
|
|
645
|
+
bodyWrap,
|
|
388
646
|
});
|
|
389
647
|
}
|
|
390
648
|
|
|
649
|
+
if (okResultOf === "decrement") {
|
|
650
|
+
result = CreateResponse.updated({
|
|
651
|
+
data,
|
|
652
|
+
message: messagesREST(prefix).DECREMENT,
|
|
653
|
+
bodyWrap,
|
|
654
|
+
});
|
|
655
|
+
}
|
|
391
656
|
} else {
|
|
392
|
-
let messageErr =
|
|
393
|
-
if (okResultOf ===
|
|
394
|
-
if (okResultOf ===
|
|
395
|
-
if (okResultOf ===
|
|
396
|
-
|
|
397
|
-
if (okResultOf ===
|
|
657
|
+
let messageErr = "";
|
|
658
|
+
if (okResultOf === "create") messageErr = messagesREST(prefix).NOT_CREATE;
|
|
659
|
+
if (okResultOf === "update") messageErr = messagesREST(prefix).NOT_UPDATE;
|
|
660
|
+
if (okResultOf === "update_or_create")
|
|
661
|
+
messageErr = messagesREST(prefix).NOT_UPDATE_OR_CREATE;
|
|
662
|
+
if (okResultOf === "update_many")
|
|
663
|
+
messageErr = messagesREST(prefix).NOT_UPDATE_MANY;
|
|
664
|
+
if (okResultOf === "increment")
|
|
665
|
+
messageErr = messagesREST(prefix).NOT_INCREMENT;
|
|
666
|
+
if (okResultOf === "decrement")
|
|
667
|
+
messageErr = messagesREST(prefix).NOT_DECREMENT;
|
|
398
668
|
|
|
399
669
|
result = CreateResponse.error({
|
|
400
|
-
data: data,
|
|
670
|
+
data: data,
|
|
671
|
+
message: messageErr,
|
|
672
|
+
bodyWrap,
|
|
401
673
|
});
|
|
402
674
|
}
|
|
403
675
|
|
package/package.json
CHANGED