@macss/modular-api 0.7.2 → 0.7.3
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
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.7.3](https://github.com/ccisnedev/modular_api/compare/modular_api-v0.7.2...modular_api-v0.7.3) (2026-11-27)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **handler:** answer 400, not 500, to a body that cannot become the input ([#64](https://github.com/ccisnedev/modular_api/issues/64)) ([fc4c1a9](https://github.com/ccisnedev/modular_api/commit/fc4c1a9e5c8f6b732617ffe67b0632b3a6bb5578)), closes [#44](https://github.com/ccisnedev/modular_api/issues/44)
|
|
9
|
+
* **metrics:** serialize the root value of an unlabeled counter ([#61](https://github.com/ccisnedev/modular_api/issues/61)) ([559a39e](https://github.com/ccisnedev/modular_api/commit/559a39e9c24e58a4a1d22b48180c357911f79b21)), closes [#46](https://github.com/ccisnedev/modular_api/issues/46)
|
|
10
|
+
* **openapi:** carry the ModularApi version into info.version in Dart ([#62](https://github.com/ccisnedev/modular_api/issues/62)) ([b73c8e8](https://github.com/ccisnedev/modular_api/commit/b73c8e871eaaf8de1f0cb65a1aae16271901b833)), closes [#38](https://github.com/ccisnedev/modular_api/issues/38)
|
|
11
|
+
* **schema:** fail at class definition when @Field is a legacy decorator ([#65](https://github.com/ccisnedev/modular_api/issues/65)) ([87015b8](https://github.com/ccisnedev/modular_api/commit/87015b851f28cf46607c75c14a64e2be9068c021)), closes [#19](https://github.com/ccisnedev/modular_api/issues/19)
|
|
12
|
+
|
|
3
13
|
## [0.7.2](https://github.com/ccisnedev/modular_api/compare/modular_api-v0.7.1...modular_api-v0.7.2) (2026-11-26)
|
|
4
14
|
|
|
5
15
|
|
|
@@ -83,8 +83,12 @@ class MetricRegistry {
|
|
|
83
83
|
}
|
|
84
84
|
serializeCounter(lines, counter) {
|
|
85
85
|
const samples = counter.collect();
|
|
86
|
-
if (samples.length === 0)
|
|
86
|
+
if (samples.length === 0) {
|
|
87
|
+
// Root counter with no labeled children — emit the root value, like a gauge.
|
|
88
|
+
// Once a labeled child exists, the root value is not serialized.
|
|
89
|
+
lines.push(`${counter.name} ${formatValue(counter.value)}`);
|
|
87
90
|
return;
|
|
91
|
+
}
|
|
88
92
|
for (const s of samples) {
|
|
89
93
|
lines.push(`${counter.name}${formatLabels(s.labels)} ${formatValue(s.value)}`);
|
|
90
94
|
}
|
|
@@ -24,6 +24,7 @@ Symbol.metadata ??= Symbol('Symbol.metadata');
|
|
|
24
24
|
const FIELD_KEY = Symbol('FieldMeta');
|
|
25
25
|
function makeFieldDecorator(type, options = {}, extra = {}) {
|
|
26
26
|
return function (_value, context) {
|
|
27
|
+
assertStage3Context(context);
|
|
27
28
|
const meta = {
|
|
28
29
|
name: String(context.name),
|
|
29
30
|
type,
|
|
@@ -42,6 +43,26 @@ function makeFieldDecorator(type, options = {}, extra = {}) {
|
|
|
42
43
|
metadata[FIELD_KEY].push(meta);
|
|
43
44
|
};
|
|
44
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Fails at class definition when `@Field` is not called as a Stage 3 decorator.
|
|
48
|
+
*
|
|
49
|
+
* With `experimentalDecorators: true` the compiler calls `decorator(prototype, propertyKey)`, so
|
|
50
|
+
* `context` is the property name, and a transformer without Stage 3 metadata passes a context
|
|
51
|
+
* without `metadata`. Either way the field would never be registered and the DTO would break
|
|
52
|
+
* later, far from the cause (`toJson() not implemented`).
|
|
53
|
+
*/
|
|
54
|
+
function assertStage3Context(context) {
|
|
55
|
+
const ctx = context;
|
|
56
|
+
if (typeof ctx === 'object' && ctx !== null && typeof ctx.metadata === 'object' && ctx.metadata) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const property = typeof context === 'string' || typeof context === 'symbol' ? String(context) : ctx?.name;
|
|
60
|
+
throw new TypeError(`@Field${property !== undefined ? ` on '${String(property)}'` : ''} was not called as a ` +
|
|
61
|
+
'TC39 Stage 3 decorator, so its metadata cannot be registered. Remove ' +
|
|
62
|
+
'`experimentalDecorators` from every tsconfig of the project (including the ones it ' +
|
|
63
|
+
'extends) and use a transformer with Stage 3 decorator support (tsc 5, esbuild). ' +
|
|
64
|
+
'See docs/pitfalls.md.');
|
|
65
|
+
}
|
|
45
66
|
// ── Public API ───────────────────────────────────────────────
|
|
46
67
|
exports.Field = {
|
|
47
68
|
string(options = {}) {
|
|
@@ -18,6 +18,8 @@ export interface UseCaseHandlerOptions {
|
|
|
18
18
|
* 7. Return output.toJson() with output.statusCode
|
|
19
19
|
*
|
|
20
20
|
* Errors:
|
|
21
|
+
* - Body that is not a JSON object, or a TypeError/SyntaxError/RangeError thrown by fromJson
|
|
22
|
+
* → 400 (the cause is logged at warning level, not returned)
|
|
21
23
|
* - InputValidationError → 400 with structured error message
|
|
22
24
|
* - UseCaseException → statusCode from exception, structured JSON body
|
|
23
25
|
* - Any other Error → 500 Internal Server Error
|
|
@@ -24,6 +24,8 @@ const JSON_HEADERS = { 'Content-Type': 'application/json; charset=utf-8' };
|
|
|
24
24
|
* 7. Return output.toJson() with output.statusCode
|
|
25
25
|
*
|
|
26
26
|
* Errors:
|
|
27
|
+
* - Body that is not a JSON object, or a TypeError/SyntaxError/RangeError thrown by fromJson
|
|
28
|
+
* → 400 (the cause is logged at warning level, not returned)
|
|
27
29
|
* - InputValidationError → 400 with structured error message
|
|
28
30
|
* - UseCaseException → statusCode from exception, structured JSON body
|
|
29
31
|
* - Any other Error → 500 Internal Server Error
|
|
@@ -37,15 +39,34 @@ function useCaseHandler(factory, options = {}) {
|
|
|
37
39
|
return async (req, res) => {
|
|
38
40
|
try {
|
|
39
41
|
// 1. Extract payload
|
|
40
|
-
const
|
|
42
|
+
const isQuery = req.method.toUpperCase() === 'GET' || req.method.toUpperCase() === 'DELETE';
|
|
43
|
+
const body = isQuery ? undefined : req.body;
|
|
44
|
+
// express.json() accepts objects and arrays; only an object can be the use case input.
|
|
45
|
+
if (!isQuery && (Array.isArray(body) || (body !== undefined && typeof body !== 'object'))) {
|
|
46
|
+
badRequest(res, 'Request body must be a JSON object');
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const data = isQuery
|
|
41
50
|
? { ...req.query, ...req.params }
|
|
42
|
-
: (
|
|
51
|
+
: (body ?? {});
|
|
43
52
|
// 2. Pre-validate BEFORE fromJson (when inputClass provided)
|
|
44
53
|
if (options.inputClass) {
|
|
45
54
|
usecase_1.Input.validateJson(data, options.inputClass);
|
|
46
55
|
}
|
|
47
|
-
// 3. Build use case
|
|
48
|
-
|
|
56
|
+
// 3. Build use case. fromJson only maps untrusted input into the use case input, so a
|
|
57
|
+
// TypeError, SyntaxError or RangeError thrown there means the body has the wrong shape:
|
|
58
|
+
// a client error. UseCaseException and InputValidationError keep their own handling.
|
|
59
|
+
let useCase;
|
|
60
|
+
try {
|
|
61
|
+
useCase = factory(data);
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
if (err instanceof TypeError || err instanceof SyntaxError || err instanceof RangeError) {
|
|
65
|
+
badRequest(res, 'Invalid request body', err);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
49
70
|
// 3a. Post-validate for legacy path (no inputClass)
|
|
50
71
|
if (!options.inputClass) {
|
|
51
72
|
usecase_1.Input.validateJson(data, useCase.input.constructor);
|
|
@@ -82,3 +103,12 @@ function useCaseHandler(factory, options = {}) {
|
|
|
82
103
|
}
|
|
83
104
|
};
|
|
84
105
|
}
|
|
106
|
+
/** A 400 for a body that cannot become the use case input; the cause is logged, never returned. */
|
|
107
|
+
function badRequest(res, message, cause) {
|
|
108
|
+
const logger = res.locals[logging_middleware_1.LOGGER_LOCALS_KEY];
|
|
109
|
+
logger?.warning(message, {
|
|
110
|
+
status: 400,
|
|
111
|
+
...(cause ? { error: `${cause.name}: ${cause.message}` } : {}),
|
|
112
|
+
});
|
|
113
|
+
res.status(400).set(JSON_HEADERS).json({ error: message });
|
|
114
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@macss/modular-api",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Use-case-centric toolkit for building modular APIs with Express. Define UseCase classes (input → validate → execute → output), connect them to HTTP routes, and expose Swagger/OpenAPI documentation automatically.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|