@api3/commons 0.2.0 → 0.4.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/README.md +13 -24
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/logger/async-storage.d.ts +5 -0
- package/dist/logger/async-storage.d.ts.map +1 -0
- package/dist/logger/async-storage.js +12 -0
- package/dist/logger/async-storage.js.map +1 -0
- package/dist/logger/index.d.ts +14 -21
- package/dist/logger/index.d.ts.map +1 -1
- package/dist/logger/index.js +63 -20
- package/dist/logger/index.js.map +1 -1
- package/dist/processing/processing.d.ts.map +1 -1
- package/dist/processing/processing.js +1 -1
- package/dist/processing/processing.js.map +1 -1
- package/dist/processing/schema.d.ts +2 -3
- package/dist/processing/schema.d.ts.map +1 -1
- package/dist/processing/schema.js +9 -3
- package/dist/processing/schema.js.map +1 -1
- package/package.json +5 -10
- package/src/index.ts +3 -0
- package/src/logger/README.md +1 -2
- package/src/logger/async-storage.ts +10 -0
- package/src/logger/index.test.ts +111 -0
- package/src/logger/index.ts +76 -25
- package/src/processing/README.md +0 -4
- package/src/processing/processing.ts +2 -2
- package/src/processing/schema.ts +8 -3
package/README.md
CHANGED
|
@@ -39,27 +39,6 @@ import { createLogger } from '@api3/commons/dist/logger';
|
|
|
39
39
|
import { createLogger } from '@api3/commons/logger';
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
#### Import relative to the "node_modules"
|
|
43
|
-
|
|
44
|
-
This is the default CommonJS style and it applies if you have `moduleResolution` set to `Node` or `Node10` inside the
|
|
45
|
-
`tsconfig.json`.
|
|
46
|
-
|
|
47
|
-
#### The "exports" based import
|
|
48
|
-
|
|
49
|
-
This is done via `package.json` field called [exports](https://nodejs.org/api/packages.html#package-entry-points). This
|
|
50
|
-
field works for both CJS and ESM starting from Node version 12. In order for TS to support this, you need to make sure
|
|
51
|
-
you use `moduleResolution` and `module` set to `Node16` inside `tsconfig.json`.
|
|
52
|
-
|
|
53
|
-
```jsonc
|
|
54
|
-
// tsconfig.json
|
|
55
|
-
{
|
|
56
|
-
"compilerOptions": {
|
|
57
|
-
"module": "Node16",
|
|
58
|
-
"moduleResolution": "Node16"
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
42
|
## Release
|
|
64
43
|
|
|
65
44
|
To release a new version follow these steps:
|
|
@@ -77,11 +56,21 @@ To release a new version follow these steps:
|
|
|
77
56
|
|
|
78
57
|
1. Create a new directory in `src` with the name of the utility.
|
|
79
58
|
2. Create `index.ts` file in the directory. This file will be the entry point for the utility.
|
|
80
|
-
3.
|
|
81
|
-
[exports](https://nodejs.org/api/packages.html#package-entry-points) for more information.
|
|
59
|
+
3. Re-export the new utility from the root `index.ts` file.
|
|
82
60
|
4. Create a `README.md` with documentation.
|
|
83
61
|
|
|
84
|
-
|
|
62
|
+
#### Testing the package locally
|
|
63
|
+
|
|
64
|
+
It is preferred (simpler and faster) to test the package using the `file:` protocol.
|
|
65
|
+
|
|
66
|
+
1. Implement some changes and run `pnpm run build` to build the commons package.
|
|
67
|
+
2. In some other project, specify `file:<RELATIVE_PATH_TO_COMMONS>`. For example: `"@api3/commons": "file:../commons"`.
|
|
68
|
+
3. Re-install the dependencies in the project (doesn't matter what package manager you use). For example: `pnpm i`.
|
|
69
|
+
|
|
70
|
+
You can repeat the above steps as many times as you want. The package will be re-installed from the local directory with
|
|
71
|
+
the locally built content.
|
|
72
|
+
|
|
73
|
+
##### Using Verdaccio
|
|
85
74
|
|
|
86
75
|
The common pattern is to move some part of implementation to commons and then use it in some other repo. It is valuable
|
|
87
76
|
to see whether nothing broke in the process (before publishing the package). You can use
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// NOTE: Not exporting ESLint rules because they need to be imported in a special way inside .eslintrc.js config.
|
|
18
|
+
__exportStar(require("./logger"), exports);
|
|
19
|
+
__exportStar(require("./processing"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iHAAiH;AACjH,2CAAyB;AACzB,+CAA6B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-storage.d.ts","sourceRoot":"","sources":["../../src/logger/async-storage.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,GAAG,CAAC;AAIpC,eAAO,MAAM,oBAAoB,qCAGhC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAsyncLocalStorage = void 0;
|
|
4
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
5
|
+
let asyncLocalStorage;
|
|
6
|
+
const getAsyncLocalStorage = () => {
|
|
7
|
+
if (!asyncLocalStorage)
|
|
8
|
+
asyncLocalStorage = new node_async_hooks_1.AsyncLocalStorage();
|
|
9
|
+
return asyncLocalStorage;
|
|
10
|
+
};
|
|
11
|
+
exports.getAsyncLocalStorage = getAsyncLocalStorage;
|
|
12
|
+
//# sourceMappingURL=async-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-storage.js","sourceRoot":"","sources":["../../src/logger/async-storage.ts"],"names":[],"mappings":";;;AAAA,uDAAqD;AAIrD,IAAI,iBAAgD,CAAC;AAE9C,MAAM,oBAAoB,GAAG,GAAG,EAAE;IACvC,IAAI,CAAC,iBAAiB;QAAE,iBAAiB,GAAG,IAAI,oCAAiB,EAAc,CAAC;IAChF,OAAO,iBAAiB,CAAC;AAC3B,CAAC,CAAC;AAHW,QAAA,oBAAoB,wBAG/B"}
|
package/dist/logger/index.d.ts
CHANGED
|
@@ -1,34 +1,27 @@
|
|
|
1
|
-
import
|
|
2
|
-
export declare const
|
|
3
|
-
export type
|
|
4
|
-
export declare const
|
|
5
|
-
export type LogLevel =
|
|
6
|
-
export
|
|
7
|
-
colorize: z.ZodBoolean;
|
|
8
|
-
enabled: z.ZodBoolean;
|
|
9
|
-
format: z.ZodUnion<[z.ZodLiteral<"json">, z.ZodLiteral<"pretty">]>;
|
|
10
|
-
minLevel: z.ZodUnion<[z.ZodLiteral<"debug">, z.ZodLiteral<"info">, z.ZodLiteral<"warn">, z.ZodLiteral<"error">]>;
|
|
11
|
-
}, "strip", z.ZodTypeAny, {
|
|
1
|
+
import winston from 'winston';
|
|
2
|
+
export declare const logFormatOptions: readonly ["json", "pretty"];
|
|
3
|
+
export type LogFormat = (typeof logFormatOptions)[number];
|
|
4
|
+
export declare const logLevelOptions: readonly ["debug", "info", "warn", "error"];
|
|
5
|
+
export type LogLevel = (typeof logLevelOptions)[number];
|
|
6
|
+
export interface LogConfig {
|
|
12
7
|
colorize: boolean;
|
|
13
8
|
enabled: boolean;
|
|
14
|
-
format:
|
|
15
|
-
minLevel:
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
enabled: boolean;
|
|
19
|
-
format: "json" | "pretty";
|
|
20
|
-
minLevel: "error" | "info" | "debug" | "warn";
|
|
21
|
-
}>;
|
|
22
|
-
export type LogConfig = z.infer<typeof logConfigSchema>;
|
|
9
|
+
format: LogFormat;
|
|
10
|
+
minLevel: LogLevel;
|
|
11
|
+
}
|
|
12
|
+
export declare const createBaseLogger: (config: LogConfig) => winston.Logger;
|
|
23
13
|
export type LogContext = Record<string, any>;
|
|
24
14
|
export interface Logger {
|
|
15
|
+
runWithContext: <T>(context: LogContext, fn: () => T) => T;
|
|
25
16
|
debug: (message: string, context?: LogContext) => void;
|
|
26
17
|
info: (message: string, context?: LogContext) => void;
|
|
27
18
|
warn: (message: string, context?: LogContext) => void;
|
|
28
|
-
error: ((message: string, context?: LogContext) => void)
|
|
19
|
+
error: ((message: string, context?: LogContext) => void) | ((message: string, error: Error, context?: LogContext) => void);
|
|
29
20
|
child: (options: {
|
|
30
21
|
name: string;
|
|
31
22
|
}) => Logger;
|
|
32
23
|
}
|
|
24
|
+
export declare const wrapper: (logger: winston.Logger) => Logger;
|
|
25
|
+
export declare const validateLogConfig: (config: unknown) => LogConfig;
|
|
33
26
|
export declare const createLogger: (config: LogConfig) => Logger;
|
|
34
27
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":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AAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,KAAK,MAAM,CAAC;CAC9C;AAID,eAAO,MAAM,OAAO,WAAY,cAAc,KAAG,MAyChD,CAAC;AAEF,eAAO,MAAM,iBAAiB,WAAY,OAAO,KAAG,SAuBnD,CAAC;AAEF,eAAO,MAAM,YAAY,WAAY,SAAS,WAG7C,CAAC"}
|
package/dist/logger/index.js
CHANGED
|
@@ -3,18 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createLogger = exports.
|
|
6
|
+
exports.createLogger = exports.validateLogConfig = exports.wrapper = exports.createBaseLogger = exports.logLevelOptions = exports.logFormatOptions = void 0;
|
|
7
7
|
const winston_1 = __importDefault(require("winston"));
|
|
8
8
|
const winston_console_format_1 = require("winston-console-format");
|
|
9
|
-
const
|
|
10
|
-
exports.
|
|
11
|
-
exports.
|
|
12
|
-
exports.logConfigSchema = zod_1.z.object({
|
|
13
|
-
colorize: zod_1.z.boolean(),
|
|
14
|
-
enabled: zod_1.z.boolean(),
|
|
15
|
-
format: exports.logFormatSchema,
|
|
16
|
-
minLevel: exports.logLevelSchema,
|
|
17
|
-
});
|
|
9
|
+
const async_storage_1 = require("./async-storage");
|
|
10
|
+
exports.logFormatOptions = ['json', 'pretty'];
|
|
11
|
+
exports.logLevelOptions = ['debug', 'info', 'warn', 'error'];
|
|
18
12
|
const createConsoleTransport = (config) => {
|
|
19
13
|
const { colorize, enabled, format } = config;
|
|
20
14
|
if (!enabled) {
|
|
@@ -57,29 +51,78 @@ const createBaseLogger = (config) => {
|
|
|
57
51
|
transports: [createConsoleTransport(config)],
|
|
58
52
|
});
|
|
59
53
|
};
|
|
54
|
+
exports.createBaseLogger = createBaseLogger;
|
|
60
55
|
// Winston by default merges content of `context` among the rest of the fields for the JSON format.
|
|
61
56
|
// That's causing an override of fields `name` and `message` if they are present.
|
|
62
57
|
const wrapper = (logger) => {
|
|
63
58
|
return {
|
|
64
|
-
debug: (message,
|
|
65
|
-
|
|
66
|
-
|
|
59
|
+
debug: (message, localContext) => {
|
|
60
|
+
const globalContext = (0, async_storage_1.getAsyncLocalStorage)().getStore();
|
|
61
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
62
|
+
logger.debug(message, fullContext);
|
|
63
|
+
},
|
|
64
|
+
info: (message, localContext) => {
|
|
65
|
+
const globalContext = (0, async_storage_1.getAsyncLocalStorage)().getStore();
|
|
66
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
67
|
+
logger.info(message, fullContext);
|
|
68
|
+
},
|
|
69
|
+
warn: (message, localContext) => {
|
|
70
|
+
const globalContext = (0, async_storage_1.getAsyncLocalStorage)().getStore();
|
|
71
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
72
|
+
logger.warn(message, fullContext);
|
|
73
|
+
},
|
|
67
74
|
// We need to handle both overloads of the `error` function
|
|
68
|
-
error: (message,
|
|
75
|
+
error: (message, errorOrLocalContext, localContext) => {
|
|
76
|
+
const globalContext = (0, async_storage_1.getAsyncLocalStorage)().getStore();
|
|
69
77
|
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
70
|
-
if (
|
|
71
|
-
|
|
78
|
+
if (errorOrLocalContext instanceof Error) {
|
|
79
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
80
|
+
logger.error(message, errorOrLocalContext, fullContext);
|
|
72
81
|
}
|
|
73
82
|
else {
|
|
74
|
-
|
|
83
|
+
const fullContext = globalContext || errorOrLocalContext ? { ...globalContext, ...errorOrLocalContext } : undefined;
|
|
84
|
+
logger.error(message, fullContext);
|
|
75
85
|
}
|
|
76
86
|
},
|
|
77
|
-
child: (options) => wrapper(logger.child(options)),
|
|
87
|
+
child: (options) => (0, exports.wrapper)(logger.child(options)),
|
|
88
|
+
runWithContext: (context, fn) => {
|
|
89
|
+
const asyncStorage = (0, async_storage_1.getAsyncLocalStorage)();
|
|
90
|
+
const oldContext = asyncStorage.getStore() ?? {};
|
|
91
|
+
// From https://nodejs.org/api/async_context.html#asynclocalstoragerunstore-callback-args
|
|
92
|
+
//
|
|
93
|
+
// If the callback function throws an error, the error is thrown by run() too. The stacktrace is not impacted by
|
|
94
|
+
// this call and the context is exited.
|
|
95
|
+
return asyncStorage.run({ ...oldContext, ...context }, fn);
|
|
96
|
+
},
|
|
78
97
|
};
|
|
79
98
|
};
|
|
99
|
+
exports.wrapper = wrapper;
|
|
100
|
+
const validateLogConfig = (config) => {
|
|
101
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
102
|
+
if (typeof config !== 'object' || config === null) {
|
|
103
|
+
throw new Error('Invalid logger configuration');
|
|
104
|
+
}
|
|
105
|
+
const { colorize, enabled, format, minLevel } = config;
|
|
106
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
107
|
+
if (typeof colorize !== 'boolean') {
|
|
108
|
+
throw new TypeError('Invalid logger configuration: colorize must be a boolean');
|
|
109
|
+
}
|
|
110
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
111
|
+
if (typeof enabled !== 'boolean') {
|
|
112
|
+
throw new TypeError('Invalid logger configuration: enabled must be a boolean');
|
|
113
|
+
}
|
|
114
|
+
if (!exports.logFormatOptions.includes(format)) {
|
|
115
|
+
throw new TypeError('Invalid logger configuration: format must be one of "json" or "pretty"');
|
|
116
|
+
}
|
|
117
|
+
if (!exports.logLevelOptions.includes(minLevel)) {
|
|
118
|
+
throw new TypeError('Invalid logger configuration: minLevel must be one of "debug", "info", "warn" or "error"');
|
|
119
|
+
}
|
|
120
|
+
return config;
|
|
121
|
+
};
|
|
122
|
+
exports.validateLogConfig = validateLogConfig;
|
|
80
123
|
const createLogger = (config) => {
|
|
81
|
-
// Ensure that the configuration is valid
|
|
82
|
-
return wrapper(createBaseLogger(exports.
|
|
124
|
+
// Ensure that the logger configuration is valid.
|
|
125
|
+
return (0, exports.wrapper)((0, exports.createBaseLogger)((0, exports.validateLogConfig)(config)));
|
|
83
126
|
};
|
|
84
127
|
exports.createLogger = createLogger;
|
|
85
128
|
//# sourceMappingURL=index.js.map
|
package/dist/logger/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA8B;AAC9B,mEAAuD;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA8B;AAC9B,mEAAuD;AAEvD,mDAAuD;AAE1C,QAAA,gBAAgB,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAU,CAAC;AAI/C,QAAA,eAAe,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAU,CAAC;AAW3E,MAAM,sBAAsB,GAAG,CAAC,MAAiB,EAAE,EAAE;IACnD,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IAE7C,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,IAAI,iBAAO,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;KACzD;IAED,QAAQ,MAAM,EAAE;QACd,KAAK,MAAM,CAAC,CAAC;YACX,OAAO,IAAI,iBAAO,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,iBAAO,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;SAC1E;QACD,KAAK,QAAQ,CAAC,CAAC;YACb,MAAM,OAAO,GAAG;gBACd,QAAQ,CAAC,CAAC,CAAC,iBAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,IAA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,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,MAA4B,CAAC;IAC7E,0DAA0D;IAC1D,IAAI,OAAO,QAAQ,KAAK,SAAS,EAAE;QACjC,MAAM,IAAI,SAAS,CAAC,0DAA0D,CAAC,CAAC;KACjF;IACD,0DAA0D;IAC1D,IAAI,OAAO,OAAO,KAAK,SAAS,EAAE;QAChC,MAAM,IAAI,SAAS,CAAC,yDAAyD,CAAC,CAAC;KAChF;IACD,IAAI,CAAC,wBAAgB,CAAC,QAAQ,CAAC,MAAa,CAAC,EAAE;QAC7C,MAAM,IAAI,SAAS,CAAC,wEAAwE,CAAC,CAAC;KAC/F;IACD,IAAI,CAAC,uBAAe,CAAC,QAAQ,CAAC,QAAe,CAAC,EAAE;QAC9C,MAAM,IAAI,SAAS,CAAC,0FAA0F,CAAC,CAAC;KACjH;IAED,OAAO,MAAmB,CAAC;AAC7B,CAAC,CAAC;AAvBW,QAAA,iBAAiB,qBAuB5B;AAEK,MAAM,YAAY,GAAG,CAAC,MAAiB,EAAE,EAAE;IAChD,iDAAiD;IACjD,OAAO,IAAA,eAAO,EAAC,IAAA,wBAAgB,EAAC,IAAA,yBAAiB,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAC9D,CAAC,CAAC;AAHW,QAAA,YAAY,gBAGvB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processing.d.ts","sourceRoot":"","sources":["../../src/processing/processing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAuB,MAAM,WAAW,CAAC;AAC/D,OAAO,EAAE,KAAK,cAAc,EAAM,MAAM,qBAAqB,CAAC;AAE9D,OAAO,EAAE,KAAK,iBAAiB,
|
|
1
|
+
{"version":3,"file":"processing.d.ts","sourceRoot":"","sources":["../../src/processing/processing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAuB,MAAM,WAAW,CAAC;AAC/D,OAAO,EAAE,KAAK,cAAc,EAAM,MAAM,qBAAqB,CAAC;AAE9D,OAAO,EAAE,KAAK,iBAAiB,EAA6B,MAAM,UAAU,CAAC;AAG7E,eAAO,MAAM,6BAA6B,QAAS,CAAC;AAIpD;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,eAAgB,iBAAiB,KAAG,iBAUxE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,sBACb,iBAAiB,sBAChB,iBAAiB,KACpC,iBAQF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,2BAA2B,aAC5B,QAAQ,qBACC,iBAAiB,sBACjB,cAAc,KAChC,QAAQ,iBAAiB,CA4C3B,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,0BAA0B,oBACpB,OAAO,YACd,QAAQ,qBACC,iBAAiB,sBACjB,cAAc,qBAwClC,CAAC"}
|
|
@@ -74,7 +74,7 @@ const preProcessApiCallParameters = async (endpoint, apiCallParameters, processi
|
|
|
74
74
|
if (!goProcessedParameters.success)
|
|
75
75
|
throw goProcessedParameters.error;
|
|
76
76
|
// Let this throw if the processed parameters are invalid.
|
|
77
|
-
const parsedParameters = schema_1.
|
|
77
|
+
const parsedParameters = (0, schema_1.validateApiCallParameters)(goProcessedParameters.data);
|
|
78
78
|
// Having removed reserved parameters for pre-processing, we need to re-insert them for the API call.
|
|
79
79
|
return (0, exports.addReservedParameters)(apiCallParameters, parsedParameters);
|
|
80
80
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processing.js","sourceRoot":"","sources":["../../src/processing/processing.ts"],"names":[],"mappings":";;;AAAA,mCAA+D;AAC/D,uDAA8D;AAE9D,
|
|
1
|
+
{"version":3,"file":"processing.js","sourceRoot":"","sources":["../../src/processing/processing.ts"],"names":[],"mappings":";;;AAAA,mCAA+D;AAC/D,uDAA8D;AAE9D,qCAA6E;AAC7E,uDAAwE;AAE3D,QAAA,6BAA6B,GAAG,MAAM,CAAC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AAM,KAAK,CAAC,EAAE;QAC/E,OAAO,eAAe,CAAC;KACxB;IAED,mFAAmF;IACnF,MAAM,QAAQ,GAAG,MAAM,IAAA,kBAAE,EAAC,KAAK,IAAI,EAAE;QACnC,IAAI,YAAY,GAAY,eAAe,CAAC;QAE5C,KAAK,MAAM,UAAU,IAAI,4BAA4B,EAAE;YACrD,2GAA2G;YAC3G,+EAA+E;YAC/E,MAAM,kBAAkB,GAAG,IAAA,gCAAwB,EAAC,iBAAiB,CAAC,CAAC;YACvE,QAAQ,UAAU,CAAC,WAAW,EAAE;gBAC9B,KAAK,MAAM,CAAC,CAAC;oBACX,YAAY,GAAG,MAAM,IAAA,gCAAc,EACjC,UAAU,CAAC,KAAK,EAChB,EAAE,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,EAC3C,UAAU,CAAC,SAAS,CACrB,CAAC;oBACF,MAAM;iBACP;gBACD,KAAK,YAAY,CAAC,CAAC;oBACjB,YAAY,GAAG,MAAM,IAAA,qCAAmB,EACtC,UAAU,CAAC,KAAK,EAChB,EAAE,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,EAC3C,UAAU,CAAC,SAAS,CACrB,CAAC;oBACF,MAAM;iBACP;aACF;SACF;QAED,OAAO,YAAY,CAAC;IACtB,CAAC,EAAE,iBAAiB,CAAC,CAAC;IACtB,IAAI,CAAC,QAAQ,CAAC,OAAO;QAAE,MAAM,QAAQ,CAAC,KAAK,CAAC;IAE5C,OAAO,QAAQ,CAAC,IAAI,CAAC;AACvB,CAAC,CAAC;AA5CW,QAAA,0BAA0B,8BA4CrC"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export type ApiCallParameters = z.infer<typeof apiCallParametersSchema>;
|
|
1
|
+
export declare const validateApiCallParameters: (apiCallParameters: unknown) => ApiCallParameters;
|
|
2
|
+
export type ApiCallParameters = Record<string, any>;
|
|
4
3
|
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/processing/schema.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/processing/schema.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,yBAAyB,sBAAuB,OAAO,KAAG,iBAOtE,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC"}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
|
|
3
|
+
exports.validateApiCallParameters = void 0;
|
|
4
|
+
const validateApiCallParameters = (apiCallParameters) => {
|
|
5
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
6
|
+
if (typeof apiCallParameters !== 'object' || apiCallParameters === null) {
|
|
7
|
+
throw new TypeError('Invalid API call parameters');
|
|
8
|
+
}
|
|
9
|
+
return apiCallParameters;
|
|
10
|
+
};
|
|
11
|
+
exports.validateApiCallParameters = validateApiCallParameters;
|
|
6
12
|
//# sourceMappingURL=schema.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/processing/schema.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/processing/schema.ts"],"names":[],"mappings":";;;AAAO,MAAM,yBAAyB,GAAG,CAAC,iBAA0B,EAAqB,EAAE;IACzF,0DAA0D;IAC1D,IAAI,OAAO,iBAAiB,KAAK,QAAQ,IAAI,iBAAiB,KAAK,IAAI,EAAE;QACvE,MAAM,IAAI,SAAS,CAAC,6BAA6B,CAAC,CAAC;KACpD;IAED,OAAO,iBAAsC,CAAC;AAChD,CAAC,CAAC;AAPW,QAAA,yBAAyB,6BAOpC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@api3/commons",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"engines": {
|
|
@@ -11,11 +11,8 @@
|
|
|
11
11
|
"dist",
|
|
12
12
|
"src"
|
|
13
13
|
],
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
"./logger": "./dist/logger/index.js",
|
|
17
|
-
"./processing": "./dist/processing/index.js"
|
|
18
|
-
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"exports": "./dist/index.js",
|
|
19
16
|
"dependencies": {
|
|
20
17
|
"@api3/ois": "^2.2.1",
|
|
21
18
|
"@api3/promise-utils": "^0.4.0",
|
|
@@ -51,12 +48,10 @@
|
|
|
51
48
|
"rimraf": "^5.0.5",
|
|
52
49
|
"ts-jest": "^29.1.1",
|
|
53
50
|
"ts-node": "^10.9.1",
|
|
54
|
-
"typescript": "^5.2.2"
|
|
55
|
-
"zod": "^3.22.2"
|
|
51
|
+
"typescript": "^5.2.2"
|
|
56
52
|
},
|
|
57
53
|
"peerDependencies": {
|
|
58
|
-
"eslint": "^8.50.0"
|
|
59
|
-
"zod": "^3.22.2"
|
|
54
|
+
"eslint": "^8.50.0"
|
|
60
55
|
},
|
|
61
56
|
"scripts": {
|
|
62
57
|
"build": "tsc --project tsconfig.build.json",
|
package/src/index.ts
ADDED
package/src/logger/README.md
CHANGED
|
@@ -6,8 +6,7 @@ Backend-only logger for Node.js packages based on Winston logger.
|
|
|
6
6
|
|
|
7
7
|
## Getting started
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
2. Use `createLogger` function to create a logger instance.
|
|
9
|
+
Import `createLogger` function to create a logger instance.
|
|
11
10
|
|
|
12
11
|
## Configuration
|
|
13
12
|
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
|
|
3
|
+
import type { LogContext } from '.';
|
|
4
|
+
|
|
5
|
+
let asyncLocalStorage: AsyncLocalStorage<LogContext>;
|
|
6
|
+
|
|
7
|
+
export const getAsyncLocalStorage = () => {
|
|
8
|
+
if (!asyncLocalStorage) asyncLocalStorage = new AsyncLocalStorage<LogContext>();
|
|
9
|
+
return asyncLocalStorage;
|
|
10
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { noop } from 'lodash';
|
|
2
|
+
|
|
3
|
+
import { type LogConfig, createBaseLogger, wrapper } from '.';
|
|
4
|
+
|
|
5
|
+
const createTestLogger = (
|
|
6
|
+
logConfig: LogConfig = { enabled: true, minLevel: 'debug', format: 'json', colorize: false }
|
|
7
|
+
) => {
|
|
8
|
+
const baseLogger = createBaseLogger(logConfig);
|
|
9
|
+
const logger = wrapper(baseLogger);
|
|
10
|
+
jest.spyOn(baseLogger, 'debug').mockImplementation(noop as any);
|
|
11
|
+
jest.spyOn(baseLogger, 'info').mockImplementation(noop as any);
|
|
12
|
+
jest.spyOn(baseLogger, 'warn').mockImplementation(noop as any);
|
|
13
|
+
jest.spyOn(baseLogger, 'error').mockImplementation(noop as any);
|
|
14
|
+
jest.spyOn(baseLogger, 'child').mockImplementation(noop as any);
|
|
15
|
+
|
|
16
|
+
return { baseLogger, logger };
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
describe('log context', () => {
|
|
20
|
+
it('works with sync functions', () => {
|
|
21
|
+
const { baseLogger, logger } = createTestLogger();
|
|
22
|
+
|
|
23
|
+
logger.runWithContext({ requestId: 'parent' }, () => {
|
|
24
|
+
logger.debug('parent start');
|
|
25
|
+
logger.runWithContext({ requestId: 'child' }, () => {
|
|
26
|
+
logger.debug('child');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
logger.debug('parent end');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent start', { requestId: 'parent' });
|
|
33
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('child', { requestId: 'child' });
|
|
34
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent end', { requestId: 'parent' });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('works with async functions', async () => {
|
|
38
|
+
const { baseLogger, logger } = createTestLogger();
|
|
39
|
+
|
|
40
|
+
await logger.runWithContext({ requestId: 'parent' }, async () => {
|
|
41
|
+
logger.debug('parent start');
|
|
42
|
+
await logger.runWithContext({ requestId: 'child' }, async () => {
|
|
43
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
44
|
+
logger.debug('child');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
logger.debug('parent end');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
expect(baseLogger.debug).toHaveBeenCalledTimes(3);
|
|
51
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent start', { requestId: 'parent' });
|
|
52
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('child', { requestId: 'child' });
|
|
53
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent end', { requestId: 'parent' });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('works with deeply nested functions', async () => {
|
|
57
|
+
const { baseLogger, logger } = createTestLogger();
|
|
58
|
+
|
|
59
|
+
await logger.runWithContext({ parent: true }, async () => {
|
|
60
|
+
logger.debug('parent start');
|
|
61
|
+
|
|
62
|
+
await logger.runWithContext({ A: true }, async () => {
|
|
63
|
+
logger.debug('A start');
|
|
64
|
+
|
|
65
|
+
await logger.runWithContext({ B: true }, async () => {
|
|
66
|
+
setTimeout(() => logger.debug('C'), 25);
|
|
67
|
+
setTimeout(() => logger.debug('D'), 50);
|
|
68
|
+
setTimeout(() => logger.debug('E'), 75);
|
|
69
|
+
|
|
70
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
71
|
+
logger.debug('B end');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
logger.debug('A end');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
logger.debug('parent end');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
expect(baseLogger.debug).toHaveBeenCalledTimes(8);
|
|
81
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent start', { parent: true });
|
|
82
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('A start', { parent: true, A: true });
|
|
83
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('C', { parent: true, A: true, B: true });
|
|
84
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('D', { parent: true, A: true, B: true });
|
|
85
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('E', { parent: true, A: true, B: true });
|
|
86
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('B end', { parent: true, A: true, B: true });
|
|
87
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('A end', { parent: true, A: true });
|
|
88
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent end', { parent: true });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('throws if the sync callback function throws', () => {
|
|
92
|
+
const { logger } = createTestLogger();
|
|
93
|
+
|
|
94
|
+
expect(() =>
|
|
95
|
+
logger.runWithContext({}, () => {
|
|
96
|
+
throw new Error('some-error');
|
|
97
|
+
})
|
|
98
|
+
).toThrow('some-error');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('returns rejected promise if the async callback function rejects', async () => {
|
|
102
|
+
const { logger } = createTestLogger();
|
|
103
|
+
|
|
104
|
+
await expect(async () =>
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
106
|
+
logger.runWithContext({}, async () => {
|
|
107
|
+
throw new Error('some-error');
|
|
108
|
+
})
|
|
109
|
+
).rejects.toThrow('some-error');
|
|
110
|
+
});
|
|
111
|
+
});
|
package/src/logger/index.ts
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
import winston from 'winston';
|
|
2
2
|
import { consoleFormat } from 'winston-console-format';
|
|
3
|
-
import { z } from 'zod';
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
import { getAsyncLocalStorage } from './async-storage';
|
|
6
5
|
|
|
7
|
-
export
|
|
6
|
+
export const logFormatOptions = ['json', 'pretty'] as const;
|
|
8
7
|
|
|
9
|
-
export
|
|
8
|
+
export type LogFormat = (typeof logFormatOptions)[number];
|
|
10
9
|
|
|
11
|
-
export
|
|
10
|
+
export const logLevelOptions = ['debug', 'info', 'warn', 'error'] as const;
|
|
12
11
|
|
|
13
|
-
export
|
|
14
|
-
colorize: z.boolean(),
|
|
15
|
-
enabled: z.boolean(),
|
|
16
|
-
format: logFormatSchema,
|
|
17
|
-
minLevel: logLevelSchema,
|
|
18
|
-
});
|
|
12
|
+
export type LogLevel = (typeof logLevelOptions)[number];
|
|
19
13
|
|
|
20
|
-
export
|
|
14
|
+
export interface LogConfig {
|
|
15
|
+
colorize: boolean;
|
|
16
|
+
enabled: boolean;
|
|
17
|
+
format: LogFormat;
|
|
18
|
+
minLevel: LogLevel;
|
|
19
|
+
}
|
|
21
20
|
|
|
22
21
|
const createConsoleTransport = (config: LogConfig) => {
|
|
23
22
|
const { colorize, enabled, format } = config;
|
|
@@ -54,7 +53,7 @@ const createConsoleTransport = (config: LogConfig) => {
|
|
|
54
53
|
}
|
|
55
54
|
};
|
|
56
55
|
|
|
57
|
-
const createBaseLogger = (config: LogConfig) => {
|
|
56
|
+
export const createBaseLogger = (config: LogConfig) => {
|
|
58
57
|
const { enabled, minLevel } = config;
|
|
59
58
|
|
|
60
59
|
return winston.createLogger({
|
|
@@ -76,35 +75,87 @@ const createBaseLogger = (config: LogConfig) => {
|
|
|
76
75
|
export type LogContext = Record<string, any>;
|
|
77
76
|
|
|
78
77
|
export interface Logger {
|
|
78
|
+
runWithContext: <T>(context: LogContext, fn: () => T) => T;
|
|
79
79
|
debug: (message: string, context?: LogContext) => void;
|
|
80
80
|
info: (message: string, context?: LogContext) => void;
|
|
81
81
|
warn: (message: string, context?: LogContext) => void;
|
|
82
|
-
error:
|
|
83
|
-
((message: string,
|
|
82
|
+
error:
|
|
83
|
+
| ((message: string, context?: LogContext) => void)
|
|
84
|
+
| ((message: string, error: Error, context?: LogContext) => void);
|
|
84
85
|
child: (options: { name: string }) => Logger;
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
// Winston by default merges content of `context` among the rest of the fields for the JSON format.
|
|
88
89
|
// That's causing an override of fields `name` and `message` if they are present.
|
|
89
|
-
const wrapper = (logger: Logger): Logger => {
|
|
90
|
+
export const wrapper = (logger: winston.Logger): Logger => {
|
|
90
91
|
return {
|
|
91
|
-
debug: (message,
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
debug: (message, localContext) => {
|
|
93
|
+
const globalContext = getAsyncLocalStorage().getStore();
|
|
94
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
95
|
+
logger.debug(message, fullContext);
|
|
96
|
+
},
|
|
97
|
+
info: (message, localContext) => {
|
|
98
|
+
const globalContext = getAsyncLocalStorage().getStore();
|
|
99
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
100
|
+
logger.info(message, fullContext);
|
|
101
|
+
},
|
|
102
|
+
warn: (message, localContext) => {
|
|
103
|
+
const globalContext = getAsyncLocalStorage().getStore();
|
|
104
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
105
|
+
logger.warn(message, fullContext);
|
|
106
|
+
},
|
|
94
107
|
// We need to handle both overloads of the `error` function
|
|
95
|
-
error: (message,
|
|
108
|
+
error: (message, errorOrLocalContext, localContext) => {
|
|
109
|
+
const globalContext = getAsyncLocalStorage().getStore();
|
|
96
110
|
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
97
|
-
if (
|
|
98
|
-
|
|
111
|
+
if (errorOrLocalContext instanceof Error) {
|
|
112
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
113
|
+
logger.error(message, errorOrLocalContext, fullContext);
|
|
99
114
|
} else {
|
|
100
|
-
|
|
115
|
+
const fullContext =
|
|
116
|
+
globalContext || errorOrLocalContext ? { ...globalContext, ...(errorOrLocalContext as any) } : undefined;
|
|
117
|
+
logger.error(message, fullContext);
|
|
101
118
|
}
|
|
102
119
|
},
|
|
103
120
|
child: (options) => wrapper(logger.child(options)),
|
|
121
|
+
runWithContext: (context, fn) => {
|
|
122
|
+
const asyncStorage = getAsyncLocalStorage();
|
|
123
|
+
const oldContext = asyncStorage.getStore() ?? {};
|
|
124
|
+
// From https://nodejs.org/api/async_context.html#asynclocalstoragerunstore-callback-args
|
|
125
|
+
//
|
|
126
|
+
// If the callback function throws an error, the error is thrown by run() too. The stacktrace is not impacted by
|
|
127
|
+
// this call and the context is exited.
|
|
128
|
+
return asyncStorage.run({ ...oldContext, ...context }, fn);
|
|
129
|
+
},
|
|
104
130
|
} as Logger;
|
|
105
131
|
};
|
|
106
132
|
|
|
133
|
+
export const validateLogConfig = (config: unknown): LogConfig => {
|
|
134
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
135
|
+
if (typeof config !== 'object' || config === null) {
|
|
136
|
+
throw new Error('Invalid logger configuration');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const { colorize, enabled, format, minLevel } = config as Partial<LogConfig>;
|
|
140
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
141
|
+
if (typeof colorize !== 'boolean') {
|
|
142
|
+
throw new TypeError('Invalid logger configuration: colorize must be a boolean');
|
|
143
|
+
}
|
|
144
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
145
|
+
if (typeof enabled !== 'boolean') {
|
|
146
|
+
throw new TypeError('Invalid logger configuration: enabled must be a boolean');
|
|
147
|
+
}
|
|
148
|
+
if (!logFormatOptions.includes(format as any)) {
|
|
149
|
+
throw new TypeError('Invalid logger configuration: format must be one of "json" or "pretty"');
|
|
150
|
+
}
|
|
151
|
+
if (!logLevelOptions.includes(minLevel as any)) {
|
|
152
|
+
throw new TypeError('Invalid logger configuration: minLevel must be one of "debug", "info", "warn" or "error"');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return config as LogConfig;
|
|
156
|
+
};
|
|
157
|
+
|
|
107
158
|
export const createLogger = (config: LogConfig) => {
|
|
108
|
-
// Ensure that the configuration is valid
|
|
109
|
-
return wrapper(createBaseLogger(
|
|
159
|
+
// Ensure that the logger configuration is valid.
|
|
160
|
+
return wrapper(createBaseLogger(validateLogConfig(config)));
|
|
110
161
|
};
|
package/src/processing/README.md
CHANGED
|
@@ -4,10 +4,6 @@
|
|
|
4
4
|
|
|
5
5
|
The pre/post processing is only supported for Node.js environments and uses internal Node.js modules.
|
|
6
6
|
|
|
7
|
-
## Getting started
|
|
8
|
-
|
|
9
|
-
1. Install `zod` which is a peer dependency of this module. Zod is used for validating the logger configuration.
|
|
10
|
-
|
|
11
7
|
## Documentation
|
|
12
8
|
|
|
13
9
|
The processing module exports two main functions:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Endpoint, RESERVED_PARAMETERS } from '@api3/ois';
|
|
2
2
|
import { type GoAsyncOptions, go } from '@api3/promise-utils';
|
|
3
3
|
|
|
4
|
-
import { type ApiCallParameters,
|
|
4
|
+
import { type ApiCallParameters, validateApiCallParameters } from './schema';
|
|
5
5
|
import { unsafeEvaluate, unsafeEvaluateAsync } from './unsafe-evaluate';
|
|
6
6
|
|
|
7
7
|
export const DEFAULT_PROCESSING_TIMEOUT_MS = 10_000;
|
|
@@ -97,7 +97,7 @@ export const preProcessApiCallParameters = async (
|
|
|
97
97
|
if (!goProcessedParameters.success) throw goProcessedParameters.error;
|
|
98
98
|
|
|
99
99
|
// Let this throw if the processed parameters are invalid.
|
|
100
|
-
const parsedParameters =
|
|
100
|
+
const parsedParameters = validateApiCallParameters(goProcessedParameters.data);
|
|
101
101
|
|
|
102
102
|
// Having removed reserved parameters for pre-processing, we need to re-insert them for the API call.
|
|
103
103
|
return addReservedParameters(apiCallParameters, parsedParameters);
|
package/src/processing/schema.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
export const validateApiCallParameters = (apiCallParameters: unknown): ApiCallParameters => {
|
|
2
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
3
|
+
if (typeof apiCallParameters !== 'object' || apiCallParameters === null) {
|
|
4
|
+
throw new TypeError('Invalid API call parameters');
|
|
5
|
+
}
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
return apiCallParameters as ApiCallParameters;
|
|
8
|
+
};
|
|
4
9
|
|
|
5
|
-
export type ApiCallParameters =
|
|
10
|
+
export type ApiCallParameters = Record<string, any>;
|