@hz-9/a5-tenant 0.2.0-alpha.33

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 ADDED
@@ -0,0 +1,129 @@
1
+ # @hz-9/a5-tenant
2
+
3
+ Tenant module for the @hz-9/a5-* series of repositories.
4
+
5
+ ## Features
6
+
7
+ - ✅ 租户检查 Guard
8
+ - ✅ 从 headers 和 route params 中提取租户 ID
9
+ - ✅ 可配置的租户 Service
10
+ - ✅ TypeScript 完整类型支持
11
+ - ✅ 全局模块支持
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pnpm add @hz-9/a5-tenant
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ ### 1. 实现租户 Service
22
+
23
+ ```typescript
24
+ import { Injectable } from '@nestjs/common'
25
+ import { A5TenantService, A5TenantCheckResult } from '@hz-9/a5-tenant'
26
+
27
+ @Injectable()
28
+ export class MyTenantService implements A5TenantService {
29
+ async checkTenant(tenantId: string, userId?: string): Promise<A5TenantCheckResult> {
30
+ // 在此实现你的租户检查逻辑
31
+ const tenant = await this.db.tenant.findUnique({ where: { id: tenantId } })
32
+
33
+ if (!tenant) {
34
+ return {
35
+ valid: false,
36
+ message: 'Tenant not found',
37
+ }
38
+ }
39
+
40
+ return {
41
+ valid: true,
42
+ context: {
43
+ tenantId,
44
+ userId,
45
+ },
46
+ }
47
+ }
48
+ }
49
+ ```
50
+
51
+ ### 2. 注册模块
52
+
53
+ ```typescript
54
+ import { Module } from '@nestjs/common'
55
+ import { A5TenantModule } from '@hz-9/a5-tenant'
56
+ import { MyTenantService } from './my-tenant.service'
57
+
58
+ @Module({
59
+ imports: [
60
+ A5TenantModule.forRoot({
61
+ tenantService: new MyTenantService(),
62
+ isGlobal: true,
63
+ }),
64
+ ],
65
+ })
66
+ export class AppModule {}
67
+ ```
68
+
69
+ ### 3. 使用 Guard
70
+
71
+ ```typescript
72
+ import { Controller, Get, UseGuards } from '@nestjs/common'
73
+ import { A5TenantGuard } from '@hz-9/a5-tenant'
74
+
75
+ @Controller('/api/data')
76
+ @UseGuards(A5TenantGuard)
77
+ export class DataController {
78
+ @Get()
79
+ getData() {
80
+ return { data: 'example' }
81
+ }
82
+ }
83
+ ```
84
+
85
+ ## 获取租户 ID
86
+
87
+ 在控制器中可以通过 request 对象获取租户信息:
88
+
89
+ ```typescript
90
+ import { Controller, Get, Req, UseGuards } from '@nestjs/common'
91
+ import { FastifyRequest } from '@hz-9/a5-core/interface/http'
92
+ import { A5TenantGuard } from '@hz-9/a5-tenant'
93
+
94
+ @Controller('/api/data')
95
+ @UseGuards(A5TenantGuard)
96
+ export class DataController {
97
+ @Get()
98
+ getData(@Req() request: FastifyRequest) {
99
+ const tenantId = (request as any).tenantId
100
+ const tenantContext = (request as any).tenantContext
101
+ return { tenantId, tenantContext }
102
+ }
103
+ }
104
+ ```
105
+
106
+ ## 租户 ID 来源
107
+
108
+ Guard 会依次从以下位置尝试获取租户 ID:
109
+
110
+ 1. **Headers**: `x-tenant-id` header
111
+ 2. **Route Params**: `tenantId` 路由参数
112
+
113
+ ## 配置选项
114
+
115
+ ### A5TenantModuleOptions
116
+
117
+ ```typescript
118
+ interface A5TenantModuleOptions {
119
+ // 租户 Service 提供者
120
+ tenantService: A5TenantService | ProviderConfig
121
+
122
+ // 是否为全局模块(默认:true)
123
+ isGlobal?: boolean
124
+ }
125
+ ```
126
+
127
+ ## License
128
+
129
+ MIT
package/dist/all.d.ts ADDED
@@ -0,0 +1,156 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * A5 Tenant Module - Tenant management and validation for A5 framework
4
+ */
5
+
6
+ import { A5TenantGuardConstructorOptions as A5TenantGuardConstructorOptions_2 } from '../interfaces';
7
+ import { CanActivate } from '@nestjs/common';
8
+ import { ConfigurableModuleCls } from '@nestjs/common';
9
+ import { ExecutionContext } from '@nestjs/common';
10
+
11
+ /**
12
+ * @public
13
+ */
14
+ export declare const A5_TENANT_ID_HEADER: "x-tenant-id";
15
+
16
+ /**
17
+ * @public
18
+ */
19
+ export declare const A5_TENANT_ID_PARAM: "tenantId";
20
+
21
+ /**
22
+ * @public
23
+ */
24
+ export declare const A5_TENANT_MODULE_OPTIONS: string | symbol;
25
+
26
+ /**
27
+ * @public
28
+ */
29
+ export declare const A5_TENANT_SERVICE_TOKEN: "A5_TENANT_SERVICE_TOKEN";
30
+
31
+ /**
32
+ * @public
33
+ */
34
+ export declare const A5TenantConfigurableModule: ConfigurableModuleCls<A5TenantGuardConstructorOptions_2, "forRoot", "createOptions", {
35
+ isGlobal: boolean;
36
+ }>;
37
+
38
+ /**
39
+ * @public
40
+ */
41
+ export declare const A5TenantCurrentTenant: (...dataOrPipes: unknown[]) => ParameterDecorator;
42
+
43
+ /**
44
+ * @public
45
+ */
46
+ export declare const A5TenantCurrentTenantId: (...dataOrPipes: unknown[]) => ParameterDecorator;
47
+
48
+ /**
49
+ * 租户检查 Guard
50
+ *
51
+ * 从 headers 或 route params 中获取租户 ID,并检查租户是否存在
52
+ *
53
+ * @public
54
+ */
55
+ export declare class A5TenantGuard implements CanActivate {
56
+ private readonly tenantService;
57
+ private readonly options;
58
+ constructor(options: A5TenantGuardConstructorOptions, tenantService: A5TenantService);
59
+ canActivate(context: ExecutionContext): Promise<boolean>;
60
+ private parseFromHeaders;
61
+ /**
62
+ * 从 headers 中获取租户 ID
63
+ *
64
+ * @param request - FastifyRequest 对象
65
+ * @returns 租户 ID 或 undefined
66
+ */
67
+ private getTenantIdFromHeaders;
68
+ }
69
+
70
+ /**
71
+ * @public
72
+ */
73
+ export declare interface A5TenantGuardConstructorOptions {
74
+ /**
75
+ * 是否启用空白自动转换为默认租户功能
76
+ *
77
+ * @defaultValue true
78
+ */
79
+ emptyToDefault?: boolean;
80
+ /**
81
+ * 默认租户 ID
82
+ *
83
+ * @defaultValue '-'
84
+ */
85
+ defaultTenantId?: string;
86
+ }
87
+
88
+ /**
89
+ * @public
90
+ */
91
+ export declare type A5TenantGuardOptions = Required<A5TenantGuardConstructorOptions>;
92
+
93
+ /**
94
+ * @public
95
+ */
96
+ export declare class A5TenantModule extends A5TenantConfigurableModule {
97
+ }
98
+
99
+ /**
100
+ * A5 缓存模块异步配置选项接口
101
+ *
102
+ * @public
103
+ */
104
+ export declare interface A5TenantModuleAsyncOptions {
105
+ /**
106
+ * 工厂函数,返回 A5TenantModuleOptions 配置对象
107
+ */
108
+ useFactory?: (...args: unknown[]) => Promise<A5TenantModuleOptions> | A5TenantModuleOptions;
109
+ /**
110
+ * 需要注入到 useFactory 函数中的依赖项
111
+ */
112
+ inject?: unknown[];
113
+ /**
114
+ * 需要导入的模块列表
115
+ */
116
+ imports?: unknown[];
117
+ }
118
+
119
+ /**
120
+ * A5 缓存模块配置选项
121
+ *
122
+ * @public
123
+ */
124
+ export declare type A5TenantModuleOptions = A5TenantGuardConstructorOptions;
125
+
126
+ /**
127
+ * @public
128
+ */
129
+ export declare interface A5TenantPayload {
130
+ /**
131
+ * 租户 ID
132
+ */
133
+ tenantId: string;
134
+ }
135
+
136
+ /**
137
+ * @public
138
+ */
139
+ export declare interface A5TenantService {
140
+ /**
141
+ * 获取租户信息,如果没有,则返回 null
142
+ *
143
+ * @param tenantId - 租户 ID
144
+ * @returns 检查结果
145
+ */
146
+ getTenant(tenantId: string): Promise<A5TenantPayload | null>;
147
+ /**
148
+ * 返回默认租户信息
149
+ *
150
+ * @param tenantId - 租户 ID
151
+ * @returns 检查结果
152
+ */
153
+ getDefaultTenant(defaultTenantId: string): Promise<A5TenantPayload>;
154
+ }
155
+
156
+ export { }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @public
3
+ */
4
+ export declare const A5_TENANT_SERVICE_TOKEN: "A5_TENANT_SERVICE_TOKEN";
5
+ /**
6
+ * @public
7
+ */
8
+ export declare const A5_TENANT_ID_HEADER: "x-tenant-id";
9
+ /**
10
+ * @public
11
+ */
12
+ export declare const A5_TENANT_ID_PARAM: "tenantId";
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.A5_TENANT_ID_PARAM = exports.A5_TENANT_ID_HEADER = exports.A5_TENANT_SERVICE_TOKEN = void 0;
4
+ /**
5
+ * @public
6
+ */
7
+ exports.A5_TENANT_SERVICE_TOKEN = 'A5_TENANT_SERVICE_TOKEN';
8
+ /**
9
+ * @public
10
+ */
11
+ exports.A5_TENANT_ID_HEADER = 'x-tenant-id';
12
+ /**
13
+ * @public
14
+ */
15
+ exports.A5_TENANT_ID_PARAM = 'tenantId';
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,24 @@
1
+ import { CanActivate, ExecutionContext } from '@nestjs/common';
2
+ import type { A5TenantGuardConstructorOptions } from '../interfaces/tenant';
3
+ import type { A5TenantService } from './a5-tenant.service';
4
+ /**
5
+ * 租户检查 Guard
6
+ *
7
+ * 从 headers 或 route params 中获取租户 ID,并检查租户是否存在
8
+ *
9
+ * @public
10
+ */
11
+ export declare class A5TenantGuard implements CanActivate {
12
+ private readonly tenantService;
13
+ private readonly options;
14
+ constructor(options: A5TenantGuardConstructorOptions, tenantService: A5TenantService);
15
+ canActivate(context: ExecutionContext): Promise<boolean>;
16
+ private parseFromHeaders;
17
+ /**
18
+ * 从 headers 中获取租户 ID
19
+ *
20
+ * @param request - FastifyRequest 对象
21
+ * @returns 租户 ID 或 undefined
22
+ */
23
+ private getTenantIdFromHeaders;
24
+ }
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.A5TenantGuard = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const const_1 = require("../const");
18
+ const module_definition_1 = require("./module-definition");
19
+ const optionsWithDefaults = (options) => ({
20
+ emptyToDefault: true,
21
+ defaultTenantId: '-',
22
+ ...options,
23
+ });
24
+ /**
25
+ * 租户检查 Guard
26
+ *
27
+ * 从 headers 或 route params 中获取租户 ID,并检查租户是否存在
28
+ *
29
+ * @public
30
+ */
31
+ let A5TenantGuard = class A5TenantGuard {
32
+ constructor(options, tenantService) {
33
+ this.tenantService = tenantService;
34
+ this.options = optionsWithDefaults(options);
35
+ }
36
+ async canActivate(context) {
37
+ const request = context.switchToHttp().getRequest();
38
+ // TODO 后续可以基于 metadata 信息,对解析方案进行自定义优化
39
+ return this.parseFromHeaders(request);
40
+ }
41
+ async parseFromHeaders(request) {
42
+ // 尝试从 headers 中获取租户 ID
43
+ let tenantId = this.getTenantIdFromHeaders(request);
44
+ if (!tenantId) {
45
+ if (this.options.emptyToDefault) {
46
+ tenantId = this.options.defaultTenantId;
47
+ const tenant = this.tenantService.getDefaultTenant(tenantId);
48
+ // 将租户信息存储到 request 对象中供后续使用
49
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
+ const requestWithTenant = request;
51
+ requestWithTenant.tenantId = tenantId;
52
+ requestWithTenant.tenantContext = tenant;
53
+ return true;
54
+ }
55
+ throw new common_1.UnauthorizedException('No tenant ID found');
56
+ }
57
+ // 检查租户是否存在和有效
58
+ const tenant = await this.tenantService.getTenant(tenantId);
59
+ if (!tenant) {
60
+ throw new common_1.ForbiddenException('tenant ID is not valid');
61
+ }
62
+ // 将租户信息存储到 request 对象中供后续使用
63
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
+ const requestWithTenant = request;
65
+ requestWithTenant.tenantId = tenantId;
66
+ requestWithTenant.tenantContext = tenant;
67
+ return true;
68
+ }
69
+ /**
70
+ * 从 headers 中获取租户 ID
71
+ *
72
+ * @param request - FastifyRequest 对象
73
+ * @returns 租户 ID 或 undefined
74
+ */
75
+ getTenantIdFromHeaders(request) {
76
+ return request.headers[const_1.A5_TENANT_ID_HEADER];
77
+ }
78
+ };
79
+ exports.A5TenantGuard = A5TenantGuard;
80
+ exports.A5TenantGuard = A5TenantGuard = __decorate([
81
+ (0, common_1.Injectable)(),
82
+ __param(0, (0, common_1.Inject)(module_definition_1.A5_TENANT_MODULE_OPTIONS)),
83
+ __param(1, (0, common_1.Inject)(const_1.A5_TENANT_SERVICE_TOKEN)),
84
+ __metadata("design:paramtypes", [Object, Object])
85
+ ], A5TenantGuard);
86
+ //# sourceMappingURL=a5-tenant.guard.js.map
@@ -0,0 +1,6 @@
1
+ import { A5TenantConfigurableModule } from './module-definition';
2
+ /**
3
+ * @public
4
+ */
5
+ export declare class A5TenantModule extends A5TenantConfigurableModule {
6
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.A5TenantModule = void 0;
10
+ const common_1 = require("@nestjs/common");
11
+ const core_1 = require("@nestjs/core");
12
+ const a5_tenant_guard_1 = require("./a5-tenant.guard");
13
+ const module_definition_1 = require("./module-definition");
14
+ /**
15
+ * @public
16
+ */
17
+ let A5TenantModule = class A5TenantModule extends module_definition_1.A5TenantConfigurableModule {
18
+ };
19
+ exports.A5TenantModule = A5TenantModule;
20
+ exports.A5TenantModule = A5TenantModule = __decorate([
21
+ (0, common_1.Global)(),
22
+ (0, common_1.Module)({
23
+ providers: [
24
+ {
25
+ provide: core_1.APP_GUARD,
26
+ useClass: a5_tenant_guard_1.A5TenantGuard,
27
+ },
28
+ ],
29
+ exports: [],
30
+ })
31
+ ], A5TenantModule);
32
+ //# sourceMappingURL=a5-tenant.module.js.map
@@ -0,0 +1,20 @@
1
+ import { A5TenantPayload } from '../interfaces/tenant';
2
+ /**
3
+ * @public
4
+ */
5
+ export interface A5TenantService {
6
+ /**
7
+ * 获取租户信息,如果没有,则返回 null
8
+ *
9
+ * @param tenantId - 租户 ID
10
+ * @returns 检查结果
11
+ */
12
+ getTenant(tenantId: string): Promise<A5TenantPayload | null>;
13
+ /**
14
+ * 返回默认租户信息
15
+ *
16
+ * @param tenantId - 租户 ID
17
+ * @returns 检查结果
18
+ */
19
+ getDefaultTenant(defaultTenantId: string): Promise<A5TenantPayload>;
20
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=a5-tenant.service.js.map
@@ -0,0 +1,4 @@
1
+ export * from './a5-tenant.guard';
2
+ export * from './a5-tenant.module';
3
+ export * from './a5-tenant.service';
4
+ export * from './module-definition';
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./a5-tenant.guard"), exports);
18
+ __exportStar(require("./a5-tenant.module"), exports);
19
+ __exportStar(require("./a5-tenant.service"), exports);
20
+ __exportStar(require("./module-definition"), exports);
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @public
3
+ */
4
+ export declare const A5TenantConfigurableModule: import("@nestjs/common").ConfigurableModuleCls<import("../interfaces").A5TenantGuardConstructorOptions, "forRoot", "createOptions", {
5
+ isGlobal: boolean;
6
+ }>;
7
+ /**
8
+ * @public
9
+ */
10
+ export declare const A5_TENANT_MODULE_OPTIONS: string | symbol;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.A5_TENANT_MODULE_OPTIONS = exports.A5TenantConfigurableModule = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ // eslint-disable-next-line @rushstack/typedef-var
6
+ const moduleDefinition = new common_1.ConfigurableModuleBuilder({
7
+ moduleName: 'A5Tenant',
8
+ })
9
+ .setExtras({
10
+ isGlobal: true,
11
+ }, (definition, options) => ({
12
+ ...definition,
13
+ global: options.isGlobal,
14
+ }))
15
+ .setClassMethodName('forRoot')
16
+ .setFactoryMethodName('createOptions')
17
+ .build();
18
+ /**
19
+ * @public
20
+ */
21
+ // eslint-disable-next-line @rushstack/typedef-var
22
+ exports.A5TenantConfigurableModule = moduleDefinition.ConfigurableModuleClass;
23
+ /**
24
+ * @public
25
+ */
26
+ // eslint-disable-next-line @rushstack/typedef-var
27
+ exports.A5_TENANT_MODULE_OPTIONS = moduleDefinition.MODULE_OPTIONS_TOKEN;
28
+ //# sourceMappingURL=module-definition.js.map
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @public
3
+ */
4
+ export declare const A5TenantCurrentTenantId: (...dataOrPipes: unknown[]) => ParameterDecorator;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.A5TenantCurrentTenantId = void 0;
4
+ /* eslint-disable @rushstack/typedef-var */
5
+ const common_1 = require("@nestjs/common");
6
+ /**
7
+ * @public
8
+ */
9
+ exports.A5TenantCurrentTenantId = (0, common_1.createParamDecorator)((data, ctx) => {
10
+ const request = ctx.switchToHttp().getRequest();
11
+ return request.tenantId;
12
+ });
13
+ //# sourceMappingURL=a5-tenant-current-tenant-id.decorator.js.map
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @public
3
+ */
4
+ export declare const A5TenantCurrentTenant: (...dataOrPipes: unknown[]) => ParameterDecorator;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.A5TenantCurrentTenant = void 0;
4
+ /* eslint-disable @rushstack/typedef-var */
5
+ const common_1 = require("@nestjs/common");
6
+ /**
7
+ * @public
8
+ */
9
+ exports.A5TenantCurrentTenant = (0, common_1.createParamDecorator)((data, ctx) => {
10
+ const request = ctx.switchToHttp().getRequest();
11
+ return request.tenantContext;
12
+ });
13
+ //# sourceMappingURL=a5-tenant-current-tenant.decorator.js.map
@@ -0,0 +1,2 @@
1
+ export * from './a5-tenant-current-tenant-id.decorator';
2
+ export * from './a5-tenant-current-tenant.decorator';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./a5-tenant-current-tenant-id.decorator"), exports);
18
+ __exportStar(require("./a5-tenant-current-tenant.decorator"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * A5 Tenant Module - Tenant management and validation for A5 framework
4
+ */
5
+ export * from './const';
6
+ export * from './core';
7
+ export * from './decorators';
8
+ export * from './interfaces';
package/dist/index.js ADDED
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ /**
3
+ * @packageDocumentation
4
+ * A5 Tenant Module - Tenant management and validation for A5 framework
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
18
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ __exportStar(require("./const"), exports);
22
+ __exportStar(require("./core"), exports);
23
+ __exportStar(require("./decorators"), exports);
24
+ __exportStar(require("./interfaces"), exports);
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,2 @@
1
+ export * from './tenant';
2
+ export * from './module';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./tenant"), exports);
18
+ __exportStar(require("./module"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,26 @@
1
+ import type { A5TenantGuardConstructorOptions } from './tenant';
2
+ /**
3
+ * A5 缓存模块配置选项
4
+ *
5
+ * @public
6
+ */
7
+ export type A5TenantModuleOptions = A5TenantGuardConstructorOptions;
8
+ /**
9
+ * A5 缓存模块异步配置选项接口
10
+ *
11
+ * @public
12
+ */
13
+ export interface A5TenantModuleAsyncOptions {
14
+ /**
15
+ * 工厂函数,返回 A5TenantModuleOptions 配置对象
16
+ */
17
+ useFactory?: (...args: unknown[]) => Promise<A5TenantModuleOptions> | A5TenantModuleOptions;
18
+ /**
19
+ * 需要注入到 useFactory 函数中的依赖项
20
+ */
21
+ inject?: unknown[];
22
+ /**
23
+ * 需要导入的模块列表
24
+ */
25
+ imports?: unknown[];
26
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=module.js.map
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @public
3
+ */
4
+ export interface A5TenantGuardConstructorOptions {
5
+ /**
6
+ * 是否启用空白自动转换为默认租户功能
7
+ *
8
+ * @defaultValue true
9
+ */
10
+ emptyToDefault?: boolean;
11
+ /**
12
+ * 默认租户 ID
13
+ *
14
+ * @defaultValue '-'
15
+ */
16
+ defaultTenantId?: string;
17
+ }
18
+ /**
19
+ * @public
20
+ */
21
+ export type A5TenantGuardOptions = Required<A5TenantGuardConstructorOptions>;
22
+ /**
23
+ * @public
24
+ */
25
+ export interface A5TenantPayload {
26
+ /**
27
+ * 租户 ID
28
+ */
29
+ tenantId: string;
30
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=tenant.js.map
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@hz-9/a5-tenant",
3
+ "version": "0.2.0-alpha.33",
4
+ "description": "Tenant module for the @hz-9/a5-* series of repositories.",
5
+ "keywords": [
6
+ "nest",
7
+ "a5",
8
+ "a5-tenant",
9
+ "tenant"
10
+ ],
11
+ "homepage": "https://hz-9.github.io/a5/guide/a5-tenant",
12
+ "bugs": {
13
+ "url": "https://github.com/hz-9/a5/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/hz-9/a5.git",
18
+ "directory": "library/tenant"
19
+ },
20
+ "license": "MIT",
21
+ "author": "Chen Zhen <heavenzhen999@163.com>",
22
+ "exports": {
23
+ ".": {
24
+ "require": "./dist/index.js",
25
+ "types": "./dist/index.d.ts"
26
+ },
27
+ "./*": {
28
+ "require": "./dist/*.js",
29
+ "types": "./dist/*.d.ts"
30
+ }
31
+ },
32
+ "main": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "files": [
35
+ "dist/**/*.{d.ts,js}"
36
+ ],
37
+ "devDependencies": {
38
+ "@hz-9/eslint-config-airbnb-ts": "~0.6.0",
39
+ "@nestjs/cli": "^10.0.0",
40
+ "@nestjs/common": "^10.0.0",
41
+ "@nestjs/core": "^10.0.0",
42
+ "@nestjs/testing": "^10.0.0",
43
+ "@rushstack/heft": "0.66.1",
44
+ "@types/heft-jest": "~1.0.3",
45
+ "@types/node": "~20.3.1",
46
+ "eslint": "^8.2.0",
47
+ "reflect-metadata": "^0.1.13",
48
+ "rxjs": "^7.8.1",
49
+ "ts-node": "^10.9.1",
50
+ "typescript": ">=5.0.0 <5.4.0",
51
+ "@hz-9/a5-core": "0.2.0-alpha.33",
52
+ "@hz-9/heft-nest-rig": "0.1.2"
53
+ },
54
+ "peerDependencies": {
55
+ "@nestjs/common": "^10.0.0",
56
+ "@nestjs/core": "^10.0.0",
57
+ "reflect-metadata": "^0.1.13",
58
+ "rxjs": "^7.8.1",
59
+ "@hz-9/a5-core": "0.2.0-alpha.33"
60
+ },
61
+ "publishConfig": {
62
+ "access": "public"
63
+ },
64
+ "scripts": {
65
+ "build": "heft test-all --clean",
66
+ "lint": "heft lint",
67
+ "test": "heft test --clean",
68
+ "test:watch": "heft test-watch --clean"
69
+ }
70
+ }