response-standardizer 1.1.4 → 1.1.6

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
@@ -1,5 +1,5 @@
1
1
  import { Request, Response } from "express";
2
- import { RestResponseFunctions, RestMiddlewareFunctions } from "./types.js";
2
+ import { RestResponseFunctions, RestMiddlewareFunctions, AuthRequest } from "./types.js";
3
3
  declare global {
4
4
  namespace Express {
5
5
  interface Response {
@@ -24,3 +24,4 @@ export declare const warn: (message: string, meta?: any) => void;
24
24
  export declare const info: (message: string, meta?: any) => void;
25
25
  export declare const log: (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => void;
26
26
  export declare const handleRestException: (req: Request, res: Response, err: Error) => void;
27
+ export declare const controller: (req: AuthRequest, res: Response, process: () => {}) => void;
package/dist/index.js CHANGED
@@ -189,3 +189,11 @@ export const handleRestException = (req, res, err) => {
189
189
  RestResponse.exceptionError(req, res, err.message);
190
190
  }
191
191
  };
192
+ export const controller = (req, res, process) => {
193
+ try {
194
+ process();
195
+ }
196
+ catch (err) {
197
+ handleRestException(req, res, err);
198
+ }
199
+ };
package/dist/types.d.ts CHANGED
@@ -8,12 +8,15 @@ export interface StandardResponse<T = any> {
8
8
  timestamp: string;
9
9
  };
10
10
  }
11
- export interface Pagination<T> {
12
- data: T[];
11
+ export interface PaginationMeta {
13
12
  total: number;
14
13
  page: number;
15
- perPage: number;
16
- pageCount: number;
14
+ limit: number;
15
+ totalPages: number;
16
+ }
17
+ export interface Pagination<T> {
18
+ data: T[];
19
+ meta: PaginationMeta;
17
20
  }
18
21
  export type ErrorFields = Record<string, string[]>;
19
22
  export type Middleware = (req: Request, res: Response, next: NextFunction) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Express middleware to standardize API responses",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -244,4 +244,12 @@ export const handleRestException = (req: Request, res: Response, err: Error) =>
244
244
  }else if(err instanceof Error){
245
245
  RestResponse.exceptionError(req, res, err.message)
246
246
  }
247
+ }
248
+
249
+ export const controller = (req: AuthRequest, res: Response, process: () => {}) => {
250
+ try{
251
+ process();
252
+ }catch(err){
253
+ handleRestException(req, res, err as Error)
254
+ }
247
255
  }
@@ -1,3 +1,7 @@
1
+ import { Response } from "express";
2
+ import { handleRestException } from ".";
3
+ import { AuthRequest } from "./types";
4
+
1
5
  export class ServiceException extends Error {
2
6
  status: number;
3
7
  errors: any
@@ -7,4 +11,5 @@ export class ServiceException extends Error {
7
11
  this.status = status;
8
12
  this.errors = errors;
9
13
  }
10
- }
14
+ }
15
+
package/src/types.ts CHANGED
@@ -9,13 +9,15 @@ export interface StandardResponse<T = any> {
9
9
  timestamp: string;
10
10
  };
11
11
  }
12
-
13
- export interface Pagination<T>{
14
- data: T[];
12
+ export interface PaginationMeta{
15
13
  total: number;
16
14
  page: number;
17
- perPage: number;
18
- pageCount: number;
15
+ limit: number;
16
+ totalPages: number;
17
+ }
18
+ export interface Pagination<T>{
19
+ data: T[];
20
+ meta: PaginationMeta
19
21
  }
20
22
 
21
23
  export type ErrorFields = Record<string, string[]>;