@usebetterdev/tenant-express 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 usebetter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @better-tenant/express
2
+
3
+ Express middleware helpers for Better Tenant.
package/dist/index.cjs ADDED
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createExpressMiddleware: () => createExpressMiddleware
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ var import_tenant_core = require("@usebetterdev/tenant-core");
27
+ function createExpressMiddleware(tenant, options = {}) {
28
+ const missingTenantStatus = options.missingTenantStatus ?? 404;
29
+ return async (request, response, next) => {
30
+ try {
31
+ await tenant.handleRequest(
32
+ (0, import_tenant_core.toResolvableRequest)(request),
33
+ async () => {
34
+ next();
35
+ },
36
+ {
37
+ onMissingTenant: async () => {
38
+ if (options.onMissingTenant) {
39
+ await options.onMissingTenant(request, response);
40
+ return;
41
+ }
42
+ response.status(missingTenantStatus).json({ error: "Tenant not found" });
43
+ }
44
+ }
45
+ );
46
+ } catch (error) {
47
+ next(error);
48
+ }
49
+ };
50
+ }
51
+ // Annotate the CommonJS export names for ESM import in node:
52
+ 0 && (module.exports = {
53
+ createExpressMiddleware
54
+ });
55
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { toResolvableRequest } from \"@usebetterdev/tenant-core\";\nimport type {\n BetterTenantInstance,\n NodeLikeRequest,\n ResolvableRequest,\n TenantContext,\n TenantScopedDatabase,\n} from \"@usebetterdev/tenant-core\";\n\nexport interface ExpressRequestLike extends NodeLikeRequest {\n headers: Record<string, string | string[] | number | undefined>;\n originalUrl?: string;\n hostname?: string;\n}\n\nexport interface ExpressResponseLike {\n status: (code: number) => ExpressResponseLike;\n json: (body: unknown) => unknown;\n}\n\nexport type ExpressNextFunction = (error?: unknown) => void;\n\nexport interface CreateExpressMiddlewareOptions {\n missingTenantStatus?: 401 | 404;\n onMissingTenant?: (\n request: ExpressRequestLike,\n response: ExpressResponseLike,\n ) => void | Promise<void>;\n}\n\nexport interface ExpressTenantLike extends Pick<BetterTenantInstance, \"handleRequest\"> {}\n\nexport type ExpressMiddleware = (\n request: ExpressRequestLike,\n response: ExpressResponseLike,\n next: ExpressNextFunction,\n) => Promise<void>;\n\nexport function createExpressMiddleware(\n tenant: ExpressTenantLike,\n options: CreateExpressMiddlewareOptions = {},\n): ExpressMiddleware {\n const missingTenantStatus = options.missingTenantStatus ?? 404;\n\n return async (request, response, next) => {\n try {\n await tenant.handleRequest<ResolvableRequest, void>(\n toResolvableRequest(request),\n async () => {\n next();\n },\n {\n onMissingTenant: async () => {\n if (options.onMissingTenant) {\n await options.onMissingTenant(request, response);\n return;\n }\n response.status(missingTenantStatus).json({ error: \"Tenant not found\" });\n },\n },\n );\n } catch (error) {\n next(error);\n }\n };\n}\n\nexport type { TenantContext, TenantScopedDatabase };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAoC;AAsC7B,SAAS,wBACd,QACA,UAA0C,CAAC,GACxB;AACnB,QAAM,sBAAsB,QAAQ,uBAAuB;AAE3D,SAAO,OAAO,SAAS,UAAU,SAAS;AACxC,QAAI;AACF,YAAM,OAAO;AAAA,YACX,wCAAoB,OAAO;AAAA,QAC3B,YAAY;AACV,eAAK;AAAA,QACP;AAAA,QACA;AAAA,UACE,iBAAiB,YAAY;AAC3B,gBAAI,QAAQ,iBAAiB;AAC3B,oBAAM,QAAQ,gBAAgB,SAAS,QAAQ;AAC/C;AAAA,YACF;AACA,qBAAS,OAAO,mBAAmB,EAAE,KAAK,EAAE,OAAO,mBAAmB,CAAC;AAAA,UACzE;AAAA,QACF;AAAA,MACF;AAAA,IACF,SAAS,OAAO;AACd,WAAK,KAAK;AAAA,IACZ;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,23 @@
1
+ import { NodeLikeRequest, BetterTenantInstance } from '@usebetterdev/tenant-core';
2
+ export { TenantContext, TenantScopedDatabase } from '@usebetterdev/tenant-core';
3
+
4
+ interface ExpressRequestLike extends NodeLikeRequest {
5
+ headers: Record<string, string | string[] | number | undefined>;
6
+ originalUrl?: string;
7
+ hostname?: string;
8
+ }
9
+ interface ExpressResponseLike {
10
+ status: (code: number) => ExpressResponseLike;
11
+ json: (body: unknown) => unknown;
12
+ }
13
+ type ExpressNextFunction = (error?: unknown) => void;
14
+ interface CreateExpressMiddlewareOptions {
15
+ missingTenantStatus?: 401 | 404;
16
+ onMissingTenant?: (request: ExpressRequestLike, response: ExpressResponseLike) => void | Promise<void>;
17
+ }
18
+ interface ExpressTenantLike extends Pick<BetterTenantInstance, "handleRequest"> {
19
+ }
20
+ type ExpressMiddleware = (request: ExpressRequestLike, response: ExpressResponseLike, next: ExpressNextFunction) => Promise<void>;
21
+ declare function createExpressMiddleware(tenant: ExpressTenantLike, options?: CreateExpressMiddlewareOptions): ExpressMiddleware;
22
+
23
+ export { type CreateExpressMiddlewareOptions, type ExpressMiddleware, type ExpressNextFunction, type ExpressRequestLike, type ExpressResponseLike, type ExpressTenantLike, createExpressMiddleware };
@@ -0,0 +1,23 @@
1
+ import { NodeLikeRequest, BetterTenantInstance } from '@usebetterdev/tenant-core';
2
+ export { TenantContext, TenantScopedDatabase } from '@usebetterdev/tenant-core';
3
+
4
+ interface ExpressRequestLike extends NodeLikeRequest {
5
+ headers: Record<string, string | string[] | number | undefined>;
6
+ originalUrl?: string;
7
+ hostname?: string;
8
+ }
9
+ interface ExpressResponseLike {
10
+ status: (code: number) => ExpressResponseLike;
11
+ json: (body: unknown) => unknown;
12
+ }
13
+ type ExpressNextFunction = (error?: unknown) => void;
14
+ interface CreateExpressMiddlewareOptions {
15
+ missingTenantStatus?: 401 | 404;
16
+ onMissingTenant?: (request: ExpressRequestLike, response: ExpressResponseLike) => void | Promise<void>;
17
+ }
18
+ interface ExpressTenantLike extends Pick<BetterTenantInstance, "handleRequest"> {
19
+ }
20
+ type ExpressMiddleware = (request: ExpressRequestLike, response: ExpressResponseLike, next: ExpressNextFunction) => Promise<void>;
21
+ declare function createExpressMiddleware(tenant: ExpressTenantLike, options?: CreateExpressMiddlewareOptions): ExpressMiddleware;
22
+
23
+ export { type CreateExpressMiddlewareOptions, type ExpressMiddleware, type ExpressNextFunction, type ExpressRequestLike, type ExpressResponseLike, type ExpressTenantLike, createExpressMiddleware };
package/dist/index.js ADDED
@@ -0,0 +1,30 @@
1
+ // src/index.ts
2
+ import { toResolvableRequest } from "@usebetterdev/tenant-core";
3
+ function createExpressMiddleware(tenant, options = {}) {
4
+ const missingTenantStatus = options.missingTenantStatus ?? 404;
5
+ return async (request, response, next) => {
6
+ try {
7
+ await tenant.handleRequest(
8
+ toResolvableRequest(request),
9
+ async () => {
10
+ next();
11
+ },
12
+ {
13
+ onMissingTenant: async () => {
14
+ if (options.onMissingTenant) {
15
+ await options.onMissingTenant(request, response);
16
+ return;
17
+ }
18
+ response.status(missingTenantStatus).json({ error: "Tenant not found" });
19
+ }
20
+ }
21
+ );
22
+ } catch (error) {
23
+ next(error);
24
+ }
25
+ };
26
+ }
27
+ export {
28
+ createExpressMiddleware
29
+ };
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { toResolvableRequest } from \"@usebetterdev/tenant-core\";\nimport type {\n BetterTenantInstance,\n NodeLikeRequest,\n ResolvableRequest,\n TenantContext,\n TenantScopedDatabase,\n} from \"@usebetterdev/tenant-core\";\n\nexport interface ExpressRequestLike extends NodeLikeRequest {\n headers: Record<string, string | string[] | number | undefined>;\n originalUrl?: string;\n hostname?: string;\n}\n\nexport interface ExpressResponseLike {\n status: (code: number) => ExpressResponseLike;\n json: (body: unknown) => unknown;\n}\n\nexport type ExpressNextFunction = (error?: unknown) => void;\n\nexport interface CreateExpressMiddlewareOptions {\n missingTenantStatus?: 401 | 404;\n onMissingTenant?: (\n request: ExpressRequestLike,\n response: ExpressResponseLike,\n ) => void | Promise<void>;\n}\n\nexport interface ExpressTenantLike extends Pick<BetterTenantInstance, \"handleRequest\"> {}\n\nexport type ExpressMiddleware = (\n request: ExpressRequestLike,\n response: ExpressResponseLike,\n next: ExpressNextFunction,\n) => Promise<void>;\n\nexport function createExpressMiddleware(\n tenant: ExpressTenantLike,\n options: CreateExpressMiddlewareOptions = {},\n): ExpressMiddleware {\n const missingTenantStatus = options.missingTenantStatus ?? 404;\n\n return async (request, response, next) => {\n try {\n await tenant.handleRequest<ResolvableRequest, void>(\n toResolvableRequest(request),\n async () => {\n next();\n },\n {\n onMissingTenant: async () => {\n if (options.onMissingTenant) {\n await options.onMissingTenant(request, response);\n return;\n }\n response.status(missingTenantStatus).json({ error: \"Tenant not found\" });\n },\n },\n );\n } catch (error) {\n next(error);\n }\n };\n}\n\nexport type { TenantContext, TenantScopedDatabase };\n"],"mappings":";AAAA,SAAS,2BAA2B;AAsC7B,SAAS,wBACd,QACA,UAA0C,CAAC,GACxB;AACnB,QAAM,sBAAsB,QAAQ,uBAAuB;AAE3D,SAAO,OAAO,SAAS,UAAU,SAAS;AACxC,QAAI;AACF,YAAM,OAAO;AAAA,QACX,oBAAoB,OAAO;AAAA,QAC3B,YAAY;AACV,eAAK;AAAA,QACP;AAAA,QACA;AAAA,UACE,iBAAiB,YAAY;AAC3B,gBAAI,QAAQ,iBAAiB;AAC3B,oBAAM,QAAQ,gBAAgB,SAAS,QAAQ;AAC/C;AAAA,YACF;AACA,qBAAS,OAAO,mBAAmB,EAAE,KAAK,EAAE,OAAO,mBAAmB,CAAC;AAAA,UACzE;AAAA,QACF;AAAA,MACF;AAAA,IACF,SAAS,OAAO;AACd,WAAK,KAAK;AAAA,IACZ;AAAA,EACF;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@usebetterdev/tenant-express",
3
+ "version": "0.1.0",
4
+ "repository": "github:usebetter-dev/usebetter",
5
+ "bugs": "https://github.com/usebetter-dev/usebetter/issues",
6
+ "homepage": "https://github.com/usebetter-dev/usebetter#readme",
7
+ "publishConfig": {
8
+ "access": "public",
9
+ "registry": "https://registry.npmjs.org/"
10
+ },
11
+ "type": "module",
12
+ "main": "./dist/index.cjs",
13
+ "module": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js",
19
+ "require": "./dist/index.cjs"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "README.md"
25
+ ],
26
+ "dependencies": {
27
+ "@usebetterdev/tenant-core": "0.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "express": ">=4.18.0"
31
+ },
32
+ "devDependencies": {
33
+ "@types/express": "^5.0.0",
34
+ "@types/node": "^22.10.0",
35
+ "@types/supertest": "^6.0.3",
36
+ "express": "^5.1.0",
37
+ "supertest": "^7.1.4",
38
+ "tsup": "^8.3.5",
39
+ "typescript": "~5.7.2",
40
+ "vitest": "^2.1.6"
41
+ },
42
+ "engines": {
43
+ "node": ">=22"
44
+ },
45
+ "scripts": {
46
+ "build": "tsup",
47
+ "lint": "oxlint",
48
+ "test": "vitest run",
49
+ "typecheck": "tsc --noEmit"
50
+ }
51
+ }