elseware-nodejs 1.9.1 → 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.d.cts CHANGED
@@ -1994,6 +1994,125 @@ declare class ZodValidator implements IValidator {
1994
1994
  validate<T = unknown>(data: unknown): ValidationResult<T>;
1995
1995
  }
1996
1996
 
1997
+ declare class CorrelationId {
1998
+ static generate(): string;
1999
+ }
2000
+
2001
+ interface RequestContextData {
2002
+ correlationId: string;
2003
+ requestId?: string;
2004
+ userId?: string;
2005
+ tenantId?: string;
2006
+ }
2007
+ declare class RequestContext {
2008
+ private static readonly storage;
2009
+ static run<T>(context: RequestContextData, callback: () => T): T;
2010
+ static get(): RequestContextData | undefined;
2011
+ static getCorrelationId(): string | undefined;
2012
+ static getRequestId(): string | undefined;
2013
+ static getUserId(): string | undefined;
2014
+ static getTenantId(): string | undefined;
2015
+ }
2016
+
2017
+ declare class TracingHeaders {
2018
+ static build(): Record<string, string>;
2019
+ }
2020
+
2021
+ interface RetryPolicy {
2022
+ shouldRetry(attempt: number, error?: unknown): boolean;
2023
+ getDelay(attempt: number, error?: unknown): number;
2024
+ }
2025
+
2026
+ interface ExponentialBackoffOptions {
2027
+ retries?: number;
2028
+ baseDelay?: number;
2029
+ maxDelay?: number;
2030
+ }
2031
+ declare class ExponentialBackoffRetryPolicy implements RetryPolicy {
2032
+ private readonly retries;
2033
+ private readonly baseDelay;
2034
+ private readonly maxDelay;
2035
+ constructor(options?: ExponentialBackoffOptions);
2036
+ shouldRetry(attempt: number): boolean;
2037
+ getDelay(attempt: number): number;
2038
+ }
2039
+
2040
+ interface FixedRetryPolicyOptions {
2041
+ retries?: number;
2042
+ delay?: number;
2043
+ }
2044
+ declare class FixedRetryPolicy implements RetryPolicy {
2045
+ private readonly retries;
2046
+ private readonly delay;
2047
+ constructor(options?: FixedRetryPolicyOptions);
2048
+ shouldRetry(attempt: number): boolean;
2049
+ getDelay(_attempt: number, _error?: unknown): number;
2050
+ }
2051
+
2052
+ interface AuthStrategy {
2053
+ getHeaders(): Promise<Record<string, string>>;
2054
+ }
2055
+
2056
+ interface TokenProvider {
2057
+ getToken(): Promise<string> | string;
2058
+ }
2059
+
2060
+ declare class BearerTokenStrategy implements AuthStrategy {
2061
+ private readonly tokenProvider;
2062
+ constructor(tokenProvider: TokenProvider);
2063
+ getHeaders(): Promise<Record<string, string>>;
2064
+ }
2065
+
2066
+ declare class StaticTokenProvider implements TokenProvider {
2067
+ private readonly token;
2068
+ constructor(token: string);
2069
+ getToken(): string;
2070
+ }
2071
+
2072
+ interface HttpClientOptions {
2073
+ timeout?: number;
2074
+ headers?: Record<string, string>;
2075
+ serviceName?: string;
2076
+ retryPolicy?: RetryPolicy;
2077
+ }
2078
+
2079
+ declare class HttpClient {
2080
+ protected readonly baseUrl: string;
2081
+ protected readonly timeout: number;
2082
+ protected readonly headers: Record<string, string>;
2083
+ protected readonly serviceName: string;
2084
+ protected readonly authStrategy?: AuthStrategy;
2085
+ protected readonly retryPolicy: RetryPolicy;
2086
+ constructor(baseUrl: string, options?: HttpClientOptions & {
2087
+ authStrategy?: AuthStrategy;
2088
+ });
2089
+ protected get<T>(path: string): Promise<T>;
2090
+ protected post<T>(path: string, body?: unknown): Promise<T>;
2091
+ protected put<T>(path: string, body?: unknown): Promise<T>;
2092
+ protected patch<T>(path: string, body?: unknown): Promise<T>;
2093
+ protected delete<T>(path: string): Promise<T>;
2094
+ private request;
2095
+ private sleep;
2096
+ }
2097
+
2098
+ declare abstract class ServiceClient extends HttpClient {
2099
+ protected getData<T>(path: string): Promise<T>;
2100
+ protected postData<T>(path: string, body?: unknown): Promise<T>;
2101
+ protected putData<T>(path: string, body?: unknown): Promise<T>;
2102
+ protected patchData<T>(path: string, body?: unknown): Promise<T>;
2103
+ protected deleteData<T>(path: string): Promise<T>;
2104
+ }
2105
+
2106
+ declare abstract class InternalServiceClient extends ServiceClient {
2107
+ constructor(baseUrl: string, serviceToken: string, serviceName?: string);
2108
+ }
2109
+
2110
+ interface ServiceResponse<T> {
2111
+ status: string;
2112
+ message?: string;
2113
+ data: T;
2114
+ }
2115
+
1997
2116
  declare class MongoRepository<T> implements Repository<T> {
1998
2117
  protected model: Model<T>;
1999
2118
  constructor(model: Model<T>);
@@ -2176,4 +2295,4 @@ declare function toMinutes(ms: number): number;
2176
2295
  declare function toHours(ms: number): number;
2177
2296
  declare function sleep(ms: number): Promise<void>;
2178
2297
 
2179
- export { AVLTree, AdjacencyList, AdjacencyMatrix, type AdjacentEdge, type AllowedOriginsOptions, ApiFeatures, type ApiMeta, ApiResponse, type ApiResponseOptions, AppError, AsyncHandler, type AsyncRequestHandler, type AuthenticatedUser, type AzureBlobStorageConfig, AzureBlobStorageService, BPlusTree, BTree, BinaryHeap, BinarySearchTree, BinaryTree, BloomFilter, CircularArray, CircularLinkedList, CircularQueue, type CloudinaryConfig, CloudinaryService, ConsistentHash, CountMinSketch, CrudControllerFactory, type CrudControllerOptions, DEFAULT_LIMIT, DEFAULT_PAGE, DEFAULT_SORT_DIRECTION, type DatabaseConnectionOptions, DatabaseManager, type DatabaseProvider, Deque, type DirectedEdge, DirectedGraph, DisjointSetUnion, DoublyLinkedList, DynamicArray, type Edge, type EmailProvider, EmailService, type ErrorMeta, ErrorMiddleware, FenwickTree, FibNode, FibonacciHeap, type Filter, Graph, HashMap, HashSet, HyperLogLog, type IValidator, type Interval, IntervalTree, JWTService, type JWTServiceOptions, JoiValidator, KDTree, LFUCache, LRUCache, type LoggerOptions, MaxHeap, MaxStack, MinHeap, MinStack, type MongoDatabaseConfig, MongoDatabaseProvider, MongoRepository, MulterFileHandlerService, MultiSet, Node, OrderedSet, type PaginationMeta, type PaginationQuery, PairingHeap, PairingNode, type ParsedQuery, type PickOptions, type Point, type Point2D, PriorityQueue, type ProgressCallback, QUERY_RESERVED_FIELDS, QuadTree, type QueryFilter, type QueryOperator, type QueryOptions, type QuerySort, Queue, RadixTree, type Rect, RedBlackTree, type Repository, SMTPProvider, SUPPORTED_OPERATORS, SegmentTree, type Select, type SendEmailOptions, Set, SinglyLinkedList, type Sort, SparseTable, SplayTree, Stack, type StandardApiResponse, StaticArray, SuffixArray, SuffixTree, TemplateEngine, TernarySearchTree, TreeNode, Trie, type UpdateQueryOptions, type UploadOptions, type ValidationResult, type ValidationSchema, ZodValidator, authMiddleware, createAllowedOrigins, createCorsOptions, days, errorToString, hours, isJoiSchema, isZodSchema, loadEnv, logger, milliseconds, minutes, pickFields, seconds, sleep, toHours, toMinutes, toSeconds, validate };
2298
+ export { AVLTree, AdjacencyList, AdjacencyMatrix, type AdjacentEdge, type AllowedOriginsOptions, ApiFeatures, type ApiMeta, ApiResponse, type ApiResponseOptions, AppError, AsyncHandler, type AsyncRequestHandler, type AuthStrategy, type AuthenticatedUser, type AzureBlobStorageConfig, AzureBlobStorageService, BPlusTree, BTree, BearerTokenStrategy, BinaryHeap, BinarySearchTree, BinaryTree, BloomFilter, CircularArray, CircularLinkedList, CircularQueue, type CloudinaryConfig, CloudinaryService, ConsistentHash, CorrelationId, CountMinSketch, CrudControllerFactory, type CrudControllerOptions, DEFAULT_LIMIT, DEFAULT_PAGE, DEFAULT_SORT_DIRECTION, type DatabaseConnectionOptions, DatabaseManager, type DatabaseProvider, Deque, type DirectedEdge, DirectedGraph, DisjointSetUnion, DoublyLinkedList, DynamicArray, type Edge, type EmailProvider, EmailService, type ErrorMeta, ErrorMiddleware, type ExponentialBackoffOptions, ExponentialBackoffRetryPolicy, FenwickTree, FibNode, FibonacciHeap, type Filter, FixedRetryPolicy, type FixedRetryPolicyOptions, Graph, HashMap, HashSet, HttpClient, type HttpClientOptions, HyperLogLog, type IValidator, InternalServiceClient, type Interval, IntervalTree, JWTService, type JWTServiceOptions, JoiValidator, KDTree, LFUCache, LRUCache, type LoggerOptions, MaxHeap, MaxStack, MinHeap, MinStack, type MongoDatabaseConfig, MongoDatabaseProvider, MongoRepository, MulterFileHandlerService, MultiSet, Node, OrderedSet, type PaginationMeta, type PaginationQuery, PairingHeap, PairingNode, type ParsedQuery, type PickOptions, type Point, type Point2D, PriorityQueue, type ProgressCallback, QUERY_RESERVED_FIELDS, QuadTree, type QueryFilter, type QueryOperator, type QueryOptions, type QuerySort, Queue, RadixTree, type Rect, RedBlackTree, type Repository, RequestContext, type RequestContextData, type RetryPolicy, SMTPProvider, SUPPORTED_OPERATORS, SegmentTree, type Select, type SendEmailOptions, ServiceClient, type ServiceResponse, Set, SinglyLinkedList, type Sort, SparseTable, SplayTree, Stack, type StandardApiResponse, StaticArray, StaticTokenProvider, SuffixArray, SuffixTree, TemplateEngine, TernarySearchTree, type TokenProvider, TracingHeaders, TreeNode, Trie, type UpdateQueryOptions, type UploadOptions, type ValidationResult, type ValidationSchema, ZodValidator, authMiddleware, createAllowedOrigins, createCorsOptions, days, errorToString, hours, isJoiSchema, isZodSchema, loadEnv, logger, milliseconds, minutes, pickFields, seconds, sleep, toHours, toMinutes, toSeconds, validate };
package/dist/index.d.ts CHANGED
@@ -1994,6 +1994,125 @@ declare class ZodValidator implements IValidator {
1994
1994
  validate<T = unknown>(data: unknown): ValidationResult<T>;
1995
1995
  }
1996
1996
 
1997
+ declare class CorrelationId {
1998
+ static generate(): string;
1999
+ }
2000
+
2001
+ interface RequestContextData {
2002
+ correlationId: string;
2003
+ requestId?: string;
2004
+ userId?: string;
2005
+ tenantId?: string;
2006
+ }
2007
+ declare class RequestContext {
2008
+ private static readonly storage;
2009
+ static run<T>(context: RequestContextData, callback: () => T): T;
2010
+ static get(): RequestContextData | undefined;
2011
+ static getCorrelationId(): string | undefined;
2012
+ static getRequestId(): string | undefined;
2013
+ static getUserId(): string | undefined;
2014
+ static getTenantId(): string | undefined;
2015
+ }
2016
+
2017
+ declare class TracingHeaders {
2018
+ static build(): Record<string, string>;
2019
+ }
2020
+
2021
+ interface RetryPolicy {
2022
+ shouldRetry(attempt: number, error?: unknown): boolean;
2023
+ getDelay(attempt: number, error?: unknown): number;
2024
+ }
2025
+
2026
+ interface ExponentialBackoffOptions {
2027
+ retries?: number;
2028
+ baseDelay?: number;
2029
+ maxDelay?: number;
2030
+ }
2031
+ declare class ExponentialBackoffRetryPolicy implements RetryPolicy {
2032
+ private readonly retries;
2033
+ private readonly baseDelay;
2034
+ private readonly maxDelay;
2035
+ constructor(options?: ExponentialBackoffOptions);
2036
+ shouldRetry(attempt: number): boolean;
2037
+ getDelay(attempt: number): number;
2038
+ }
2039
+
2040
+ interface FixedRetryPolicyOptions {
2041
+ retries?: number;
2042
+ delay?: number;
2043
+ }
2044
+ declare class FixedRetryPolicy implements RetryPolicy {
2045
+ private readonly retries;
2046
+ private readonly delay;
2047
+ constructor(options?: FixedRetryPolicyOptions);
2048
+ shouldRetry(attempt: number): boolean;
2049
+ getDelay(_attempt: number, _error?: unknown): number;
2050
+ }
2051
+
2052
+ interface AuthStrategy {
2053
+ getHeaders(): Promise<Record<string, string>>;
2054
+ }
2055
+
2056
+ interface TokenProvider {
2057
+ getToken(): Promise<string> | string;
2058
+ }
2059
+
2060
+ declare class BearerTokenStrategy implements AuthStrategy {
2061
+ private readonly tokenProvider;
2062
+ constructor(tokenProvider: TokenProvider);
2063
+ getHeaders(): Promise<Record<string, string>>;
2064
+ }
2065
+
2066
+ declare class StaticTokenProvider implements TokenProvider {
2067
+ private readonly token;
2068
+ constructor(token: string);
2069
+ getToken(): string;
2070
+ }
2071
+
2072
+ interface HttpClientOptions {
2073
+ timeout?: number;
2074
+ headers?: Record<string, string>;
2075
+ serviceName?: string;
2076
+ retryPolicy?: RetryPolicy;
2077
+ }
2078
+
2079
+ declare class HttpClient {
2080
+ protected readonly baseUrl: string;
2081
+ protected readonly timeout: number;
2082
+ protected readonly headers: Record<string, string>;
2083
+ protected readonly serviceName: string;
2084
+ protected readonly authStrategy?: AuthStrategy;
2085
+ protected readonly retryPolicy: RetryPolicy;
2086
+ constructor(baseUrl: string, options?: HttpClientOptions & {
2087
+ authStrategy?: AuthStrategy;
2088
+ });
2089
+ protected get<T>(path: string): Promise<T>;
2090
+ protected post<T>(path: string, body?: unknown): Promise<T>;
2091
+ protected put<T>(path: string, body?: unknown): Promise<T>;
2092
+ protected patch<T>(path: string, body?: unknown): Promise<T>;
2093
+ protected delete<T>(path: string): Promise<T>;
2094
+ private request;
2095
+ private sleep;
2096
+ }
2097
+
2098
+ declare abstract class ServiceClient extends HttpClient {
2099
+ protected getData<T>(path: string): Promise<T>;
2100
+ protected postData<T>(path: string, body?: unknown): Promise<T>;
2101
+ protected putData<T>(path: string, body?: unknown): Promise<T>;
2102
+ protected patchData<T>(path: string, body?: unknown): Promise<T>;
2103
+ protected deleteData<T>(path: string): Promise<T>;
2104
+ }
2105
+
2106
+ declare abstract class InternalServiceClient extends ServiceClient {
2107
+ constructor(baseUrl: string, serviceToken: string, serviceName?: string);
2108
+ }
2109
+
2110
+ interface ServiceResponse<T> {
2111
+ status: string;
2112
+ message?: string;
2113
+ data: T;
2114
+ }
2115
+
1997
2116
  declare class MongoRepository<T> implements Repository<T> {
1998
2117
  protected model: Model<T>;
1999
2118
  constructor(model: Model<T>);
@@ -2176,4 +2295,4 @@ declare function toMinutes(ms: number): number;
2176
2295
  declare function toHours(ms: number): number;
2177
2296
  declare function sleep(ms: number): Promise<void>;
2178
2297
 
2179
- export { AVLTree, AdjacencyList, AdjacencyMatrix, type AdjacentEdge, type AllowedOriginsOptions, ApiFeatures, type ApiMeta, ApiResponse, type ApiResponseOptions, AppError, AsyncHandler, type AsyncRequestHandler, type AuthenticatedUser, type AzureBlobStorageConfig, AzureBlobStorageService, BPlusTree, BTree, BinaryHeap, BinarySearchTree, BinaryTree, BloomFilter, CircularArray, CircularLinkedList, CircularQueue, type CloudinaryConfig, CloudinaryService, ConsistentHash, CountMinSketch, CrudControllerFactory, type CrudControllerOptions, DEFAULT_LIMIT, DEFAULT_PAGE, DEFAULT_SORT_DIRECTION, type DatabaseConnectionOptions, DatabaseManager, type DatabaseProvider, Deque, type DirectedEdge, DirectedGraph, DisjointSetUnion, DoublyLinkedList, DynamicArray, type Edge, type EmailProvider, EmailService, type ErrorMeta, ErrorMiddleware, FenwickTree, FibNode, FibonacciHeap, type Filter, Graph, HashMap, HashSet, HyperLogLog, type IValidator, type Interval, IntervalTree, JWTService, type JWTServiceOptions, JoiValidator, KDTree, LFUCache, LRUCache, type LoggerOptions, MaxHeap, MaxStack, MinHeap, MinStack, type MongoDatabaseConfig, MongoDatabaseProvider, MongoRepository, MulterFileHandlerService, MultiSet, Node, OrderedSet, type PaginationMeta, type PaginationQuery, PairingHeap, PairingNode, type ParsedQuery, type PickOptions, type Point, type Point2D, PriorityQueue, type ProgressCallback, QUERY_RESERVED_FIELDS, QuadTree, type QueryFilter, type QueryOperator, type QueryOptions, type QuerySort, Queue, RadixTree, type Rect, RedBlackTree, type Repository, SMTPProvider, SUPPORTED_OPERATORS, SegmentTree, type Select, type SendEmailOptions, Set, SinglyLinkedList, type Sort, SparseTable, SplayTree, Stack, type StandardApiResponse, StaticArray, SuffixArray, SuffixTree, TemplateEngine, TernarySearchTree, TreeNode, Trie, type UpdateQueryOptions, type UploadOptions, type ValidationResult, type ValidationSchema, ZodValidator, authMiddleware, createAllowedOrigins, createCorsOptions, days, errorToString, hours, isJoiSchema, isZodSchema, loadEnv, logger, milliseconds, minutes, pickFields, seconds, sleep, toHours, toMinutes, toSeconds, validate };
2298
+ export { AVLTree, AdjacencyList, AdjacencyMatrix, type AdjacentEdge, type AllowedOriginsOptions, ApiFeatures, type ApiMeta, ApiResponse, type ApiResponseOptions, AppError, AsyncHandler, type AsyncRequestHandler, type AuthStrategy, type AuthenticatedUser, type AzureBlobStorageConfig, AzureBlobStorageService, BPlusTree, BTree, BearerTokenStrategy, BinaryHeap, BinarySearchTree, BinaryTree, BloomFilter, CircularArray, CircularLinkedList, CircularQueue, type CloudinaryConfig, CloudinaryService, ConsistentHash, CorrelationId, CountMinSketch, CrudControllerFactory, type CrudControllerOptions, DEFAULT_LIMIT, DEFAULT_PAGE, DEFAULT_SORT_DIRECTION, type DatabaseConnectionOptions, DatabaseManager, type DatabaseProvider, Deque, type DirectedEdge, DirectedGraph, DisjointSetUnion, DoublyLinkedList, DynamicArray, type Edge, type EmailProvider, EmailService, type ErrorMeta, ErrorMiddleware, type ExponentialBackoffOptions, ExponentialBackoffRetryPolicy, FenwickTree, FibNode, FibonacciHeap, type Filter, FixedRetryPolicy, type FixedRetryPolicyOptions, Graph, HashMap, HashSet, HttpClient, type HttpClientOptions, HyperLogLog, type IValidator, InternalServiceClient, type Interval, IntervalTree, JWTService, type JWTServiceOptions, JoiValidator, KDTree, LFUCache, LRUCache, type LoggerOptions, MaxHeap, MaxStack, MinHeap, MinStack, type MongoDatabaseConfig, MongoDatabaseProvider, MongoRepository, MulterFileHandlerService, MultiSet, Node, OrderedSet, type PaginationMeta, type PaginationQuery, PairingHeap, PairingNode, type ParsedQuery, type PickOptions, type Point, type Point2D, PriorityQueue, type ProgressCallback, QUERY_RESERVED_FIELDS, QuadTree, type QueryFilter, type QueryOperator, type QueryOptions, type QuerySort, Queue, RadixTree, type Rect, RedBlackTree, type Repository, RequestContext, type RequestContextData, type RetryPolicy, SMTPProvider, SUPPORTED_OPERATORS, SegmentTree, type Select, type SendEmailOptions, ServiceClient, type ServiceResponse, Set, SinglyLinkedList, type Sort, SparseTable, SplayTree, Stack, type StandardApiResponse, StaticArray, StaticTokenProvider, SuffixArray, SuffixTree, TemplateEngine, TernarySearchTree, type TokenProvider, TracingHeaders, TreeNode, Trie, type UpdateQueryOptions, type UploadOptions, type ValidationResult, type ValidationSchema, ZodValidator, authMiddleware, createAllowedOrigins, createCorsOptions, days, errorToString, hours, isJoiSchema, isZodSchema, loadEnv, logger, milliseconds, minutes, pickFields, seconds, sleep, toHours, toMinutes, toSeconds, validate };
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 {
@@ -6003,6 +6248,6 @@ function sleep(ms) {
6003
6248
  return new Promise((resolve) => setTimeout(resolve, ms));
6004
6249
  }
6005
6250
 
6006
- 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 };
6007
6252
  //# sourceMappingURL=index.js.map
6008
6253
  //# sourceMappingURL=index.js.map