response-standardizer 1.2.2 → 1.2.3

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.
@@ -0,0 +1,32 @@
1
+ import { Request, Response } from "express";
2
+ import { RestResponseFunctions, RestMiddlewareFunctions } from "./types";
3
+ declare global {
4
+ namespace Express {
5
+ interface Response {
6
+ json: (data: any) => Response;
7
+ send: (data?: any) => Response;
8
+ locals: {
9
+ requestId: string;
10
+ [key: string]: any;
11
+ };
12
+ }
13
+ }
14
+ }
15
+ export declare const initKeycloak: (config: {
16
+ service?: string;
17
+ realm?: string;
18
+ }) => Promise<void>;
19
+ export declare const protect: (allowedRoles?: string[]) => (req: any, res: any, next: any) => void;
20
+ export declare const RestResponse: RestResponseFunctions;
21
+ export declare const RestMiddleware: RestMiddlewareFunctions;
22
+ export declare const error: (message: string, meta?: any) => void;
23
+ export declare const warn: (message: string, meta?: any) => void;
24
+ export declare const info: (message: string, meta?: any) => void;
25
+ export declare const log: (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => void;
26
+ export declare const handleRestException: (req: Request, res: Response, err: Error) => void;
27
+ export declare const handleValidationException: (error: any) => never;
28
+ export declare class ServiceException extends Error {
29
+ status: number;
30
+ errors: any;
31
+ constructor(message: string, status?: number, errors?: any);
32
+ }
@@ -0,0 +1,40 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export interface StandardResponse<T = any> {
3
+ code: number;
4
+ message: string;
5
+ errors: Record<string, string[]> | null;
6
+ meta: {
7
+ requestId: string;
8
+ timestamp: string;
9
+ };
10
+ }
11
+ export interface PaginationMeta {
12
+ total: number;
13
+ page: number;
14
+ limit: number;
15
+ totalPages: number;
16
+ }
17
+ export interface Pagination<T> {
18
+ data: T[];
19
+ meta: PaginationMeta;
20
+ }
21
+ export type ErrorFields = Record<string, string[]>;
22
+ export type Middleware = (req: Request, res: Response, next: NextFunction) => void;
23
+ export interface RestMiddlewareFunctions {
24
+ responseHandlerMiddleware: <T>(req: Request, res: Response, next: NextFunction) => void;
25
+ }
26
+ export interface RestResponseFunctions {
27
+ success: <T>(req: Request, res: Response, data: T) => void;
28
+ paginate: <T>(req: Request, res: Response, data: Pagination<T>) => void;
29
+ created: <T>(req: Request, res: Response, data: T) => void;
30
+ deleted: (req: Request, res: Response) => void;
31
+ validationError: (req: Request, res: Response, errors: ErrorFields, message?: string) => void;
32
+ exceptionError: (req: Request, res: Response, errors: any, message?: string) => void;
33
+ unauthorized: (req: Request, res: Response, message?: string) => void;
34
+ accessDenied: (req: Request, res: Response, message?: string) => void;
35
+ notFound: (req: Request, res: Response, message?: string) => void;
36
+ }
37
+ export interface AuthRequest extends Request {
38
+ user?: any;
39
+ token?: string;
40
+ }
@@ -0,0 +1,2 @@
1
+ export declare const generateRequestId: () => string;
2
+ export declare const getTimestamp: () => string;
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "Express middleware to standardize API responses",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
+ "type": "commonjs",
7
8
  "scripts": {
8
9
  "build": "tsc",
9
10
  "prepare": "npm run build"
package/tsconfig.json CHANGED
@@ -6,9 +6,10 @@
6
6
  "rootDir": "src",
7
7
  "outDir": "dist",
8
8
  "strict": true,
9
+ "declaration": true,
9
10
  "esModuleInterop": true,
10
11
  "forceConsistentCasingInFileNames": true,
11
12
  "skipLibCheck": true
12
13
  },
13
14
  "include": ["src/**/*"]
14
- }
15
+ }