@ajayjbtickets/common 1.0.9 → 1.0.11
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.mts +179 -0
- package/dist/index.d.ts +179 -10
- package/dist/index.js +524 -26
- package/dist/index.mjs +459 -0
- package/package.json +12 -5
- package/dist/config/config.d.ts +0 -10
- package/dist/config/config.js +0 -24
- package/dist/config/config.js.map +0 -1
- package/dist/constants/environments.d.ts +0 -5
- package/dist/constants/environments.js +0 -9
- package/dist/constants/environments.js.map +0 -1
- package/dist/core/ApiError.d.ts +0 -54
- package/dist/core/ApiError.js +0 -133
- package/dist/core/ApiError.js.map +0 -1
- package/dist/core/ApiResponse.d.ts +0 -83
- package/dist/core/ApiResponse.js +0 -140
- package/dist/core/ApiResponse.js.map +0 -1
- package/dist/core/Logger.d.ts +0 -2
- package/dist/core/Logger.js +0 -74
- package/dist/core/Logger.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/middlewares/schemaValidator.d.ts +0 -4
- package/dist/middlewares/schemaValidator.js +0 -21
- package/dist/middlewares/schemaValidator.js.map +0 -1
- package/dist/middlewares/verifyToken.d.ts +0 -2
- package/dist/middlewares/verifyToken.js +0 -33
- package/dist/middlewares/verifyToken.js.map +0 -1
- package/dist/services/jwt.service.d.ts +0 -10
- package/dist/services/jwt.service.js +0 -26
- package/dist/services/jwt.service.js.map +0 -1
- package/dist/services/password.service.d.ts +0 -4
- package/dist/services/password.service.js +0 -32
- package/dist/services/password.service.js.map +0 -1
- package/dist/types/validation.d.ts +0 -6
- package/dist/types/validation.js +0 -11
- package/dist/types/validation.js.map +0 -1
- package/dist/utils/asyncHandler.d.ts +0 -4
- package/dist/utils/asyncHandler.js +0 -13
- package/dist/utils/asyncHandler.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,27 +1,525 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
for (
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
AccessTokenError: () => AccessTokenError,
|
|
34
|
+
AccessTokenErrorResponse: () => AccessTokenErrorResponse,
|
|
35
|
+
ApiError: () => ApiError,
|
|
36
|
+
ApiResponse: () => ApiResponse,
|
|
37
|
+
AuthFailureError: () => AuthFailureError,
|
|
38
|
+
BadRequestError: () => BadRequestError,
|
|
39
|
+
BadRequestResponse: () => BadRequestResponse,
|
|
40
|
+
BadTokenError: () => BadTokenError,
|
|
41
|
+
BadTokenResponse: () => BadTokenResponse,
|
|
42
|
+
ENVIRONMENTS: () => ENVIRONMENTS,
|
|
43
|
+
ErrorType: () => ErrorType,
|
|
44
|
+
ForbiddenError: () => ForbiddenError,
|
|
45
|
+
ForbiddenResponse: () => ForbiddenResponse,
|
|
46
|
+
InternalError: () => InternalError,
|
|
47
|
+
InternalErrorResponse: () => InternalErrorResponse,
|
|
48
|
+
JwtService: () => JwtService,
|
|
49
|
+
NoDataError: () => NoDataError,
|
|
50
|
+
NoEntryError: () => NoEntryError,
|
|
51
|
+
NoFoundResponse: () => NoFoundResponse,
|
|
52
|
+
NotFoundError: () => NotFoundError,
|
|
53
|
+
Password: () => Password,
|
|
54
|
+
ResponseStatusCode: () => ResponseStatusCode,
|
|
55
|
+
StatusCode: () => StatusCode,
|
|
56
|
+
SuccessResponse: () => SuccessResponse,
|
|
57
|
+
TokenExpiredError: () => TokenExpiredError,
|
|
58
|
+
ValidationSource: () => ValidationSource,
|
|
59
|
+
asyncHandler: () => asyncHandler,
|
|
60
|
+
logger: () => logger,
|
|
61
|
+
schemaValidator: () => schemaValidator,
|
|
62
|
+
verifyToken: () => verifyToken
|
|
63
|
+
});
|
|
64
|
+
module.exports = __toCommonJS(index_exports);
|
|
65
|
+
|
|
66
|
+
// src/constants/environments.ts
|
|
67
|
+
var ENVIRONMENTS = {
|
|
68
|
+
production: "production",
|
|
69
|
+
development: "development",
|
|
70
|
+
testing: "testing"
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/core/ApiResponse.ts
|
|
74
|
+
var StatusCode = /* @__PURE__ */ ((StatusCode2) => {
|
|
75
|
+
StatusCode2["SUCCESS"] = "10000";
|
|
76
|
+
StatusCode2["FAILURE"] = "10001";
|
|
77
|
+
StatusCode2["RETRY"] = "10002";
|
|
78
|
+
StatusCode2["IN_VALID_ACCESS_TOKEN"] = "10003";
|
|
79
|
+
return StatusCode2;
|
|
80
|
+
})(StatusCode || {});
|
|
81
|
+
var ResponseStatusCode = /* @__PURE__ */ ((ResponseStatusCode2) => {
|
|
82
|
+
ResponseStatusCode2[ResponseStatusCode2["SUCCESS"] = 200] = "SUCCESS";
|
|
83
|
+
ResponseStatusCode2[ResponseStatusCode2["CREATED"] = 201] = "CREATED";
|
|
84
|
+
ResponseStatusCode2[ResponseStatusCode2["ACCEPTED"] = 202] = "ACCEPTED";
|
|
85
|
+
ResponseStatusCode2[ResponseStatusCode2["NO_CONTENT"] = 204] = "NO_CONTENT";
|
|
86
|
+
ResponseStatusCode2[ResponseStatusCode2["MOVED_PERMANENTLY"] = 301] = "MOVED_PERMANENTLY";
|
|
87
|
+
ResponseStatusCode2[ResponseStatusCode2["FOUND"] = 302] = "FOUND";
|
|
88
|
+
ResponseStatusCode2[ResponseStatusCode2["NOT_MODIFIED"] = 304] = "NOT_MODIFIED";
|
|
89
|
+
ResponseStatusCode2[ResponseStatusCode2["TEMPORARY_REDIRECT"] = 307] = "TEMPORARY_REDIRECT";
|
|
90
|
+
ResponseStatusCode2[ResponseStatusCode2["PERMANENT_REDIRECT"] = 308] = "PERMANENT_REDIRECT";
|
|
91
|
+
ResponseStatusCode2[ResponseStatusCode2["BAD_REQUEST"] = 400] = "BAD_REQUEST";
|
|
92
|
+
ResponseStatusCode2[ResponseStatusCode2["UNAUTHORIZED"] = 401] = "UNAUTHORIZED";
|
|
93
|
+
ResponseStatusCode2[ResponseStatusCode2["PAYMENT_REQUIRED"] = 402] = "PAYMENT_REQUIRED";
|
|
94
|
+
ResponseStatusCode2[ResponseStatusCode2["FORBIDDEN"] = 403] = "FORBIDDEN";
|
|
95
|
+
ResponseStatusCode2[ResponseStatusCode2["NOT_FOUND"] = 404] = "NOT_FOUND";
|
|
96
|
+
ResponseStatusCode2[ResponseStatusCode2["METHOD_NOT_ALLOWED"] = 405] = "METHOD_NOT_ALLOWED";
|
|
97
|
+
ResponseStatusCode2[ResponseStatusCode2["NOT_ACCEPTABLE"] = 406] = "NOT_ACCEPTABLE";
|
|
98
|
+
ResponseStatusCode2[ResponseStatusCode2["CONFLICT"] = 409] = "CONFLICT";
|
|
99
|
+
ResponseStatusCode2[ResponseStatusCode2["GONE"] = 410] = "GONE";
|
|
100
|
+
ResponseStatusCode2[ResponseStatusCode2["PAYLOAD_TOO_LARGE"] = 413] = "PAYLOAD_TOO_LARGE";
|
|
101
|
+
ResponseStatusCode2[ResponseStatusCode2["UNSUPPORTED_MEDIA_TYPE"] = 415] = "UNSUPPORTED_MEDIA_TYPE";
|
|
102
|
+
ResponseStatusCode2[ResponseStatusCode2["UNPROCESSABLE_ENTITY"] = 422] = "UNPROCESSABLE_ENTITY";
|
|
103
|
+
ResponseStatusCode2[ResponseStatusCode2["TOO_MANY_REQUESTS"] = 429] = "TOO_MANY_REQUESTS";
|
|
104
|
+
ResponseStatusCode2[ResponseStatusCode2["INTERNAL_ERROR"] = 500] = "INTERNAL_ERROR";
|
|
105
|
+
ResponseStatusCode2[ResponseStatusCode2["NOT_IMPLEMENTED"] = 501] = "NOT_IMPLEMENTED";
|
|
106
|
+
ResponseStatusCode2[ResponseStatusCode2["BAD_GATEWAY"] = 502] = "BAD_GATEWAY";
|
|
107
|
+
ResponseStatusCode2[ResponseStatusCode2["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
|
|
108
|
+
ResponseStatusCode2[ResponseStatusCode2["GATEWAY_TIMEOUT"] = 504] = "GATEWAY_TIMEOUT";
|
|
109
|
+
return ResponseStatusCode2;
|
|
110
|
+
})(ResponseStatusCode || {});
|
|
111
|
+
var ApiResponse = class {
|
|
112
|
+
message;
|
|
113
|
+
statusCode;
|
|
114
|
+
status;
|
|
115
|
+
constructor(status, message, statusCode) {
|
|
116
|
+
this.message = message;
|
|
117
|
+
this.status = status;
|
|
118
|
+
if (statusCode) {
|
|
119
|
+
this.statusCode = statusCode;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
prepare(res, response, headers = {}) {
|
|
123
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
124
|
+
res.append(key, value);
|
|
125
|
+
}
|
|
126
|
+
res.status(this.status).send(this.sanitize(response));
|
|
127
|
+
}
|
|
128
|
+
send(res, headers = {}) {
|
|
129
|
+
this.prepare(res, this, headers);
|
|
130
|
+
}
|
|
131
|
+
sanitize(response) {
|
|
132
|
+
const clone = {};
|
|
133
|
+
Object.assign(clone, response);
|
|
134
|
+
delete clone["statusCode"];
|
|
135
|
+
for (const key in clone) {
|
|
136
|
+
const typedKey = key;
|
|
137
|
+
if (clone[typedKey] === void 0) {
|
|
138
|
+
delete clone[typedKey];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return clone;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
var SuccessResponse = class extends ApiResponse {
|
|
145
|
+
constructor(responseStatusCode, message, data) {
|
|
146
|
+
super(responseStatusCode, message, "10000" /* SUCCESS */);
|
|
147
|
+
this.data = data;
|
|
148
|
+
this.data = data;
|
|
149
|
+
}
|
|
150
|
+
send(res) {
|
|
151
|
+
this.prepare(res, this, {});
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
var AccessTokenErrorResponse = class extends ApiResponse {
|
|
155
|
+
instruction = "refresh_token";
|
|
156
|
+
errors;
|
|
157
|
+
constructor(message, errors) {
|
|
158
|
+
super(
|
|
159
|
+
401 /* UNAUTHORIZED */,
|
|
160
|
+
message,
|
|
161
|
+
"10003" /* IN_VALID_ACCESS_TOKEN */
|
|
162
|
+
);
|
|
163
|
+
this.errors = errors || [];
|
|
164
|
+
}
|
|
165
|
+
send(res, headers = {}) {
|
|
166
|
+
headers.instruction = this.instruction;
|
|
167
|
+
this.prepare(res, this, headers);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
var BadTokenResponse = class extends ApiResponse {
|
|
171
|
+
errors;
|
|
172
|
+
constructor(message, errors) {
|
|
173
|
+
super(401 /* UNAUTHORIZED */, message, "10001" /* FAILURE */);
|
|
174
|
+
this.errors = errors || [];
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
var BadRequestResponse = class extends ApiResponse {
|
|
178
|
+
errors;
|
|
179
|
+
constructor(message, errors) {
|
|
180
|
+
super(400 /* BAD_REQUEST */, message, "10001" /* FAILURE */);
|
|
181
|
+
this.errors = errors || [];
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
var InternalErrorResponse = class extends ApiResponse {
|
|
185
|
+
errors;
|
|
186
|
+
constructor(message, errors) {
|
|
187
|
+
super(500 /* INTERNAL_ERROR */, message, "10001" /* FAILURE */);
|
|
188
|
+
this.errors = errors || [];
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
var NoFoundResponse = class extends ApiResponse {
|
|
192
|
+
errors;
|
|
193
|
+
constructor(message, errors) {
|
|
194
|
+
super(404 /* NOT_FOUND */, message, "10001" /* FAILURE */);
|
|
195
|
+
this.errors = errors || [];
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
var ForbiddenResponse = class extends ApiResponse {
|
|
199
|
+
errors;
|
|
200
|
+
constructor(message, errors) {
|
|
201
|
+
super(403 /* FORBIDDEN */, message, "10001" /* FAILURE */);
|
|
202
|
+
this.errors = errors || [];
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
// src/config/config.ts
|
|
207
|
+
var import_config = require("dotenv/config");
|
|
208
|
+
var getConfig = () => {
|
|
209
|
+
return {
|
|
210
|
+
ENVIRONMENT: process.env.ENVIRONMENT,
|
|
211
|
+
LOG_DIR: process.env.LOG_DIR,
|
|
212
|
+
LOG_LEVEL: process.env.LOG_LEVEL,
|
|
213
|
+
SALT_ROUNDS: parseInt(process.env.SALT_ROUNDS),
|
|
214
|
+
JWT_KEY: process.env.JWT_KEY
|
|
215
|
+
};
|
|
216
|
+
};
|
|
217
|
+
var getSanitizedConfig = (config2) => {
|
|
218
|
+
for (const [key, value] of Object.entries(config2)) {
|
|
219
|
+
if (!value) {
|
|
220
|
+
throw new Error(`Missing key ${key} in config.env`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return config2;
|
|
224
|
+
};
|
|
225
|
+
var config = getConfig();
|
|
226
|
+
var sanitizedConfig = getSanitizedConfig(config);
|
|
227
|
+
|
|
228
|
+
// src/core/ApiError.ts
|
|
229
|
+
var ErrorType = /* @__PURE__ */ ((ErrorType2) => {
|
|
230
|
+
ErrorType2["BAD_TOKEN"] = "BadTokenError";
|
|
231
|
+
ErrorType2["TOKEN_EXPIRED"] = "TokenExpiredError";
|
|
232
|
+
ErrorType2["UNAUTHORIZED"] = "AuthFailureError";
|
|
233
|
+
ErrorType2["ACCESS_TOKEN"] = "AccessTokenError";
|
|
234
|
+
ErrorType2["INTERNAL"] = "InternalError";
|
|
235
|
+
ErrorType2["NOT_FOUND"] = "NotFoundError";
|
|
236
|
+
ErrorType2["NO_ENTRY"] = "NoEntryError";
|
|
237
|
+
ErrorType2["NO_DATA"] = "NoDataError";
|
|
238
|
+
ErrorType2["BAD_REQUEST"] = "BadRequestError";
|
|
239
|
+
ErrorType2["FORBIDDEN"] = "ForbiddenError";
|
|
240
|
+
return ErrorType2;
|
|
241
|
+
})(ErrorType || {});
|
|
242
|
+
var ApiError = class extends Error {
|
|
243
|
+
constructor(type, message = "Error", errors) {
|
|
244
|
+
super(type);
|
|
245
|
+
this.type = type;
|
|
246
|
+
this.message = message;
|
|
247
|
+
this.errors = errors;
|
|
248
|
+
this.type = type;
|
|
249
|
+
this.message = message;
|
|
250
|
+
this.errors = errors;
|
|
251
|
+
}
|
|
252
|
+
static handle(err, res) {
|
|
253
|
+
switch (err.type) {
|
|
254
|
+
case "BadTokenError" /* BAD_TOKEN */:
|
|
255
|
+
case "TokenExpiredError" /* TOKEN_EXPIRED */:
|
|
256
|
+
case "AuthFailureError" /* UNAUTHORIZED */:
|
|
257
|
+
new BadTokenResponse(err.message, err.errors).send(res);
|
|
258
|
+
break;
|
|
259
|
+
case "AccessTokenError" /* ACCESS_TOKEN */:
|
|
260
|
+
new AccessTokenErrorResponse(err.message, err.errors).send(res);
|
|
261
|
+
break;
|
|
262
|
+
case "InternalError" /* INTERNAL */:
|
|
263
|
+
new InternalErrorResponse(err.message, err.errors).send(res);
|
|
264
|
+
break;
|
|
265
|
+
case "NotFoundError" /* NOT_FOUND */:
|
|
266
|
+
case "NoEntryError" /* NO_ENTRY */:
|
|
267
|
+
case "NoDataError" /* NO_DATA */:
|
|
268
|
+
new NoFoundResponse(err.message, err.errors).send(res);
|
|
269
|
+
break;
|
|
270
|
+
case "BadRequestError" /* BAD_REQUEST */:
|
|
271
|
+
new BadRequestResponse(err.message, err.errors).send(res);
|
|
272
|
+
break;
|
|
273
|
+
case "ForbiddenError" /* FORBIDDEN */:
|
|
274
|
+
new ForbiddenResponse(err.message, err.errors).send(res);
|
|
275
|
+
break;
|
|
276
|
+
default:
|
|
277
|
+
let messsage = err.message;
|
|
278
|
+
messsage = sanitizedConfig.ENVIRONMENT !== ENVIRONMENTS.production ? messsage : "Something went wrong!";
|
|
279
|
+
new InternalErrorResponse(messsage, err.errors).send(res);
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
var BadTokenError = class extends ApiError {
|
|
285
|
+
constructor(message = "Invalid token", errors = []) {
|
|
286
|
+
super("BadTokenError" /* BAD_TOKEN */, message, errors);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
var TokenExpiredError = class extends ApiError {
|
|
290
|
+
constructor(message = "Token expired", errors = []) {
|
|
291
|
+
super("TokenExpiredError" /* TOKEN_EXPIRED */, message, errors);
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
var AuthFailureError = class extends ApiError {
|
|
295
|
+
constructor(message = "Invalid Credentials", errors = []) {
|
|
296
|
+
super("AuthFailureError" /* UNAUTHORIZED */, message, errors);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
var AccessTokenError = class extends ApiError {
|
|
300
|
+
constructor(message = "Invalid access token", errors = []) {
|
|
301
|
+
super("AccessTokenError" /* ACCESS_TOKEN */, message, errors);
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
var InternalError = class extends ApiError {
|
|
305
|
+
constructor(message = "Internal error", errors = []) {
|
|
306
|
+
message = sanitizedConfig.ENVIRONMENT !== ENVIRONMENTS.production ? message : "Something went wrong!";
|
|
307
|
+
super("InternalError" /* INTERNAL */, message, errors);
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
var NotFoundError = class extends ApiError {
|
|
311
|
+
constructor(message = "Not found", errors = []) {
|
|
312
|
+
super("NotFoundError" /* NOT_FOUND */, message, errors);
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
var NoEntryError = class extends ApiError {
|
|
316
|
+
constructor(message = "Entry don't exists", errors = []) {
|
|
317
|
+
super("NoEntryError" /* NO_ENTRY */, message, errors);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
var NoDataError = class extends ApiError {
|
|
321
|
+
constructor(message = "No data", errors = []) {
|
|
322
|
+
super("NoDataError" /* NO_DATA */, message, errors);
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
var BadRequestError = class extends ApiError {
|
|
326
|
+
constructor(message = "Bad request", errors = []) {
|
|
327
|
+
super("BadRequestError" /* BAD_REQUEST */, message, errors);
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
var ForbiddenError = class extends ApiError {
|
|
331
|
+
constructor(message = "Permission denied", errors = []) {
|
|
332
|
+
super("ForbiddenError" /* FORBIDDEN */, message, errors);
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
// src/core/Logger.ts
|
|
337
|
+
var import_winston = __toESM(require("winston"));
|
|
338
|
+
var import_fs = __toESM(require("fs"));
|
|
339
|
+
var import_path = __toESM(require("path"));
|
|
340
|
+
var import_winston_daily_rotate_file = __toESM(require("winston-daily-rotate-file"));
|
|
341
|
+
var dir = sanitizedConfig.LOG_DIR;
|
|
342
|
+
if (!dir) {
|
|
343
|
+
dir = import_path.default.resolve("logs");
|
|
344
|
+
}
|
|
345
|
+
if (!import_fs.default.existsSync(dir)) {
|
|
346
|
+
import_fs.default.mkdirSync(dir);
|
|
347
|
+
}
|
|
348
|
+
var logfileRotateTransport = new import_winston_daily_rotate_file.default({
|
|
349
|
+
level: sanitizedConfig.LOG_LEVEL,
|
|
350
|
+
// @ts-ignore
|
|
351
|
+
filename: dir + "/%DATE%.log",
|
|
352
|
+
datePattern: "YYYY-MM-DD",
|
|
353
|
+
zippedArchive: true,
|
|
354
|
+
handleExceptions: true,
|
|
355
|
+
maxSize: "20m",
|
|
356
|
+
maxFiles: "14d",
|
|
357
|
+
format: import_winston.format.combine(
|
|
358
|
+
import_winston.format.errors({ stack: true }),
|
|
359
|
+
import_winston.format.timestamp(),
|
|
360
|
+
import_winston.format.json()
|
|
361
|
+
)
|
|
362
|
+
});
|
|
363
|
+
var consoleTransport = new import_winston.default.transports.Console({
|
|
364
|
+
level: process.env.LOG_LEVEL,
|
|
365
|
+
format: import_winston.format.combine(
|
|
366
|
+
import_winston.format.colorize({ all: true }),
|
|
367
|
+
import_winston.format.timestamp({
|
|
368
|
+
format: "YYYY-MM-DD hh:mm:ss.SSS A"
|
|
369
|
+
}),
|
|
370
|
+
import_winston.format.align(),
|
|
371
|
+
import_winston.format.printf(
|
|
372
|
+
(info) => `[${info.timestamp}] ${info.level}: ${info.message}`
|
|
373
|
+
)
|
|
374
|
+
)
|
|
375
|
+
});
|
|
376
|
+
var logger = import_winston.default.createLogger({
|
|
377
|
+
transports: [consoleTransport, logfileRotateTransport],
|
|
378
|
+
exceptionHandlers: [logfileRotateTransport],
|
|
379
|
+
exitOnError: false
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
// src/middlewares/schemaValidator.ts
|
|
383
|
+
var schemaValidator = (type, schema) => {
|
|
384
|
+
return (req, res, next) => {
|
|
385
|
+
const result = schema.safeParse(req[type]);
|
|
386
|
+
if (!result.success) {
|
|
387
|
+
const errors = Object.entries(result.error.flatten().fieldErrors).map(
|
|
388
|
+
([key, value]) => ({
|
|
389
|
+
field: key,
|
|
390
|
+
message: value
|
|
391
|
+
})
|
|
392
|
+
);
|
|
393
|
+
next(new BadRequestError("Bad request", errors));
|
|
394
|
+
} else {
|
|
395
|
+
next();
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
// src/middlewares/verifyToken.ts
|
|
401
|
+
var import_jsonwebtoken2 = __toESM(require("jsonwebtoken"));
|
|
402
|
+
|
|
403
|
+
// src/services/jwt.service.ts
|
|
404
|
+
var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
|
|
405
|
+
var JwtService = class {
|
|
406
|
+
static sign(payload, signOptions = {}) {
|
|
407
|
+
const token = import_jsonwebtoken.default.sign(
|
|
408
|
+
payload,
|
|
409
|
+
sanitizedConfig.JWT_KEY,
|
|
410
|
+
signOptions
|
|
411
|
+
);
|
|
412
|
+
return token;
|
|
413
|
+
}
|
|
414
|
+
static verify(token, verifyOptions) {
|
|
415
|
+
const decoded = import_jsonwebtoken.default.verify(
|
|
416
|
+
token,
|
|
417
|
+
sanitizedConfig.JWT_KEY,
|
|
418
|
+
verifyOptions
|
|
419
|
+
);
|
|
420
|
+
return decoded;
|
|
421
|
+
}
|
|
422
|
+
static generatePayload(user) {
|
|
423
|
+
return {
|
|
424
|
+
_id: user._id.toString(),
|
|
425
|
+
email: user.email
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
// src/middlewares/verifyToken.ts
|
|
431
|
+
var verifyToken = async (req, res, next) => {
|
|
432
|
+
try {
|
|
433
|
+
const { token } = req?.session;
|
|
434
|
+
if (!token) {
|
|
435
|
+
next(new BadTokenError("Authentication token is missing"));
|
|
436
|
+
}
|
|
437
|
+
const decoded = JwtService.verify(token);
|
|
438
|
+
req.user = decoded;
|
|
439
|
+
next();
|
|
440
|
+
} catch (error) {
|
|
441
|
+
if (error instanceof import_jsonwebtoken2.default.TokenExpiredError) {
|
|
442
|
+
next(new TokenExpiredError());
|
|
443
|
+
} else if (error instanceof import_jsonwebtoken2.default.JsonWebTokenError) {
|
|
444
|
+
next(new BadTokenError());
|
|
445
|
+
} else {
|
|
446
|
+
next(new BadTokenError());
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
// src/services/password.service.ts
|
|
452
|
+
var import_bcrypt = __toESM(require("bcrypt"));
|
|
453
|
+
var Password = class {
|
|
454
|
+
static hashPassword(password) {
|
|
455
|
+
return new Promise((resolve, reject) => {
|
|
456
|
+
import_bcrypt.default.hash(password, sanitizedConfig.SALT_ROUNDS, function(err, hash) {
|
|
457
|
+
if (err) {
|
|
458
|
+
reject(err.message);
|
|
459
|
+
}
|
|
460
|
+
resolve(hash);
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
static comparePassword(password, hashedPassword) {
|
|
465
|
+
return new Promise((resolve, reject) => {
|
|
466
|
+
import_bcrypt.default.compare(password, hashedPassword, function(err, result) {
|
|
467
|
+
if (err) {
|
|
468
|
+
reject(err.message);
|
|
469
|
+
}
|
|
470
|
+
resolve(result);
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
// src/types/validation.ts
|
|
477
|
+
var ValidationSource = /* @__PURE__ */ ((ValidationSource2) => {
|
|
478
|
+
ValidationSource2["BODY"] = "body";
|
|
479
|
+
ValidationSource2["HEADER"] = "headers";
|
|
480
|
+
ValidationSource2["QUERY"] = "query";
|
|
481
|
+
ValidationSource2["PARAM"] = "params";
|
|
482
|
+
return ValidationSource2;
|
|
483
|
+
})(ValidationSource || {});
|
|
484
|
+
|
|
485
|
+
// src/utils/asyncHandler.ts
|
|
486
|
+
var asyncHandler = (execution) => async (req, res, next) => {
|
|
487
|
+
try {
|
|
488
|
+
await execution(req, res, next);
|
|
489
|
+
} catch (err) {
|
|
490
|
+
next(err);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
494
|
+
0 && (module.exports = {
|
|
495
|
+
AccessTokenError,
|
|
496
|
+
AccessTokenErrorResponse,
|
|
497
|
+
ApiError,
|
|
498
|
+
ApiResponse,
|
|
499
|
+
AuthFailureError,
|
|
500
|
+
BadRequestError,
|
|
501
|
+
BadRequestResponse,
|
|
502
|
+
BadTokenError,
|
|
503
|
+
BadTokenResponse,
|
|
504
|
+
ENVIRONMENTS,
|
|
505
|
+
ErrorType,
|
|
506
|
+
ForbiddenError,
|
|
507
|
+
ForbiddenResponse,
|
|
508
|
+
InternalError,
|
|
509
|
+
InternalErrorResponse,
|
|
510
|
+
JwtService,
|
|
511
|
+
NoDataError,
|
|
512
|
+
NoEntryError,
|
|
513
|
+
NoFoundResponse,
|
|
514
|
+
NotFoundError,
|
|
515
|
+
Password,
|
|
516
|
+
ResponseStatusCode,
|
|
517
|
+
StatusCode,
|
|
518
|
+
SuccessResponse,
|
|
519
|
+
TokenExpiredError,
|
|
520
|
+
ValidationSource,
|
|
521
|
+
asyncHandler,
|
|
522
|
+
logger,
|
|
523
|
+
schemaValidator,
|
|
524
|
+
verifyToken
|
|
525
|
+
});
|