@longzai-intelligence-tenant/elysia 0.0.1 → 0.0.2
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 +37 -4
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ type TenantValidateFn = (tenantId: string) => Promise<{
|
|
|
12
12
|
error?: string;
|
|
13
13
|
}>;
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* 租户上下文插件配置
|
|
16
16
|
*/
|
|
17
17
|
type TenantContextOptions = {
|
|
18
18
|
/**
|
|
@@ -21,6 +21,19 @@ type TenantContextOptions = {
|
|
|
21
21
|
* 请求头未携带 X-Tenant-ID 时使用此值。
|
|
22
22
|
*/
|
|
23
23
|
defaultTenantId?: string;
|
|
24
|
+
/**
|
|
25
|
+
* 租户 ID 来源
|
|
26
|
+
*
|
|
27
|
+
* - `'header'`:从请求头解析(默认)
|
|
28
|
+
* - `'url'`:从 URL 路径参数 :tenantId 解析
|
|
29
|
+
*/
|
|
30
|
+
tenantIdSource?: "header" | "url";
|
|
31
|
+
/**
|
|
32
|
+
* 请求头名称(当 tenantIdSource 为 header 时使用)
|
|
33
|
+
*
|
|
34
|
+
* 默认 `'x-tenant-id'`。
|
|
35
|
+
*/
|
|
36
|
+
headerName?: string;
|
|
24
37
|
/**
|
|
25
38
|
* 租户验证函数(可选)
|
|
26
39
|
*
|
|
@@ -28,12 +41,32 @@ type TenantContextOptions = {
|
|
|
28
41
|
* 不提供时跳过验证(信任上游已校验)。
|
|
29
42
|
*/
|
|
30
43
|
validateFn?: TenantValidateFn;
|
|
44
|
+
/**
|
|
45
|
+
* 验证租户成员资格的回调(可选)
|
|
46
|
+
*
|
|
47
|
+
* 需要请求上下文中已存在 user 对象(userId)。
|
|
48
|
+
*/
|
|
49
|
+
validateMembership?: (userId: string, tenantId: string) => Promise<boolean>;
|
|
50
|
+
/**
|
|
51
|
+
* 是否启用数据库行级安全上下文设置
|
|
52
|
+
*
|
|
53
|
+
* 启用后,在验证租户后调用 setRLSContext,请求结束时调用 resetRLSContext。
|
|
54
|
+
*/
|
|
55
|
+
enableRLS?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* 设置 RLS 上下文的回调
|
|
58
|
+
*/
|
|
59
|
+
setRLSContext?: (tenantId: string) => Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* 重置 RLS 上下文的回调
|
|
62
|
+
*/
|
|
63
|
+
resetRLSContext?: () => Promise<void>;
|
|
31
64
|
};
|
|
32
65
|
/**
|
|
33
|
-
* 创建租户上下文 Elysia
|
|
66
|
+
* 创建租户上下文 Elysia 插件
|
|
34
67
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
68
|
+
* 从请求头或 URL 参数解析当前租户 ID,注入到 request context。
|
|
69
|
+
* 支持可选的租户验证、成员资格校验、RLS 上下文管理。
|
|
37
70
|
*
|
|
38
71
|
* 不挂载路由,不注册适配器,不依赖 tenant 域内部包。
|
|
39
72
|
*/
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Elysia as e}from"elysia";function t(t={}){let{defaultTenantId:n=`default`,validateFn:
|
|
1
|
+
import{Elysia as e}from"elysia";function t(t={}){let{defaultTenantId:n=`default`,tenantIdSource:r=`header`,headerName:i=`x-tenant-id`,validateFn:a,validateMembership:o,enableRLS:s,setRLSContext:c,resetRLSContext:l}=t;return new e({name:`@longzai-intelligence-tenant/elysia`}).derive(({request:e,params:t})=>{let a;return r===`url`&&(a=t?.tenantId),!a&&r===`header`&&(a=e.headers.get(i)??void 0),{tenantId:a??n}}).as(`scoped`).onBeforeHandle(async({tenantId:e,request:t})=>{if(a&&e!==n&&!(await a(e)).valid){let{PermissionDeniedError:e}=await import(`@longzai-intelligence/error`);throw new e(`tenant`,`access`)}if(o){let n=t;if(n.user?.userId&&!await o(n.user.userId,e)){let{PermissionDeniedError:e}=await import(`@longzai-intelligence/error`);throw new e(`tenant`,`access`)}}s&&c&&await c(e)}).as(`global`).onAfterHandle(async()=>{s&&l&&await l()}).as(`global`)}export{t as createTenantContextPlugin};
|