apienvelope 1.0.0 → 1.1.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apienvelope",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Production-grade npm package for standardized API response formatting in Express applications",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -12,11 +12,12 @@
12
12
  "require": "./dist/index.js"
13
13
  }
14
14
  },
15
+ "sideEffects": false,
15
16
  "files": [
16
17
  "dist"
17
18
  ],
18
19
  "scripts": {
19
- "build": "tsup src/index.ts --format cjs,esm --dts --clean",
20
+ "build": "tsup && rm -f dist/*.d.mts",
20
21
  "test": "vitest run",
21
22
  "test:watch": "vitest",
22
23
  "test:coverage": "vitest run --coverage",
@@ -1,213 +0,0 @@
1
- // src/errors/ApiError.ts
2
- var ApiError = class _ApiError extends Error {
3
- code;
4
- statusCode;
5
- details;
6
- fields;
7
- context;
8
- isOperational;
9
- timestamp;
10
- constructor(message, options = {}) {
11
- super(message);
12
- this.name = this.constructor.name;
13
- this.code = options.code || "INTERNAL_ERROR";
14
- this.statusCode = options.statusCode || 500;
15
- this.details = options.details;
16
- this.fields = options.fields;
17
- this.context = options.context;
18
- this.isOperational = options.isOperational ?? true;
19
- this.timestamp = (/* @__PURE__ */ new Date()).toISOString();
20
- Error.captureStackTrace(this, this.constructor);
21
- if (options.cause) {
22
- this.cause = options.cause;
23
- }
24
- }
25
- /**
26
- * Serialize error for response
27
- */
28
- serialize(includeStack = false) {
29
- const serialized = {
30
- code: this.code,
31
- message: this.message,
32
- statusCode: this.statusCode
33
- };
34
- if (this.details) {
35
- serialized.details = this.details;
36
- }
37
- if (this.fields) {
38
- serialized.fields = this.fields;
39
- }
40
- if (this.context) {
41
- serialized.context = this.context;
42
- }
43
- if (includeStack && this.stack) {
44
- serialized.stack = this.stack;
45
- }
46
- return serialized;
47
- }
48
- /**
49
- * Get error chain for nested errors
50
- */
51
- getErrorChain() {
52
- const chain = [];
53
- let current = this;
54
- const seen = /* @__PURE__ */ new WeakSet();
55
- while (current && !seen.has(current)) {
56
- seen.add(current);
57
- chain.push({
58
- name: current.name,
59
- message: current.message,
60
- code: current instanceof _ApiError ? current.code : void 0
61
- });
62
- current = current.cause;
63
- }
64
- return chain;
65
- }
66
- /**
67
- * Create a new error with additional context
68
- */
69
- withContext(context) {
70
- return new _ApiError(this.message, {
71
- code: this.code,
72
- statusCode: this.statusCode,
73
- details: this.details,
74
- fields: this.fields,
75
- context: { ...this.context, ...context },
76
- cause: this.cause,
77
- isOperational: this.isOperational
78
- });
79
- }
80
- /**
81
- * Convert to JSON for logging
82
- */
83
- toJSON() {
84
- return {
85
- name: this.name,
86
- message: this.message,
87
- code: this.code,
88
- statusCode: this.statusCode,
89
- details: this.details,
90
- fields: this.fields,
91
- context: this.context,
92
- isOperational: this.isOperational,
93
- timestamp: this.timestamp,
94
- stack: this.stack
95
- };
96
- }
97
- };
98
-
99
- // src/errors/predefined.ts
100
- var ValidationError = class extends ApiError {
101
- constructor(message = "Validation failed", fields, options = {}) {
102
- super(message, {
103
- ...options,
104
- code: "VALIDATION_ERROR",
105
- statusCode: 400,
106
- fields
107
- });
108
- }
109
- };
110
- var NotFoundError = class extends ApiError {
111
- constructor(message = "Resource not found", options = {}) {
112
- super(message, {
113
- ...options,
114
- code: "NOT_FOUND",
115
- statusCode: 404
116
- });
117
- }
118
- };
119
- var UnauthorizedError = class extends ApiError {
120
- constructor(message = "Authentication required", options = {}) {
121
- super(message, {
122
- ...options,
123
- code: "UNAUTHORIZED",
124
- statusCode: 401
125
- });
126
- }
127
- };
128
- var ForbiddenError = class extends ApiError {
129
- constructor(message = "Access forbidden", options = {}) {
130
- super(message, {
131
- ...options,
132
- code: "FORBIDDEN",
133
- statusCode: 403
134
- });
135
- }
136
- };
137
- var ConflictError = class extends ApiError {
138
- constructor(message = "Resource conflict", options = {}) {
139
- super(message, {
140
- ...options,
141
- code: "CONFLICT",
142
- statusCode: 409
143
- });
144
- }
145
- };
146
- var InternalServerError = class extends ApiError {
147
- constructor(message = "Internal server error", options = {}) {
148
- super(message, {
149
- ...options,
150
- code: "INTERNAL_ERROR",
151
- statusCode: 500,
152
- isOperational: false
153
- });
154
- }
155
- };
156
- var BadRequestError = class extends ApiError {
157
- constructor(message = "Bad request", options = {}) {
158
- super(message, {
159
- ...options,
160
- code: "BAD_REQUEST",
161
- statusCode: 400
162
- });
163
- }
164
- };
165
- var RateLimitError = class extends ApiError {
166
- retryAfter;
167
- constructor(message = "Rate limit exceeded", retryAfter, options = {}) {
168
- super(message, {
169
- ...options,
170
- code: "RATE_LIMIT_EXCEEDED",
171
- statusCode: 429,
172
- details: retryAfter ? { retryAfter } : void 0
173
- });
174
- this.retryAfter = retryAfter;
175
- }
176
- };
177
- var ServiceUnavailableError = class extends ApiError {
178
- constructor(message = "Service temporarily unavailable", options = {}) {
179
- super(message, {
180
- ...options,
181
- code: "SERVICE_UNAVAILABLE",
182
- statusCode: 503
183
- });
184
- }
185
- };
186
- var UnprocessableEntityError = class extends ApiError {
187
- constructor(message = "Unprocessable entity", options = {}) {
188
- super(message, {
189
- ...options,
190
- code: "UNPROCESSABLE_ENTITY",
191
- statusCode: 422
192
- });
193
- }
194
- };
195
- function createError(ErrorClass, message, context) {
196
- const error = new ErrorClass(message);
197
- return context ? error.withContext(context) : error;
198
- }
199
-
200
- export {
201
- ApiError,
202
- ValidationError,
203
- NotFoundError,
204
- UnauthorizedError,
205
- ForbiddenError,
206
- ConflictError,
207
- InternalServerError,
208
- BadRequestError,
209
- RateLimitError,
210
- ServiceUnavailableError,
211
- UnprocessableEntityError,
212
- createError
213
- };