@stratum-hq/hono 0.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/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.d.ts +23 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +57 -0
- package/dist/middleware.js.map +1 -0
- package/package.json +51 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,YAAY,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { MiddlewareHandler } from "hono";
|
|
2
|
+
import type { TenantContextLegacy } from "@stratum-hq/core";
|
|
3
|
+
export interface StratumMiddlewareOptions {
|
|
4
|
+
/** Header name to extract tenant ID from (default: 'x-tenant-id') */
|
|
5
|
+
header?: string;
|
|
6
|
+
/** JWT claim name to extract tenant ID from */
|
|
7
|
+
jwtClaim?: string;
|
|
8
|
+
/** URL path parameter name to extract tenant ID from */
|
|
9
|
+
pathParam?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Optional callback to resolve a full tenant context from the tenant ID.
|
|
12
|
+
* When provided, the middleware will call this to obtain ancestry, config,
|
|
13
|
+
* and permissions instead of using placeholder values.
|
|
14
|
+
*/
|
|
15
|
+
resolve?: (tenantId: string) => Promise<TenantContextLegacy> | TenantContextLegacy;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Hono middleware that extracts a tenant ID from the request and sets it
|
|
19
|
+
* in both the Hono context (`c.get('tenantId')`) and the SDK's
|
|
20
|
+
* AsyncLocalStorage context via `runWithTenantContext()`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function stratumMiddleware(options?: StratumMiddlewareOptions): MiddlewareHandler;
|
|
23
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAE7D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAG5D,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;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,mBAAmB,CAAC,GAAG,mBAAmB,CAAC;CACpF;AAkBD;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,GAAE,wBAA6B,GACrC,iBAAiB,CAqCnB"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { runWithTenantContext } from "@stratum-hq/sdk";
|
|
2
|
+
import { IsolationStrategy } from "@stratum-hq/core";
|
|
3
|
+
function extractFromHeader(c, header) {
|
|
4
|
+
return c.req.header(header) ?? undefined;
|
|
5
|
+
}
|
|
6
|
+
function extractFromJwtClaim(c, claim) {
|
|
7
|
+
// Hono's JWT middleware sets the payload on the context variable 'jwtPayload'
|
|
8
|
+
const payload = c.get("jwtPayload");
|
|
9
|
+
if (!payload)
|
|
10
|
+
return undefined;
|
|
11
|
+
const value = payload[claim];
|
|
12
|
+
return typeof value === "string" ? value : undefined;
|
|
13
|
+
}
|
|
14
|
+
function extractFromPathParam(c, param) {
|
|
15
|
+
return c.req.param(param) ?? undefined;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Hono middleware that extracts a tenant ID from the request and sets it
|
|
19
|
+
* in both the Hono context (`c.get('tenantId')`) and the SDK's
|
|
20
|
+
* AsyncLocalStorage context via `runWithTenantContext()`.
|
|
21
|
+
*/
|
|
22
|
+
export function stratumMiddleware(options = {}) {
|
|
23
|
+
return async (c, next) => {
|
|
24
|
+
let tenantId;
|
|
25
|
+
if (options.jwtClaim) {
|
|
26
|
+
tenantId = extractFromJwtClaim(c, options.jwtClaim);
|
|
27
|
+
}
|
|
28
|
+
else if (options.pathParam) {
|
|
29
|
+
tenantId = extractFromPathParam(c, options.pathParam);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const header = options.header ?? "x-tenant-id";
|
|
33
|
+
tenantId = extractFromHeader(c, header);
|
|
34
|
+
}
|
|
35
|
+
if (!tenantId) {
|
|
36
|
+
return c.json({ error: "Missing tenant ID" }, 400);
|
|
37
|
+
}
|
|
38
|
+
c.set("tenantId", tenantId);
|
|
39
|
+
const ctx = options.resolve
|
|
40
|
+
? await options.resolve(tenantId)
|
|
41
|
+
: /**
|
|
42
|
+
* @warning Placeholder context — ancestry_path, resolved_config, and
|
|
43
|
+
* resolved_permissions are stub values. Provide a `resolve` callback
|
|
44
|
+
* to populate real tenant data.
|
|
45
|
+
*/
|
|
46
|
+
{
|
|
47
|
+
tenant_id: tenantId,
|
|
48
|
+
ancestry_path: tenantId,
|
|
49
|
+
depth: 0,
|
|
50
|
+
resolved_config: {},
|
|
51
|
+
resolved_permissions: {},
|
|
52
|
+
isolation_strategy: IsolationStrategy.SHARED_RLS,
|
|
53
|
+
};
|
|
54
|
+
return runWithTenantContext(ctx, () => next());
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=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CAAC;YACN,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,aAAa,CAAC;YAC/C,QAAQ,GAAG,iBAAiB,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QAC1C,CAAC;QAED,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,mBAAmB,EAAE,EAAE,GAAG,CAAC,CAAC;QACrD,CAAC;QAED,CAAC,CAAC,GAAG,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;QAE5B,MAAM,GAAG,GAAwB,OAAO,CAAC,OAAO;YAC9C,CAAC,CAAC,MAAM,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;YACjC,CAAC,CAAC;;;;iBAIG;gBACH;oBACE,SAAS,EAAE,QAAQ;oBACnB,aAAa,EAAE,QAAQ;oBACvB,KAAK,EAAE,CAAC;oBACR,eAAe,EAAE,EAAE;oBACnB,oBAAoB,EAAE,EAAE;oBACxB,kBAAkB,EAAE,iBAAiB,CAAC,UAAU;iBACjD,CAAC;QAEN,OAAO,oBAAoB,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;IACjD,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stratum-hq/hono",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Stratum Hono integration — tenant extraction middleware with ALS context",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"multi-tenancy",
|
|
7
|
+
"multi-tenant",
|
|
8
|
+
"saas",
|
|
9
|
+
"tenant",
|
|
10
|
+
"hono",
|
|
11
|
+
"middleware",
|
|
12
|
+
"typescript",
|
|
13
|
+
"stratum"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"lint": "tsc --noEmit",
|
|
33
|
+
"clean": "rm -rf dist"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public",
|
|
37
|
+
"registry": "https://registry.npmjs.org/"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@stratum-hq/core": ">=0.2.0",
|
|
41
|
+
"@stratum-hq/sdk": ">=0.2.0",
|
|
42
|
+
"hono": ">=4.0.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@stratum-hq/core": "*",
|
|
46
|
+
"@stratum-hq/sdk": "*",
|
|
47
|
+
"hono": "^4.0.0",
|
|
48
|
+
"typescript": "^5.4.0",
|
|
49
|
+
"vitest": "^1.6.0"
|
|
50
|
+
}
|
|
51
|
+
}
|