devdad-express-utils 1.0.0 → 1.1.1
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/README.md +9 -18
- package/dist/AppError.d.ts +10 -0
- package/dist/AppError.js +10 -0
- package/dist/catchAsync.d.ts +5 -0
- package/dist/catchAsync.js +5 -0
- package/dist/errorHandler.d.ts +7 -0
- package/dist/errorHandler.js +17 -0
- package/package.json +7 -2
package/README.md
CHANGED
|
@@ -5,11 +5,11 @@ A collection of reusable utilities for Express.js applications, including error
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install express-utils
|
|
8
|
+
npm install devdad-express-utils
|
|
9
9
|
# or
|
|
10
|
-
yarn add express-utils
|
|
10
|
+
yarn add devdad-express-utils
|
|
11
11
|
# or
|
|
12
|
-
pnpm add express-utils
|
|
12
|
+
pnpm add devdad-express-utils
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
@@ -18,7 +18,7 @@ pnpm add express-utils
|
|
|
18
18
|
|
|
19
19
|
```typescript
|
|
20
20
|
import express from "express";
|
|
21
|
-
import { errorHandler, AppError } from "express-utils";
|
|
21
|
+
import { errorHandler, AppError } from "devdad-express-utils";
|
|
22
22
|
|
|
23
23
|
const app = express();
|
|
24
24
|
|
|
@@ -34,7 +34,7 @@ throw new AppError("Something went wrong", 400);
|
|
|
34
34
|
### Async Route Wrapping
|
|
35
35
|
|
|
36
36
|
```typescript
|
|
37
|
-
import { catchAsync } from "express-utils";
|
|
37
|
+
import { catchAsync } from "devdad-express-utils";
|
|
38
38
|
|
|
39
39
|
const getUsers = catchAsync(async (req, res, next) => {
|
|
40
40
|
const users = await User.find();
|
|
@@ -47,20 +47,11 @@ app.get("/users", getUsers);
|
|
|
47
47
|
### Custom Error Class
|
|
48
48
|
|
|
49
49
|
```typescript
|
|
50
|
-
import { AppError } from "express-utils";
|
|
50
|
+
import { AppError } from "devdad-express-utils";
|
|
51
51
|
|
|
52
52
|
throw new AppError("Validation failed", 400, ["Email is required"]);
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
-
#### JavaScript Usage
|
|
56
|
-
|
|
57
|
-
```javascript
|
|
58
|
-
const { AppError, catchAsync, errorHandler } = require("express-utils");
|
|
59
|
-
|
|
60
|
-
// Or with ES modules
|
|
61
|
-
import { AppError, catchAsync, errorHandler } from "express-utils";
|
|
62
|
-
```
|
|
63
|
-
|
|
64
55
|
## Error Handling Patterns
|
|
65
56
|
|
|
66
57
|
### Using AppError
|
|
@@ -89,7 +80,7 @@ throw new AppError("Database connection failed", 500);
|
|
|
89
80
|
|
|
90
81
|
```javascript
|
|
91
82
|
const express = require("express");
|
|
92
|
-
const { AppError, catchAsync, errorHandler } = require("express-utils");
|
|
83
|
+
const { AppError, catchAsync, errorHandler } = require("devdad-express-utils");
|
|
93
84
|
|
|
94
85
|
const app = express();
|
|
95
86
|
|
|
@@ -110,10 +101,10 @@ app.use(errorHandler);
|
|
|
110
101
|
#### JavaScript Usage
|
|
111
102
|
|
|
112
103
|
```javascript
|
|
113
|
-
const { AppError, catchAsync, errorHandler } = require("express-utils");
|
|
104
|
+
const { AppError, catchAsync, errorHandler } = require("devdad-express-utils");
|
|
114
105
|
|
|
115
106
|
// Or with ES modules
|
|
116
|
-
import { AppError, catchAsync, errorHandler } from "express-utils";
|
|
107
|
+
import { AppError, catchAsync, errorHandler } from "devdad-express-utils";
|
|
117
108
|
```
|
|
118
109
|
|
|
119
110
|
## API
|
package/dist/AppError.d.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for application errors.
|
|
3
|
+
* Extends the built-in Error class with additional properties for status code, status, and operational flag.
|
|
4
|
+
*/
|
|
1
5
|
export declare class AppError extends Error {
|
|
2
6
|
readonly statusCode: number;
|
|
3
7
|
readonly status: "fail" | "error";
|
|
4
8
|
readonly isOperational: true;
|
|
5
9
|
readonly errors?: string[];
|
|
10
|
+
/**
|
|
11
|
+
* Creates an instance of AppError.
|
|
12
|
+
* @param {string} message - The error message.
|
|
13
|
+
* @param {number} statusCode - The HTTP status code.
|
|
14
|
+
* @param {string[]} [errors=[]] - Optional array of additional error messages.
|
|
15
|
+
*/
|
|
6
16
|
constructor(message: string, statusCode: number, errors?: string[]);
|
|
7
17
|
}
|
package/dist/AppError.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for application errors.
|
|
3
|
+
* Extends the built-in Error class with additional properties for status code, status, and operational flag.
|
|
4
|
+
*/
|
|
1
5
|
export class AppError extends Error {
|
|
2
6
|
statusCode;
|
|
3
7
|
status;
|
|
4
8
|
isOperational;
|
|
5
9
|
errors;
|
|
10
|
+
/**
|
|
11
|
+
* Creates an instance of AppError.
|
|
12
|
+
* @param {string} message - The error message.
|
|
13
|
+
* @param {number} statusCode - The HTTP status code.
|
|
14
|
+
* @param {string[]} [errors=[]] - Optional array of additional error messages.
|
|
15
|
+
*/
|
|
6
16
|
constructor(message, statusCode, errors = []) {
|
|
7
17
|
super(message);
|
|
8
18
|
this.statusCode = statusCode;
|
package/dist/catchAsync.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
import type { NextFunction, Request, Response } from "express";
|
|
2
|
+
/**
|
|
3
|
+
* Higher-order function to catch asynchronous errors in Express route handlers.
|
|
4
|
+
* @param {(req: Request, res: Response, next: NextFunction) => Promise<any>} fn - The async route handler.
|
|
5
|
+
* @returns {(req: Request, res: Response, next: NextFunction) => void} - The wrapped handler that catches errors.
|
|
6
|
+
*/
|
|
2
7
|
export declare const catchAsync: (fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) => (req: Request, res: Response, next: NextFunction) => void;
|
package/dist/catchAsync.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
//#region Catch Async Handler
|
|
2
|
+
/**
|
|
3
|
+
* Higher-order function to catch asynchronous errors in Express route handlers.
|
|
4
|
+
* @param {(req: Request, res: Response, next: NextFunction) => Promise<any>} fn - The async route handler.
|
|
5
|
+
* @returns {(req: Request, res: Response, next: NextFunction) => void} - The wrapped handler that catches errors.
|
|
6
|
+
*/
|
|
2
7
|
export const catchAsync = (fn) => (req, res, next) => {
|
|
3
8
|
fn(req, res, next).catch(next);
|
|
4
9
|
};
|
package/dist/errorHandler.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
/**
|
|
3
|
+
* Global error handler middleware for Express.
|
|
4
|
+
* @param {any} err - The error object.
|
|
5
|
+
* @param {Request} req - The Express request object.
|
|
6
|
+
* @param {Response} res - The Express response object.
|
|
7
|
+
* @param {NextFunction} next - The Express next function.
|
|
8
|
+
*/
|
|
2
9
|
declare const errorHandler: (err: any, req: Request, res: Response, next: NextFunction) => void;
|
|
3
10
|
export { errorHandler };
|
package/dist/errorHandler.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { AppError } from "./AppError.js";
|
|
2
|
+
/**
|
|
3
|
+
* Sends detailed error information in development mode.
|
|
4
|
+
* @param {any} err - The error object.
|
|
5
|
+
* @param {Response} res - The Express response object.
|
|
6
|
+
*/
|
|
2
7
|
const sendErrorDev = (err, res) => {
|
|
3
8
|
res.status(err.statusCode || 500).json({
|
|
4
9
|
status: err.status || "error",
|
|
@@ -8,6 +13,11 @@ const sendErrorDev = (err, res) => {
|
|
|
8
13
|
stack: err.stack,
|
|
9
14
|
});
|
|
10
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* Sends error information in production mode.
|
|
18
|
+
* @param {AppError} err - The AppError instance.
|
|
19
|
+
* @param {Response} res - The Express response object.
|
|
20
|
+
*/
|
|
11
21
|
const sendErrorProd = (err, res) => {
|
|
12
22
|
// Operational, trusted error → send to client
|
|
13
23
|
if (err.isOperational) {
|
|
@@ -26,6 +36,13 @@ const sendErrorProd = (err, res) => {
|
|
|
26
36
|
});
|
|
27
37
|
}
|
|
28
38
|
};
|
|
39
|
+
/**
|
|
40
|
+
* Global error handler middleware for Express.
|
|
41
|
+
* @param {any} err - The error object.
|
|
42
|
+
* @param {Request} req - The Express request object.
|
|
43
|
+
* @param {Response} res - The Express response object.
|
|
44
|
+
* @param {NextFunction} next - The Express next function.
|
|
45
|
+
*/
|
|
29
46
|
const errorHandler = (err, req, res, next) => {
|
|
30
47
|
err.statusCode = err.statusCode || 500;
|
|
31
48
|
err.status = err.status || "error";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "devdad-express-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Reusable Express.js utilities for error handling, async wrapping, and more",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,9 +14,14 @@
|
|
|
14
14
|
"express",
|
|
15
15
|
"utilities",
|
|
16
16
|
"error-handling",
|
|
17
|
-
"typescript"
|
|
17
|
+
"typescript",
|
|
18
|
+
"javascript"
|
|
18
19
|
],
|
|
19
20
|
"author": "Oliver Metz",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/Devdad-Main/Express-Utils.git"
|
|
24
|
+
},
|
|
20
25
|
"license": "ISC",
|
|
21
26
|
"dependencies": {
|
|
22
27
|
"express": "^5.1.0"
|