elseware-nodejs 1.3.1 → 1.5.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/README.md CHANGED
@@ -1 +1 @@
1
- # elseware-nodejs
1
+ # elseware-nodejs
package/dist/index.cjs CHANGED
@@ -5,6 +5,7 @@ var fs = require('fs');
5
5
  var cloudinaryModule = require('cloudinary');
6
6
  var mongoose = require('mongoose');
7
7
  var dotenv = require('dotenv');
8
+ var jwt = require('jsonwebtoken');
8
9
 
9
10
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
11
 
@@ -12,6 +13,7 @@ var fs__default = /*#__PURE__*/_interopDefault(fs);
12
13
  var cloudinaryModule__default = /*#__PURE__*/_interopDefault(cloudinaryModule);
13
14
  var mongoose__default = /*#__PURE__*/_interopDefault(mongoose);
14
15
  var dotenv__default = /*#__PURE__*/_interopDefault(dotenv);
16
+ var jwt__default = /*#__PURE__*/_interopDefault(jwt);
15
17
 
16
18
  // src/errors/appError.ts
17
19
  var AppError = class extends Error {
@@ -212,6 +214,42 @@ var APIFactory = class {
212
214
  });
213
215
  }
214
216
  };
217
+
218
+ // src/api/pickFields.ts
219
+ function pickFields(obj, fields, options = {}) {
220
+ const removeUndefined = options.removeUndefined ?? false;
221
+ const removeNull = options.removeNull ?? false;
222
+ const removeEmptyString = options.removeEmptyString ?? false;
223
+ const strict = options.strict ?? false;
224
+ const defaults = options.defaults ?? {};
225
+ const rename = options.rename ?? {};
226
+ const transform = options.transform ?? {};
227
+ const result = {};
228
+ if (strict) {
229
+ for (const key of Object.keys(obj)) {
230
+ if (!fields.includes(key)) {
231
+ throw new Error(`Unknown field: ${key}`);
232
+ }
233
+ }
234
+ }
235
+ for (const field of fields) {
236
+ let value = obj[field];
237
+ if (value === void 0 && field in defaults) {
238
+ value = defaults[field];
239
+ }
240
+ if (removeUndefined && value === void 0) continue;
241
+ if (removeNull && value === null) continue;
242
+ if (removeEmptyString && value === "") continue;
243
+ if (transform[field]) {
244
+ value = transform[field](value);
245
+ }
246
+ const outputKey = rename[field] ?? field;
247
+ if (Object.prototype.hasOwnProperty.call(obj, field) || field in defaults) {
248
+ result[outputKey] = value;
249
+ }
250
+ }
251
+ return result;
252
+ }
215
253
  var AzureBlobService = class {
216
254
  containerName;
217
255
  blobServiceClient;
@@ -5259,51 +5297,96 @@ var validate = (schema) => (req, _res, next) => {
5259
5297
  }
5260
5298
  next();
5261
5299
  };
