@stratum-hq/hono 0.2.0 → 1.1.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 +1 -1
- package/dist/middleware.d.ts +8 -2
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +25 -16
- package/dist/middleware.js.map +1 -1
- package/package.json +14 -6
package/README.md
CHANGED
|
@@ -41,7 +41,7 @@ app.get("/users", (c) => {
|
|
|
41
41
|
| `header` | Read a request header (default: `x-tenant-id`) |
|
|
42
42
|
| `resolve` | Optional callback `(tenantId) => TenantContext` to populate ancestry, config, and permissions |
|
|
43
43
|
|
|
44
|
-
If no tenant ID is found, the middleware responds with `400 { error: "Missing tenant ID" }`. Without a `resolve` callback the context is a placeholder (empty config/permissions) — provide `resolve` for real tenant data.
|
|
44
|
+
If no tenant ID is found, the middleware responds with `400 { error: "Missing tenant ID" }`. If `resolve` rejects with a tenant error from `@stratum-hq/core`, for example from `StratumClient.resolveTenant`, the middleware responds with 404 `TENANT_NOT_FOUND`, 403 `TENANT_SUSPENDED`, 410 `TENANT_ARCHIVED`, or 403 `FORBIDDEN`. A control plane timeout gets 504 `CONTROL_PLANE_TIMEOUT`. An `UnauthorizedError` for the SDK's own API key gets 500 `CONTROL_PLANE_AUTH_FAILED` and a `console.error` line. Other errors go to the Hono error handler. Without a `resolve` callback the context is a placeholder (empty config/permissions) — provide `resolve` for real tenant data.
|
|
45
45
|
|
|
46
46
|
## Features
|
|
47
47
|
|
package/dist/middleware.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { MiddlewareHandler } from "hono";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ResolvedTenantContext } from "@stratum-hq/core";
|
|
3
3
|
export interface StratumMiddlewareOptions {
|
|
4
4
|
/** Header name to extract tenant ID from (default: 'x-tenant-id') */
|
|
5
5
|
header?: string;
|
|
@@ -11,8 +11,14 @@ export interface StratumMiddlewareOptions {
|
|
|
11
11
|
* Optional callback to resolve a full tenant context from the tenant ID.
|
|
12
12
|
* When provided, the middleware will call this to obtain ancestry, config,
|
|
13
13
|
* and permissions instead of using placeholder values.
|
|
14
|
+
*
|
|
15
|
+
* If the callback rejects with a tenant error from `@stratum-hq/core`, for
|
|
16
|
+
* example from `StratumClient.resolveTenant`, the middleware answers 404,
|
|
17
|
+
* 403 or 410. A control plane timeout answers 504. A rejected SDK API key
|
|
18
|
+
* answers 500 and writes the cause to `console.error`. Other errors go to
|
|
19
|
+
* the Hono error handler.
|
|
14
20
|
*/
|
|
15
|
-
resolve?: (tenantId: string) => Promise<
|
|
21
|
+
resolve?: (tenantId: string) => Promise<ResolvedTenantContext> | ResolvedTenantContext;
|
|
16
22
|
}
|
|
17
23
|
/**
|
|
18
24
|
* Hono middleware that extracts a tenant ID from the request and sets it
|
package/dist/middleware.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,iBAAiB,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAG7D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAG9D,MAAM,WAAW,wBAAwB;IACvC,qEAAqE;IACrE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,wDAAwD;IACxD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;;;;;;OAUG;IACH,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,qBAAqB,CAAC,GAAG,qBAAqB,CAAC;CACxF;AAkBD;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,GAAE,wBAA6B,GACrC,iBAAiB,CA4CnB"}
|
package/dist/middleware.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { runWithTenantContext } from "@stratum-hq/sdk";
|
|
1
|
+
import { controlPlaneErrorResponse, runWithTenantContext, tenantErrorResponse } from "@stratum-hq/sdk";
|
|
2
2
|
import { IsolationStrategy } from "@stratum-hq/core";
|
|
3
3
|
function extractFromHeader(c, header) {
|
|
4
4
|
return c.req.header(header) ?? undefined;
|
|
@@ -36,21 +36,30 @@ export function stratumMiddleware(options = {}) {
|
|
|
36
36
|
return c.json({ error: "Missing tenant ID" }, 400);
|
|
37
37
|
}
|
|
38
38
|
c.set("tenantId", tenantId);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
39
|
+
let ctx;
|
|
40
|
+
try {
|
|
41
|
+
ctx = options.resolve
|
|
42
|
+
? await options.resolve(tenantId)
|
|
43
|
+
: /**
|
|
44
|
+
* @warning Placeholder context — ancestry_path, resolved_config, and
|
|
45
|
+
* resolved_permissions are stub values. Provide a `resolve` callback
|
|
46
|
+
* to populate real tenant data.
|
|
47
|
+
*/
|
|
48
|
+
{
|
|
49
|
+
tenant_id: tenantId,
|
|
50
|
+
ancestry_path: tenantId,
|
|
51
|
+
depth: 0,
|
|
52
|
+
resolved_config: {},
|
|
53
|
+
resolved_permissions: {},
|
|
54
|
+
isolation_strategy: IsolationStrategy.SHARED_RLS,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
const response = tenantErrorResponse(err, tenantId) ?? controlPlaneErrorResponse(err);
|
|
59
|
+
if (!response)
|
|
60
|
+
throw err;
|
|
61
|
+
return c.json(response.body, response.status);
|
|
62
|
+
}
|
|
54
63
|
return runWithTenantContext(ctx, () => next());
|
|
55
64
|
};
|
|
56
65
|
}
|
package/dist/middleware.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,yBAAyB,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEvG,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAuBrD,SAAS,iBAAiB,CAAC,CAAU,EAAE,MAAc;IACnD,OAAO,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,SAAS,CAAC;AAC3C,CAAC;AAED,SAAS,mBAAmB,CAAC,CAAU,EAAE,KAAa;IACpD,8EAA8E;IAC9E,MAAM,OAAO,GAAG,CAAC,CAAC,GAAG,CAAC,YAAY,CAAwC,CAAC;IAC3E,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;IAC7B,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AACvD,CAAC;AAED,SAAS,oBAAoB,CAAC,CAAU,EAAE,KAAa;IACrD,OAAO,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC;AACzC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAC/B,UAAoC,EAAE;IAEtC,OAAO,KAAK,EAAE,CAAU,EAAE,IAAU,EAAE,EAAE;QACtC,IAAI,QAA4B,CAAC;QAEjC,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;YACrB,QAAQ,GAAG,mBAAmB,CAAC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AAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;IACjD,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stratum-hq/hono",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Stratum Hono integration — tenant extraction middleware with ALS context",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"multi-tenancy",
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"scripts": {
|
|
30
30
|
"build": "tsc",
|
|
31
31
|
"test": "vitest run",
|
|
32
|
-
"
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"lint": "eslint .",
|
|
33
34
|
"clean": "rm -rf dist"
|
|
34
35
|
},
|
|
35
36
|
"publishConfig": {
|
|
@@ -37,16 +38,23 @@
|
|
|
37
38
|
"registry": "https://registry.npmjs.org/"
|
|
38
39
|
},
|
|
39
40
|
"peerDependencies": {
|
|
40
|
-
"@stratum-hq/core": "
|
|
41
|
-
"@stratum-hq/sdk": "
|
|
41
|
+
"@stratum-hq/core": "^1.0.0",
|
|
42
|
+
"@stratum-hq/sdk": "^1.2.0",
|
|
42
43
|
"hono": ">=4.0.0"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"@stratum-hq/core": "*",
|
|
46
47
|
"@stratum-hq/sdk": "*",
|
|
47
|
-
"hono": "^4.
|
|
48
|
+
"hono": "^4.13.9",
|
|
48
49
|
"typescript": "^5.4.0",
|
|
49
|
-
"vitest": "^1.
|
|
50
|
+
"vitest": "^4.1.11"
|
|
51
|
+
},
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"author": "Christian Crank",
|
|
54
|
+
"homepage": "https://github.com/stratum-hq/Stratum/tree/main/packages/hono#readme",
|
|
55
|
+
"bugs": "https://github.com/stratum-hq/Stratum/issues",
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">=20.0.0"
|
|
50
58
|
},
|
|
51
59
|
"repository": {
|
|
52
60
|
"type": "git",
|