@longzai-intelligence-tenant/elysia 0.0.1
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 +73 -0
- package/dist/index.js +1 -0
- package/package.json +43 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Elysia } from "elysia";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* 租户验证函数类型
|
|
6
|
+
*
|
|
7
|
+
* @param tenantId - 待验证的租户 ID
|
|
8
|
+
* @returns 验证结果
|
|
9
|
+
*/
|
|
10
|
+
type TenantValidateFn = (tenantId: string) => Promise<{
|
|
11
|
+
valid: boolean;
|
|
12
|
+
error?: string;
|
|
13
|
+
}>;
|
|
14
|
+
/**
|
|
15
|
+
* 租户上下文轻插件配置
|
|
16
|
+
*/
|
|
17
|
+
type TenantContextOptions = {
|
|
18
|
+
/**
|
|
19
|
+
* 默认租户 ID
|
|
20
|
+
*
|
|
21
|
+
* 请求头未携带 X-Tenant-ID 时使用此值。
|
|
22
|
+
*/
|
|
23
|
+
defaultTenantId?: string;
|
|
24
|
+
/**
|
|
25
|
+
* 租户验证函数(可选)
|
|
26
|
+
*
|
|
27
|
+
* 非默认租户时调用,用于校验租户存在性和状态。
|
|
28
|
+
* 不提供时跳过验证(信任上游已校验)。
|
|
29
|
+
*/
|
|
30
|
+
validateFn?: TenantValidateFn;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* 创建租户上下文 Elysia 轻插件
|
|
34
|
+
*
|
|
35
|
+
* 从请求头 X-Tenant-ID 解析当前租户 ID,注入到 request context。
|
|
36
|
+
* 可选地调用 validateFn 校验租户有效性。
|
|
37
|
+
*
|
|
38
|
+
* 不挂载路由,不注册适配器,不依赖 tenant 域内部包。
|
|
39
|
+
*/
|
|
40
|
+
declare function createTenantContextPlugin(options?: TenantContextOptions): Elysia<"", {
|
|
41
|
+
decorator: {};
|
|
42
|
+
store: {};
|
|
43
|
+
derive: {
|
|
44
|
+
tenantId: string;
|
|
45
|
+
};
|
|
46
|
+
resolve: {};
|
|
47
|
+
}, {
|
|
48
|
+
typebox: {};
|
|
49
|
+
error: {};
|
|
50
|
+
}, {
|
|
51
|
+
schema: {};
|
|
52
|
+
standaloneSchema: {};
|
|
53
|
+
macro: {};
|
|
54
|
+
macroFn: {};
|
|
55
|
+
parser: {};
|
|
56
|
+
response: import("elysia").ExtractErrorFromHandle<{
|
|
57
|
+
tenantId: string;
|
|
58
|
+
}> & {};
|
|
59
|
+
}, {}, {
|
|
60
|
+
derive: {};
|
|
61
|
+
resolve: {};
|
|
62
|
+
schema: {};
|
|
63
|
+
standaloneSchema: {};
|
|
64
|
+
response: {};
|
|
65
|
+
}, {
|
|
66
|
+
derive: {};
|
|
67
|
+
resolve: {};
|
|
68
|
+
schema: {};
|
|
69
|
+
standaloneSchema: {};
|
|
70
|
+
response: {};
|
|
71
|
+
}>;
|
|
72
|
+
//#endregion
|
|
73
|
+
export { TenantContextOptions, TenantValidateFn, createTenantContextPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Elysia as e}from"elysia";function t(t={}){let{defaultTenantId:n=`default`,validateFn:r}=t;return new e({name:`@longzai-intelligence-tenant/elysia`}).derive(({request:e})=>({tenantId:e.headers.get(`x-tenant-id`)??n})).as(`scoped`).onBeforeHandle(async({tenantId:e})=>{if(r&&e!==n&&!(await r(e)).valid){let{PermissionDeniedError:e}=await import(`@longzai-intelligence/error`);throw new e(`tenant`,`access`)}}).as(`global`)}export{t as createTenantContextPlugin};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@longzai-intelligence-tenant/elysia",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsgo --build tsconfig/build.json && resolve-aliases -p tsconfig/build.json",
|
|
20
|
+
"build:prod": "NODE_ENV=production tsdown",
|
|
21
|
+
"prepublishOnly": "bun run build:prod",
|
|
22
|
+
"typecheck": "bun run typecheck:app && bun run typecheck:node && bun run typecheck:test",
|
|
23
|
+
"typecheck:app": "tsgo --noEmit -p tsconfig/app.json",
|
|
24
|
+
"typecheck:node": "tsgo --noEmit -p tsconfig/node.json",
|
|
25
|
+
"typecheck:test": "tsgo --noEmit -p tsconfig/test.json",
|
|
26
|
+
"lint": "oxlint src && oxfmt --check src",
|
|
27
|
+
"lint:fix": "oxlint --fix src && oxfmt src",
|
|
28
|
+
"test": "bun test",
|
|
29
|
+
"test:watch": "bun test --watch",
|
|
30
|
+
"test:coverage": "bun test --coverage",
|
|
31
|
+
"clean": "rm -rf dist out .cache"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@longzai-intelligence/error": "^0.0.5"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"elysia": "^1.3"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"elysia": "^1.4.28"
|
|
41
|
+
},
|
|
42
|
+
"packageManager": "bun@1.3.14"
|
|
43
|
+
}
|