nexus-backend 1.0.0 → 1.0.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 CHANGED
@@ -1,288 +1,288 @@
1
- # Nexus Backend
2
-
3
- A lightweight backend utility library for [Express.js](https://expressjs.com/) providing structured error classes, standardised API response helpers, and a plug-and-play error handling middleware.
4
-
5
- ---
6
-
7
- ## Table of Contents
8
-
9
- - [Installation](#installation)
10
- - [Quick Start](#quick-start)
11
- - [API Reference](#api-reference)
12
- - [Response Helpers](#response-helpers)
13
- - [Error Classes](#error-classes)
14
- - [Error Middleware](#error-middleware)
15
- - [Response Shape](#response-shape)
16
- - [TypeScript](#typescript)
17
- - [License](#license)
18
-
19
- ---
20
-
21
- ## Installation
22
-
23
- ```bash
24
- npm install nexus-backend
25
- ```
26
-
27
- Express is a peer dependency — make sure it is installed in your project:
28
-
29
- ```bash
30
- npm install express
31
- ```
32
-
33
- ---
34
-
35
- ## Quick Start
36
-
37
- ```ts
38
- import express from "express";
39
- import {
40
- successResponse,
41
- errorMiddleware,
42
- NotFoundError,
43
- ValidationError,
44
- } from "nexusjs";
45
-
46
- const app = express();
47
- app.use(express.json());
48
-
49
- // Example route
50
- app.get("/users/:id", async (req, res, next) => {
51
- try {
52
- const user = await getUserById(req.params.id);
53
-
54
- if (!user) {
55
- throw new NotFoundError("User not found");
56
- }
57
-
58
- res.json(successResponse(user, "User fetched successfully"));
59
- } catch (err) {
60
- next(err);
61
- }
62
- });
63
-
64
- // Register error middleware last
65
- app.use(errorMiddleware);
66
-
67
- app.listen(3000);
68
- ```
69
-
70
- ---
71
-
72
- ## API Reference
73
-
74
- ### Response Helpers
75
-
76
- #### `successResponse(data, message?)`
77
-
78
- Returns a structured success payload. Pass the result directly to `res.json()`.
79
-
80
- ```ts
81
- successResponse<T>(data: T, message?: string): SuccessResponse<T>
82
- ```
83
-
84
- **Example**
85
-
86
- ```ts
87
- res.status(200).json(
88
- successResponse({ id: 1, name: "Alice" }, "User fetched successfully")
89
- );
90
- ```
91
-
92
- ```json
93
- {
94
- "success": true,
95
- "message": "User fetched successfully",
96
- "data": { "id": 1, "name": "Alice" }
97
- }
98
- ```
99
-
100
- ---
101
-
102
- #### `errorResponse(message, errors?, stack?)`
103
-
104
- Returns a structured error payload. You will rarely call this directly — `errorMiddleware` calls it internally. Useful if you need to construct an error response manually.
105
-
106
- ```ts
107
- errorResponse(message: string, errors?: any, stack?: string): ErrorResponse
108
- ```
109
-
110
- **Example**
111
-
112
- ```ts
113
- res.status(400).json(
114
- errorResponse("Validation failed", { field: "email", issue: "Required" })
115
- );
116
- ```
117
-
118
- ```json
119
- {
120
- "success": false,
121
- "message": "Validation failed",
122
- "errors": { "field": "email", "issue": "Required" }
123
- }
124
- ```
125
-
126
- ---
127
-
128
- ### Error Classes
129
-
130
- All error classes extend the base `ApiError` class, which itself extends the native `Error`. Throw any of these inside a route and let `errorMiddleware` handle the rest.
131
-
132
- #### `ApiError`
133
-
134
- The base error class. Use this when none of the specific subclasses fit your use case.
135
-
136
- ```ts
137
- new ApiError(message: string, statusCode?: number, errors?: any, isOperational?: boolean)
138
- ```
139
-
140
- | Parameter | Type | Default | Description |
141
- |---|---|---|---|
142
- | `message` | `string` | — | Human-readable error message |
143
- | `statusCode` | `number` | `500` | HTTP status code |
144
- | `errors` | `any` | `undefined` | Additional error details or field errors |
145
- | `isOperational` | `boolean` | `true` | Marks the error as an expected operational error |
146
-
147
- ---
148
-
149
- #### `BadRequestError`
150
-
151
- **Status:** `400 Bad Request`
152
-
153
- Use when the client sends a malformed or invalid request.
154
-
155
- ```ts
156
- throw new BadRequestError("Invalid request body");
157
- ```
158
-
159
- ---
160
-
161
- #### `UnauthorizedError`
162
-
163
- **Status:** `401 Unauthorized`
164
-
165
- Use when authentication is missing or invalid.
166
-
167
- ```ts
168
- throw new UnauthorizedError("Invalid token");
169
- ```
170
-
171
- ---
172
-
173
- #### `ForbiddenError`
174
-
175
- **Status:** `403 Forbidden`
176
-
177
- Use when an authenticated user lacks permission to access a resource.
178
-
179
- ```ts
180
- throw new ForbiddenError("You do not have access to this resource");
181
- ```
182
-
183
- ---
184
-
185
- #### `NotFoundError`
186
-
187
- **Status:** `404 Not Found`
188
-
189
- Use when a requested resource does not exist.
190
-
191
- ```ts
192
- throw new NotFoundError("Post not found");
193
- ```
194
-
195
- ---
196
-
197
- #### `ValidationError`
198
-
199
- **Status:** `422 Unprocessable Entity`
200
-
201
- Use when request data fails validation. Pass field-level errors via the `errors` argument.
202
-
203
- ```ts
204
- throw new ValidationError("Validation failed", [
205
- { field: "email", message: "Must be a valid email address" },
206
- { field: "password", message: "Must be at least 8 characters" },
207
- ]);
208
- ```
209
-
210
- ---
211
-
212
- ### Error Middleware
213
-
214
- #### `errorMiddleware`
215
-
216
- An Express-compatible error handling middleware. It catches any error passed to `next(err)`, determines the appropriate HTTP status code and message, and sends a structured `ErrorResponse`.
217
-
218
- Register it **after all routes** and other middleware.
219
-
220
- ```ts
221
- import { errorMiddleware } from "nexusjs";
222
-
223
- app.use(errorMiddleware);
224
- ```
225
-
226
- **Behaviour**
227
-
228
- - If the error is an instance of `ApiError` (or any subclass), the middleware uses the error's own `statusCode` and `message`.
229
- - For all other unhandled errors, it falls back to `500 Internal Server Error`.
230
- - When `NODE_ENV` is set to `"development"`, the response includes a `stack` field with the full stack trace to aid debugging. The `stack` field is omitted in production.
231
-
232
- **Development response example**
233
-
234
- ```json
235
- {
236
- "success": false,
237
- "message": "User not found",
238
- "errors": null,
239
- "stack": "NotFoundError: User not found\n at ..."
240
- }
241
- ```
242
-
243
- ---
244
-
245
- ## Response Shape
246
-
247
- All responses follow a consistent structure.
248
-
249
- #### Success
250
-
251
- ```ts
252
- interface SuccessResponse<T> {
253
- success: true;
254
- message?: string;
255
- data: T;
256
- }
257
- ```
258
-
259
- #### Error
260
-
261
- ```ts
262
- interface ErrorResponse {
263
- success: false;
264
- message: string;
265
- errors?: any;
266
- stack?: string; // only present in development
267
- }
268
- ```
269
-
270
- ---
271
-
272
- ## TypeScript
273
-
274
- nexusjs is written in TypeScript and ships with type declarations out of the box. No `@types` package is needed.
275
-
276
- `SuccessResponse` and `ErrorResponse` are exported and can be used to type your own response wrappers or API clients:
277
-
278
- ```ts
279
- import type { SuccessResponse, ErrorResponse } from "nexusjs";
280
-
281
- type ApiResult<T> = SuccessResponse<T> | ErrorResponse;
282
- ```
283
-
284
- ---
285
-
286
- ## License
287
-
1
+ # Nexus Backend
2
+
3
+ A lightweight backend utility library for [Express.js](https://expressjs.com/) providing structured error classes, standardised API response helpers, and a plug-and-play error handling middleware.
4
+
5
+ ---
6
+
7
+ ## Table of Contents
8
+
9
+ - [Installation](#installation)
10
+ - [Quick Start](#quick-start)
11
+ - [API Reference](#api-reference)
12
+ - [Response Helpers](#response-helpers)
13
+ - [Error Classes](#error-classes)
14
+ - [Error Middleware](#error-middleware)
15
+ - [Response Shape](#response-shape)
16
+ - [TypeScript](#typescript)
17
+ - [License](#license)
18
+
19
+ ---
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install nexus-backend
25
+ ```
26
+
27
+ Express is a peer dependency — make sure it is installed in your project:
28
+
29
+ ```bash
30
+ npm install express
31
+ ```
32
+
33
+ ---
34
+
35
+ ## Quick Start
36
+
37
+ ```ts
38
+ import express from "express";
39
+ import {
40
+ successResponse,
41
+ errorMiddleware,
42
+ NotFoundError,
43
+ ValidationError,
44
+ } from "nexus-backend";
45
+
46
+ const app = express();
47
+ app.use(express.json());
48
+
49
+ // Example route
50
+ app.get("/users/:id", async (req, res, next) => {
51
+ try {
52
+ const user = await getUserById(req.params.id);
53
+
54
+ if (!user) {
55
+ throw new NotFoundError("User not found");
56
+ }
57
+
58
+ res.json(successResponse(user, "User fetched successfully"));
59
+ } catch (err) {
60
+ next(err);
61
+ }
62
+ });
63
+
64
+ // Register error middleware last
65
+ app.use(errorMiddleware);
66
+
67
+ app.listen(3000);
68
+ ```
69
+
70
+ ---
71
+
72
+ ## API Reference
73
+
74
+ ### Response Helpers
75
+
76
+ #### `successResponse(data, message?)`
77
+
78
+ Returns a structured success payload. Pass the result directly to `res.json()`.
79
+
80
+ ```ts
81
+ successResponse<T>(data: T, message?: string): SuccessResponse<T>
82
+ ```
83
+
84
+ **Example**
85
+
86
+ ```ts
87
+ res.status(200).json(
88
+ successResponse({ id: 1, name: "Alice" }, "User fetched successfully")
89
+ );
90
+ ```
91
+
92
+ ```json
93
+ {
94
+ "success": true,
95
+ "message": "User fetched successfully",
96
+ "data": { "id": 1, "name": "Alice" }
97
+ }
98
+ ```
99
+
100
+ ---
101
+
102
+ #### `errorResponse(message, errors?, stack?)`
103
+
104
+ Returns a structured error payload. You will rarely call this directly — `errorMiddleware` calls it internally. Useful if you need to construct an error response manually.
105
+
106
+ ```ts
107
+ errorResponse(message: string, errors?: any, stack?: string): ErrorResponse
108
+ ```
109
+
110
+ **Example**
111
+
112
+ ```ts
113
+ res.status(400).json(
114
+ errorResponse("Validation failed", { field: "email", issue: "Required" })
115
+ );
116
+ ```
117
+
118
+ ```json
119
+ {
120
+ "success": false,
121
+ "message": "Validation failed",
122
+ "errors": { "field": "email", "issue": "Required" }
123
+ }
124
+ ```
125
+
126
+ ---
127
+
128
+ ### Error Classes
129
+
130
+ All error classes extend the base `ApiError` class, which itself extends the native `Error`. Throw any of these inside a route and let `errorMiddleware` handle the rest.
131
+
132
+ #### `ApiError`
133
+
134
+ The base error class. Use this when none of the specific subclasses fit your use case.
135
+
136
+ ```ts
137
+ new ApiError(message: string, statusCode?: number, errors?: any, isOperational?: boolean)
138
+ ```
139
+
140
+ | Parameter | Type | Default | Description |
141
+ |---|---|---|---|
142
+ | `message` | `string` | — | Human-readable error message |
143
+ | `statusCode` | `number` | `500` | HTTP status code |
144
+ | `errors` | `any` | `undefined` | Additional error details or field errors |
145
+ | `isOperational` | `boolean` | `true` | Marks the error as an expected operational error |
146
+
147
+ ---
148
+
149
+ #### `BadRequestError`
150
+
151
+ **Status:** `400 Bad Request`
152
+
153
+ Use when the client sends a malformed or invalid request.
154
+
155
+ ```ts
156
+ throw new BadRequestError("Invalid request body");
157
+ ```
158
+
159
+ ---
160
+
161
+ #### `UnauthorizedError`
162
+
163
+ **Status:** `401 Unauthorized`
164
+
165
+ Use when authentication is missing or invalid.
166
+
167
+ ```ts
168
+ throw new UnauthorizedError("Invalid token");
169
+ ```
170
+
171
+ ---
172
+
173
+ #### `ForbiddenError`
174
+
175
+ **Status:** `403 Forbidden`
176
+
177
+ Use when an authenticated user lacks permission to access a resource.
178
+
179
+ ```ts
180
+ throw new ForbiddenError("You do not have access to this resource");
181
+ ```
182
+
183
+ ---
184
+
185
+ #### `NotFoundError`
186
+
187
+ **Status:** `404 Not Found`
188
+
189
+ Use when a requested resource does not exist.
190
+
191
+ ```ts
192
+ throw new NotFoundError("Post not found");
193
+ ```
194
+
195
+ ---
196
+
197
+ #### `ValidationError`
198
+
199
+ **Status:** `422 Unprocessable Entity`
200
+
201
+ Use when request data fails validation. Pass field-level errors via the `errors` argument.
202
+
203
+ ```ts
204
+ throw new ValidationError("Validation failed", [
205
+ { field: "email", message: "Must be a valid email address" },
206
+ { field: "password", message: "Must be at least 8 characters" },
207
+ ]);
208
+ ```
209
+
210
+ ---
211
+
212
+ ### Error Middleware
213
+
214
+ #### `errorMiddleware`
215
+
216
+ An Express-compatible error handling middleware. It catches any error passed to `next(err)`, determines the appropriate HTTP status code and message, and sends a structured `ErrorResponse`.
217
+
218
+ Register it **after all routes** and other middleware.
219
+
220
+ ```ts
221
+ import { errorMiddleware } from "nexusjs";
222
+
223
+ app.use(errorMiddleware);
224
+ ```
225
+
226
+ **Behaviour**
227
+
228
+ - If the error is an instance of `ApiError` (or any subclass), the middleware uses the error's own `statusCode` and `message`.
229
+ - For all other unhandled errors, it falls back to `500 Internal Server Error`.
230
+ - When `NODE_ENV` is set to `"development"`, the response includes a `stack` field with the full stack trace to aid debugging. The `stack` field is omitted in production.
231
+
232
+ **Development response example**
233
+
234
+ ```json
235
+ {
236
+ "success": false,
237
+ "message": "User not found",
238
+ "errors": null,
239
+ "stack": "NotFoundError: User not found\n at ..."
240
+ }
241
+ ```
242
+
243
+ ---
244
+
245
+ ## Response Shape
246
+
247
+ All responses follow a consistent structure.
248
+
249
+ #### Success
250
+
251
+ ```ts
252
+ interface SuccessResponse<T> {
253
+ success: true;
254
+ message?: string;
255
+ data: T;
256
+ }
257
+ ```
258
+
259
+ #### Error
260
+
261
+ ```ts
262
+ interface ErrorResponse {
263
+ success: false;
264
+ message: string;
265
+ errors?: any;
266
+ stack?: string; // only present in development
267
+ }
268
+ ```
269
+
270
+ ---
271
+
272
+ ## TypeScript
273
+
274
+ nexusjs is written in TypeScript and ships with type declarations out of the box. No `@types` package is needed.
275
+
276
+ `SuccessResponse` and `ErrorResponse` are exported and can be used to type your own response wrappers or API clients:
277
+
278
+ ```ts
279
+ import type { SuccessResponse, ErrorResponse } from "nexusjs";
280
+
281
+ type ApiResult<T> = SuccessResponse<T> | ErrorResponse;
282
+ ```
283
+
284
+ ---
285
+
286
+ ## License
287
+
288
288
  MIT
package/dist/index.d.mts CHANGED
@@ -43,6 +43,9 @@ declare class ValidationError extends ApiError {
43
43
  constructor(message?: string, errors?: any);
44
44
  }
45
45
 
46
- declare const errorHandler: (err: any, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>>;
46
+ declare const errorHandler: [
47
+ (req: Request, res: Response, next: NextFunction) => void,
48
+ (err: any, req: Request, res: Response, next: NextFunction) => void
49
+ ];
47
50
 
48
51
  export { ApiError, BadRequestError, type ErrorResponse, ForbiddenError, NotFoundError, type SuccessResponse, UnauthorizedError, ValidationError, errorHandler as errorMiddleware, errorResponse, successResponse };
package/dist/index.d.ts CHANGED
@@ -43,6 +43,9 @@ declare class ValidationError extends ApiError {
43
43
  constructor(message?: string, errors?: any);
44
44
  }
45
45
 
46
- declare const errorHandler: (err: any, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>>;
46
+ declare const errorHandler: [
47
+ (req: Request, res: Response, next: NextFunction) => void,
48
+ (err: any, req: Request, res: Response, next: NextFunction) => void
49
+ ];
47
50
 
48
51
  export { ApiError, BadRequestError, type ErrorResponse, ForbiddenError, NotFoundError, type SuccessResponse, UnauthorizedError, ValidationError, errorHandler as errorMiddleware, errorResponse, successResponse };
package/dist/index.js CHANGED
@@ -94,7 +94,10 @@ var ValidationError = class extends ApiError {
94
94
  };
95
95
 
96
96
  // src/handlers/errorHandler.ts
97
- var errorHandler = (err, req, res, next) => {
97
+ var routeNotFoundHandler = (req, _, next) => {
98
+ next(new ApiError(`Route ${req.originalUrl} not found`, 404));
99
+ };
100
+ var globalErrorHandler = (err, req, res, next) => {
98
101
  console.error("Error:", err);
99
102
  let statusCode = 500;
100
103
  let message = "Internal Server Error";
@@ -108,14 +111,9 @@ var errorHandler = (err, req, res, next) => {
108
111
  if (process.env.NODE_ENV === "development") {
109
112
  stack = err.stack;
110
113
  }
111
- return res.status(statusCode).json(
112
- errorResponse(
113
- message,
114
- errors,
115
- stack
116
- )
117
- );
114
+ return res.status(statusCode).json(errorResponse(message, errors, stack));
118
115
  };
116
+ var errorHandler = [routeNotFoundHandler, globalErrorHandler];
119
117
  var errorHandler_default = errorHandler;
120
118
  // Annotate the CommonJS export names for ESM import in node:
121
119
  0 && (module.exports = {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/responses/successResponse.ts","../src/responses/errorResponse.ts","../src/errors/ApiError.ts","../src/errors/BadRequestError.ts","../src/errors/UnauthorisedError.ts","../src/errors/ForbiddenError.ts","../src/errors/NotFoundError.ts","../src/errors/ValidationError.ts","../src/handlers/errorHandler.ts"],"sourcesContent":["export { successResponse } from \"./responses/successResponse\";\r\nexport { errorResponse } from \"./responses/errorResponse\";\r\n\r\nexport { default as ApiError } from \"./errors/ApiError\";\r\nexport { default as BadRequestError } from \"./errors/BadRequestError\";\r\nexport { default as UnauthorizedError } from \"./errors/UnauthorisedError\";\r\nexport { default as ForbiddenError } from \"./errors/ForbiddenError\";\r\nexport { default as NotFoundError } from \"./errors/NotFoundError\";\r\nexport { default as ValidationError } from \"./errors/ValidationError\";\r\n\r\nexport { default as errorMiddleware } from \"./handlers/errorHandler\";\r\n\r\nexport type {\r\n SuccessResponse,\r\n ErrorResponse,\r\n} from \"./types/response.types\";","import { SuccessResponse } from \"../types/response.types\";\r\n\r\nexport const successResponse = <T>(\r\n data: T,\r\n message?: string\r\n): SuccessResponse<T> => ({\r\n success: true,\r\n data,\r\n message,\r\n});","import { ErrorResponse } from \"../types/response.types\";\r\n\r\nexport const errorResponse = (\r\n message: string,\r\n errors?: any,\r\n stack?: string\r\n): ErrorResponse => ({\r\n success: false,\r\n message,\r\n errors,\r\n stack,\r\n});","export default class ApiError extends Error {\r\n statusCode: number;\r\n isOperational: boolean;\r\n errors?: any;\r\n\r\n constructor(\r\n message: string,\r\n statusCode = 500,\r\n errors?: any,\r\n isOperational = true\r\n ) {\r\n super(message);\r\n\r\n this.statusCode = statusCode;\r\n this.errors = errors;\r\n this.isOperational = isOperational;\r\n\r\n Error.captureStackTrace(this, this.constructor);\r\n }\r\n}","import ApiError from \"./ApiError\";\r\n\r\nexport default class BadRequestError extends ApiError {\r\n constructor(message = \"Bad Request\", errors?: any) {\r\n super(message, 400, errors);\r\n }\r\n}","import ApiError from \"./ApiError\";\r\n\r\nexport default class UnauthorizedError extends ApiError {\r\n constructor(message = \"Unauthorized\", errors?: any) {\r\n super(message, 401, errors);\r\n }\r\n}","import ApiError from \"./ApiError\";\r\n\r\nexport default class ForbiddenError extends ApiError {\r\n constructor(message = \"Forbidden\", errors?: any) {\r\n super(message, 403, errors);\r\n }\r\n}","import ApiError from \"./ApiError\";\r\n\r\nexport default class NotFoundError extends ApiError {\r\n constructor(message = \"Resource Not Found\", errors?: any) {\r\n super(message, 404, errors);\r\n }\r\n}","import ApiError from \"./ApiError\";\r\n\r\nexport default class ValidationError extends ApiError {\r\n constructor(message = \"Validation Failed\", errors?: any) {\r\n super(message, 422, errors);\r\n }\r\n}","import { Request, Response, NextFunction } from \"express\";\r\n\r\nimport ApiError from \"../errors/ApiError\";\r\nimport { errorResponse } from \"../responses/errorResponse\";\r\n\r\nconst errorHandler = (\r\n err: any,\r\n req: Request,\r\n res: Response,\r\n next: NextFunction\r\n) => {\r\n\r\n console.error(\"Error:\", err);\r\n\r\n let statusCode = 500;\r\n let message = \"Internal Server Error\";\r\n let errors = undefined;\r\n let stack = undefined;\r\n\r\n // Custom application errors\r\n if (err instanceof ApiError) {\r\n statusCode = err.statusCode;\r\n message = err.message;\r\n errors = err.errors;\r\n }\r\n\r\n // Development stack trace\r\n if (process.env.NODE_ENV === \"development\") {\r\n stack = err.stack;\r\n }\r\n\r\n return res.status(statusCode).json(\r\n errorResponse(\r\n message,\r\n errors,\r\n stack\r\n )\r\n );\r\n};\r\n\r\nexport default errorHandler;"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,kBAAkB,CAC7B,MACA,aACwB;AAAA,EACxB,SAAS;AAAA,EACT;AAAA,EACA;AACF;;;ACPO,IAAM,gBAAgB,CAC3B,SACA,QACA,WACmB;AAAA,EACnB,SAAS;AAAA,EACT;AAAA,EACA;AAAA,EACA;AACF;;;ACXA,IAAqB,WAArB,cAAsC,MAAM;AAAA,EAK1C,YACE,SACA,aAAa,KACb,QACA,gBAAgB,MAChB;AACA,UAAM,OAAO;AAEb,SAAK,aAAa;AAClB,SAAK,SAAS;AACd,SAAK,gBAAgB;AAErB,UAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,EAChD;AACF;;;ACjBA,IAAqB,kBAArB,cAA6C,SAAS;AAAA,EACpD,YAAY,UAAU,eAAe,QAAc;AACjD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,oBAArB,cAA+C,SAAS;AAAA,EACtD,YAAY,UAAU,gBAAgB,QAAc;AAClD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,iBAArB,cAA4C,SAAS;AAAA,EACnD,YAAY,UAAU,aAAa,QAAc;AAC/C,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,gBAArB,cAA2C,SAAS;AAAA,EAClD,YAAY,UAAU,sBAAsB,QAAc;AACxD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,kBAArB,cAA6C,SAAS;AAAA,EACpD,YAAY,UAAU,qBAAqB,QAAc;AACvD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACDA,IAAM,eAAe,CACnB,KACA,KACA,KACA,SACG;AAEH,UAAQ,MAAM,UAAU,GAAG;AAE3B,MAAI,aAAa;AACjB,MAAI,UAAU;AACd,MAAI,SAAS;AACb,MAAI,QAAQ;AAGZ,MAAI,eAAe,UAAU;AAC3B,iBAAa,IAAI;AACjB,cAAU,IAAI;AACd,aAAS,IAAI;AAAA,EACf;AAGA,MAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,YAAQ,IAAI;AAAA,EACd;AAEA,SAAO,IAAI,OAAO,UAAU,EAAE;AAAA,IAC5B;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAO,uBAAQ;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/responses/successResponse.ts","../src/responses/errorResponse.ts","../src/errors/ApiError.ts","../src/errors/BadRequestError.ts","../src/errors/UnauthorisedError.ts","../src/errors/ForbiddenError.ts","../src/errors/NotFoundError.ts","../src/errors/ValidationError.ts","../src/handlers/errorHandler.ts"],"sourcesContent":["export { successResponse } from \"./responses/successResponse\";\nexport { errorResponse } from \"./responses/errorResponse\";\n\nexport { default as ApiError } from \"./errors/ApiError\";\nexport { default as BadRequestError } from \"./errors/BadRequestError\";\nexport { default as UnauthorizedError } from \"./errors/UnauthorisedError\";\nexport { default as ForbiddenError } from \"./errors/ForbiddenError\";\nexport { default as NotFoundError } from \"./errors/NotFoundError\";\nexport { default as ValidationError } from \"./errors/ValidationError\";\n\nexport { default as errorMiddleware } from \"./handlers/errorHandler\";\n\nexport type {\n SuccessResponse,\n ErrorResponse,\n} from \"./types/response.types\";","import { SuccessResponse } from \"../types/response.types\";\n\nexport const successResponse = <T>(\n data: T,\n message?: string\n): SuccessResponse<T> => ({\n success: true,\n data,\n message,\n});","import { ErrorResponse } from \"../types/response.types\";\n\nexport const errorResponse = (\n message: string,\n errors?: any,\n stack?: string\n): ErrorResponse => ({\n success: false,\n message,\n errors,\n stack,\n});","export default class ApiError extends Error {\n statusCode: number;\n isOperational: boolean;\n errors?: any;\n\n constructor(\n message: string,\n statusCode = 500,\n errors?: any,\n isOperational = true\n ) {\n super(message);\n\n this.statusCode = statusCode;\n this.errors = errors;\n this.isOperational = isOperational;\n\n Error.captureStackTrace(this, this.constructor);\n }\n}","import ApiError from \"./ApiError\";\n\nexport default class BadRequestError extends ApiError {\n constructor(message = \"Bad Request\", errors?: any) {\n super(message, 400, errors);\n }\n}","import ApiError from \"./ApiError\";\n\nexport default class UnauthorizedError extends ApiError {\n constructor(message = \"Unauthorized\", errors?: any) {\n super(message, 401, errors);\n }\n}","import ApiError from \"./ApiError\";\n\nexport default class ForbiddenError extends ApiError {\n constructor(message = \"Forbidden\", errors?: any) {\n super(message, 403, errors);\n }\n}","import ApiError from \"./ApiError\";\n\nexport default class NotFoundError extends ApiError {\n constructor(message = \"Resource Not Found\", errors?: any) {\n super(message, 404, errors);\n }\n}","import ApiError from \"./ApiError\";\n\nexport default class ValidationError extends ApiError {\n constructor(message = \"Validation Failed\", errors?: any) {\n super(message, 422, errors);\n }\n}","// errorHandler.ts\nimport { Request, Response, NextFunction } from \"express\";\nimport ApiError from \"../errors/ApiError\";\nimport { errorResponse } from \"../responses/errorResponse\";\n\nconst routeNotFoundHandler = (req: Request, _: Response, next: NextFunction) => {\n next(new ApiError(`Route ${req.originalUrl} not found`, 404));\n};\n\nconst globalErrorHandler = (err: any, req: Request, res: Response, next: NextFunction) => {\n console.error(\"Error:\", err);\n let statusCode = 500;\n let message = \"Internal Server Error\";\n let errors = undefined;\n let stack = undefined;\n\n if (err instanceof ApiError) {\n statusCode = err.statusCode;\n message = err.message;\n errors = err.errors;\n }\n\n if (process.env.NODE_ENV === \"development\") {\n stack = err.stack;\n }\n\n return res.status(statusCode).json(errorResponse(message, errors, stack));\n};\n\n// Export as a tuple with explicit types so spread works correctly\nconst errorHandler: [\n (req: Request, res: Response, next: NextFunction) => void,\n (err: any, req: Request, res: Response, next: NextFunction) => void\n] = [routeNotFoundHandler, globalErrorHandler];\n\nexport default errorHandler;"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,kBAAkB,CAC7B,MACA,aACwB;AAAA,EACxB,SAAS;AAAA,EACT;AAAA,EACA;AACF;;;ACPO,IAAM,gBAAgB,CAC3B,SACA,QACA,WACmB;AAAA,EACnB,SAAS;AAAA,EACT;AAAA,EACA;AAAA,EACA;AACF;;;ACXA,IAAqB,WAArB,cAAsC,MAAM;AAAA,EAK1C,YACE,SACA,aAAa,KACb,QACA,gBAAgB,MAChB;AACA,UAAM,OAAO;AAEb,SAAK,aAAa;AAClB,SAAK,SAAS;AACd,SAAK,gBAAgB;AAErB,UAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,EAChD;AACF;;;ACjBA,IAAqB,kBAArB,cAA6C,SAAS;AAAA,EACpD,YAAY,UAAU,eAAe,QAAc;AACjD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,oBAArB,cAA+C,SAAS;AAAA,EACtD,YAAY,UAAU,gBAAgB,QAAc;AAClD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,iBAArB,cAA4C,SAAS;AAAA,EACnD,YAAY,UAAU,aAAa,QAAc;AAC/C,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,gBAArB,cAA2C,SAAS;AAAA,EAClD,YAAY,UAAU,sBAAsB,QAAc;AACxD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,kBAArB,cAA6C,SAAS;AAAA,EACpD,YAAY,UAAU,qBAAqB,QAAc;AACvD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACDA,IAAM,uBAAuB,CAAC,KAAc,GAAa,SAAuB;AAC9E,OAAK,IAAI,SAAS,SAAS,IAAI,WAAW,cAAc,GAAG,CAAC;AAC9D;AAEA,IAAM,qBAAqB,CAAC,KAAU,KAAc,KAAe,SAAuB;AACxF,UAAQ,MAAM,UAAU,GAAG;AAC3B,MAAI,aAAa;AACjB,MAAI,UAAU;AACd,MAAI,SAAS;AACb,MAAI,QAAQ;AAEZ,MAAI,eAAe,UAAU;AAC3B,iBAAa,IAAI;AACjB,cAAU,IAAI;AACd,aAAS,IAAI;AAAA,EACf;AAEA,MAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,YAAQ,IAAI;AAAA,EACd;AAEA,SAAO,IAAI,OAAO,UAAU,EAAE,KAAK,cAAc,SAAS,QAAQ,KAAK,CAAC;AAC1E;AAGA,IAAM,eAGF,CAAC,sBAAsB,kBAAkB;AAE7C,IAAO,uBAAQ;","names":[]}
package/dist/index.mjs CHANGED
@@ -60,7 +60,10 @@ var ValidationError = class extends ApiError {
60
60
  };
61
61
 
62
62
  // src/handlers/errorHandler.ts
63
- var errorHandler = (err, req, res, next) => {
63
+ var routeNotFoundHandler = (req, _, next) => {
64
+ next(new ApiError(`Route ${req.originalUrl} not found`, 404));
65
+ };
66
+ var globalErrorHandler = (err, req, res, next) => {
64
67
  console.error("Error:", err);
65
68
  let statusCode = 500;
66
69
  let message = "Internal Server Error";
@@ -74,14 +77,9 @@ var errorHandler = (err, req, res, next) => {
74
77
  if (process.env.NODE_ENV === "development") {
75
78
  stack = err.stack;
76
79
  }
77
- return res.status(statusCode).json(
78
- errorResponse(
79
- message,
80
- errors,
81
- stack
82
- )
83
- );
80
+ return res.status(statusCode).json(errorResponse(message, errors, stack));
84
81
  };
82
+ var errorHandler = [routeNotFoundHandler, globalErrorHandler];
85
83
  var errorHandler_default = errorHandler;
86
84
  export {
87
85
  ApiError,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/responses/successResponse.ts","../src/responses/errorResponse.ts","../src/errors/ApiError.ts","../src/errors/BadRequestError.ts","../src/errors/UnauthorisedError.ts","../src/errors/ForbiddenError.ts","../src/errors/NotFoundError.ts","../src/errors/ValidationError.ts","../src/handlers/errorHandler.ts"],"sourcesContent":["import { SuccessResponse } from \"../types/response.types\";\r\n\r\nexport const successResponse = <T>(\r\n data: T,\r\n message?: string\r\n): SuccessResponse<T> => ({\r\n success: true,\r\n data,\r\n message,\r\n});","import { ErrorResponse } from \"../types/response.types\";\r\n\r\nexport const errorResponse = (\r\n message: string,\r\n errors?: any,\r\n stack?: string\r\n): ErrorResponse => ({\r\n success: false,\r\n message,\r\n errors,\r\n stack,\r\n});","export default class ApiError extends Error {\r\n statusCode: number;\r\n isOperational: boolean;\r\n errors?: any;\r\n\r\n constructor(\r\n message: string,\r\n statusCode = 500,\r\n errors?: any,\r\n isOperational = true\r\n ) {\r\n super(message);\r\n\r\n this.statusCode = statusCode;\r\n this.errors = errors;\r\n this.isOperational = isOperational;\r\n\r\n Error.captureStackTrace(this, this.constructor);\r\n }\r\n}","import ApiError from \"./ApiError\";\r\n\r\nexport default class BadRequestError extends ApiError {\r\n constructor(message = \"Bad Request\", errors?: any) {\r\n super(message, 400, errors);\r\n }\r\n}","import ApiError from \"./ApiError\";\r\n\r\nexport default class UnauthorizedError extends ApiError {\r\n constructor(message = \"Unauthorized\", errors?: any) {\r\n super(message, 401, errors);\r\n }\r\n}","import ApiError from \"./ApiError\";\r\n\r\nexport default class ForbiddenError extends ApiError {\r\n constructor(message = \"Forbidden\", errors?: any) {\r\n super(message, 403, errors);\r\n }\r\n}","import ApiError from \"./ApiError\";\r\n\r\nexport default class NotFoundError extends ApiError {\r\n constructor(message = \"Resource Not Found\", errors?: any) {\r\n super(message, 404, errors);\r\n }\r\n}","import ApiError from \"./ApiError\";\r\n\r\nexport default class ValidationError extends ApiError {\r\n constructor(message = \"Validation Failed\", errors?: any) {\r\n super(message, 422, errors);\r\n }\r\n}","import { Request, Response, NextFunction } from \"express\";\r\n\r\nimport ApiError from \"../errors/ApiError\";\r\nimport { errorResponse } from \"../responses/errorResponse\";\r\n\r\nconst errorHandler = (\r\n err: any,\r\n req: Request,\r\n res: Response,\r\n next: NextFunction\r\n) => {\r\n\r\n console.error(\"Error:\", err);\r\n\r\n let statusCode = 500;\r\n let message = \"Internal Server Error\";\r\n let errors = undefined;\r\n let stack = undefined;\r\n\r\n // Custom application errors\r\n if (err instanceof ApiError) {\r\n statusCode = err.statusCode;\r\n message = err.message;\r\n errors = err.errors;\r\n }\r\n\r\n // Development stack trace\r\n if (process.env.NODE_ENV === \"development\") {\r\n stack = err.stack;\r\n }\r\n\r\n return res.status(statusCode).json(\r\n errorResponse(\r\n message,\r\n errors,\r\n stack\r\n )\r\n );\r\n};\r\n\r\nexport default errorHandler;"],"mappings":";AAEO,IAAM,kBAAkB,CAC7B,MACA,aACwB;AAAA,EACxB,SAAS;AAAA,EACT;AAAA,EACA;AACF;;;ACPO,IAAM,gBAAgB,CAC3B,SACA,QACA,WACmB;AAAA,EACnB,SAAS;AAAA,EACT;AAAA,EACA;AAAA,EACA;AACF;;;ACXA,IAAqB,WAArB,cAAsC,MAAM;AAAA,EAK1C,YACE,SACA,aAAa,KACb,QACA,gBAAgB,MAChB;AACA,UAAM,OAAO;AAEb,SAAK,aAAa;AAClB,SAAK,SAAS;AACd,SAAK,gBAAgB;AAErB,UAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,EAChD;AACF;;;ACjBA,IAAqB,kBAArB,cAA6C,SAAS;AAAA,EACpD,YAAY,UAAU,eAAe,QAAc;AACjD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,oBAArB,cAA+C,SAAS;AAAA,EACtD,YAAY,UAAU,gBAAgB,QAAc;AAClD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,iBAArB,cAA4C,SAAS;AAAA,EACnD,YAAY,UAAU,aAAa,QAAc;AAC/C,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,gBAArB,cAA2C,SAAS;AAAA,EAClD,YAAY,UAAU,sBAAsB,QAAc;AACxD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,kBAArB,cAA6C,SAAS;AAAA,EACpD,YAAY,UAAU,qBAAqB,QAAc;AACvD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACDA,IAAM,eAAe,CACnB,KACA,KACA,KACA,SACG;AAEH,UAAQ,MAAM,UAAU,GAAG;AAE3B,MAAI,aAAa;AACjB,MAAI,UAAU;AACd,MAAI,SAAS;AACb,MAAI,QAAQ;AAGZ,MAAI,eAAe,UAAU;AAC3B,iBAAa,IAAI;AACjB,cAAU,IAAI;AACd,aAAS,IAAI;AAAA,EACf;AAGA,MAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,YAAQ,IAAI;AAAA,EACd;AAEA,SAAO,IAAI,OAAO,UAAU,EAAE;AAAA,IAC5B;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAO,uBAAQ;","names":[]}
1
+ {"version":3,"sources":["../src/responses/successResponse.ts","../src/responses/errorResponse.ts","../src/errors/ApiError.ts","../src/errors/BadRequestError.ts","../src/errors/UnauthorisedError.ts","../src/errors/ForbiddenError.ts","../src/errors/NotFoundError.ts","../src/errors/ValidationError.ts","../src/handlers/errorHandler.ts"],"sourcesContent":["import { SuccessResponse } from \"../types/response.types\";\n\nexport const successResponse = <T>(\n data: T,\n message?: string\n): SuccessResponse<T> => ({\n success: true,\n data,\n message,\n});","import { ErrorResponse } from \"../types/response.types\";\n\nexport const errorResponse = (\n message: string,\n errors?: any,\n stack?: string\n): ErrorResponse => ({\n success: false,\n message,\n errors,\n stack,\n});","export default class ApiError extends Error {\n statusCode: number;\n isOperational: boolean;\n errors?: any;\n\n constructor(\n message: string,\n statusCode = 500,\n errors?: any,\n isOperational = true\n ) {\n super(message);\n\n this.statusCode = statusCode;\n this.errors = errors;\n this.isOperational = isOperational;\n\n Error.captureStackTrace(this, this.constructor);\n }\n}","import ApiError from \"./ApiError\";\n\nexport default class BadRequestError extends ApiError {\n constructor(message = \"Bad Request\", errors?: any) {\n super(message, 400, errors);\n }\n}","import ApiError from \"./ApiError\";\n\nexport default class UnauthorizedError extends ApiError {\n constructor(message = \"Unauthorized\", errors?: any) {\n super(message, 401, errors);\n }\n}","import ApiError from \"./ApiError\";\n\nexport default class ForbiddenError extends ApiError {\n constructor(message = \"Forbidden\", errors?: any) {\n super(message, 403, errors);\n }\n}","import ApiError from \"./ApiError\";\n\nexport default class NotFoundError extends ApiError {\n constructor(message = \"Resource Not Found\", errors?: any) {\n super(message, 404, errors);\n }\n}","import ApiError from \"./ApiError\";\n\nexport default class ValidationError extends ApiError {\n constructor(message = \"Validation Failed\", errors?: any) {\n super(message, 422, errors);\n }\n}","// errorHandler.ts\nimport { Request, Response, NextFunction } from \"express\";\nimport ApiError from \"../errors/ApiError\";\nimport { errorResponse } from \"../responses/errorResponse\";\n\nconst routeNotFoundHandler = (req: Request, _: Response, next: NextFunction) => {\n next(new ApiError(`Route ${req.originalUrl} not found`, 404));\n};\n\nconst globalErrorHandler = (err: any, req: Request, res: Response, next: NextFunction) => {\n console.error(\"Error:\", err);\n let statusCode = 500;\n let message = \"Internal Server Error\";\n let errors = undefined;\n let stack = undefined;\n\n if (err instanceof ApiError) {\n statusCode = err.statusCode;\n message = err.message;\n errors = err.errors;\n }\n\n if (process.env.NODE_ENV === \"development\") {\n stack = err.stack;\n }\n\n return res.status(statusCode).json(errorResponse(message, errors, stack));\n};\n\n// Export as a tuple with explicit types so spread works correctly\nconst errorHandler: [\n (req: Request, res: Response, next: NextFunction) => void,\n (err: any, req: Request, res: Response, next: NextFunction) => void\n] = [routeNotFoundHandler, globalErrorHandler];\n\nexport default errorHandler;"],"mappings":";AAEO,IAAM,kBAAkB,CAC7B,MACA,aACwB;AAAA,EACxB,SAAS;AAAA,EACT;AAAA,EACA;AACF;;;ACPO,IAAM,gBAAgB,CAC3B,SACA,QACA,WACmB;AAAA,EACnB,SAAS;AAAA,EACT;AAAA,EACA;AAAA,EACA;AACF;;;ACXA,IAAqB,WAArB,cAAsC,MAAM;AAAA,EAK1C,YACE,SACA,aAAa,KACb,QACA,gBAAgB,MAChB;AACA,UAAM,OAAO;AAEb,SAAK,aAAa;AAClB,SAAK,SAAS;AACd,SAAK,gBAAgB;AAErB,UAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,EAChD;AACF;;;ACjBA,IAAqB,kBAArB,cAA6C,SAAS;AAAA,EACpD,YAAY,UAAU,eAAe,QAAc;AACjD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,oBAArB,cAA+C,SAAS;AAAA,EACtD,YAAY,UAAU,gBAAgB,QAAc;AAClD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,iBAArB,cAA4C,SAAS;AAAA,EACnD,YAAY,UAAU,aAAa,QAAc;AAC/C,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,gBAArB,cAA2C,SAAS;AAAA,EAClD,YAAY,UAAU,sBAAsB,QAAc;AACxD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACJA,IAAqB,kBAArB,cAA6C,SAAS;AAAA,EACpD,YAAY,UAAU,qBAAqB,QAAc;AACvD,UAAM,SAAS,KAAK,MAAM;AAAA,EAC5B;AACF;;;ACDA,IAAM,uBAAuB,CAAC,KAAc,GAAa,SAAuB;AAC9E,OAAK,IAAI,SAAS,SAAS,IAAI,WAAW,cAAc,GAAG,CAAC;AAC9D;AAEA,IAAM,qBAAqB,CAAC,KAAU,KAAc,KAAe,SAAuB;AACxF,UAAQ,MAAM,UAAU,GAAG;AAC3B,MAAI,aAAa;AACjB,MAAI,UAAU;AACd,MAAI,SAAS;AACb,MAAI,QAAQ;AAEZ,MAAI,eAAe,UAAU;AAC3B,iBAAa,IAAI;AACjB,cAAU,IAAI;AACd,aAAS,IAAI;AAAA,EACf;AAEA,MAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,YAAQ,IAAI;AAAA,EACd;AAEA,SAAO,IAAI,OAAO,UAAU,EAAE,KAAK,cAAc,SAAS,QAAQ,KAAK,CAAC;AAC1E;AAGA,IAAM,eAGF,CAAC,sBAAsB,kBAAkB;AAE7C,IAAO,uBAAQ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexus-backend",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Backend utility library for Express.js",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",