@venturekit/tenancy 0.0.0-dev.20260307234057

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.
Files changed (46) hide show
  1. package/LICENSE +191 -0
  2. package/dist/context/index.d.ts +8 -0
  3. package/dist/context/index.d.ts.map +1 -0
  4. package/dist/context/index.js +8 -0
  5. package/dist/context/index.js.map +1 -0
  6. package/dist/context/resolver.d.ts +22 -0
  7. package/dist/context/resolver.d.ts.map +1 -0
  8. package/dist/context/resolver.js +116 -0
  9. package/dist/context/resolver.js.map +1 -0
  10. package/dist/context/tenant-context.d.ts +46 -0
  11. package/dist/context/tenant-context.d.ts.map +1 -0
  12. package/dist/context/tenant-context.js +88 -0
  13. package/dist/context/tenant-context.js.map +1 -0
  14. package/dist/index.d.ts +10 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +13 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/middleware/index.d.ts +6 -0
  19. package/dist/middleware/index.d.ts.map +1 -0
  20. package/dist/middleware/index.js +6 -0
  21. package/dist/middleware/index.js.map +1 -0
  22. package/dist/middleware/quota-middleware.d.ts +48 -0
  23. package/dist/middleware/quota-middleware.d.ts.map +1 -0
  24. package/dist/middleware/quota-middleware.js +63 -0
  25. package/dist/middleware/quota-middleware.js.map +1 -0
  26. package/dist/middleware/tenant-middleware.d.ts +51 -0
  27. package/dist/middleware/tenant-middleware.d.ts.map +1 -0
  28. package/dist/middleware/tenant-middleware.js +74 -0
  29. package/dist/middleware/tenant-middleware.js.map +1 -0
  30. package/dist/types/config.d.ts +69 -0
  31. package/dist/types/config.d.ts.map +1 -0
  32. package/dist/types/config.js +5 -0
  33. package/dist/types/config.js.map +1 -0
  34. package/dist/types/index.d.ts +7 -0
  35. package/dist/types/index.d.ts.map +1 -0
  36. package/dist/types/index.js +7 -0
  37. package/dist/types/index.js.map +1 -0
  38. package/dist/types/isolation.d.ts +61 -0
  39. package/dist/types/isolation.d.ts.map +1 -0
  40. package/dist/types/isolation.js +5 -0
  41. package/dist/types/isolation.js.map +1 -0
  42. package/dist/types/tenant.d.ts +129 -0
  43. package/dist/types/tenant.d.ts.map +1 -0
  44. package/dist/types/tenant.js +5 -0
  45. package/dist/types/tenant.js.map +1 -0
  46. package/package.json +39 -0
