@xeno-js/core 2.0.1 → 3.0.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/db.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/infrastructure/datasources/base-postgressql.datasource.ts","../src/infrastructure/datasources/base-sqllite.datasource.ts","../src/infrastructure/datasources/remote.datasource.ts"],"sourcesContent":["import type { Dictionary } from '@xeno-js/shared'\r\nimport type { NodePgDatabase } from 'drizzle-orm/node-postgres'\r\n\r\nimport type { DbContext } from '../db'\r\n\r\n/**\r\n * @description BasePostgresSqlDataSource is an abstract class that serves as a foundation for creating data sources that interact with a PostgreSQL database. It provides a common interface for accessing the database connection and ensures that the database connection is properly initialized and managed. By extending this class, developers can easily create data sources that are tailored to their specific use cases.\r\n *\r\n * @author Xeno\r\n * @version 1.0.0\r\n * @since 2025-09-30\r\n * @link https://github.com/xeno-js/xeno-js\r\n */\r\nexport abstract class BasePostgresSqlDataSource<TSchema extends Dictionary = Dictionary> {\r\n constructor(private readonly _db: DbContext<TSchema>) {}\r\n\r\n /**\r\n * @description Returns the database connection.\r\n * @returns {NodePgDatabase<TSchema>} The database connection.\r\n */\r\n protected get db(): NodePgDatabase<TSchema> {\r\n return this._db as NodePgDatabase<TSchema>\r\n }\r\n}\r\n","import type { Dictionary } from '@xeno-js/shared'\r\nimport type { LibSQLDatabase } from 'drizzle-orm/libsql'\r\n\r\nimport type { DbContext } from '../db'\r\n\r\n/**\r\n * @description BaseSqliteSqlDataSource is an abstract class that serves as a foundation for creating data sources that interact with a SQLite / libSQL database.\r\n *\r\n * @author Xeno\r\n * @version 1.0.0\r\n * @since 2025-09-30\r\n * @link https://github.com/xeno-js/xeno-js\r\n */\r\nexport abstract class BaseSqliteSqlDataSource<TSchema extends Dictionary = Dictionary> {\r\n constructor(private readonly _db: DbContext<TSchema>) {}\r\n\r\n /**\r\n * @description Returns the SQLite database connection.\r\n * @returns {LibSQLDatabase<TSchema>} The database connection.\r\n */\r\n protected get db(): LibSQLDatabase<TSchema> {\r\n return this._db as LibSQLDatabase<TSchema>\r\n }\r\n}\r\n","import type {\r\n HttpBaseRequest,\r\n HttpMethod,\r\n HttpRequest,\r\n IHttpClient,\r\n IRemoteDataSource,\r\n IServiceResilience,\r\n Optional,\r\n ResultType,\r\n} from '@xeno-js/shared'\r\nimport { AppError, Result } from '@xeno-js/shared'\r\n\r\n/**\r\n * @description Concrete implementation of the IRemoteDataSource contract that utilizes an agnostic HTTP client and a resilience service to fetch data from remote endpoints. The RemoteDataSource class is responsible for sending HTTP requests based on the provided HttpClientRequest parameters, while leveraging the resilience features of the IServiceResilience to ensure reliable communication with external services. This implementation abstracts away the details of how HTTP requests are made and how resilience is handled, allowing for flexibility in choosing different HTTP clients and resilience strategies without affecting the consumers of the IRemoteDataSource interface.\r\n *\r\n * @author Xeno\r\n * @version 1.0.0\r\n * @since 2025-09-30\r\n * @link https://github.com/xeno-js/xeno-js\r\n */\r\nexport abstract class RemoteDataSource implements IRemoteDataSource {\r\n /** The constructor of the RemoteDataSource class takes two dependencies: an instance of an agnostic HTTP client that implements the IHttpClient interface, and an instance of a resilience service that implements the IServiceResilience interface.\r\n * These dependencies are injected into the class, allowing for greater flexibility and testability.\r\n * The HTTP client is used to send requests to remote endpoints, while the resilience service is used to execute these requests with built-in support for retries, timeouts, and circuit breakers, ensuring that the remote calls are more resilient to failures and can recover gracefully from errors.\r\n * @param _httpClient An instance of an agnostic HTTP client that implements the IHttpClient interface, used for sending HTTP requests to remote endpoints.\r\n * @param _resilienceService An instance of a resilience service that implements the IServiceResilience interface, used for executing HTTP requests with built-in support for retries, timeouts, and circuit breakers to enhance the reliability of remote calls.\r\n *\r\n * @author Xeno\r\n * @version 1.0.0\r\n * @since 2025-09-30\r\n * @link https://github.com/xeno-js/xeno-js\r\n */\r\n constructor(\r\n private readonly _httpClient: IHttpClient,\r\n private readonly _resilienceService: IServiceResilience,\r\n ) {}\r\n\r\n /** @description Private method that sends an HTTP request to a specified endpoint using the provided HttpRequest options. The method utilizes the resilience service to execute the request, ensuring that it is performed with built-in support for retries, timeouts, and circuit breakers. The response from the HTTP client is then wrapped in a ResultType, which encapsulates either the successful response data or an error, providing a consistent way to handle both success and failure cases.\r\n * @param endpoint The URL or endpoint to which the HTTP request is sent.\r\n * @param options The HttpRequest options that specify the HTTP method, headers, query parameters, body, and other request configurations.\r\n * @returns A Promise that resolves to a ResultType containing either the successful response data or an error if the request fails.\r\n *\r\n * @author Xeno\r\n * @version 1.0.0\r\n * @since 2025-09-30\r\n * @link https://github.com/xeno-js/xeno-js\r\n */\r\n private async send<TResponse, TBody = unknown>(\r\n endpoint: string,\r\n options: HttpRequest<TBody>,\r\n ): Promise<ResultType<TResponse>> {\r\n const response = await this._resilienceService.execute(async () => {\r\n switch (options.method) {\r\n case 'GET':\r\n return await this._httpClient.get<TResponse>(endpoint, options)\r\n case 'POST':\r\n return await this._httpClient.post<TResponse, TBody>(endpoint, options.body, options)\r\n case 'PUT':\r\n return await this._httpClient.put<TResponse, TBody>(endpoint, options.body, options)\r\n case 'PATCH':\r\n return await this._httpClient.patch<TResponse, TBody>(endpoint, options.body, options)\r\n case 'DELETE':\r\n return await this._httpClient.delete<TResponse>(endpoint, options)\r\n default:\r\n throw AppError.notSupported(\r\n this.constructor.name,\r\n `Unsupported HTTP method: ${options.method}`,\r\n )\r\n }\r\n }, options.signal)\r\n\r\n return Result.ok(response.data)\r\n }\r\n\r\n protected setOptions<T>(\r\n request: Optional<HttpBaseRequest>,\r\n method: HttpMethod,\r\n body: Optional<T> = undefined,\r\n ): HttpRequest<T> {\r\n return {\r\n query: request?.query,\r\n signal: request?.signal,\r\n timeoutMs: request?.timeoutMs,\r\n headers: request?.headers,\r\n responseType: request?.responseType,\r\n method,\r\n body,\r\n }\r\n }\r\n\r\n public async get<TResponse>(\r\n endpoint: string,\r\n request?: HttpBaseRequest,\r\n ): Promise<ResultType<TResponse>> {\r\n const options = this.setOptions(request, 'GET')\r\n\r\n return await this.send<TResponse>(endpoint, options)\r\n }\r\n\r\n public async post<TResponse, TBody = unknown>(\r\n endpoint: string,\r\n body: TBody,\r\n request?: HttpBaseRequest,\r\n ): Promise<ResultType<TResponse>> {\r\n const options = this.setOptions(request, 'POST', body)\r\n\r\n return await this.send<TResponse, TBody>(endpoint, options)\r\n }\r\n\r\n public async put<TResponse, TBody = unknown>(\r\n endpoint: string,\r\n body: TBody,\r\n request?: HttpBaseRequest,\r\n ): Promise<ResultType<TResponse>> {\r\n const options = this.setOptions(request, 'PUT', body)\r\n\r\n return await this.send<TResponse, TBody>(endpoint, options)\r\n }\r\n\r\n public async patch<TResponse, TBody = unknown>(\r\n endpoint: string,\r\n body: TBody,\r\n request?: HttpBaseRequest,\r\n ): Promise<ResultType<TResponse>> {\r\n const options = this.setOptions(request, 'PATCH', body)\r\n\r\n return await this.send<TResponse, TBody>(endpoint, options)\r\n }\r\n\r\n public async delete<TResponse>(\r\n endpoint: string,\r\n request?: HttpBaseRequest,\r\n ): Promise<ResultType<TResponse>> {\r\n const options = this.setOptions(request, 'DELETE')\r\n\r\n return await this.send<TResponse>(endpoint, options)\r\n }\r\n}\r\n"],"mappings":";AAaO,IAAe,4BAAf,MAAkF;AAAA,EACvF,YAA6B,KAAyB;AAAzB;AAAA,EAA0B;AAAA,EAA1B;AAAA;AAAA;AAAA;AAAA;AAAA,EAM7B,IAAc,KAA8B;AAC1C,WAAO,KAAK;AAAA,EACd;AACF;;;ACVO,IAAe,0BAAf,MAAgF;AAAA,EACrF,YAA6B,KAAyB;AAAzB;AAAA,EAA0B;AAAA,EAA1B;AAAA;AAAA;AAAA;AAAA;AAAA,EAM7B,IAAc,KAA8B;AAC1C,WAAO,KAAK;AAAA,EACd;AACF;;;ACbA,SAAS,UAAU,cAAc;AAU1B,IAAe,mBAAf,MAA6D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYlE,YACmB,aACA,oBACjB;AAFiB;AACA;AAAA,EAChB;AAAA,EAFgB;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAanB,MAAc,KACZ,UACA,SACgC;AAChC,UAAM,WAAW,MAAM,KAAK,mBAAmB,QAAQ,YAAY;AACjE,cAAQ,QAAQ,QAAQ;AAAA,QACtB,KAAK;AACH,iBAAO,MAAM,KAAK,YAAY,IAAe,UAAU,OAAO;AAAA,QAChE,KAAK;AACH,iBAAO,MAAM,KAAK,YAAY,KAAuB,UAAU,QAAQ,MAAM,OAAO;AAAA,QACtF,KAAK;AACH,iBAAO,MAAM,KAAK,YAAY,IAAsB,UAAU,QAAQ,MAAM,OAAO;AAAA,QACrF,KAAK;AACH,iBAAO,MAAM,KAAK,YAAY,MAAwB,UAAU,QAAQ,MAAM,OAAO;AAAA,QACvF,KAAK;AACH,iBAAO,MAAM,KAAK,YAAY,OAAkB,UAAU,OAAO;AAAA,QACnE;AACE,gBAAM,SAAS;AAAA,YACb,KAAK,YAAY;AAAA,YACjB,4BAA4B,QAAQ,MAAM;AAAA,UAC5C;AAAA,MACJ;AAAA,IACF,GAAG,QAAQ,MAAM;AAEjB,WAAO,OAAO,GAAG,SAAS,IAAI;AAAA,EAChC;AAAA,EAEU,WACR,SACA,QACA,OAAoB,QACJ;AAChB,WAAO;AAAA,MACL,OAAO,SAAS;AAAA,MAChB,QAAQ,SAAS;AAAA,MACjB,WAAW,SAAS;AAAA,MACpB,SAAS,SAAS;AAAA,MAClB,cAAc,SAAS;AAAA,MACvB;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAa,IACX,UACA,SACgC;AAChC,UAAM,UAAU,KAAK,WAAW,SAAS,KAAK;AAE9C,WAAO,MAAM,KAAK,KAAgB,UAAU,OAAO;AAAA,EACrD;AAAA,EAEA,MAAa,KACX,UACA,MACA,SACgC;AAChC,UAAM,UAAU,KAAK,WAAW,SAAS,QAAQ,IAAI;AAErD,WAAO,MAAM,KAAK,KAAuB,UAAU,OAAO;AAAA,EAC5D;AAAA,EAEA,MAAa,IACX,UACA,MACA,SACgC;AAChC,UAAM,UAAU,KAAK,WAAW,SAAS,OAAO,IAAI;AAEpD,WAAO,MAAM,KAAK,KAAuB,UAAU,OAAO;AAAA,EAC5D;AAAA,EAEA,MAAa,MACX,UACA,MACA,SACgC;AAChC,UAAM,UAAU,KAAK,WAAW,SAAS,SAAS,IAAI;AAEtD,WAAO,MAAM,KAAK,KAAuB,UAAU,OAAO;AAAA,EAC5D;AAAA,EAEA,MAAa,OACX,UACA,SACgC;AAChC,UAAM,UAAU,KAAK,WAAW,SAAS,QAAQ;AAEjD,WAAO,MAAM,KAAK,KAAgB,UAAU,OAAO;AAAA,EACrD;AACF;","names":[]}
@@ -1,527 +0,0 @@
1
- import { ICache, Optional, IExtendendAuthService, IBaseAuthService, ICacheKeyBuilder, IPipelineBehavior, ICommand, IConcurrencyService, IConfigurationService, ILogger, IMediator, IMiddleware, IQuery, RequestContext, IServiceResilience, IServiceExtractor, Metadata, IUnitOfWork, IDisposable, IValidatorService, IIdentityAccessor, IContextAccessor, INetworkContextAccessor, IFactory, UserContext, ITransactionState, Factory, IBaseAccessor, Identity, ExtendedRequest } from '@xeno-js/shared';
2
-
3
- /**
4
- * @description IAtomicCache is an interface that extends the ICache interface and adds an increment method. The increment method takes a key and an optional ttlSeconds parameter and returns a Promise that resolves to an incremented value.
5
- *
6
- */
7
- interface IAtomicCache extends ICache {
8
- /**
9
- * @description The increment method increments the value associated with the given key in the cache. If the key does not exist, it is created with an initial value of 1. The method returns a Promise that resolves to the incremented value.
10
- *
11
- * @param key The key to increment the value for.
12
- * @param ttlSeconds The time-to-live (TTL) in seconds for the key-value pair. If not provided, the default TTL is used.
13
- * @returns A Promise that resolves to the incremented value.
14
- */
15
- increment(key: string, ttlSeconds: Optional<number | string>): Promise<number>;
16
- }
17
-
18
- /**
19
- * @description This file defines the injection tokens used for dependency injection in the application.
20
- * Injection tokens are unique identifiers that are used to register and resolve dependencies in the container.
21
- * They can be symbols, strings, or classes, but using symbols is a common practice to avoid naming collisions.
22
- *
23
- * @author Xeno
24
- * @version 1.0.0
25
- * @since 2025-09-30
26
- * @link https://github.com/xeno-js/xeno-js
27
- */
28
- interface ApplicationRegistry<T = unknown, Ttx = unknown> {
29
- /** @description Token used to register and resolve the Extended AuthService instance in the dependency injection container.
30
- *
31
- * @author Xeno
32
- * @version 1.0.0
33
- * @since 2025-09-30
34
- * @link https://github.com/xeno-js/xeno-js
35
- */
36
- readonly AUTH_SERVICE: IExtendendAuthService;
37
- /** @description Token used to register and resolve the BaseAuthService instance in the dependency injection container.
38
- *
39
- * @author Xeno
40
- * @version 1.0.0
41
- * @since 2025-09-30
42
- * @link https://github.com/xeno-js/xeno-js
43
- */
44
- readonly BASE_AUTH_SERVICE: IBaseAuthService;
45
- /** @description Token used to register and resolve the InMemoryCache instance in the dependency injection container.
46
- *
47
- * @author Xeno
48
- * @version 1.0.0
49
- * @since 2025-09-30
50
- * @link https://github.com/xeno-js/xeno-js
51
- */
52
- readonly CACHE: IAtomicCache;
53
- /** @description Token used to register and resolve the CacheKeyBuilder instance in the dependency injection container.
54
- *
55
- * @author Xeno
56
- * @version 1.0.0
57
- * @since 2025-09-30
58
- * @link https://github.com/xeno-js/xeno-js
59
- */
60
- readonly CACHE_KEY_BUILDER: ICacheKeyBuilder;
61
- /** @description Token used to register and resolve the CommandPipeline behaviors in the dependency injection container.
62
- *
63
- * @author Xeno
64
- * @version 1.0.0
65
- * @since 2025-09-30
66
- * @link https://github.com/xeno-js/xeno-js
67
- */
68
- readonly COMMAND_PIPELINES_BEHAVIOR: IPipelineBehavior<ICommand, unknown>;
69
- /** @description Token used to register and resolve the ConcurrencyService instance in the dependency injection container.
70
- *
71
- * @author Xeno
72
- * @version 1.0.0
73
- * @since 2025-09-30
74
- * @link https://github.com/xeno-js/xeno-js
75
- */
76
- readonly CONCURRENCY_SERVICE: IConcurrencyService;
77
- /** @description Token used to register and resolve the ConfigurationService instance in the dependency injection container.
78
- *
79
- * @author Xeno
80
- * @version 1.0.0
81
- * @since 2025-09-30
82
- * @link https://github.com/xeno-js/xeno-js
83
- */
84
- readonly CONFIGURATION_SERVICE: IConfigurationService;
85
- /** @description Token used to register and resolve the CsrfTokenService instance in the dependency injection container.
86
- *
87
- * @author Xeno
88
- * @version 1.0.0
89
- * @since 2025-09-30
90
- * @link https://github.com/xeno-js/xeno-js
91
- */
92
- readonly CSRF_TOKEN_SERVICE: ICsrfTokenService;
93
- /** @description Token used to register and resolve the CryptoService instance in the dependency injection container.
94
- *
95
- * @author Xeno
96
- * @version 1.0.0
97
- * @since 2025-09-30
98
- * @link https://github.com/xeno-js/xeno-js
99
- */
100
- readonly CRYPTO_SERVICE: ICryptoService;
101
- /** @description Token used to register and resolve the DbContext instance in the dependency injection container.
102
- *
103
- * @author Xeno
104
- * @version 1.0.0
105
- * @since 2025-09-30
106
- * @link https://github.com/xeno-js/xeno-js
107
- */
108
- readonly DB_CONTEXT: T;
109
- /** @description Token used to register and resolve the HTTP Adapter instance in the dependency injection container.
110
- *
111
- * @author Xeno
112
- * @version 1.2.1
113
- * @since 2025-09-30
114
- * @link https://github.com/xeno-js/xeno-js
115
- */
116
- readonly HTTP_ADAPTER: IHttpAdapter<unknown, unknown>;
117
- /** @description Token used to register and resolve the Logger instance in the dependency injection container.
118
- *
119
- * @author Xeno
120
- * @version 1.0.0
121
- * @since 2025-09-30
122
- * @link https://github.com/xeno-js/xeno-js
123
- */
124
- readonly LOGGER: ILogger;
125
- /** @description Token used to register and resolve the Mediator instance in the dependency injection container.
126
- *
127
- * @author Xeno
128
- * @version 1.0.0
129
- * @since 2025-09-30
130
- * @link https://github.com/xeno-js/xeno-js
131
- */
132
- readonly MEDIATOR: IMediator;
133
- /** @description Token used to register and resolve the RequestContextMiddleware in the dependency injection container.
134
- *
135
- * @author Xeno
136
- * @version 1.0.0
137
- * @since 2025-09-30
138
- * @link https://github.com/xeno-js/xeno-js
139
- */
140
- readonly MIDDLEWARE: IMiddleware;
141
- /** @description Token used to register and resolve the QueryPipeline behaviors in the dependency injection container.
142
- *
143
- * @author Xeno
144
- * @version 1.0.0
145
- * @since 2025-09-30
146
- * @link https://github.com/xeno-js/xeno-js
147
- */
148
- readonly QUERY_PIPELINES_BEHAVIOR: IPipelineBehavior<IQuery, unknown>;
149
- /** @description Token used to register and resolve the RequestContext instance in the dependency injection container.
150
- *
151
- * @author Xeno
152
- * @version 1.0.0
153
- * @since 2025-09-30
154
- * @link https://github.com/xeno-js/xeno-js
155
- */
156
- readonly REQUEST_CONTEXT: IRequestContext<RequestContext, ApplicationRegistry<T>>;
157
- /** @description Token used to register and resolve the IServiceResilience instance in the dependency injection container.
158
- *
159
- * @author Xeno
160
- * @version 1.0.0
161
- * @since 2025-09-30
162
- * @link https://github.com/xeno-js/xeno-js
163
- */
164
- readonly RESILIENCE_CLIENT: IServiceResilience;
165
- /** @description Token used to register and resolve the ServiceContainer instance in the dependency injection container.
166
- *
167
- * @author Xeno
168
- * @version 1.0.0
169
- * @since 2025-09-30
170
- * @link https://github.com/xeno-js/xeno-js
171
- */
172
- readonly SERVICE_CONTAINER: IServiceContainer;
173
- /** @description Token used to register and resolve the ServiceExtractor instance in the dependency injection container.
174
- *
175
- * @author Xeno
176
- * @version 1.0.0
177
- * @since 2025-09-30
178
- * @link https://github.com/xeno-js/xeno-js
179
- */
180
- readonly SERVICE_EXTRACTOR: IServiceExtractor<Request['headers'], Metadata>;
181
- /** @description Token used to register and resolve the UnitOfWork instance in the dependency injection container.
182
- *
183
- * @author Xeno
184
- * @version 1.0.0
185
- * @since 2025-09-30
186
- * @link https://github.com/xeno-js/xeno-js
187
- */
188
- readonly UNIT_OF_WORK: IUnitOfWork & IDisposable;
189
- /** @description Token used to register and resolve the ZodValidator instance in the dependency injection container.
190
- *
191
- * @author Xeno
192
- * @version 1.0.0
193
- * @since 2025-09-30
194
- * @link https://github.com/xeno-js/xeno-js
195
- */
196
- readonly VALIDATOR_SERVICE: IValidatorService;
197
- /** @description Token used to register and resolve the IIdentityAccessor in the dependency injection container, allowing access only to current user identity information.
198
- *
199
- * @author Xeno
200
- * @version 1.0.0
201
- * @since 2025-09-30
202
- * @link https://github.com/xeno-js/xeno-js
203
- */
204
- readonly IDENTITY_ACCESSOR: IIdentityAccessor;
205
- /** @description Token used to register and resolve the IServiceScopeAccessor in the dependency injection container, granting controlled access to the current request scope.
206
- *
207
- * @author Xeno
208
- * @version 1.0.0
209
- * @since 2025-09-30
210
- * @link https://github.com/xeno-js/xeno-js
211
- */
212
- readonly SERVICE_SCOPE_ACCESSOR: IServiceScopeAccessor<ApplicationRegistry<T>>;
213
- /** @description Token used to register and resolve the IContextAccessor instance in the dependency injection container to fetch execution context properties.
214
- *
215
- * @author Xeno
216
- * @version 1.0.0
217
- * @since 2025-09-30
218
- * @link https://github.com/xeno-js/xeno-js
219
- */
220
- readonly CONTEXT_ACCESSOR: IContextAccessor<RequestContext>;
221
- /** @description Token used to register and resolve the INetworkContextAccessor instance in the dependency injection container to fetch networking/observability context data.
222
- *
223
- * @author Xeno
224
- * @version 1.0.0
225
- * @since 2025-09-30
226
- * @link https://github.com/xeno-js/xeno-js
227
- */
228
- readonly NETWORK_CONTEXT_ACCESSOR: INetworkContextAccessor;
229
- /** @description Token used to register and resolve the UserContext factory in the dependency injection container.
230
- *
231
- * @author Xeno
232
- * @version 1.0.0
233
- * @since 2025-09-30
234
- * @link https://github.com/xeno-js/xeno-js
235
- */
236
- readonly USER_CONTEXT_FACTORY: IFactory<void, UserContext>;
237
- /** @description Token used to register and resolve the TransactionState instance in the dependency injection container.
238
- *
239
- * @author Xeno
240
- * @version 1.0.0
241
- * @since 2025-09-30
242
- * @link https://github.com/xeno-js/xeno-js
243
- */
244
- readonly TRANSACTION_STATE: ITransactionState<Ttx>;
245
- }
246
-
247
- /**
248
- * @fileoverview Defines the IServiceScope interface for scoped dependency injection.
249
- *
250
- * @author Xeno
251
- * @version 1.0.0
252
- * @since 2025-09-30
253
- * @link https://github.com/xeno-js/xeno-js
254
- */
255
- /**
256
- * @description Represents a logical scope for resolving scoped services,
257
- * mimicking the .NET `IServiceScope` pattern.
258
- *
259
- * A scope is created by {@link IServiceContainer.createScope} and provides
260
- * its own isolated instance cache for services registered with scoped lifetime.
261
- * Singleton and transient services are still resolved through the root container.
262
- *
263
- * Call {@link dispose} when the scope is no longer needed to release all
264
- * scoped instances and invalidate the scope.
265
- *
266
- * @author Xeno
267
- * @version 1.0.0
268
- * @since 2025-09-30
269
- * @link https://github.com/xeno-js/xeno-js
270
- */
271
- interface IServiceScope<Registry extends ApplicationRegistry<unknown> = ApplicationRegistry<unknown>> extends IDisposable {
272
- /**
273
- * Resolves and returns the service registered under the given token.
274
- * Scoped services can only be resolved through a scope;
275
- * resolving them directly from the root container throws an error.
276
- *
277
- * @param token - The injection token identifying the service to resolve.
278
- * @returns The resolved service instance of type `T`.
279
- * @throws An error if no registration is found for the given token.
280
- * @throws An error if the scope has already been disposed.
281
- *
282
- * @author Xeno
283
- * @version 1.0.0
284
- * @since 2025-09-30
285
- * @link https://github.com/xeno-js/xeno-js
286
- */
287
- resolve<K extends keyof Registry>(token: K): Registry[K];
288
- }
289
-
290
- /**
291
- * @fileoverview Defines the IServiceContainer interface for a dependency injection container.
292
- *
293
- * @author Xeno
294
- * @version 1.0.0
295
- * @since 2025-09-30
296
- * @link https://github.com/xeno-js/xeno-js
297
- */
298
- interface IServiceProvider<Registry extends ApplicationRegistry<unknown> = ApplicationRegistry<unknown>> {
299
- /**
300
- * Resolves and returns the service registered under the given token.
301
- * Scoped services can only be resolved through a scope;
302
- * resolving them directly from the root container throws an error.
303
- *
304
- * @param token - The injection token identifying the service to resolve.
305
- * @returns The resolved service instance of type `T`.
306
- * @throws An error if no registration is found for the given token.
307
- * @throws An error if the scope has already been disposed.
308
- *
309
- * @author Xeno
310
- * @version 1.0.0
311
- * @since 2025-09-30
312
- * @link https://github.com/xeno-js/xeno-js
313
- */
314
- resolve<K extends keyof Registry>(token: K): Registry[K];
315
- }
316
- /**
317
- * @description Agnostic contract for a dependency injection container that mimics
318
- * the .NET ServiceCollection builder pattern.
319
- *
320
- * Each registration method returns `this` to enable a fluent builder chain.
321
- * Dependencies are expressed as an ordered array of injection tokens
322
- * that the container will resolve and inject into the constructor.
323
- *
324
- * @author Xeno
325
- * @version 1.0.0
326
- * @since 2025-09-30
327
- * @link https://github.com/xeno-js/xeno-js
328
- */
329
- interface IServiceContainer<Registry extends ApplicationRegistry<unknown> = ApplicationRegistry<unknown>> extends IServiceProvider<Registry>, IDisposable {
330
- /**
331
- * Registers an implementation under the given token with **singleton** lifetime.
332
- * A single instance is created on first resolution and reused for every
333
- * subsequent call within the container's lifetime.
334
- *
335
- * @param token - The unique injection token that identifies this service binding.
336
- * @param factory - Factory function that creates the service instance, receiving the scope as an argument.
337
- * @returns The container instance to allow method chaining.
338
- *
339
- * @author Xeno
340
- * @version 1.0.0
341
- * @since 2025-09-30
342
- * @link https://github.com/xeno-js/xeno-js
343
- */
344
- addSingleton<K extends keyof Registry>(token: K, factory: Factory<Registry[K], [IServiceScope<Registry>]>): this;
345
- /**
346
- * Registers an implementation under the given token with **transient** lifetime.
347
- * A new instance is created on every call to {@link resolve}.
348
- *
349
- * @param token - The unique injection token that identifies this service binding.
350
- * @param factory - Factory function that creates the service instance, receiving the scope as an argument.
351
- * @returns The container instance to allow method chaining.
352
- *
353
- * @author Xeno
354
- * @version 1.0.0
355
- * @since 2025-09-30
356
- * @link https://github.com/xeno-js/xeno-js
357
- */
358
- addTransient<K extends keyof Registry>(token: K, factory: Factory<Registry[K], [IServiceScope<Registry>]>): this;
359
- /**
360
- * Registers an implementation under the given token with **scoped** lifetime.
361
- * One instance is created per logical scope (e.g. per HTTP request).
362
- * Scoped services must be resolved through an {@link IServiceScope} obtained
363
- * via {@link createScope}; resolving them directly from the root container throws.
364
- *
365
- * @param token - The unique injection token that identifies this service binding.
366
- * @param factory - Factory function that creates the service instance, receiving the scope as an argument.
367
- * @returns The container instance to allow method chaining.
368
- *
369
- * @author Xeno
370
- * @version 1.0.0
371
- * @since 2025-09-30
372
- * @link https://github.com/xeno-js/xeno-js
373
- */
374
- addScoped<K extends keyof Registry>(token: K, factory: Factory<Registry[K], [IServiceScope<Registry>]>): this;
375
- /**
376
- * Creates a new {@link IServiceScope}.
377
- *
378
- * The returned scope shares singleton instances with the root container
379
- * and maintains its own isolated cache for scoped services.
380
- * Call {@link IServiceScope.dispose} when the scope is no longer needed.
381
- *
382
- * @returns A new scope instance.
383
- *
384
- * @author Xeno
385
- * @version 1.0.0
386
- * @since 2025-09-30
387
- * @link https://github.com/xeno-js/xeno-js
388
- */
389
- createScope(): IServiceScope<Registry>;
390
- }
391
-
392
- /**
393
- * @fileoverview Defines the IRequestContext interface for managing user identity context within the application.
394
- *
395
- * @author Xeno
396
- * @version 1.0.0
397
- * @since 2025-09-30
398
- * @link https://github.com/xeno-js/xeno-js
399
- */
400
-
401
- /**
402
- * An interface for managing user identity context within the application. This interface provides methods for executing asynchronous functions with the current user's identity context and retrieving the current user's identity information. It allows for seamless integration of identity management into various parts of the application, ensuring that identity-related data is properly propagated and accessible when needed.
403
- *
404
- * @author Xeno
405
- * @version 1.0.0
406
- * @since 2025-09-30
407
- * @link https://github.com/xeno-js/xeno-js
408
- */
409
- interface IRequestContext<TCtx, TRegistry extends ApplicationRegistry = ApplicationRegistry> extends IBaseAccessor<TCtx>, IServiceScopeAccessor<TRegistry> {
410
- /**
411
- * Executes the provided asynchronous function with the current user's identity context. This allows the function to access identity information such as user ID, roles, and correlation ID while performing its operations. The function will be executed within the scope of the current request's identity, ensuring that any identity-related data is properly propagated throughout the execution flow.
412
- * @param ctx The current user's identity context.
413
- * @param fn An asynchronous function that takes the current user's identity as an argument and returns a promise of type T. This function will be executed with the identity context of the current request.
414
- * @returns A promise that resolves to the result of the provided function, allowing the caller to handle the outcome of the operation performed within the identity context.
415
- *
416
- * @author Xeno
417
- * @version 1.0.0
418
- * @since 2025-09-30
419
- * @link https://github.com/xeno-js/xeno-js
420
- */
421
- runAsync<T = unknown>(ctx: TCtx, fn: () => Promise<T>): Promise<T>;
422
- updateIdentity(identity: Identity): void;
423
- }
424
- /**
425
- * @description The IIdentityAccessor interface is a contract that defines the structure and behavior of an identity accessor within the application. It provides a method for retrieving the current user's identity information, including user ID, roles, and correlation ID. This interface is essential for managing user identity and ensuring that identity-related data is accessible when needed.
426
- *
427
- *
428
- * @author Xeno
429
- * @version 1.0.0
430
- * @since 2025-09-30
431
- * @link https://github.com/xeno-js/xeno-js
432
- */
433
- interface IServiceScopeAccessor<TRegistry extends ApplicationRegistry<unknown> = ApplicationRegistry<unknown>> {
434
- /**
435
- * Retrieves the current service scope, which allows for managing dependencies during the execution of a request. This method enables access to the service scope, ensuring that services are properly scoped and disposed of after the request is processed.
436
- * @returns An object representing the current service scope, allowing for resolution of dependencies and management of services during the execution of a request.
437
- *
438
- * @author Xeno
439
- * @version 1.0.0
440
- * @since 2025-09-30
441
- * @link https://github.com/xeno-js/xeno-js
442
- */
443
- getScope(): Optional<IServiceScope<TRegistry>>;
444
- }
445
-
446
- /**
447
- * @description Interface for the crypto service
448
- */
449
- interface ICryptoService {
450
- /**
451
- * @description Generates a random byte array of the specified size.
452
- * @param size The size of the byte array to generate.
453
- * @returns A random byte array of the specified size.
454
- */
455
- randomBytes(size: number): Uint8Array;
456
- /**
457
- * @description Generates a HMAC SHA-256 hash of the specified data using the specified secret.
458
- * @param secret The secret to use for the HMAC SHA-256 hash.
459
- * @param data The data to hash.
460
- * @returns A promise that resolves to the HMAC SHA-256 hash of the specified data.
461
- */
462
- hmacSha256(secret: string, data: string): Promise<string>;
463
- /**
464
- * @description Compares two byte arrays in a way that is resistant to timing attacks.
465
- * @param a The first byte array to compare.
466
- * @param b The second byte array to compare.
467
- * @returns True if the two byte arrays are equal, false otherwise.
468
- */
469
- timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;
470
- }
471
-
472
- /**
473
- * @description Interface for a WebTransport.
474
- * @author Xeno
475
- * @version 1.2.1
476
- * @since 2025-09-30
477
- * @link https://github.com/xeno-js/xeno-js
478
- */
479
- interface XenoWebTransport {
480
- /**
481
- * @description The Extendend native request.
482
- * @type ExtendedRequest
483
- */
484
- request: ExtendedRequest;
485
- /**
486
- * @description The native response.
487
- * @type Response
488
- */
489
- response: Response;
490
- }
491
- /**
492
- * @description Interface for an HTTP adapter.
493
- * @author Xeno
494
- * @version 1.2.1
495
- * @since 2025-09-30
496
- * @link https://github.com/xeno-js/xeno-js
497
- */
498
- interface IHttpAdapter<TReq = Request, TRes = Response> {
499
- /**
500
- * @description Adapts an HTTP request and response to an ExtendedRequest and Response.
501
- * @param req - The HTTP request.
502
- * @param res - The HTTP response.
503
- * @returns An object containing the adapted ExtendedRequest and Response.
504
- */
505
- adapt(req: TReq, res: TRes): XenoWebTransport;
506
- }
507
-
508
- /**
509
- * @description Interface for the CSRF token service
510
- */
511
- interface ICsrfTokenService {
512
- /**
513
- * @description Generate a new CSRF token
514
- * @param subject The subject of the token
515
- * @returns A promise that resolves to the token
516
- */
517
- generate(subject: string): Promise<string>;
518
- /**
519
- * @description Validate a CSRF token
520
- * @param token The token to validate
521
- * @param subject The subject of the token
522
- * @returns A priomise that resolves to true if the token is valid, false otherwise
523
- */
524
- validate(token: string, subject: string): Promise<boolean>;
525
- }
526
-
527
- export type { ApplicationRegistry as A, IServiceProvider as I, XenoWebTransport as X, IServiceScope as a, IServiceContainer as b, IHttpAdapter as c, IAtomicCache as d, ICryptoService as e, ICsrfTokenService as f, IRequestContext as g, IServiceScopeAccessor as h };