@discovercloudai/shared 1.0.4 → 1.0.5

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 CHANGED
@@ -4,3 +4,5 @@ export * from "./context";
4
4
  export * from "./dto";
5
5
  export * from "./internal";
6
6
  export * from "./permissions";
7
+ export * from "./utils";
8
+ export * from "./middleware";
package/dist/index.js CHANGED
@@ -20,3 +20,5 @@ __exportStar(require("./context"), exports);
20
20
  __exportStar(require("./dto"), exports);
21
21
  __exportStar(require("./internal"), exports);
22
22
  __exportStar(require("./permissions"), exports);
23
+ __exportStar(require("./utils"), exports);
24
+ __exportStar(require("./middleware"), exports);
@@ -0,0 +1,4 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare class GlobalErrorHandler {
3
+ static handle(err: unknown, req: Request, res: Response, _next: NextFunction): Response<any, Record<string, any>>;
4
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GlobalErrorHandler = void 0;
4
+ const zod_1 = require("zod");
5
+ const utils_1 = require("../utils");
6
+ class GlobalErrorHandler {
7
+ static handle(err, req, res, _next) {
8
+ // Standardized logging using req.id from your requestId middleware
9
+ console.error(`[req ${req.id}]`, err);
10
+ // 1. Zod Validation Errors
11
+ if (err instanceof zod_1.ZodError) {
12
+ return (0, utils_1.failure)(res, "Validation failed", 400, err.flatten());
13
+ }
14
+ // 2. Custom App Errors (instanceof checks for BadRequestError, etc.)
15
+ const custom = err;
16
+ if (custom.statusCode) {
17
+ return (0, utils_1.failure)(res, custom.message ?? "Error", custom.statusCode, custom.details);
18
+ }
19
+ // 3. Fallback: Internal Server Error
20
+ return (0, utils_1.failure)(res, "Internal Server Error", 500);
21
+ }
22
+ }
23
+ exports.GlobalErrorHandler = GlobalErrorHandler;
@@ -0,0 +1,4 @@
1
+ export * from "./error-handler";
2
+ export * from "./validate";
3
+ export * from "./verify-internal-jwt";
4
+ export * from "./request-id";
@@ -0,0 +1,20 @@
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("./error-handler"), exports);
18
+ __exportStar(require("./validate"), exports);
19
+ __exportStar(require("./verify-internal-jwt"), exports);
20
+ __exportStar(require("./request-id"), exports);
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare const requestId: (req: Request, _res: Response, next: NextFunction) => void;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requestId = void 0;
4
+ const crypto_1 = require("crypto");
5
+ const requestId = (req, _res, next) => {
6
+ req.id = (0, crypto_1.randomUUID)();
7
+ next();
8
+ };
9
+ exports.requestId = requestId;
@@ -0,0 +1,12 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export interface IJwtVerifier {
3
+ verifyAccessToken(token: string): Promise<{
4
+ accountId: string;
5
+ [key: string]: any;
6
+ }>;
7
+ }
8
+ export declare class RequireAuthMiddleware {
9
+ private readonly verifier;
10
+ constructor(verifier: IJwtVerifier);
11
+ handle: (req: Request, _res: Response, next: NextFunction) => Promise<void>;
12
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequireAuthMiddleware = void 0;
4
+ const errors_1 = require("../errors"); // Your shared error
5
+ class RequireAuthMiddleware {
6
+ constructor(verifier) {
7
+ this.verifier = verifier;
8
+ this.handle = async (req, _res, next) => {
9
+ const header = req.headers.authorization;
10
+ if (!header?.startsWith("Bearer ")) {
11
+ return next(new errors_1.UnauthorizedError("Missing Authorization header"));
12
+ }
13
+ const token = header.slice(7);
14
+ try {
15
+ const payload = await this.verifier.verifyAccessToken(token);
16
+ // req.accountId is recognized because of your shared .d.ts augmentation!
17
+ req.accountId = payload.accountId;
18
+ next();
19
+ }
20
+ catch (err) {
21
+ next(new errors_1.UnauthorizedError("Invalid or expired token"));
22
+ }
23
+ };
24
+ }
25
+ }
26
+ exports.RequireAuthMiddleware = RequireAuthMiddleware;
@@ -0,0 +1,5 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ import { ZodType } from "zod";
3
+ export declare class Validator {
4
+ static validate<T>(schema: ZodType<T>, source?: "body" | "params" | "query"): (req: Request, _res: Response, next: NextFunction) => void;
5
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Validator = void 0;
4
+ class Validator {
5
+ static validate(schema, source = "body") {
6
+ return (req, _res, next) => {
7
+ const result = schema.safeParse(req[source]);
8
+ if (!result.success) {
9
+ // Pass to GlobalErrorHandler
10
+ return next(result.error);
11
+ }
12
+ // Populate req.validated (defined in your shared .d.ts)
13
+ req.validated = result.data;
14
+ next();
15
+ };
16
+ }
17
+ }
18
+ exports.Validator = Validator;
@@ -0,0 +1,7 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ import { InternalJwtService } from "../internal";
3
+ export declare class VerifyInternalJwtMiddleware {
4
+ private readonly internalJwt;
5
+ constructor(internalJwt: InternalJwtService);
6
+ handle: (req: Request, _res: Response, next: NextFunction) => Promise<void>;
7
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VerifyInternalJwtMiddleware = void 0;
4
+ const errors_1 = require("../errors"); // Shared errors
5
+ class VerifyInternalJwtMiddleware {
6
+ constructor(internalJwt) {
7
+ this.internalJwt = internalJwt;
8
+ this.handle = async (req, _res, next) => {
9
+ const raw = req.headers["x-internal-token"]; // Standardize header name
10
+ if (!raw || typeof raw !== "string") {
11
+ return next(new errors_1.UnauthorizedError("Missing internal token"));
12
+ }
13
+ try {
14
+ const payload = await this.internalJwt.verify(raw);
15
+ // Populate standardized internalAuth (from your shared .d.ts)
16
+ req.internalAuth = payload;
17
+ next();
18
+ }
19
+ catch (err) {
20
+ next(new errors_1.UnauthorizedError("Invalid internal token"));
21
+ }
22
+ };
23
+ }
24
+ }
25
+ exports.VerifyInternalJwtMiddleware = VerifyInternalJwtMiddleware;
@@ -0,0 +1 @@
1
+ export * from "./response";
@@ -0,0 +1,17 @@
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("./response"), exports);
@@ -0,0 +1,3 @@
1
+ import { Response } from "express";
2
+ export declare const success: <T>(res: Response, data: T, statusCode?: number) => Response<any, Record<string, any>>;
3
+ export declare const failure: (res: Response, message: string, statusCode?: number, details?: unknown) => Response<any, Record<string, any>>;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.failure = exports.success = void 0;
4
+ const success = (res, data, statusCode = 200) => {
5
+ const req = res.req;
6
+ const response = {
7
+ success: true,
8
+ data,
9
+ meta: {
10
+ requestId: req?.id ?? null,
11
+ timestamp: new Date().toISOString()
12
+ }
13
+ };
14
+ return res.status(statusCode).json(response);
15
+ };
16
+ exports.success = success;
17
+ const failure = (res, message, statusCode = 400, details) => {
18
+ const req = res.req;
19
+ const response = {
20
+ success: false,
21
+ error: {
22
+ message,
23
+ details: details ?? null
24
+ },
25
+ meta: {
26
+ requestId: req?.id ?? null,
27
+ timestamp: new Date().toISOString()
28
+ }
29
+ };
30
+ return res.status(statusCode).json(response);
31
+ };
32
+ exports.failure = failure;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discovercloudai/shared",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
6
  "main": "dist/index.js",
@@ -17,11 +17,19 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "axios-retry": "^4.5.0",
20
- "jose": "^6.1.3",
21
- "tsc": "^2.0.4",
22
- "typescript": "^5.9.3"
20
+ "jose": "^6.1.3"
21
+ },
22
+ "peerDependencies": {
23
+ "axios": "^1.x.x",
24
+ "express": "^4.x.x",
25
+ "zod": "^3.x.x"
23
26
  },
24
27
  "devDependencies": {
25
- "@types/node": "^25.3.0"
28
+ "@types/express": "^4.17.21",
29
+ "@types/node": "^20.10.6",
30
+ "axios": "^1.6.2",
31
+ "express": "^4.18.2",
32
+ "typescript": "^5.3.3",
33
+ "zod": "^3.22.4"
26
34
  }
27
- }
35
+ }