elseware-nodejs 1.9.0 → 1.10.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/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import dotenv from 'dotenv';
2
2
  import mongoose from 'mongoose';
3
+ import { randomUUID } from 'crypto';
4
+ import { AsyncLocalStorage } from 'async_hooks';
3
5
  import { BlobServiceClient, StorageSharedKeyCredential, generateBlobSASQueryParameters, BlobSASPermissions } from '@azure/storage-blob';
4
6
  import fs3 from 'fs';
5
7
  import cloudinaryModule from 'cloudinary';
@@ -1904,7 +1906,6 @@ var CircularQueue = class {
1904
1906
  }
1905
1907
  this.data = new Array(capacity);
1906
1908
  }
1907
- capacity;
1908
1909
  data;
1909
1910
  head = 0;
1910
1911
  tail = 0;
@@ -4833,8 +4834,6 @@ var DatabaseManager = class {
4833
4834
  this.provider = provider;
4834
4835
  this.options = options;
4835
4836
  }
4836
- provider;
4837
- options;
4838
4837
  async connect() {
4839
4838
  try {
4840
4839
  await this.provider.connect();
@@ -4877,7 +4876,6 @@ var MongoDatabaseProvider = class {
4877
4876
  constructor(config) {
4878
4877
  this.config = config;
4879
4878
  }
4880
- config;
4881
4879
  async connect() {
4882
4880
  mongoose.set("strictQuery", this.config.strictQuery ?? true);
4883
4881
  await mongoose.connect(this.config.uri);
@@ -5308,7 +5306,6 @@ var JoiValidator = class {
5308
5306
  constructor(schema) {
5309
5307
  this.schema = schema;
5310
5308
  }
5311
- schema;
5312
5309
  validate(data) {
5313
5310
  const result = this.schema.validate(data, {
5314
5311
  abortEarly: false,
@@ -5337,7 +5334,6 @@ var ZodValidator = class {
5337
5334
  constructor(schema) {
5338
5335
  this.schema = schema;
5339
5336
  }
5340
- schema;
5341
5337
  validate(data) {
5342
5338
  const result = this.schema.safeParse(data);
5343
5339
  if (!result.success) {
@@ -5415,6 +5411,255 @@ function pickFields(obj, fields, options = {}) {
5415
5411
  }
5416
5412
  return result;
5417
5413
  }
5414
+ var CorrelationId = class {
5415
+ static generate() {
5416
+ return randomUUID();
5417
+ }
5418
+ };
5419
+ var RequestContext = class {
5420
+ static storage = new AsyncLocalStorage();
5421
+ static run(context, callback) {
5422
+ return this.storage.run(context, callback);
5423
+ }
5424
+ static get() {
5425
+ return this.storage.getStore();
5426
+ }
5427
+ static getCorrelationId() {
5428
+ return this.get()?.correlationId;
5429
+ }
5430
+ static getRequestId() {
5431
+ return this.get()?.requestId;
5432
+ }
5433
+ static getUserId() {
5434
+ return this.get()?.userId;
5435
+ }
5436
+ static getTenantId() {
5437
+ return this.get()?.tenantId;
5438
+ }
5439
+ };
5440
+
5441
+ // src/networking/observability/TracingHeaders.ts
5442
+ var TracingHeaders = class {
5443
+ static build() {
5444
+ const headers = {};
5445
+ const correlationId = RequestContext.getCorrelationId();
5446
+ const requestId = RequestContext.getRequestId();
5447
+ const userId = RequestContext.getUserId();
5448
+ const tenantId = RequestContext.getTenantId();
5449
+ if (correlationId) {
5450
+ headers["X-Correlation-Id"] = correlationId;
5451
+ }
5452
+ if (requestId) {
5453
+ headers["X-Request-Id"] = requestId;
5454
+ }
5455
+ if (userId) {
5456
+ headers["X-User-Id"] = userId;
5457
+ }
5458
+ if (tenantId) {
5459
+ headers["X-Tenant-Id"] = tenantId;
5460
+ }
5461
+ return headers;
5462
+ }
5463
+ };
5464
+
5465
+ // src/networking/resilience/ExponentialBackoffRetryPolicy.ts
5466
+ var ExponentialBackoffRetryPolicy = class {
5467
+ retries;
5468
+ baseDelay;
5469
+ maxDelay;
5470
+ constructor(options = {}) {
5471
+ this.retries = options.retries ?? 5;
5472
+ this.baseDelay = options.baseDelay ?? 200;
5473
+ this.maxDelay = options.maxDelay ?? 1e4;
5474
+ }
5475
+ shouldRetry(attempt) {
5476
+ return attempt < this.retries;
5477
+ }
5478
+ getDelay(attempt) {
5479
+ const delay = this.baseDelay * Math.pow(2, attempt);
5480
+ return Math.min(delay, this.maxDelay);
5481
+ }
5482
+ };
5483
+
5484
+ // src/networking/resilience/FixedRetryPolicy.ts
5485
+ var FixedRetryPolicy = class {
5486
+ retries;
5487
+ delay;
5488
+ constructor(options = {}) {
5489
+ this.retries = options.retries ?? 2;
5490
+ this.delay = options.delay ?? 500;
5491
+ }
5492
+ shouldRetry(attempt) {
5493
+ return attempt < this.retries;
5494
+ }
5495
+ getDelay(_attempt, _error) {
5496
+ return this.delay;
5497
+ }
5498
+ };
5499
+
5500
+ // src/networking/security/BearerTokenStrategy.ts
5501
+ var BearerTokenStrategy = class {
5502
+ constructor(tokenProvider) {
5503
+ this.tokenProvider = tokenProvider;
5504
+ }
5505
+ async getHeaders() {
5506
+ const token = await this.tokenProvider.getToken();
5507
+ return {
5508
+ Authorization: `Bearer ${token}`
5509
+ };
5510
+ }
5511
+ };
5512
+
5513
+ // src/networking/security/StaticTokenProvider.ts
5514
+ var StaticTokenProvider = class {
5515
+ constructor(token) {
5516
+ this.token = token;
5517
+ }
5518
+ getToken() {
5519
+ return this.token;
5520
+ }
5521
+ };
5522
+
5523
+ // src/networking/transport/HttpClient.ts
5524
+ var HttpClient = class {
5525
+ baseUrl;
5526
+ timeout;
5527
+ headers;
5528
+ serviceName;
5529
+ authStrategy;
5530
+ retryPolicy;
5531
+ constructor(baseUrl, options = {}) {
5532
+ this.baseUrl = baseUrl;
5533
+ this.timeout = options.timeout ?? 5e3;
5534
+ this.headers = options.headers ?? {};
5535
+ this.serviceName = options.serviceName ?? "UnknownService";
5536
+ this.authStrategy = options.authStrategy;
5537
+ this.retryPolicy = options.retryPolicy ?? new FixedRetryPolicy({
5538
+ retries: 2,
5539
+ delay: 500
5540
+ });
5541
+ }
5542
+ async get(path2) {
5543
+ return this.request("GET", path2);
5544
+ }
5545
+ async post(path2, body) {
5546
+ return this.request("POST", path2, body);
5547
+ }
5548
+ async put(path2, body) {
5549
+ return this.request("PUT", path2, body);
5550
+ }
5551
+ async patch(path2, body) {
5552
+ return this.request("PATCH", path2, body);
5553
+ }
5554
+ async delete(path2) {
5555
+ return this.request("DELETE", path2);
5556
+ }
5557
+ async request(method, path2, body) {
5558
+ let attempt = 0;
5559
+ let lastError;
5560
+ while (true) {
5561
+ try {
5562
+ const controller = new AbortController();
5563
+ const timeout = setTimeout(() => controller.abort(), this.timeout);
5564
+ const authHeaders = await this.authStrategy?.getHeaders();
5565
+ const response = await fetch(`${this.baseUrl}${path2}`, {
5566
+ method,
5567
+ signal: controller.signal,
5568
+ headers: {
5569
+ "Content-Type": "application/json",
5570
+ ...TracingHeaders.build(),
5571
+ ...this.headers,
5572
+ ...authHeaders
5573
+ },
5574
+ body: body !== void 0 ? JSON.stringify(body) : void 0
5575
+ });
5576
+ clearTimeout(timeout);
5577
+ const json = await response.json();
5578
+ if (!response.ok) {
5579
+ throw new AppError(
5580
+ json?.message ?? "HTTP request failed",
5581
+ response.status,
5582
+ {
5583
+ code: json?.code ?? "HTTP_REQUEST_FAILED",
5584
+ details: {
5585
+ service: this.serviceName,
5586
+ method,
5587
+ path: path2
5588
+ }
5589
+ }
5590
+ );
5591
+ }
5592
+ const correlationId2 = RequestContext.getCorrelationId();
5593
+ logger.info(
5594
+ `[${this.serviceName}]` + (correlationId2 ? ` [${correlationId2}]` : "") + ` ${method} ${path2}`
5595
+ );
5596
+ return json;
5597
+ } catch (error) {
5598
+ lastError = error;
5599
+ if (this.retryPolicy.shouldRetry(attempt, error)) {
5600
+ const delay = this.retryPolicy.getDelay(attempt, error);
5601
+ logger.warning(
5602
+ `[${this.serviceName}] retry ${attempt + 1} after ${delay}ms`
5603
+ );
5604
+ await this.sleep(delay);
5605
+ attempt++;
5606
+ continue;
5607
+ }
5608
+ break;
5609
+ }
5610
+ }
5611
+ const correlationId = RequestContext.getCorrelationId();
5612
+ logger.danger(
5613
+ `[${this.serviceName}]` + (correlationId ? ` [${correlationId}]` : "") + ` request failed`,
5614
+ lastError
5615
+ );
5616
+ throw lastError;
5617
+ }
5618
+ sleep(ms) {
5619
+ return new Promise((resolve) => setTimeout(resolve, ms));
5620
+ }
5621
+ };
5622
+
5623
+ // src/networking/services/ServiceClient.ts
5624
+ var ServiceClient = class extends HttpClient {
5625
+ async getData(path2) {
5626
+ const response = await super.get(path2);
5627
+ return response.data;
5628
+ }
5629
+ async postData(path2, body) {
5630
+ const response = await super.post(path2, body);
5631
+ return response.data;
5632
+ }
5633
+ async putData(path2, body) {
5634
+ const response = await super.put(path2, body);
5635
+ return response.data;
5636
+ }
5637
+ async patchData(path2, body) {
5638
+ const response = await super.patch(path2, body);
5639
+ return response.data;
5640
+ }
5641
+ async deleteData(path2) {
5642
+ const response = await super.delete(path2);
5643
+ return response.data;
5644
+ }
5645
+ };
5646
+
5647
+ // src/networking/services/InternalServiceClient.ts
5648
+ var InternalServiceClient = class extends ServiceClient {
5649
+ constructor(baseUrl, serviceToken, serviceName) {
5650
+ super(baseUrl, {
5651
+ serviceName,
5652
+ authStrategy: new BearerTokenStrategy(
5653
+ new StaticTokenProvider(serviceToken)
5654
+ ),
5655
+ timeout: 5e3,
5656
+ retryPolicy: new ExponentialBackoffRetryPolicy({
5657
+ retries: 3,
5658
+ baseDelay: 200
5659
+ })
5660
+ });
5661
+ }
5662
+ };
5418
5663
 
5419
5664
  // src/repositories/providers/MongoRepository.ts
5420
5665
  var MongoRepository = class {
@@ -5423,7 +5668,9 @@ var MongoRepository = class {
5423
5668
  this.model = model;
5424
5669
  }
5425
5670
  buildSelect(select) {
5426
- if (!select) return void 0;
5671
+ if (!select) {
5672
+ return void 0;
5673
+ }
5427
5674
  if (Array.isArray(select)) {
5428
5675
  return select.join(" ");
5429
5676
  }
@@ -5431,14 +5678,22 @@ var MongoRepository = class {
5431
5678
  }
5432
5679
  applyQueryOptions(query, options) {
5433
5680
  const select = this.buildSelect(options.select);
5434
- if (select) query = query.select(select);
5435
- if (options.sort) query = query.sort(options.sort);
5436
- if (options.limit) query = query.limit(options.limit);
5437
- if (options.skip) query = query.skip(options.skip);
5681
+ if (select) {
5682
+ query = query.select(select);
5683
+ }
5684
+ if (options.sort) {
5685
+ query = query.sort(options.sort);
5686
+ }
5687
+ if (options.limit) {
5688
+ query = query.limit(options.limit);
5689
+ }
5690
+ if (options.skip) {
5691
+ query = query.skip(options.skip);
5692
+ }
5438
5693
  if (options.populate) {
5439
5694
  if (Array.isArray(options.populate)) {
5440
- options.populate.forEach((p) => {
5441
- query = query.populate(p);
5695
+ options.populate.forEach((item) => {
5696
+ query = query.populate(item);
5442
5697
  });
5443
5698
  } else {
5444
5699
  query = query.populate(options.populate);
@@ -5447,7 +5702,13 @@ var MongoRepository = class {
5447
5702
  return query;
5448
5703
  }
5449
5704
  toPlain(doc) {
5450
- return doc.toObject();
5705
+ if (!doc) {
5706
+ return doc;
5707
+ }
5708
+ return typeof doc.toObject === "function" ? doc.toObject() : doc;
5709
+ }
5710
+ toPlainArray(docs) {
5711
+ return docs.map((doc) => this.toPlain(doc));
5451
5712
  }
5452
5713
  /**
5453
5714
  * Create
@@ -5458,7 +5719,7 @@ var MongoRepository = class {
5458
5719
  }
5459
5720
  async createMany(data) {
5460
5721
  const docs = await this.model.insertMany(data);
5461
- return docs.map((doc) => this.toPlain(doc));
5722
+ return this.toPlainArray(docs);
5462
5723
  }
5463
5724
  /**
5464
5725
  * Read
@@ -5466,22 +5727,26 @@ var MongoRepository = class {
5466
5727
  async findAll(filter = {}, options = {}) {
5467
5728
  let query = this.model.find(filter);
5468
5729
  query = this.applyQueryOptions(query, options);
5469
- return query.exec();
5730
+ return query.lean().exec();
5470
5731
  }
5471
5732
  async findById(id, options = {}) {
5472
5733
  let query = this.model.findById(id);
5473
5734
  query = this.applyQueryOptions(query, options);
5474
- return query.exec();
5735
+ return query.lean().exec();
5475
5736
  }
5476
5737
  async findOne(filter = {}, options = {}) {
5477
5738
  let query = this.model.findOne(filter);
5478
5739
  query = this.applyQueryOptions(query, options);
5479
- return query.exec();
5740
+ return query.lean().exec();
5480
5741
  }
5481
5742
  async findManyByIds(ids, options = {}) {
5482
- let query = this.model.find({ _id: { $in: ids } });
5743
+ let query = this.model.find({
5744
+ _id: {
5745
+ $in: ids
5746
+ }
5747
+ });
5483
5748
  query = this.applyQueryOptions(query, options);
5484
- return query.exec();
5749
+ return query.lean().exec();
5485
5750
  }
5486
5751
  async count(filter) {
5487
5752
  return this.model.countDocuments(filter).exec();
@@ -5494,12 +5759,22 @@ var MongoRepository = class {
5494
5759
  * Update
5495
5760
  */
5496
5761
  async updateById(id, data, options) {
5497
- const finalOptions = { new: true, runValidators: true, ...options };
5498
- return this.model.findByIdAndUpdate(id, data, finalOptions).exec();
5762
+ const finalOptions = {
5763
+ new: true,
5764
+ runValidators: true,
5765
+ ...options
5766
+ };
5767
+ const doc = await this.model.findByIdAndUpdate(id, data, finalOptions).exec();
5768
+ return doc ? this.toPlain(doc) : null;
5499
5769
  }
5500
5770
  async updateOne(filter, data, options) {
5501
- const finalOptions = { new: true, runValidators: true, ...options };
5502
- return this.model.findOneAndUpdate(filter, data, finalOptions).exec();
5771
+ const finalOptions = {
5772
+ new: true,
5773
+ runValidators: true,
5774
+ ...options
5775
+ };
5776
+ const doc = await this.model.findOneAndUpdate(filter, data, finalOptions).exec();
5777
+ return doc ? this.toPlain(doc) : null;
5503
5778
  }
5504
5779
  async updateMany(filter, data) {
5505
5780
  const result = await this.model.updateMany(filter, data).exec();
@@ -5509,10 +5784,12 @@ var MongoRepository = class {
5509
5784
  * Delete
5510
5785
  */
5511
5786
  async deleteById(id) {
5512
- return this.model.findByIdAndDelete(id).exec();
5787
+ const doc = await this.model.findByIdAndDelete(id).exec();
5788
+ return doc ? this.toPlain(doc) : null;
5513
5789
  }
5514
5790
  async deleteOne(filter) {
5515
- return this.model.findOneAndDelete(filter).exec();
5791
+ const doc = await this.model.findOneAndDelete(filter).exec();
5792
+ return doc ? this.toPlain(doc) : null;
5516
5793
  }
5517
5794
  async deleteMany(filter) {
5518
5795
  const result = await this.model.deleteMany(filter).exec();
@@ -5971,6 +6248,6 @@ function sleep(ms) {
5971
6248
  return new Promise((resolve) => setTimeout(resolve, ms));
5972
6249
  }
5973
6250
 
5974
- export { AVLTree, AdjacencyList, AdjacencyMatrix, ApiFeatures, ApiResponse, AppError, AsyncHandler, AzureBlobStorageService, BPlusTree, BTree, BinaryHeap, BinarySearchTree, BinaryTree, BloomFilter, CircularArray, CircularLinkedList, CircularQueue, CloudinaryService, ConsistentHash, CountMinSketch, CrudControllerFactory, DEFAULT_LIMIT, DEFAULT_PAGE, DEFAULT_SORT_DIRECTION, DatabaseManager, Deque, DirectedGraph, DisjointSetUnion, DoublyLinkedList, DynamicArray, EmailService, ErrorMiddleware, FenwickTree, FibNode, FibonacciHeap, Graph, HashMap, HashSet, HyperLogLog, IntervalTree, JWTService, JoiValidator, KDTree, LFUCache, LRUCache, MaxHeap, MaxStack, MinHeap, MinStack, MongoDatabaseProvider, MongoRepository, MulterFileHandlerService, MultiSet, Node, OrderedSet, PairingHeap, PairingNode, PriorityQueue, QUERY_RESERVED_FIELDS, QuadTree, Queue, RadixTree, RedBlackTree, SMTPProvider, SUPPORTED_OPERATORS, SegmentTree, Set2 as Set, SinglyLinkedList, SparseTable, SplayTree, Stack, StaticArray, SuffixArray, SuffixTree, TemplateEngine, TernarySearchTree, TreeNode, Trie, ZodValidator, authMiddleware, createAllowedOrigins, createCorsOptions, days, errorToString, hours, isJoiSchema, isZodSchema, loadEnv, logger, milliseconds, minutes, pickFields, seconds, sleep, toHours, toMinutes, toSeconds, validate };
6251
+ export { AVLTree, AdjacencyList, AdjacencyMatrix, ApiFeatures, ApiResponse, AppError, AsyncHandler, AzureBlobStorageService, BPlusTree, BTree, BearerTokenStrategy, BinaryHeap, BinarySearchTree, BinaryTree, BloomFilter, CircularArray, CircularLinkedList, CircularQueue, CloudinaryService, ConsistentHash, CorrelationId, CountMinSketch, CrudControllerFactory, DEFAULT_LIMIT, DEFAULT_PAGE, DEFAULT_SORT_DIRECTION, DatabaseManager, Deque, DirectedGraph, DisjointSetUnion, DoublyLinkedList, DynamicArray, EmailService, ErrorMiddleware, ExponentialBackoffRetryPolicy, FenwickTree, FibNode, FibonacciHeap, FixedRetryPolicy, Graph, HashMap, HashSet, HttpClient, HyperLogLog, InternalServiceClient, IntervalTree, JWTService, JoiValidator, KDTree, LFUCache, LRUCache, MaxHeap, MaxStack, MinHeap, MinStack, MongoDatabaseProvider, MongoRepository, MulterFileHandlerService, MultiSet, Node, OrderedSet, PairingHeap, PairingNode, PriorityQueue, QUERY_RESERVED_FIELDS, QuadTree, Queue, RadixTree, RedBlackTree, RequestContext, SMTPProvider, SUPPORTED_OPERATORS, SegmentTree, ServiceClient, Set2 as Set, SinglyLinkedList, SparseTable, SplayTree, Stack, StaticArray, StaticTokenProvider, SuffixArray, SuffixTree, TemplateEngine, TernarySearchTree, TracingHeaders, TreeNode, Trie, ZodValidator, authMiddleware, createAllowedOrigins, createCorsOptions, days, errorToString, hours, isJoiSchema, isZodSchema, loadEnv, logger, milliseconds, minutes, pickFields, seconds, sleep, toHours, toMinutes, toSeconds, validate };
5975
6252
  //# sourceMappingURL=index.js.map
5976
6253
  //# sourceMappingURL=index.js.map