@onlineapps/service-wrapper 2.2.8 → 2.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/package.json +1 -1
- package/src/ServiceWrapper.js +22 -0
- package/src/createTenantContextMiddleware.js +4 -1
- package/src/index.js +11 -2
package/package.json
CHANGED
package/src/ServiceWrapper.js
CHANGED
|
@@ -1814,6 +1814,28 @@ class ServiceWrapper {
|
|
|
1814
1814
|
}
|
|
1815
1815
|
}
|
|
1816
1816
|
|
|
1817
|
+
// Extension 1: specificationEndpoint must match an Express route
|
|
1818
|
+
// See: docs/standards/api-versioning-contract.md
|
|
1819
|
+
const specEndpoint = this.config.service?.specificationEndpoint;
|
|
1820
|
+
if (specEndpoint && !routeSet.has(`GET:${specEndpoint}`)) {
|
|
1821
|
+
errors.push(
|
|
1822
|
+
`specificationEndpoint '${specEndpoint}' not found in Express routes — ` +
|
|
1823
|
+
`config claims this path but no GET route is registered`
|
|
1824
|
+
);
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
// Extension 3: operations.json endpoints under /api/ must be versioned
|
|
1828
|
+
// See: docs/standards/api-versioning-contract.md
|
|
1829
|
+
const versionPattern = /^\/api\/v\d+\//;
|
|
1830
|
+
for (const [opName, opDef] of Object.entries(ops)) {
|
|
1831
|
+
if (opDef.endpoint?.startsWith('/api') && !versionPattern.test(opDef.endpoint)) {
|
|
1832
|
+
errors.push(
|
|
1833
|
+
`Operation '${opName}' endpoint '${opDef.endpoint}' missing version — ` +
|
|
1834
|
+
`expected /api/v{N}/... (see docs/standards/api-versioning-contract.md)`
|
|
1835
|
+
);
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1817
1839
|
if (errors.length > 0) {
|
|
1818
1840
|
throw new Error(
|
|
1819
1841
|
`[ServiceWrapper] Operations-Routes alignment failed (${errors.length} issue${errors.length > 1 ? 's' : ''}):\n` +
|
|
@@ -54,7 +54,10 @@ function createTenantContextMiddleware(options = {}) {
|
|
|
54
54
|
|
|
55
55
|
function sendJson(res, statusCode, payload) {
|
|
56
56
|
if (!res || typeof res.status !== 'function' || typeof res.json !== 'function') {
|
|
57
|
-
throw new Error(
|
|
57
|
+
throw new Error(
|
|
58
|
+
'[service-wrapper][TenantContext] res.status/res.json unavailable - ' +
|
|
59
|
+
'middleware is likely running before expressInit (check bootstrap stack order)'
|
|
60
|
+
);
|
|
58
61
|
}
|
|
59
62
|
return res.status(statusCode).json(payload);
|
|
60
63
|
}
|
package/src/index.js
CHANGED
|
@@ -93,7 +93,16 @@ async function bootstrap(serviceRoot, options = {}) {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
stack.pop();
|
|
96
|
-
|
|
96
|
+
// Insert AFTER expressInit (which sets up res.status/res.json via setPrototypeOf).
|
|
97
|
+
// Position 0 = query, 1 = expressInit. Tenant context must run after both.
|
|
98
|
+
const expressInitIdx = stack.findIndex(l => l.name === 'expressInit');
|
|
99
|
+
if (expressInitIdx === -1) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
'[service-wrapper][TenantContext] Express stack missing expressInit layer - ' +
|
|
102
|
+
'cannot guarantee tenant context runs after Express initialization'
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
stack.splice(expressInitIdx + 1, 0, lastLayer);
|
|
97
106
|
|
|
98
107
|
// Register /health route BEFORE service's 404 handler.
|
|
99
108
|
// ServiceWrapper._setupHealthChecks() will later set _healthImpl with actual MQ checks.
|
|
@@ -111,7 +120,7 @@ async function bootstrap(serviceRoot, options = {}) {
|
|
|
111
120
|
const healthLayer = stack.pop();
|
|
112
121
|
if (healthLayer) {
|
|
113
122
|
const catchAllIdx = stack.findIndex((layer, i) =>
|
|
114
|
-
i > 0 && !layer.route && !['query', 'expressInit', 'jsonParser', 'urlencodedParser'].includes(layer.name)
|
|
123
|
+
i > 0 && !layer.route && !['query', 'expressInit', 'tenantContextMiddleware', 'jsonParser', 'urlencodedParser'].includes(layer.name)
|
|
115
124
|
);
|
|
116
125
|
if (catchAllIdx > 0) {
|
|
117
126
|
stack.splice(catchAllIdx, 0, healthLayer);
|