@naturalcycles/js-lib 14.64.0 → 14.68.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.
- package/dist/decorators/logMethod.decorator.d.ts +5 -0
- package/dist/decorators/logMethod.decorator.js +9 -9
- package/dist/decorators/memo.decorator.d.ts +11 -0
- package/dist/decorators/memo.decorator.js +6 -6
- package/dist/decorators/memoFn.js +5 -5
- package/dist/decorators/memoSimple.decorator.d.ts +2 -0
- package/dist/decorators/memoSimple.decorator.js +4 -10
- package/dist/error/error.model.d.ts +0 -1
- package/dist/error/tryCatch.d.ts +5 -0
- package/dist/error/tryCatch.js +3 -6
- package/dist/index.d.ts +3 -3
- package/dist/index.js +6 -3
- package/dist/log/commonLogger.d.ts +11 -12
- package/dist/log/commonLogger.js +64 -18
- package/dist/promise/pRetry.d.ts +5 -0
- package/dist/promise/pRetry.js +4 -4
- package/dist-esm/decorators/logMethod.decorator.js +9 -9
- package/dist-esm/decorators/memo.decorator.js +6 -6
- package/dist-esm/decorators/memoFn.js +5 -5
- package/dist-esm/decorators/memoSimple.decorator.js +4 -10
- package/dist-esm/error/tryCatch.js +3 -3
- package/dist-esm/index.js +2 -2
- package/dist-esm/log/commonLogger.js +60 -16
- package/dist-esm/promise/pRetry.js +4 -4
- package/package.json +1 -1
- package/src/decorators/logMethod.decorator.ts +15 -9
- package/src/decorators/memo.decorator.ts +27 -17
- package/src/decorators/memoFn.ts +11 -10
- package/src/decorators/memoSimple.decorator.ts +6 -4
- package/src/error/error.model.ts +1 -1
- package/src/error/tryCatch.ts +9 -7
- package/src/index.ts +10 -6
- package/src/log/commonLogger.ts +67 -21
- package/src/promise/pRetry.ts +16 -5
package/src/error/tryCatch.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _since, _stringifyAny } from '../index'
|
|
1
|
+
import { _since, _stringifyAny, CommonLogger } from '../index'
|
|
2
2
|
import { AnyFunction } from '../types'
|
|
3
3
|
|
|
4
4
|
export interface TryCatchOptions {
|
|
@@ -17,6 +17,11 @@ export interface TryCatchOptions {
|
|
|
17
17
|
* @default true
|
|
18
18
|
*/
|
|
19
19
|
logError?: boolean
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Default to `console`
|
|
23
|
+
*/
|
|
24
|
+
logger?: CommonLogger
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
/**
|
|
@@ -28,10 +33,7 @@ export interface TryCatchOptions {
|
|
|
28
33
|
* @experimental
|
|
29
34
|
*/
|
|
30
35
|
export function _tryCatch<T extends AnyFunction>(fn: T, opt: TryCatchOptions = {}): T {
|
|
31
|
-
const { onError, logError, logSuccess } =
|
|
32
|
-
logError: true,
|
|
33
|
-
...opt,
|
|
34
|
-
}
|
|
36
|
+
const { onError, logError = true, logSuccess = false, logger = console } = opt
|
|
35
37
|
|
|
36
38
|
const fname = fn.name || 'anonymous'
|
|
37
39
|
|
|
@@ -42,13 +44,13 @@ export function _tryCatch<T extends AnyFunction>(fn: T, opt: TryCatchOptions = {
|
|
|
42
44
|
const r = await fn.apply(this, args)
|
|
43
45
|
|
|
44
46
|
if (logSuccess) {
|
|
45
|
-
|
|
47
|
+
logger.log(`tryCatch.${fname} succeeded in ${_since(started)}`)
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
return r
|
|
49
51
|
} catch (err) {
|
|
50
52
|
if (logError) {
|
|
51
|
-
|
|
53
|
+
logger.warn(
|
|
52
54
|
`tryCatch.${fname} error in ${_since(started)}:\n${_stringifyAny(err, {
|
|
53
55
|
includeErrorData: true,
|
|
54
56
|
})}`,
|
package/src/index.ts
CHANGED
|
@@ -228,9 +228,11 @@ import {
|
|
|
228
228
|
CommonLogLevel,
|
|
229
229
|
CommonLogFunction,
|
|
230
230
|
CommonLogger,
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
231
|
+
commonLoggerMinLevel,
|
|
232
|
+
commonLoggerNoop,
|
|
233
|
+
commonLogLevelNumber,
|
|
234
|
+
commonLoggerPipe,
|
|
235
|
+
commonLoggerPrefix,
|
|
234
236
|
} from './log/commonLogger'
|
|
235
237
|
import { _safeJsonStringify } from './string/safeJsonStringify'
|
|
236
238
|
|
|
@@ -307,7 +309,6 @@ export type {
|
|
|
307
309
|
CommonLogLevel,
|
|
308
310
|
CommonLogFunction,
|
|
309
311
|
CommonLogger,
|
|
310
|
-
SimpleLogger,
|
|
311
312
|
}
|
|
312
313
|
|
|
313
314
|
export {
|
|
@@ -466,7 +467,10 @@ export {
|
|
|
466
467
|
_defineLazyProperty,
|
|
467
468
|
_defineLazyProps,
|
|
468
469
|
_lazyValue,
|
|
469
|
-
|
|
470
|
-
|
|
470
|
+
commonLoggerMinLevel,
|
|
471
|
+
commonLoggerNoop,
|
|
472
|
+
commonLogLevelNumber,
|
|
473
|
+
commonLoggerPipe,
|
|
474
|
+
commonLoggerPrefix,
|
|
471
475
|
_safeJsonStringify,
|
|
472
476
|
}
|
package/src/log/commonLogger.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { _noop } from '../index'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* These levels follow console.* naming,
|
|
3
5
|
* so you can use console[level] safely.
|
|
@@ -10,6 +12,12 @@
|
|
|
10
12
|
*/
|
|
11
13
|
export type CommonLogLevel = 'log' | 'warn' | 'error'
|
|
12
14
|
|
|
15
|
+
export const commonLogLevelNumber: Record<CommonLogLevel, number> = {
|
|
16
|
+
log: 10,
|
|
17
|
+
warn: 20,
|
|
18
|
+
error: 30,
|
|
19
|
+
}
|
|
20
|
+
|
|
13
21
|
/**
|
|
14
22
|
* Function that takes any number of arguments and logs them all.
|
|
15
23
|
* It is expected that logged arguments are separated by "space", like console.log does.
|
|
@@ -31,35 +39,73 @@ export interface CommonLogger {
|
|
|
31
39
|
}
|
|
32
40
|
|
|
33
41
|
/**
|
|
34
|
-
*
|
|
35
|
-
* So you can do `logger('hey')` which is the same as `logger.log('hey')`
|
|
42
|
+
* SimpleLogger that does nothing (noop).
|
|
36
43
|
*
|
|
37
44
|
* @experimental
|
|
38
45
|
*/
|
|
39
|
-
export
|
|
46
|
+
export const commonLoggerNoop: CommonLogger = {
|
|
47
|
+
log: _noop,
|
|
48
|
+
warn: _noop,
|
|
49
|
+
error: _noop,
|
|
50
|
+
}
|
|
40
51
|
|
|
41
52
|
/**
|
|
42
|
-
* Creates a
|
|
43
|
-
*
|
|
44
|
-
* @experimental
|
|
53
|
+
* Creates a "child" logger that is "limited" to the specified CommonLogLevel.
|
|
45
54
|
*/
|
|
46
|
-
export function
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
export function commonLoggerMinLevel(
|
|
56
|
+
logger: CommonLogger,
|
|
57
|
+
minLevel: CommonLogLevel,
|
|
58
|
+
mutate = false,
|
|
59
|
+
): CommonLogger {
|
|
60
|
+
const level = commonLogLevelNumber[minLevel]
|
|
61
|
+
if (mutate) {
|
|
62
|
+
if (level > commonLogLevelNumber['log']) {
|
|
63
|
+
logger.log = _noop
|
|
64
|
+
if (level > commonLogLevelNumber['warn']) {
|
|
65
|
+
logger.warn = _noop
|
|
66
|
+
if (level > commonLogLevelNumber['error']) {
|
|
67
|
+
logger.error = _noop
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return logger
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (level <= commonLogLevelNumber['log']) {
|
|
75
|
+
// All levels are kept
|
|
76
|
+
return logger
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (level > commonLogLevelNumber['error']) {
|
|
80
|
+
// "Log nothing" logger
|
|
81
|
+
return commonLoggerNoop
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
log: _noop, // otherwise it is "log everything" logger (same logger as input)
|
|
86
|
+
warn: level <= commonLogLevelNumber['warn'] ? logger.warn.bind(logger) : _noop,
|
|
87
|
+
error: logger.error.bind(logger), // otherwise it's "log nothing" logger (same as noopLogger)
|
|
88
|
+
}
|
|
52
89
|
}
|
|
53
90
|
|
|
54
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Creates a "proxy" CommonLogger that pipes log messages to all provided sub-loggers.
|
|
93
|
+
*/
|
|
94
|
+
export function commonLoggerPipe(loggers: CommonLogger[]): CommonLogger {
|
|
95
|
+
return {
|
|
96
|
+
log: (...args) => loggers.forEach(logger => logger.log(...args)),
|
|
97
|
+
warn: (...args) => loggers.forEach(logger => logger.warn(...args)),
|
|
98
|
+
error: (...args) => loggers.forEach(logger => logger.error(...args)),
|
|
99
|
+
}
|
|
100
|
+
}
|
|
55
101
|
|
|
56
102
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* @experimental
|
|
103
|
+
* Creates a "child" CommonLogger with prefix (one or multiple).
|
|
60
104
|
*/
|
|
61
|
-
export
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
105
|
+
export function commonLoggerPrefix(logger: CommonLogger, ...prefixes: any[]): CommonLogger {
|
|
106
|
+
return {
|
|
107
|
+
log: (...args) => logger.log(...prefixes, ...args),
|
|
108
|
+
warn: (...args) => logger.warn(...prefixes, ...args),
|
|
109
|
+
error: (...args) => logger.error(...prefixes, ...args),
|
|
110
|
+
}
|
|
111
|
+
}
|
package/src/promise/pRetry.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _since, _stringifyAny } from '..'
|
|
1
|
+
import { _since, _stringifyAny, CommonLogger } from '..'
|
|
2
2
|
|
|
3
3
|
export interface PRetryOptions {
|
|
4
4
|
/**
|
|
@@ -63,6 +63,11 @@ export interface PRetryOptions {
|
|
|
63
63
|
* @default false
|
|
64
64
|
*/
|
|
65
65
|
logNone?: boolean
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Default to `console`
|
|
69
|
+
*/
|
|
70
|
+
logger?: CommonLogger
|
|
66
71
|
}
|
|
67
72
|
|
|
68
73
|
/**
|
|
@@ -71,7 +76,13 @@ export interface PRetryOptions {
|
|
|
71
76
|
*/
|
|
72
77
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
73
78
|
export function pRetry<T extends Function>(fn: T, opt: PRetryOptions = {}): T {
|
|
74
|
-
const {
|
|
79
|
+
const {
|
|
80
|
+
maxAttempts = 4,
|
|
81
|
+
delay: initialDelay = 1000,
|
|
82
|
+
delayMultiplier = 2,
|
|
83
|
+
predicate,
|
|
84
|
+
logger = console,
|
|
85
|
+
} = opt
|
|
75
86
|
|
|
76
87
|
let { logFirstAttempt = false, logRetries = true, logFailures = false, logSuccess = false } = opt
|
|
77
88
|
|
|
@@ -95,18 +106,18 @@ export function pRetry<T extends Function>(fn: T, opt: PRetryOptions = {}): T {
|
|
|
95
106
|
try {
|
|
96
107
|
attempt++
|
|
97
108
|
if ((attempt === 1 && logFirstAttempt) || (attempt > 1 && logRetries)) {
|
|
98
|
-
|
|
109
|
+
logger.log(`${fname} attempt #${attempt}...`)
|
|
99
110
|
}
|
|
100
111
|
|
|
101
112
|
const r = await fn.apply(this, args)
|
|
102
113
|
|
|
103
114
|
if (logSuccess) {
|
|
104
|
-
|
|
115
|
+
logger.log(`${fname} attempt #${attempt} succeeded in ${_since(started)}`)
|
|
105
116
|
}
|
|
106
117
|
resolve(r)
|
|
107
118
|
} catch (err) {
|
|
108
119
|
if (logFailures) {
|
|
109
|
-
|
|
120
|
+
logger.warn(
|
|
110
121
|
`${fname} attempt #${attempt} error in ${_since(started)}:\n${_stringifyAny(err, {
|
|
111
122
|
includeErrorData: true,
|
|
112
123
|
})}`,
|