@api3/commons 0.2.0 → 0.3.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/index.d.ts +9 -20
- package/dist/logger/index.d.ts.map +1 -1
- package/dist/logger/index.js +28 -12
- 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/index.ts +37 -15
- 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"}
|
package/dist/logger/index.d.ts
CHANGED
|
@@ -1,25 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export declare const logConfigSchema: z.ZodObject<{
|
|
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
|
+
export declare const logFormatOptions: readonly ["json", "pretty"];
|
|
2
|
+
export type LogFormat = (typeof logFormatOptions)[number];
|
|
3
|
+
export declare const logLevelOptions: readonly ["debug", "info", "warn", "error"];
|
|
4
|
+
export type LogLevel = (typeof logLevelOptions)[number];
|
|
5
|
+
export interface LogConfig {
|
|
12
6
|
colorize: boolean;
|
|
13
7
|
enabled: boolean;
|
|
14
|
-
format:
|
|
15
|
-
minLevel:
|
|
16
|
-
}
|
|
17
|
-
colorize: boolean;
|
|
18
|
-
enabled: boolean;
|
|
19
|
-
format: "json" | "pretty";
|
|
20
|
-
minLevel: "error" | "info" | "debug" | "warn";
|
|
21
|
-
}>;
|
|
22
|
-
export type LogConfig = z.infer<typeof logConfigSchema>;
|
|
8
|
+
format: LogFormat;
|
|
9
|
+
minLevel: LogLevel;
|
|
10
|
+
}
|
|
23
11
|
export type LogContext = Record<string, any>;
|
|
24
12
|
export interface Logger {
|
|
25
13
|
debug: (message: string, context?: LogContext) => void;
|
|
@@ -30,5 +18,6 @@ export interface Logger {
|
|
|
30
18
|
name: string;
|
|
31
19
|
}) => Logger;
|
|
32
20
|
}
|
|
21
|
+
export declare const validateLogConfig: (config: unknown) => LogConfig;
|
|
33
22
|
export declare const createLogger: (config: LogConfig) => Logger;
|
|
34
23
|
//# 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":["../../src/logger/index.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,gBAAgB,6BAA8B,CAAC;AAE5D,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE1D,eAAO,MAAM,eAAe,6CAA8C,CAAC;AAE3E,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,CAAC,CAAC;AAExD,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,SAAS,CAAC;IAClB,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAwDD,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAE7C,MAAM,WAAW,MAAM;IACrB,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,KAAK,IAAI,CAAC;IACvD,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,KAAK,IAAI,CAAC;IACtD,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,KAAK,IAAI,CAAC;IACtD,KAAK,EAAE,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,KAAK,IAAI,CAAC,GACtD,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,UAAU,KAAK,IAAI,CAAC,CAAC;IAClE,KAAK,EAAE,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,KAAK,MAAM,CAAC;CAC9C;AAsBD,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,11 @@ 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.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
|
-
|
|
10
|
-
exports.
|
|
11
|
-
exports.logLevelSchema = zod_1.z.union([zod_1.z.literal('debug'), zod_1.z.literal('info'), zod_1.z.literal('warn'), zod_1.z.literal('error')]);
|
|
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
|
+
exports.logFormatOptions = ['json', 'pretty'];
|
|
10
|
+
exports.logLevelOptions = ['debug', 'info', 'warn', 'error'];
|
|
18
11
|
const createConsoleTransport = (config) => {
|
|
19
12
|
const { colorize, enabled, format } = config;
|
|
20
13
|
if (!enabled) {
|
|
@@ -77,9 +70,32 @@ const wrapper = (logger) => {
|
|
|
77
70
|
child: (options) => wrapper(logger.child(options)),
|
|
78
71
|
};
|
|
79
72
|
};
|
|
73
|
+
const validateLogConfig = (config) => {
|
|
74
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
75
|
+
if (typeof config !== 'object' || config === null) {
|
|
76
|
+
throw new Error('Invalid logger configuration');
|
|
77
|
+
}
|
|
78
|
+
const { colorize, enabled, format, minLevel } = config;
|
|
79
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
80
|
+
if (typeof colorize !== 'boolean') {
|
|
81
|
+
throw new TypeError('Invalid logger configuration: colorize must be a boolean');
|
|
82
|
+
}
|
|
83
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
84
|
+
if (typeof enabled !== 'boolean') {
|
|
85
|
+
throw new TypeError('Invalid logger configuration: enabled must be a boolean');
|
|
86
|
+
}
|
|
87
|
+
if (!exports.logFormatOptions.includes(format)) {
|
|
88
|
+
throw new TypeError('Invalid logger configuration: format must be one of "json" or "pretty"');
|
|
89
|
+
}
|
|
90
|
+
if (!exports.logLevelOptions.includes(minLevel)) {
|
|
91
|
+
throw new TypeError('Invalid logger configuration: minLevel must be one of "debug", "info", "warn" or "error"');
|
|
92
|
+
}
|
|
93
|
+
return config;
|
|
94
|
+
};
|
|
95
|
+
exports.validateLogConfig = validateLogConfig;
|
|
80
96
|
const createLogger = (config) => {
|
|
81
|
-
// Ensure that the configuration is valid
|
|
82
|
-
return wrapper(createBaseLogger(exports.
|
|
97
|
+
// Ensure that the logger configuration is valid.
|
|
98
|
+
return wrapper(createBaseLogger((0, exports.validateLogConfig)(config)));
|
|
83
99
|
};
|
|
84
100
|
exports.createLogger = createLogger;
|
|
85
101
|
//# 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;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,C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.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;AAEpD,MAAM,kBAAkB,GAAG,yBAA+B,CAAC,CAAC,6BAA6B;AAEzF;;;;GAIG;AACI,MAAM,wBAAwB,GAAG,CAAC,UAA6B,EAAqB,EAAE;IAC3F,MAAM,MAAM,GAAsB,EAAE,CAAC;IAErC,KAAK,MAAM,GAAG,IAAI,UAAU,EAAE;QAC5B,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;YACrC,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;SAC/B;KACF;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAVW,QAAA,wBAAwB,4BAUnC;AAEF;;;;;GAKG;AACI,MAAM,qBAAqB,GAAG,CACnC,iBAAoC,EACpC,kBAAqC,EAClB,EAAE;IACrB,KAAK,MAAM,GAAG,IAAI,iBAAiB,EAAE;QACnC,IAAI,kBAAkB,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;YACpC,kBAAkB,CAAC,GAAG,CAAC,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC;SAClD;KACF;IAED,OAAO,kBAAkB,CAAC;AAC5B,CAAC,CAAC;AAXW,QAAA,qBAAqB,yBAWhC;AAEF;;;;;;;;GAQG;AACI,MAAM,2BAA2B,GAAG,KAAK,EAC9C,QAAkB,EAClB,iBAAoC,EACpC,oBAAoC,EAAE,OAAO,EAAE,CAAC,EAAE,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,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.3.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
|
|
package/src/logger/index.ts
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
import winston from 'winston';
|
|
2
2
|
import { consoleFormat } from 'winston-console-format';
|
|
3
|
-
import { z } from 'zod';
|
|
4
3
|
|
|
5
|
-
export const
|
|
4
|
+
export const logFormatOptions = ['json', 'pretty'] as const;
|
|
6
5
|
|
|
7
|
-
export type
|
|
6
|
+
export type LogFormat = (typeof logFormatOptions)[number];
|
|
8
7
|
|
|
9
|
-
export const
|
|
8
|
+
export const logLevelOptions = ['debug', 'info', 'warn', 'error'] as const;
|
|
10
9
|
|
|
11
|
-
export type LogLevel =
|
|
10
|
+
export type LogLevel = (typeof logLevelOptions)[number];
|
|
12
11
|
|
|
13
|
-
export
|
|
14
|
-
colorize:
|
|
15
|
-
enabled:
|
|
16
|
-
format:
|
|
17
|
-
minLevel:
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export type LogConfig = z.infer<typeof logConfigSchema>;
|
|
12
|
+
export interface LogConfig {
|
|
13
|
+
colorize: boolean;
|
|
14
|
+
enabled: boolean;
|
|
15
|
+
format: LogFormat;
|
|
16
|
+
minLevel: LogLevel;
|
|
17
|
+
}
|
|
21
18
|
|
|
22
19
|
const createConsoleTransport = (config: LogConfig) => {
|
|
23
20
|
const { colorize, enabled, format } = config;
|
|
@@ -104,7 +101,32 @@ const wrapper = (logger: Logger): Logger => {
|
|
|
104
101
|
} as Logger;
|
|
105
102
|
};
|
|
106
103
|
|
|
104
|
+
export const validateLogConfig = (config: unknown): LogConfig => {
|
|
105
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
106
|
+
if (typeof config !== 'object' || config === null) {
|
|
107
|
+
throw new Error('Invalid logger configuration');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const { colorize, enabled, format, minLevel } = config as Partial<LogConfig>;
|
|
111
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
112
|
+
if (typeof colorize !== 'boolean') {
|
|
113
|
+
throw new TypeError('Invalid logger configuration: colorize must be a boolean');
|
|
114
|
+
}
|
|
115
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
116
|
+
if (typeof enabled !== 'boolean') {
|
|
117
|
+
throw new TypeError('Invalid logger configuration: enabled must be a boolean');
|
|
118
|
+
}
|
|
119
|
+
if (!logFormatOptions.includes(format as any)) {
|
|
120
|
+
throw new TypeError('Invalid logger configuration: format must be one of "json" or "pretty"');
|
|
121
|
+
}
|
|
122
|
+
if (!logLevelOptions.includes(minLevel as any)) {
|
|
123
|
+
throw new TypeError('Invalid logger configuration: minLevel must be one of "debug", "info", "warn" or "error"');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return config as LogConfig;
|
|
127
|
+
};
|
|
128
|
+
|
|
107
129
|
export const createLogger = (config: LogConfig) => {
|
|
108
|
-
// Ensure that the configuration is valid
|
|
109
|
-
return wrapper(createBaseLogger(
|
|
130
|
+
// Ensure that the logger configuration is valid.
|
|
131
|
+
return wrapper(createBaseLogger(validateLogConfig(config)));
|
|
110
132
|
};
|
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>;
|