5262
-
5263
- // src/services/base.service.ts
5264
- var BaseService = class {
5265
- model;
5266
- resourceName;
5267
- constructor(model, resourceName) {
5268
- this.model = model;
5269
- this.resourceName = resourceName;
5270
- }
5271
- async create(data) {
5272
- const doc = await this.model.create(data);
5273
- return doc;
5274
- }
5275
- async findAll() {
5276
- return this.model.find();
5277
- }
5278
- async findById(id) {
5279
- const doc = await this.model.findById(id);
5280
- if (!doc) {
5281
- throw new AppError(`${this.resourceName} not found`, 404, {
5282
- code: `${this.resourceName.toUpperCase()}_NOT_FOUND`
5283
- });
5284
- }
5285
- return doc;
5300
+ var JWTService = class {
5301
+ accessSecret;
5302
+ refreshSecret;
5303
+ accessExpires;
5304
+ refreshExpires;
5305
+ nodeEnv;
5306
+ refreshCookieName;
5307
+ constructor(options) {
5308
+ this.accessSecret = options.accessSecret;
5309
+ this.refreshSecret = options.refreshSecret;
5310
+ this.accessExpires = options.accessExpires || "15m";
5311
+ this.refreshExpires = options.refreshExpires || "30d";
5312
+ this.nodeEnv = options.nodeEnv || "development";
5313
+ this.refreshCookieName = options.refreshCookieName || "refreshJwt";
5314
+ }
5315
+ // ---------------------------------
5316
+ // Access and Refresh Token Creation
5317
+ // ---------------------------------
5318
+ createAccessToken(payload) {
5319
+ return jwt__default.default.sign(payload, this.accessSecret, {
5320
+ expiresIn: this.accessExpires
5321
+ });
5286
5322
  }
5287
- async updateById(id, data) {
5288
- const doc = await this.model.findByIdAndUpdate(id, data, {
5289
- new: true,
5290
- runValidators: true
5323
+ createRefreshToken(payload) {
5324
+ return jwt__default.default.sign(payload, this.refreshSecret, {
5325
+ expiresIn: this.refreshExpires
5291
5326
  });
5292
- if (!doc) {
5293
- throw new AppError(`${this.resourceName} not found`, 404, {
5294
- code: `${this.resourceName.toUpperCase()}_NOT_FOUND`
5295
- });
5327
+ }
5328
+ // ---------------------------------
5329
+ // Token Verification
5330
+ // ---------------------------------
5331
+ verifyAccessToken(token) {
5332
+ try {
5333
+ return jwt__default.default.verify(token, this.accessSecret);
5334
+ } catch (err) {
5335
+ console.error("Access token verification failed:", err.message);
5336
+ return null;
5296
5337
  }
5297
- return doc;
5298
5338
  }
5299
- async deleteById(id) {
5300
- const doc = await this.model.findByIdAndDelete(id);
5301
- if (!doc) {
5302
- throw new AppError(`${this.resourceName} not found`, 404, {
5303
- code: `${this.resourceName.toUpperCase()}_NOT_FOUND`
5304
- });
5339
+ verifyRefreshToken(token) {
5340
+ try {
5341
+ return jwt__default.default.verify(token, this.refreshSecret);
5342
+ } catch (err) {
5343
+ console.error("Refresh token verification failed:", err.message);
5344
+ return null;
5345
+ }
5346
+ }
5347
+ // ---------------------------------
5348
+ // Token Extraction
5349
+ // ---------------------------------
5350
+ extractToken(req) {
5351
+ if (req.headers.authorization?.startsWith("Bearer ")) {
5352
+ return req.headers.authorization.split(" ")[1];
5353
+ }
5354
+ if (req.cookies?.[this.refreshCookieName]) {
5355
+ return req.cookies[this.refreshCookieName];
5305
5356
  }
5306
- return doc;
5357
+ if (req.cookies?.jwt) {
5358
+ return req.cookies.jwt;
5359
+ }
5360
+ return null;
5361
+ }
5362
+ // ---------------------------------
5363
+ // Cookie Helpers
5364
+ // ---------------------------------
5365
+ setRefreshTokenCookie(res, refreshToken) {
5366
+ res.cookie(this.refreshCookieName, refreshToken, {
5367
+ httpOnly: true,
5368
+ secure: this.nodeEnv === "production",
5369
+ sameSite: this.nodeEnv === "production" ? "none" : "lax",
5370
+ maxAge: 30 * 24 * 60 * 60 * 1e3,
5371
+ path: "/"
5372
+ });
5373
+ }
5374
+ clearRefreshTokenCookie(res) {
5375
+ res.clearCookie(this.refreshCookieName, {
5376
+ httpOnly: true,
5377
+ secure: this.nodeEnv === "production",
5378
+ sameSite: this.nodeEnv === "production" ? "none" : "lax",
5379
+ path: "/"
5380
+ });
5381
+ }
5382
+ // ---------------------------------
5383
+ // Login Helpers
5384
+ // ---------------------------------
5385
+ sendAuthTokens(res, payload) {
5386
+ const accessToken = this.createAccessToken(payload);
5387
+ const refreshToken = this.createRefreshToken(payload);
5388
+ this.setRefreshTokenCookie(res, refreshToken);
5389
+ return accessToken;
5307
5390
  }
5308
5391
  };
5309
5392
 
@@ -5346,7 +5429,6 @@ exports.AppError = AppError;
5346
5429
  exports.AzureBlobService = AzureBlobService;
5347
5430
  exports.BPlusTree = BPlusTree;
5348
5431
  exports.BTree = BTree;
5349
- exports.BaseService = BaseService;
5350
5432
  exports.BinaryHeap = BinaryHeap;
5351
5433
  exports.BinarySearchTree = BinarySearchTree;
5352
5434
  exports.BinaryTree = BinaryTree;
@@ -5371,6 +5453,7 @@ exports.HashMap = HashMap;
5371
5453
  exports.HashSet = HashSet;
5372
5454
  exports.HyperLogLog = HyperLogLog;
5373
5455
  exports.IntervalTree = IntervalTree;
5456
+ exports.JWTService = JWTService;
5374
5457
  exports.KDTree = KDTree;
5375
5458
  exports.LFUCache = LFUCache;
5376
5459
  exports.LRUCache = LRUCache;
@@ -5412,6 +5495,7 @@ exports.loadEnv = loadEnv;
5412
5495
  exports.logger = logger;
5413
5496
  exports.milliseconds = milliseconds;
5414
5497
  exports.minutes = minutes;
5498
+ exports.pickFields = pickFields;
5415
5499
  exports.seconds = seconds;
5416
5500
  exports.sleep = sleep;
5417
5501
  exports.toHours = toHours;