package/LICENSE ADDED
@@ -0,0 +1,191 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to the Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by the Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding any notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2025 VentureKit Contributors
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ http://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Tenant Context
3
+ *
4
+ * Utilities for managing tenant context in requests.
5
+ */
6
+ export { TenantContext, createTenantContext, getCurrentTenant } from './tenant-context';
7
+ export { resolveTenant } from './resolver';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACxF,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Tenant Context
3
+ *
4
+ * Utilities for managing tenant context in requests.
5
+ */
6
+ export { createTenantContext, getCurrentTenant } from './tenant-context';
7
+ export { resolveTenant } from './resolver';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAiB,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACxF,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Tenant Resolver
3
+ *
4
+ * Resolves tenant from request based on configured strategy.
5
+ */
6
+ import type { IsolationConfig } from '../types';
7
+ /**
8
+ * Request-like object for tenant resolution
9
+ */
10
+ export interface TenantRequest {
11
+ headers?: Record<string, string | string[] | undefined>;
12
+ url?: string;
13
+ hostname?: string;
14
+ query?: Record<string, string | string[] | undefined>;
15
+ path?: string;
16
+ claims?: Record<string, unknown>;
17
+ }
18
+ /**
19
+ * Resolve tenant ID from request
20
+ */
21
+ export declare function resolveTenant(request: TenantRequest, config: IsolationConfig): Promise<string | null>;
22
+ //# sourceMappingURL=resolver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolver.d.ts","sourceRoot":"","sources":["../../src/context/resolver.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAsB,MAAM,UAAU,CAAC;AAEpE;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;IACxD,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;IACtD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAED;;GAEG;AACH,wBAAsB,aAAa,CACjC,OAAO,EAAE,aAAa,EACtB,MAAM,EAAE,eAAe,GACtB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAiBxB"}
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Tenant Resolver
3
+ *
4
+ * Resolves tenant from request based on configured strategy.
5
+ */
6
+ /**
7
+ * Resolve tenant ID from request
8
+ */
9
+ export async function resolveTenant(request, config) {
10
+ // Check if route is public
11
+ if (config.publicRoutes && request.path) {
12
+ const isPublic = config.publicRoutes.some(route => {
13
+ if (route.endsWith('*')) {
14
+ return request.path.startsWith(route.slice(0, -1));
15
+ }
16
+ return request.path === route;
17
+ });
18
+ if (isPublic) {
19
+ return config.defaultTenantId || null;
20
+ }
21
+ }
22
+ const tenantId = await resolveByStrategy(request, config);
23
+ return tenantId || config.defaultTenantId || null;
24
+ }
25
+ /**
26
+ * Resolve tenant by configured strategy
27
+ */
28
+ async function resolveByStrategy(request, config) {
29
+ switch (config.resolution) {
30
+ case 'subdomain':
31
+ return resolveFromSubdomain(request);
32
+ case 'custom_domain':
33
+ return resolveFromCustomDomain(request, config.domainLookup);
34
+ case 'path':
35
+ return resolveFromPath(request);
36
+ case 'header':
37
+ return resolveFromHeader(request, config.headerName || 'X-Tenant-ID');
38
+ case 'query':
39
+ return resolveFromQuery(request, config.queryParam || 'tenant');
40
+ case 'jwt':
41
+ return resolveFromJwt(request, config.jwtClaim || 'tenant_id');
42
+ case 'custom':
43
+ if (config.customResolver) {
44
+ return config.customResolver(request);
45
+ }
46
+ return null;
47
+ default:
48
+ return null;
49
+ }
50
+ }
51
+ /**
52
+ * Resolve from subdomain (tenant.example.com)
53
+ */
54
+ function resolveFromSubdomain(request) {
55
+ const hostname = request.hostname || '';
56
+ const parts = hostname.split('.');
57
+ // Need at least 3 parts (tenant.example.com)
58
+ if (parts.length < 3)
59
+ return null;
60
+ // Skip www
61
+ const subdomain = parts[0];
62
+ if (subdomain === 'www')
63
+ return null;
64
+ return subdomain;
65
+ }
66
+ /**
67
+ * Resolve from path (example.com/tenant/...)
68
+ */
69
+ function resolveFromPath(request) {
70
+ const path = request.path || request.url || '';
71
+ const parts = path.split('/').filter(Boolean);
72
+ if (parts.length === 0)
73
+ return null;
74
+ return parts[0];
75
+ }
76
+ /**
77
+ * Resolve from header
78
+ */
79
+ function resolveFromHeader(request, headerName) {
80
+ const headers = request.headers || {};
81
+ const value = headers[headerName.toLowerCase()] || headers[headerName];
82
+ if (Array.isArray(value))
83
+ return value[0] || null;
84
+ return value || null;
85
+ }
86
+ /**
87
+ * Resolve from query parameter
88
+ */
89
+ function resolveFromQuery(request, paramName) {
90
+ const query = request.query || {};
91
+ const value = query[paramName];
92
+ if (Array.isArray(value))
93
+ return value[0] || null;
94
+ return value || null;
95
+ }
96
+ /**
97
+ * Resolve from JWT claims
98
+ */
99
+ function resolveFromJwt(request, claimName) {
100
+ const claims = request.claims || {};
101
+ const value = claims[claimName];
102
+ if (typeof value === 'string')
103
+ return value;
104
+ return null;
105
+ }
106
+ /**
107
+ * Resolve from custom domain (white-label)
108
+ * Looks up tenant ID by the full hostname
109
+ */
110
+ async function resolveFromCustomDomain(request, domainLookup) {
111
+ const hostname = request.hostname || '';
112
+ if (!hostname || !domainLookup)
113
+ return null;
114
+ return domainLookup(hostname);
115
+ }
116
+ //# sourceMappingURL=resolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolver.js","sourceRoot":"","sources":["../../src/context/resolver.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAgBH;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,OAAsB,EACtB,MAAuB;IAEvB,2BAA2B;IAC3B,IAAI,MAAM,CAAC,YAAY,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;QACxC,MAAM,QAAQ,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;YAChD,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;gBACxB,OAAO,OAAO,CAAC,IAAK,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;YACtD,CAAC;YACD,OAAO,OAAO,CAAC,IAAI,KAAK,KAAK,CAAC;QAChC,CAAC,CAAC,CAAC;QACH,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,MAAM,CAAC,eAAe,IAAI,IAAI,CAAC;QACxC,CAAC;IACH,CAAC;IAED,MAAM,QAAQ,GAAG,MAAM,iBAAiB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAE1D,OAAO,QAAQ,IAAI,MAAM,CAAC,eAAe,IAAI,IAAI,CAAC;AACpD,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,iBAAiB,CAC9B,OAAsB,EACtB,MAAuB;IAEvB,QAAQ,MAAM,CAAC,UAAU,EAAE,CAAC;QAC1B,KAAK,WAAW;YACd,OAAO,oBAAoB,CAAC,OAAO,CAAC,CAAC;Q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CAAC,QAAQ,IAAI,EAAE,CAAC;IACxC,IAAI,CAAC,QAAQ,IAAI,CAAC,YAAY;QAAE,OAAO,IAAI,CAAC;IAE5C,OAAO,YAAY,CAAC,QAAQ,CAAC,CAAC;AAChC,CAAC"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Tenant Context
3
+ *
4
+ * Manages tenant context for the current request.
5
+ */
6
+ import type { Tenant, TenantMember } from '../types';
7
+ /**
8
+ * Tenant context for current request
9
+ */
10
+ export interface TenantContext {
11
+ /** Current tenant */
12
+ tenant: Tenant;
13
+ /** Current user's membership (if authenticated) */
14
+ membership?: TenantMember;
15
+ /** Is this a system/admin context */
16
+ isSystem?: boolean;
17
+ }
18
+ /**
19
+ * Create tenant context for a request
20
+ */
21
+ export declare function createTenantContext(tenant: Tenant, membership?: TenantMember): TenantContext;
22
+ /**
23
+ * Get current tenant from context
24
+ */
25
+ export declare function getCurrentTenant(): Tenant | null;
26
+ /**
27
+ * Get current tenant context
28
+ */
29
+ export declare function getTenantContext(): TenantContext | null;
30
+ /**
31
+ * Run function with tenant context
32
+ */
33
+ export declare function withTenantContext<T>(context: TenantContext, fn: () => T | Promise<T>): Promise<T>;
34
+ /**
35
+ * Run function with system context (bypasses tenant checks)
36
+ */
37
+ export declare function withSystemContext<T>(fn: () => T | Promise<T>): Promise<T>;
38
+ /**
39
+ * Assert tenant context exists
40
+ */
41
+ export declare function requireTenantContext(): TenantContext;
42
+ /**
43
+ * Assert current tenant exists
44
+ */
45
+ export declare function requireTenant(): Tenant;
46
+ //# sourceMappingURL=tenant-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tenant-context.d.ts","sourceRoot":"","sources":["../../src/context/tenant-context.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,qBAAqB;IACrB,MAAM,EAAE,MAAM,CAAC;IAEf,mDAAmD;IACnD,UAAU,CAAC,EAAE,YAAY,CAAC;IAE1B,qCAAqC;IACrC,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAgBD;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,YAAY,GAAG,aAAa,CAM5F;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,MAAM,GAAG,IAAI,CAGhD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,aAAa,GAAG,IAAI,CAEvD;AAED;;GAEG;AACH,wBAAsB,iBAAiB,CAAC,CAAC,EACvC,OAAO,EAAE,aAAa,EACtB,EAAE,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GACvB,OAAO,CAAC,CAAC,CAAC,CAGZ;AAED;;GAEG;AACH,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAgB/E;AAED;;GAEG;AACH,wBAAgB,oBAAoB,IAAI,aAAa,CAMpD;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAMtC"}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Tenant Context
3
+ *
4
+ * Manages tenant context for the current request.
5
+ */
6
+ // AsyncLocalStorage for tenant context (Node.js)
7
+ let asyncLocalStorage = null;
8
+ /**
9
+ * Initialize async local storage (call once at app startup)
10
+ */
11
+ async function initAsyncLocalStorage() {
12
+ if (!asyncLocalStorage) {
13
+ const { AsyncLocalStorage } = await import('node:async_hooks');
14
+ asyncLocalStorage = new AsyncLocalStorage();
15
+ }
16
+ return asyncLocalStorage;
17
+ }
18
+ /**
19
+ * Create tenant context for a request
20
+ */
21
+ export function createTenantContext(tenant, membership) {
22
+ return {
23
+ tenant,
24
+ membership,
25
+ isSystem: false,
26
+ };
27
+ }
28
+ /**
29
+ * Get current tenant from context
30
+ */
31
+ export function getCurrentTenant() {
32
+ const store = asyncLocalStorage?.getStore();
33
+ return store?.tenant || null;
34
+ }
35
+ /**
36
+ * Get current tenant context
37
+ */
38
+ export function getTenantContext() {
39
+ return asyncLocalStorage?.getStore() || null;
40
+ }
41
+ /**
42
+ * Run function with tenant context
43
+ */
44
+ export async function withTenantContext(context, fn) {
45
+ const storage = await initAsyncLocalStorage();
46
+ return storage.run(context, fn);
47
+ }
48
+ /**
49
+ * Run function with system context (bypasses tenant checks)
50
+ */
51
+ export async function withSystemContext(fn) {
52
+ const storage = await initAsyncLocalStorage();
53
+ const systemContext = {
54
+ tenant: {
55
+ id: 'system',
56
+ name: 'System',
57
+ slug: 'system',
58
+ status: 'active',
59
+ plan: 'enterprise',
60
+ ownerId: 'system',
61
+ createdAt: new Date(),
62
+ updatedAt: new Date(),
63
+ },
64
+ isSystem: true,
65
+ };
66
+ return storage.run(systemContext, fn);
67
+ }
68
+ /**
69
+ * Assert tenant context exists
70
+ */
71
+ export function requireTenantContext() {
72
+ const context = getTenantContext();
73
+ if (!context) {
74
+ throw new Error('Tenant context required but not found');
75
+ }
76
+ return context;
77
+ }
78
+ /**
79
+ * Assert current tenant exists
80
+ */
81
+ export function requireTenant() {
82
+ const tenant = getCurrentTenant();
83
+ if (!tenant) {
84
+ throw new Error('Tenant required but not found in context');
85
+ }
86
+ return tenant;
87
+ }
88
+ //# sourceMappingURL=tenant-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tenant-context.js","sourceRoot":"","sources":["../../src/context/tenant-context.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAkBH,iDAAiD;AACjD,IAAI,iBAAiB,GAA2G,IAAI,CAAC;AAErI;;GAEG;AACH,KAAK,UAAU,qBAAqB;IAClC,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,MAAM,EAAE,iBAAiB,EAAE,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;QAC/D,iBAAiB,GAAG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IAClC,MAAM,OAAO,GAAG,gBAAgB,EAAE,CAAC;IACnC,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa;IAC3B,MAAM,MAAM,GAAG,gBAAgB,EAAE,CAAC;IAClC,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAC9D,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @venturekit/tenancy
3
+ *
4
+ * Multi-tenant utilities for VentureKit.
5
+ * Documentation: https://venturekit.dev
6
+ */
7
+ export * from './types';
8
+ export { TenantContext, createTenantContext, getCurrentTenant, resolveTenant, } from './context';
9
+ export { createTenantMiddleware, createQuotaMiddleware, checkQuotas, TenantNotFoundError, TenantSuspendedError, TenantInactiveError, QuotaExceededError, } from './middleware';
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,cAAc,SAAS,CAAC;AAGxB,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EAChB,aAAa,GACd,MAAM,WAAW,CAAC;AAGnB,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACrB,WAAW,EACX,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @venturekit/tenancy
3
+ *
4
+ * Multi-tenant utilities for VentureKit.
5
+ * Documentation: https://venturekit.dev
6
+ */
7
+ // Types
8
+ export * from './types';
9
+ // Context
10
+ export { createTenantContext, getCurrentTenant, resolveTenant, } from './context';
11
+ // Middleware
12
+ export { createTenantMiddleware, createQuotaMiddleware, checkQuotas, TenantNotFoundError, TenantSuspendedError, TenantInactiveError, QuotaExceededError, } from './middleware';
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,QAAQ;AACR,cAAc,SAAS,CAAC;AAExB,UAAU;AACV,OAAO,EAEL,mBAAmB,EACnB,gBAAgB,EAChB,aAAa,GACd,MAAM,WAAW,CAAC;AAEnB,aAAa;AACb,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACrB,WAAW,EACX,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,cAAc,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Tenancy Middleware
3
+ */
4
+ export { createTenantMiddleware, TenantNotFoundError, TenantSuspendedError, TenantInactiveError, } from './tenant-middleware';
5
+ export { createQuotaMiddleware, QuotaExceededError, checkQuotas } from './quota-middleware';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,sBAAsB,EACtB,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,GACpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Tenancy Middleware
3
+ */
4
+ export { createTenantMiddleware, TenantNotFoundError, TenantSuspendedError, TenantInactiveError, } from './tenant-middleware';
5
+ export { createQuotaMiddleware, QuotaExceededError, checkQuotas } from './quota-middleware';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,sBAAsB,EACtB,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,GACpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Quota Middleware
3
+ *
4
+ * Middleware for enforcing tenant quotas.
5
+ */
6
+ import type { TenantQuotas } from '../types';
7
+ /**
8
+ * Quota check function type
9
+ */
10
+ export type QuotaCheck = (tenantId: string, quotaKey: string) => Promise<{
11
+ current: number;
12
+ limit: number;
13
+ }>;
14
+ /**
15
+ * Quota middleware options
16
+ */
17
+ export interface QuotaMiddlewareOptions {
18
+ /** Function to check current usage */
19
+ checkUsage: QuotaCheck;
20
+ /** Quota key to check */
21
+ quotaKey: keyof TenantQuotas;
22
+ /** Called when quota exceeded */
23
+ onQuotaExceeded?: (tenantId: string, current: number, limit: number) => void;
24
+ }
25
+ /**
26
+ * Create quota enforcement middleware
27
+ */
28
+ export declare function createQuotaMiddleware(options: QuotaMiddlewareOptions): <T>(next: () => Promise<T>) => Promise<T>;
29
+ /**
30
+ * Quota exceeded error
31
+ */
32
+ export declare class QuotaExceededError extends Error {
33
+ tenantId: string;
34
+ quotaKey: string;
35
+ current: number;
36
+ limit: number;
37
+ constructor(tenantId: string, quotaKey: string, current: number, limit: number);
38
+ }
39
+ /**
40
+ * Check multiple quotas at once
41
+ */
42
+ export declare function checkQuotas(tenantId: string, quotas: TenantQuotas, checkUsage: QuotaCheck): Promise<{
43
+ key: string;
44
+ current: number;
45
+ limit: number;
46
+ exceeded: boolean;
47
+ }[]>;
48
+ //# sourceMappingURL=quota-middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quota-middleware.d.ts","sourceRoot":"","sources":["../../src/middleware/quota-middleware.ts"],"names":[],"mappings":"AAAA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
2
+ * Quota Middleware
3
+ *
4
+ * Middleware for enforcing tenant quotas.
5
+ */
6
+ import { requireTenant } from '../context/tenant-context';
7
+ /**
8
+ * Create quota enforcement middleware
9
+ */
10
+ export function createQuotaMiddleware(options) {
11
+ const { checkUsage, quotaKey, onQuotaExceeded } = options;
12
+ return async function quotaMiddleware(next) {
13
+ const tenant = requireTenant();
14
+ const limit = tenant.quotas?.[quotaKey];
15
+ // No limit set, allow
16
+ if (limit === undefined) {
17
+ return next();
18
+ }
19
+ // Check current usage
20
+ const { current } = await checkUsage(tenant.id, quotaKey);
21
+ if (current >= limit) {
22
+ onQuotaExceeded?.(tenant.id, current, limit);
23
+ throw new QuotaExceededError(tenant.id, quotaKey, current, limit);
24
+ }
25
+ return next();
26
+ };
27
+ }
28
+ /**
29
+ * Quota exceeded error
30
+ */
31
+ export class QuotaExceededError extends Error {
32
+ tenantId;
33
+ quotaKey;
34
+ current;
35
+ limit;
36
+ constructor(tenantId, quotaKey, current, limit) {
37
+ super(`Quota exceeded for ${quotaKey}: ${current}/${limit}`);
38
+ this.tenantId = tenantId;
39
+ this.quotaKey = quotaKey;
40
+ this.current = current;
41
+ this.limit = limit;
42
+ this.name = 'QuotaExceededError';
43
+ }
44
+ }
45
+ /**
46
+ * Check multiple quotas at once
47
+ */
48
+ export async function checkQuotas(tenantId, quotas, checkUsage) {
49
+ const results = [];
50
+ for (const [key, limit] of Object.entries(quotas)) {
51
+ if (limit === undefined)
52
+ continue;
53
+ const { current } = await checkUsage(tenantId, key);
54
+ results.push({
55
+ key,
56
+ current,
57
+ limit,
58
+ exceeded: current >= limit,
59
+ });
60
+ }
61
+ return results;
62
+ }
63
+ //# sourceMappingURL=quota-middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quota-middleware.js","sourceRoot":"","sources":["../../src/middleware/quota-middleware.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAwB1D;;GA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AgB,EAChB,MAAoB,EACpB,UAAsB;IAEtB,MAAM,OAAO,GAAG,EAAE,CAAC;IAEnB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAClD,IAAI,KAAK,KAAK,SAAS;YAAE,SAAS;QAElC,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,UAAU,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;QACpD,OAAO,CAAC,IAAI,CAAC;YACX,GAAG;YACH,OAAO;YACP,KAAK;YACL,QAAQ,EAAE,OAAO,IAAI,KAAK;SAC3B,CAAC,CAAC;IACL,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Tenant Middleware
3
+ *
4
+ * Middleware for resolving and setting tenant context.
5
+ */
6
+ import type { IsolationConfig, Tenant } from '../types';
7
+ import { TenantRequest } from '../context/resolver';
8
+ /**
9
+ * Tenant lookup function type
10
+ */
11
+ export type TenantLookup = (tenantId: string) => Promise<Tenant | null>;
12
+ /**
13
+ * Tenant middleware options
14
+ */
15
+ export interface TenantMiddlewareOptions {
16
+ /** Isolation config */
17
+ config: IsolationConfig;
18
+ /** Function to lookup tenant by ID */
19
+ lookupTenant: TenantLookup;
20
+ /** Called when tenant not found */
21
+ onTenantNotFound?: (tenantId: string) => void;
22
+ /** Called when tenant is suspended */
23
+ onTenantSuspended?: (tenant: Tenant) => void;
24
+ }
25
+ /**
26
+ * Create tenant middleware
27
+ */
28
+ export declare function createTenantMiddleware(options: TenantMiddlewareOptions): <T>(request: TenantRequest, next: () => Promise<T>) => Promise<T>;
29
+ /**
30
+ * Tenant not found error
31
+ */
32
+ export declare class TenantNotFoundError extends Error {
33
+ tenantId: string;
34
+ constructor(tenantId: string);
35
+ }
36
+ /**
37
+ * Tenant suspended error
38
+ */
39
+ export declare class TenantSuspendedError extends Error {
40
+ tenantId: string;
41
+ constructor(tenantId: string);
42
+ }
43
+ /**
44
+ * Tenant inactive error
45
+ */
46
+ export declare class TenantInactiveError extends Error {
47
+ tenantId: string;
48
+ status: string;
49
+ constructor(tenantId: string, status: string);
50
+ }
51
+ //# sourceMappingURL=tenant-middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tenant-middleware.d.ts","sourceRoot":"","sources":["../../src/middleware/tenant-middleware.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAiB,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGnE;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;AAExE;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,uBAAuB;IACvB,MAAM,EAAE,eAAe,CAAC;IAExB,sCAAsC;IACtC,YAAY,EAAE,YAAY,CAAC;IAE3B,mCAAmC;IACnC,gBAAgB,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IAE9C,sCAAsC;IACtC,iBAAiB,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9C;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,uBAAuB,IAG9B,CAAC,EACtC,SAAS,aAAa,EACtB,MAAM,MAAM,OAAO,CAAC,CAAC,CAAC,KACrB,OAAO,CAAC,CAAC,CAAC,CA+Bd;AAED;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;IACzB,QAAQ,EAAE,MAAM;gBAAhB,QAAQ,EAAE,MAAM;CAIpC;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;IAC1B,QAAQ,EAAE,MAAM;gBAAhB,QAAQ,EAAE,MAAM;CAIpC;AAED;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;IACzB,QAAQ,EAAE,MAAM;IAAS,MAAM,EAAE,MAAM;gBAAvC,QAAQ,EAAE,MAAM,EAAS,MAAM,EAAE,MAAM;CAI3D"}
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Tenant Middleware
3
+ *
4
+ * Middleware for resolving and setting tenant context.
5
+ */
6
+ import { resolveTenant } from '../context/resolver';
7
+ import { createTenantContext, withTenantContext } from '../context/tenant-context';
8
+ /**
9
+ * Create tenant middleware
10
+ */
11
+ export function createTenantMiddleware(options) {
12
+ const { config, lookupTenant, onTenantNotFound, onTenantSuspended } = options;
13
+ return async function tenantMiddleware(request, next) {
14
+ // Resolve tenant ID from request
15
+ const tenantId = await resolveTenant(request, config);
16
+ if (!tenantId) {
17
+ // No tenant required (public route or default)
18
+ return next();
19
+ }
20
+ // Lookup tenant
21
+ const tenant = await lookupTenant(tenantId);
22
+ if (!tenant) {
23
+ onTenantNotFound?.(tenantId);
24
+ throw new TenantNotFoundError(tenantId);
25
+ }
26
+ // Check tenant status
27
+ if (tenant.status === 'suspended') {
28
+ onTenantSuspended?.(tenant);
29
+ throw new TenantSuspendedError(tenant.id);
30
+ }
31
+ if (tenant.status !== 'active') {
32
+ throw new TenantInactiveError(tenant.id, tenant.status);
33
+ }
34
+ // Create context and run handler
35
+ const context = createTenantContext(tenant);
36
+ return withTenantContext(context, next);
37
+ };
38
+ }
39
+ /**
40
+ * Tenant not found error
41
+ */
42
+ export class TenantNotFoundError extends Error {
43
+ tenantId;
44
+ constructor(tenantId) {
45
+ super(`Tenant not found: ${tenantId}`);
46
+ this.tenantId = tenantId;
47
+ this.name = 'TenantNotFoundError';
48
+ }
49
+ }
50
+ /**
51
+ * Tenant suspended error
52
+ */
53
+ export class TenantSuspendedError extends Error {
54
+ tenantId;
55
+ constructor(tenantId) {
56
+ super(`Tenant suspended: ${tenantId}`);
57
+ this.tenantId = tenantId;
58
+ this.name = 'TenantSuspendedError';
59
+ }
60
+ }
61
+ /**
62
+ * Tenant inactive error
63
+ */
64
+ export class TenantInactiveError extends Error {
65
+ tenantId;
66
+ status;
67
+ constructor(tenantId, status) {
68
+ super(`Tenant inactive (${status}): ${tenantId}`);
69
+ this.tenantId = tenantId;
70
+ this.status = status;
71
+ this.name = 'TenantInactiveError';
72
+ }
73
+ }
74
+ //# sourceMappingURL=tenant-middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tenant-middleware.js","sourceRoot":"","sources":["../../src/middleware/tenant-middleware.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,aAAa,EAAiB,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAwBnF;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,OAAgC;IACrE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC;IAE9E,OAAO,KAAK,UAAU,gBAAgB,CACpC,OAAsB,EACtB,IAAsB;QAEtB,iCAAiC;QACjC,MAAM,QAAQ,GAAG,MAAM,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAEtD,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,+CAA+C;YAC/C,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,gBAAgB;QAChB,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,QAAQ,CAAC,CAAC;QAE5C,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,gBAAgB,EAAE,CAAC,QAAQ,CAAC,CAAC;YAC7B,MAAM,IAAI,mBAAmB,CAAC,QAAQ,CAAC,CAAC;QAC1C,CAAC;QAED,sBAAsB;QACtB,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;YAClC,iBAAiB,EAAE,CAAC,MAAM,CAAC,CAAC;YAC5B,MAAM,IAAI,oBAAoB,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAC5C,CAAC;QAED,IAAI,MAAM,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC/B,MAAM,IAAI,mBAAmB,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;QAC1D,CAAC;QAED,iCAAiC;QACjC,MAAM,OAAO,GAAG,mBAAmB,CAAC,MAAM,CAAC,CAAC;QAC5C,OAAO,iBAAiB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC1C,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACzB;IAAnB,YAAmB,QAAgB;QACjC,KAAK,CAAC,qBAAqB,QAAQ,EAAE,CAAC,CAAC;QADtB,aAAQ,GAAR,QAAQ,CAAQ;QAEjC,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC1B;IAAnB,YAAmB,QAAgB;QACjC,KAAK,CAAC,qBAAqB,QAAQ,EAAE,CAAC,CAAC;QADtB,aAAQ,GAAR,QAAQ,CAAQ;QAEjC,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACzB;IAAyB;IAA5C,YAAmB,QAAgB,EAAS,MAAc;QACxD,KAAK,CAAC,oBAAoB,MAAM,MAAM,QAAQ,EAAE,CAAC,CAAC;QADjC,aAAQ,GAAR,QAAQ,CAAQ;QAAS,WAAM,GAAN,MAAM,CAAQ;QAExD,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF"}
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Tenancy Configuration Types
3
+ */
4
+ import type { IsolationConfig, DatabaseIsolationConfig, StorageIsolationConfig } from './isolation';
5
+ import type { TenantQuotas, TenantPlan } from './tenant';
6
+ /**
7
+ * Plan configuration
8
+ */
9
+ export interface PlanConfig {
10
+ /** Plan ID */
11
+ id: TenantPlan;
12
+ /** Plan name */
13
+ name: string;
14
+ /** Plan description */
15
+ description?: string;
16
+ /** Default quotas for this plan */
17
+ quotas: TenantQuotas;
18
+ /** Features enabled for this plan */
19
+ features?: Record<string, boolean>;
20
+ /** Monthly price (in cents) */
21
+ priceMonthly?: number;
22
+ /** Yearly price (in cents) */
23
+ priceYearly?: number;
24
+ }
25
+ /**
26
+ * Tenancy configuration
27
+ */
28
+ export interface TenancyConfig {
29
+ /** Enable multi-tenancy */
30
+ enabled: boolean;
31
+ /** Isolation configuration */
32
+ isolation: IsolationConfig;
33
+ /** Database isolation */
34
+ database?: DatabaseIsolationConfig;
35
+ /** Storage isolation */
36
+ storage?: StorageIsolationConfig;
37
+ /** Available plans */
38
+ plans?: PlanConfig[];
39
+ /** Default plan for new tenants */
40
+ defaultPlan?: TenantPlan;
41
+ /** Enforce quotas */
42
+ enforceQuotas?: boolean;
43
+ /** Allow custom domains */
44
+ allowCustomDomains?: boolean;
45
+ /** Require email verification for new tenants */
46
+ requireVerification?: boolean;
47
+ }
48
+ /**
49
+ * Tenancy config input (partial for overrides)
50
+ */
51
+ export interface TenancyConfigInput {
52
+ enabled?: boolean;
53
+ isolation?: Partial<IsolationConfig>;
54
+ database?: Partial<DatabaseIsolationConfig>;
55
+ storage?: Partial<StorageIsolationConfig>;
56
+ plans?: PlanConfig[];
57
+ defaultPlan?: TenantPlan;
58
+ enforceQuotas?: boolean;
59
+ allowCustomDomains?: boolean;
60
+ requireVerification?: boolean;
61
+ }
62
+ /**
63
+ * Tenancy environment config
64
+ */
65
+ export interface TenancyEnvConfig {
66
+ /** Base tenancy config */
67
+ tenancy: TenancyConfig;
68
+ }
69
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/types/config.ts"],"names":[],"mappings":"AAAA;;G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OAAO,CAAC,uBAAuB,CAAC,CAAC;IAC5C,OAAO,CAAC,EAAE,OAAO,CAAC,sBAAsB,CAAC,CAAC;IAC1C,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC;IACrB,WAAW,CAAC,EAAE,UAAU,CAAC;IACzB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,mBAAmB,CAAC,EAAE,OAAO,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,0BAA0B;IAC1B,OAAO,EAAE,aAAa,CAAC;CACxB"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Tenancy Configuration Types
3
+ */
4
+ export {};
5
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/types/config.ts"],"names":[],"mappings":"AAAA;;GAEG"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Tenancy Types
3
+ */
4
+ export * from './tenant';
5
+ export * from './isolation';
6
+ export * from './config';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Tenancy Types
3
+ */
4
+ export * from './tenant';
5
+ export * from './isolation';
6
+ export * from './config';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Isolation Types
3
+ */
4
+ /**
5
+ * Data isolation strategy
6
+ */
7
+ export type IsolationStrategy = 'shared' | 'schema' | 'database' | 'hybrid';
8
+ /**
9
+ * Tenant resolution strategy
10
+ */
11
+ export type ResolutionStrategy = 'subdomain' | 'custom_domain' | 'path' | 'header' | 'query' | 'jwt' | 'custom';
12
+ /**
13
+ * Isolation configuration
14
+ */
15
+ export interface IsolationConfig {
16
+ /** Data isolation strategy */
17
+ strategy: IsolationStrategy;
18
+ /** Tenant resolution strategy */
19
+ resolution: ResolutionStrategy;
20
+ /** Header name for header-based resolution */
21
+ headerName?: string;
22
+ /** Query param name for query-based resolution */
23
+ queryParam?: string;
24
+ /** JWT claim name for JWT-based resolution */
25
+ jwtClaim?: string;
26
+ /** Domain lookup function for custom_domain resolution */
27
+ domainLookup?: (domain: string) => Promise<string | null>;
28
+ /** Custom resolver function */
29
+ customResolver?: (request: unknown) => Promise<string | null>;
30
+ /** Default tenant ID (for public routes) */
31
+ defaultTenantId?: string;
32
+ /** Routes that don't require tenant context */
33
+ publicRoutes?: string[];
34
+ }
35
+ /**
36
+ * Database isolation config
37
+ */
38
+ export interface DatabaseIsolationConfig {
39
+ /** Isolation strategy */
40
+ strategy: IsolationStrategy;
41
+ /** Schema prefix for schema-based isolation */
42
+ schemaPrefix?: string;
43
+ /** Database name template for database-based isolation */
44
+ databaseTemplate?: string;
45
+ /** Connection pool per tenant */
46
+ poolPerTenant?: boolean;
47
+ /** Max connections per tenant pool */
48
+ maxConnectionsPerTenant?: number;
49
+ }
50
+ /**
51
+ * Storage isolation config
52
+ */
53
+ export interface StorageIsolationConfig {
54
+ /** Use separate S3 prefix per tenant */
55
+ prefixByTenant: boolean;
56
+ /** Prefix template (e.g., "tenants/{tenantId}/") */
57
+ prefixTemplate?: string;
58
+ /** Use separate bucket per tenant */
59
+ bucketPerTenant?: boolean;
60
+ }
61
+ //# sourceMappingURL=isolation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isolation.d.ts","sourceRoot":"","sources":["../../src/types/isolation.ts"],"names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C;IACxC,cAAc,EAAE,OAAO,CAAC;IAExB,oDAAoD;IACpD,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB,qCAAqC;IACrC,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Isolation Types
3
+ */
4
+ export {};
5
+ //# sourceMappingURL=isolation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isolation.js","sourceRoot":"","sources":["../../src/types/isolation.ts"],"names":[],"mappings":"AAAA;;GAEG"}
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Tenant Types
3
+ */
4
+ /**
5
+ * Tenant status
6
+ */
7
+ export type TenantStatus = 'active' | 'suspended' | 'pending' | 'deleted';
8
+ /**
9
+ * Tenant plan identifier
10
+ */
11
+ export type TenantPlan = string;
12
+ /**
13
+ * Tenant entity
14
+ */
15
+ export interface Tenant {
16
+ /** Unique tenant ID */
17
+ id: string;
18
+ /** Tenant name */
19
+ name: string;
20
+ /** Tenant slug (for subdomain/URL) */
21
+ slug: string;
22
+ /** Tenant status */
23
+ status: TenantStatus;
24
+ /** Subscription plan (project-specific) */
25
+ plan?: string;
26
+ /** Owner user ID */
27
+ ownerId: string;
28
+ /** Custom domain (optional) */
29
+ customDomain?: string;
30
+ /** Tenant settings */
31
+ settings?: TenantSettings;
32
+ /** Quota limits */
33
+ quotas?: TenantQuotas;
34
+ /** Metadata */
35
+ metadata?: Record<string, unknown>;
36
+ /** Created timestamp */
37
+ createdAt: Date;
38
+ /** Updated timestamp */
39
+ updatedAt: Date;
40
+ }
41
+ /**
42
+ * Tenant settings
43
+ */
44
+ export interface TenantSettings {
45
+ /** Locale/language */
46
+ locale?: string;
47
+ /** Timezone */
48
+ timezone?: string;
49
+ /** Branding */
50
+ branding?: {
51
+ logo?: string;
52
+ primaryColor?: string;
53
+ secondaryColor?: string;
54
+ };
55
+ /** Feature flags */
56
+ features?: Record<string, boolean>;
57
+ }
58
+ /**
59
+ * Tenant quotas/limits
60
+ */
61
+ export interface TenantQuotas {
62
+ /** Max users */
63
+ maxUsers?: number;
64
+ /** Max storage (bytes) */
65
+ maxStorage?: number;
66
+ /** Max API requests per month */
67
+ maxApiRequests?: number;
68
+ /** Max projects/resources */
69
+ maxProjects?: number;
70
+ /** Custom quotas */
71
+ [key: string]: number | undefined;
72
+ }
73
+ /**
74
+ * Tenant member
75
+ */
76
+ export interface TenantMember {
77
+ /** User ID */
78
+ userId: string;
79
+ /** Tenant ID */
80
+ tenantId: string;
81
+ /** Role within tenant (project-specific) */
82
+ role: string;
83
+ /** Joined timestamp */
84
+ joinedAt: Date;
85
+ }
86
+ /**
87
+ * Tenant invitation
88
+ */
89
+ export interface TenantInvitation {
90
+ /** Invitation ID */
91
+ id: string;
92
+ /** Tenant ID */
93
+ tenantId: string;
94
+ /** Invitee email */
95
+ email: string;
96
+ /** Assigned role */
97
+ role: string;
98
+ /** Invitation token */
99
+ token: string;
100
+ /** Expiry timestamp */
101
+ expiresAt: Date;
102
+ /** Created timestamp */
103
+ createdAt: Date;
104
+ }
105
+ /**
106
+ * Input for creating a tenant
107
+ */
108
+ export interface CreateTenantInput {
109
+ name: string;
110
+ slug: string;
111
+ ownerId: string;
112
+ plan?: string;
113
+ settings?: TenantSettings;
114
+ metadata?: Record<string, unknown>;
115
+ }
116
+ /**
117
+ * Input for updating a tenant
118
+ */
119
+ export interface UpdateTenantInput {
120
+ name?: string;
121
+ slug?: string;
122
+ status?: TenantStatus;
123
+ plan?: string;
124
+ customDomain?: string;
125
+ settings?: Partial<TenantSettings>;
126
+ quotas?: Partial<TenantQuotas>;
127
+ metadata?: Record<string, unknown>;
128
+ }
129
+ //# sourceMappingURL=tenant.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tenant.d.ts","sourceRoot":"","sources":["../../src/types/tenant.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,WAAW,GAAG,SAAS,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CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;IACnC,MAAM,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Tenant Types
3
+ */
4
+ export {};
5
+ //# sourceMappingURL=tenant.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tenant.js","sourceRoot":"","sources":["../../src/types/tenant.ts"],"names":[],"mappings":"AAAA;;GAEG"}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@venturekit/tenancy",
3
+ "version": "0.0.0-dev.20260307234057",
4
+ "description": "Multi-tenant utilities for VentureKit",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/venturekit-dev/venturekit.private.git",
14
+ "directory": "packages/tenancy"
15
+ },
16
+ "publishConfig": {
17
+ "registry": "https://registry.npmjs.org",
18
+ "access": "public"
19
+ },
20
+ "license": "Apache-2.0",
21
+ "exports": {
22
+ ".": {
23
+ "import": "./dist/index.js",
24
+ "types": "./dist/index.d.ts"
25
+ }
26
+ },
27
+ "dependencies": {
28
+ "@venturekit/core": "0.0.0-dev.20260307234057"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^20.10.0",
32
+ "typescript": "^5.3.0"
33
+ },
34
+ "scripts": {
35
+ "build": "tsc",
36
+ "dev": "tsc --watch",
37
+ "clean": "rm -rf dist"
38
+ }
39
+ }