topkat-utils 1.1.14 → 1.2.1
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/CHANGELOG.md +4 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/src/clean-stack-trace.d.ts +1 -0
- package/dist/src/clean-stack-trace.js +46 -0
- package/dist/src/clean-stack-trace.js.map +1 -0
- package/dist/src/config.d.ts +1 -1
- package/dist/src/date-utils.js +6 -6
- package/dist/src/date-utils.js.map +1 -1
- package/dist/src/error-utils.d.ts +23 -2
- package/dist/src/error-utils.js +114 -47
- package/dist/src/error-utils.js.map +1 -1
- package/dist/src/logger-utils.d.ts +1 -1
- package/dist/src/logger-utils.js +2 -2
- package/dist/src/logger-utils.js.map +1 -1
- package/dist/src/loop-utils.d.ts +1 -1
- package/dist/src/object-utils.d.ts +1 -1
- package/dist/src/object-utils.js +5 -5
- package/dist/src/object-utils.js.map +1 -1
- package/dist/src/string-utils.d.ts +1 -1
- package/dist/src/tests-utils.d.ts +1 -1
- package/dist/src/tests-utils.js +1 -1
- package/dist/src/tests-utils.js.map +1 -1
- package/dist/src/timer-utils.js +2 -2
- package/dist/src/timer-utils.js.map +1 -1
- package/dist/src/types.d.ts +5 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/validation-utils.d.ts +1 -1
- package/dist/src/validation-utils.js +7 -7
- package/dist/src/validation-utils.js.map +1 -1
- package/index.ts +1 -1
- package/package.json +1 -1
- package/src/clean-stack-trace.ts +40 -0
- package/src/config.ts +1 -1
- package/src/date-utils.ts +5 -5
- package/src/error-utils.ts +86 -44
- package/src/logger-utils.ts +2 -2
- package/src/loop-utils.ts +1 -1
- package/src/object-utils.ts +6 -6
- package/src/string-utils.ts +1 -1
- package/src/tests-utils.ts +2 -2
- package/src/timer-utils.ts +2 -2
- package/src/{private/types.ts → types.ts} +0 -0
- package/src/validation-utils.ts +8 -8
- package/src/private/error-handler.ts +0 -21
package/src/object-utils.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
//----------------------------------------
|
|
2
2
|
// OBJECT UTILS
|
|
3
3
|
//----------------------------------------
|
|
4
|
-
import { ObjectGeneric } from "./
|
|
4
|
+
import { ObjectGeneric } from "./types"
|
|
5
5
|
import { err500IfNotSet } from "./error-utils"
|
|
6
6
|
import { recursiveGenericFunctionSync } from "./loop-utils"
|
|
7
7
|
import { isset } from "./isset"
|
|
8
8
|
import { isObject } from "./is-object"
|
|
9
|
-
import {
|
|
9
|
+
import { DescriptiveError } from "./error-utils"
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
*
|
|
@@ -97,7 +97,7 @@ export function objForceWrite(obj: ObjectGeneric, addr: string, item) {
|
|
|
97
97
|
const nextChunk = chunks[i + 1]
|
|
98
98
|
if (isset(nextChunk) && nextChunk.startsWith('[')) lastItem[chunk] = []
|
|
99
99
|
else lastItem[chunk] = {}
|
|
100
|
-
} else if (typeof lastItem[chunk] !== 'object') throw new
|
|
100
|
+
} else if (typeof lastItem[chunk] !== 'object') throw new DescriptiveError(`itemNotTypeObjectOrArrayInAddrChainForObjForceWrite`, { code: 500, origin: 'Validator', chunks, actualValueOfItem: lastItem[chunk], actualChunk: chunk })
|
|
101
101
|
lastItem = lastItem[chunk]
|
|
102
102
|
})
|
|
103
103
|
}
|
|
@@ -191,7 +191,7 @@ export function objFilterUndefined(o) {
|
|
|
191
191
|
|
|
192
192
|
/** Lock all 1st level props of an object to read only */
|
|
193
193
|
export function readOnly(o) {
|
|
194
|
-
const throwErr = () => { throw new
|
|
194
|
+
const throwErr = () => { throw new DescriptiveError('Cannot modify object that is read only', { code: 500 }) }
|
|
195
195
|
return new Proxy(o, {
|
|
196
196
|
set: throwErr,
|
|
197
197
|
defineProperty: throwErr,
|
|
@@ -203,14 +203,14 @@ export function readOnly(o) {
|
|
|
203
203
|
export function reassignForbidden(o) {
|
|
204
204
|
return new Proxy(o, {
|
|
205
205
|
defineProperty: function (that, key, value) {
|
|
206
|
-
if (key in that) throw new
|
|
206
|
+
if (key in that) throw new DescriptiveError(`Cannot reassign the property ${key.toString()} of this object`, { code: 500 })
|
|
207
207
|
else {
|
|
208
208
|
that[key] = value
|
|
209
209
|
return true
|
|
210
210
|
}
|
|
211
211
|
},
|
|
212
212
|
deleteProperty: function (_, key) {
|
|
213
|
-
throw new
|
|
213
|
+
throw new DescriptiveError(`Cannot delete the property ${key.toString()} of this object`, { code: 500 })
|
|
214
214
|
}
|
|
215
215
|
})
|
|
216
216
|
}
|
package/src/string-utils.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// STRING UTILS
|
|
3
3
|
//----------------------------------------
|
|
4
4
|
import { err500IfEmptyOrNotSet } from "./error-utils"
|
|
5
|
-
import { ObjectGeneric } from "./
|
|
5
|
+
import { ObjectGeneric } from "./types"
|
|
6
6
|
import { isset } from "./isset"
|
|
7
7
|
|
|
8
8
|
/**Eg: camelCase */
|
package/src/tests-utils.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { C } from "./logger-utils"
|
|
|
5
5
|
import { ValidatorObject } from "./validation-utils"
|
|
6
6
|
import { isset } from "./isset"
|
|
7
7
|
import { validatorReturnErrArray } from "./validation-utils"
|
|
8
|
-
import { Override } from "./
|
|
8
|
+
import { Override } from "./types"
|
|
9
9
|
import { isObject } from "./is-object"
|
|
10
10
|
|
|
11
11
|
export const restTestMini = {
|
|
@@ -55,7 +55,7 @@ export function assert(description: string, value: any, validatorObject?: Overri
|
|
|
55
55
|
else if (isset(validatorObject)) validatorObjReal.eq = validatorObject
|
|
56
56
|
|
|
57
57
|
const issetCheck = !isset(validatorObjReal) // isEmpty()
|
|
58
|
-
const [errMsg,
|
|
58
|
+
const [errMsg, extraInfos] = validatorReturnErrArray(validatorObjReal)
|
|
59
59
|
const msg2 = description + ` ${issetCheck ? 'isset' : `${JSON.stringify(validatorObject)}`}`
|
|
60
60
|
if (isset(errMsg)) {
|
|
61
61
|
const err = msg2 + `\n ${errMsg}\n ${JSON.stringify(extraInfos)}`
|
package/src/timer-utils.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// TIMEOUT UTILS
|
|
4
4
|
//----------------------------------------
|
|
5
5
|
import { cliProgressBar, C } from "./logger-utils"
|
|
6
|
-
import {
|
|
6
|
+
import { DescriptiveError } from "./error-utils"
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
export async function timeout(ms, fn = () => { }) { return new Promise(res => setTimeout(res, ms)).then(fn) }
|
|
@@ -29,7 +29,7 @@ export async function waitUntilTrue(callback, timeoutSec = 10, errorAfterNSecond
|
|
|
29
29
|
await timeout(300)
|
|
30
30
|
}
|
|
31
31
|
if (cliOutput) process.stdout.write(`\n`)
|
|
32
|
-
if (!generalTimeout && errorAfterNSeconds) throw new
|
|
32
|
+
if (!generalTimeout && errorAfterNSeconds) throw new DescriptiveError(errMess, { code: 500 })
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
|
|
File without changes
|
package/src/validation-utils.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
//----------------------------------------
|
|
2
2
|
// VALIDATION UTILS
|
|
3
3
|
//----------------------------------------
|
|
4
|
-
import { dataValidationUtilErrorHandler } from "./private/error-handler"
|
|
5
4
|
import { isset } from "./isset"
|
|
6
5
|
import { isDateIsoOrObjectValid, isDateIntOrStringValid, isTimeStringValid } from "./date-utils"
|
|
7
6
|
import { asArray } from "./array-utils"
|
|
8
7
|
import { configFn } from "./config"
|
|
9
8
|
import { isEmpty } from "./is-empty"
|
|
9
|
+
import { DescriptiveError } from "./error-utils"
|
|
10
10
|
import { removeCircularJSONstringify } from "./remove-circular-json-stringify"
|
|
11
11
|
|
|
12
12
|
export type BaseTypes = 'objectId' | 'dateInt6' | 'dateInt' | 'dateInt8' | 'dateInt12' | 'time' | 'humanReadableTimestamp' | 'date' | 'dateObject' | 'array' | 'object' | 'buffer' | 'string' | 'function' | 'boolean' | 'number' | 'bigint' | 'year' | 'email' | 'any'
|
|
@@ -22,7 +22,7 @@ export function checkAllObjectValuesAreEmpty(o) { return Object.values(o).every(
|
|
|
22
22
|
|
|
23
23
|
/** Throw an error in case data passed is not a valid ctx */
|
|
24
24
|
export function checkCtxIntegrity(ctx) {
|
|
25
|
-
if (!isset(ctx) || !isset(ctx.user)) throw new
|
|
25
|
+
if (!isset(ctx) || !isset(ctx.user)) throw new DescriptiveError('ctxNotSet', { code: 500 })
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
@@ -90,7 +90,7 @@ export type ValidatorObject = {
|
|
|
90
90
|
}
|
|
91
91
|
export function validator(...paramsToValidate: ValidatorObject[]) {
|
|
92
92
|
const errArray = validatorReturnErrArray(...paramsToValidate)
|
|
93
|
-
if (errArray.length) throw new
|
|
93
|
+
if (errArray.length) throw new DescriptiveError(...errArray)
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
|
|
@@ -107,13 +107,13 @@ export function isValid(...paramsToValidate) {
|
|
|
107
107
|
*/
|
|
108
108
|
export function isType(value, type: BaseTypes) { return isValid({ name: 'Is type check', value, type }) }
|
|
109
109
|
|
|
110
|
-
export function validatorReturnErrArray(...paramsToValidate: ValidatorObject[]): [string?,
|
|
110
|
+
export function validatorReturnErrArray(...paramsToValidate: ValidatorObject[]): [string?, object?] {
|
|
111
111
|
let paramsFormatted: ValidatorObject[] = []
|
|
112
112
|
|
|
113
113
|
// support for multiple names with multiple values for one rule. Eg: {name: [{startDate:'20180101'}, {endDate:'20180101'}], type: 'dateInt8'}
|
|
114
114
|
paramsToValidate.forEach(param => {
|
|
115
115
|
if (typeof param !== 'object' || Array.isArray(param))
|
|
116
|
-
throw new
|
|
116
|
+
throw new DescriptiveError(`wrongTypeForDataValidatorArgument`, { code: 500, origin: 'Generic validator', expectedType: 'object', actualType: Array.isArray(param) ? 'array' : typeof param })
|
|
117
117
|
|
|
118
118
|
// parse => name: {myVar1: 'blah, myvar2: myvar2}
|
|
119
119
|
if (typeof param.name === 'object' && !Array.isArray(param.name))
|
|
@@ -127,12 +127,12 @@ export function validatorReturnErrArray(...paramsToValidate: ValidatorObject[]):
|
|
|
127
127
|
let optional = paramObj.optional || false
|
|
128
128
|
let emptyAllowed = optional || paramObj.emptyAllowed || false
|
|
129
129
|
if (paramObj.isset === false) paramObj.mustNotBeSet = true // ALIAS
|
|
130
|
-
const errMess = (msg, extraInfos = {}, errCode = 422): [string,
|
|
130
|
+
const errMess = (msg, extraInfos = {}, errCode = 422): [string, object] => [msg, { code: errCode, origin: 'Generic validator', varName: name, gotValue: isset(value) && isset(value.data) && isset(value.data.data) ? { ...value, data: 'Buffer' } : value, ...extraInfos }]
|
|
131
131
|
|
|
132
132
|
// accept syntax { 'myVar.var2': myVar.var2, ... }
|
|
133
133
|
if (!isset(name)) {
|
|
134
134
|
name = Object.keys(paramObj).find(param => !['in', 'eq', 'lte', 'gte', 'name', 'value', 'type', 'regexp', 'minLength', 'maxLength', 'optional', 'emptyAllowed', 'mustNotBeSet', 'includes', 'length'].includes(param))
|
|
135
|
-
if (isset(name)) value = paramObj[name]
|
|
135
|
+
if (isset(name)) value = paramObj[name]
|
|
136
136
|
}
|
|
137
137
|
// if nameString ends by $ sign it is optional
|
|
138
138
|
if (isset(name) && /.*\$$/.test(name)) {
|
|
@@ -183,7 +183,7 @@ export function validatorReturnErrArray(...paramsToValidate: ValidatorObject[]):
|
|
|
183
183
|
//...Object.keys(configFn().customTypes)
|
|
184
184
|
]
|
|
185
185
|
|
|
186
|
-
if (!allTypes.includes(type)) throw new
|
|
186
|
+
if (!allTypes.includes(type)) throw new DescriptiveError('typeDoNotExist', { code: 500, type })
|
|
187
187
|
|
|
188
188
|
const basicTypeCheck = {
|
|
189
189
|
objectId: val => /^[0-9a-fA-F-]{24,}$/.test(val), // "0c65940b-6b0c-4dd8-9c7a-7c5fe1ba907a"
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export class dataValidationUtilErrorHandler extends Error {
|
|
2
|
-
fromDataValidation: boolean
|
|
3
|
-
code: number
|
|
4
|
-
extraInfos: object
|
|
5
|
-
msg: string
|
|
6
|
-
errorDescription: { [k: string]: any }
|
|
7
|
-
constructor(msg, code, extraInfos?) {
|
|
8
|
-
super(msg)
|
|
9
|
-
this.message = msg
|
|
10
|
-
this.msg = msg
|
|
11
|
-
this.name = `${code} ${msg}`
|
|
12
|
-
this.fromDataValidation = true // will be catched by express error handler
|
|
13
|
-
this.code = code
|
|
14
|
-
this.extraInfos = extraInfos
|
|
15
|
-
this.errorDescription = {
|
|
16
|
-
msg,
|
|
17
|
-
code,
|
|
18
|
-
...extraInfos,
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|