@elysiumjs/core 0.10.0 → 0.11.1

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/http.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- import type { Context as ElysiaContext, Handler, HTTPMethod, TSchema } from 'elysia';
1
+ import type { Context as ElysiaContext } from 'elysia';
2
2
  import type { Class, MergeDeep } from 'type-fest';
3
+ import type { Static, TSchema } from 'typebox';
3
4
  import type { AppHttpContext } from '.';
5
+ import type { FilterClass } from './filter';
6
+ import type { InterceptorClass } from './interceptor';
4
7
  import type { Module } from './module';
5
8
  import { Elysia } from 'elysia';
6
9
  /**
@@ -8,6 +11,16 @@ import { Elysia } from 'elysia';
8
11
  * @author Axel Nana <axel.nana@workbud.com>
9
12
  */
10
13
  export type Route = `/${string}`;
14
+ /**
15
+ * An HTTP method name.
16
+ * @author Axel Nana <axel.nana@workbud.com>
17
+ */
18
+ export type HTTPMethod = string;
19
+ /**
20
+ * A function computing a value from the request context, used by custom parameter decorators.
21
+ * @author Axel Nana <axel.nana@workbud.com>
22
+ */
23
+ type Handler = (c: Context) => unknown;
11
24
  /**
12
25
  * A class for validating data against a schema.
13
26
  * @author Axel Nana <axel.nana@workbud.com>
@@ -22,14 +35,14 @@ export type ValidatorClass<T extends TSchema = TSchema> = {
22
35
  * Whether the request should be authorized before validation. If the request is not authorized,
23
36
  * the validation will be skipped.
24
37
  */
25
- authorize(ctx: Context<T['static']>): Promise<boolean>;
38
+ authorize(ctx: Context<Static<T>>): Promise<boolean>;
26
39
  /**
27
40
  * Processes custom validation logic. This method is called after the validation schema is checked
28
41
  * against the input data. So it's safe to assume that the schema is valid.
29
42
  * @param data The input data to validate.
30
43
  * @returns Whether the input data is valid.
31
44
  */
32
- validate(ctx: Context<T['static']>): Promise<boolean>;
45
+ validate(ctx: Context<Static<T>>): Promise<boolean>;
33
46
  };
34
47
  /**
35
48
  * Metadata for the HTTP context.
@@ -47,7 +60,7 @@ export type Singleton<TController = unknown, TModule extends Module = Module> =
47
60
  */
48
61
  module: TModule;
49
62
  };
50
- resolve: {
63
+ derive: {
51
64
  /**
52
65
  * The tenant ID.
53
66
  */
@@ -61,7 +74,7 @@ export type Singleton<TController = unknown, TModule extends Module = Module> =
61
74
  export type Context<TBody = unknown, TController = unknown, TModule extends Module = Module> = ElysiaContext<{
62
75
  body: TBody;
63
76
  }, Singleton<TController, TModule>>;
64
- export type ElysiaApp = Elysia<Route, Singleton>;
77
+ export type ElysiaApp = Elysia<Route, 'local', Singleton>;
65
78
  /**
66
79
  * Name of the global service running a callback in a database transaction, used by
67
80
  * `@transactional` handlers. Provided by database modules such as `DrizzleModule`.
@@ -83,14 +96,10 @@ export declare enum HttpControllerScope {
83
96
  REQUEST = 1
84
97
  }
85
98
  /**
86
- * Properties required when declaring an HTTP controller using the `@http()` decorator.
99
+ * Options of `@Http.controller()`.
87
100
  * @author Axel Nana <axel.nana@workbud.com>
88
101
  */
89
- export type HttpControllerProps = {
90
- /**
91
- * The path of the HTTP route.
92
- */
93
- path: Route;
102
+ export type HttpControllerOptions = {
94
103
  /**
95
104
  * The scope of the HTTP controller.
96
105
  * @default HttpControllerScope.SERVER
@@ -106,20 +115,30 @@ export type HttpControllerProps = {
106
115
  * for all the request handlers in this controller.
107
116
  */
108
117
  transactional?: boolean;
109
- };
110
- /**
111
- * Properties required when declaring an HTTP request handler.
112
- * @author Axel Nana <axel.nana@workbud.com>
113
- */
114
- export type RequestHandlerDecoratorProps = {
115
118
  /**
116
- * The HTTP method of the handler.
119
+ * Exception filters for every route of the controller. They run after the filters of the route,
120
+ * and before those of the module and the app.
117
121
  */
118
- method?: HTTPMethod;
122
+ filters?: FilterClass[];
119
123
  /**
120
- * The path of the HTTP route.
124
+ * Interceptors around every handler of the controller, inside those of the module and the app,
125
+ * and outside those of the route.
121
126
  */
122
- path?: Route;
127
+ interceptors?: InterceptorClass[];
128
+ };
129
+ /**
130
+ * What an HTTP controller records: its path and its options.
131
+ * @author Axel Nana <axel.nana@workbud.com>
132
+ */
133
+ export type HttpControllerProps = HttpControllerOptions & {
134
+ /** The path of the controller. */
135
+ path: Route;
136
+ };
137
+ /**
138
+ * Options of the route decorators, such as `@Http.get()`.
139
+ * @author Axel Nana <axel.nana@workbud.com>
140
+ */
141
+ export type HttpRouteOptions = {
123
142
  /**
124
143
  * The schema of the response body.
125
144
  */
@@ -139,76 +158,158 @@ export type RequestHandlerDecoratorProps = {
139
158
  * for this request handler.
140
159
  */
141
160
  transactional?: boolean;
161
+ /**
162
+ * Whether to serialize the handler result through the `response` schema: undeclared and hidden
163
+ * fields are stripped, dates become ISO strings, and `?fields=` selects fields. Defaults to
164
+ * `true` when the route has a `response` schema.
165
+ */
166
+ serialize?: boolean;
167
+ /** Exception filters for this route. They run before those of the controller. */
168
+ filters?: FilterClass[];
169
+ /** Interceptors around this handler, inside those of the controller. */
170
+ interceptors?: InterceptorClass[];
171
+ };
172
+ /**
173
+ * What a route decorator records: its method, its path and its options.
174
+ * @author Axel Nana <axel.nana@workbud.com>
175
+ */
176
+ export type RequestHandlerDecoratorProps = HttpRouteOptions & {
177
+ /** The HTTP method of the handler. */
178
+ method?: HTTPMethod;
179
+ /** The path of the route, relative to the controller path. */
180
+ path?: Route;
181
+ };
182
+ /**
183
+ * A route of an HTTP controller, as `Http.describe()` reports it.
184
+ * @author Axel Nana <axel.nana@workbud.com>
185
+ */
186
+ export type HttpRouteDescription = {
187
+ /** The name of the controller method handling the route. */
188
+ name: string;
189
+ /** The HTTP method, in upper case. Server-sent events routes use `GET`. */
190
+ method: string;
191
+ /** The path of the route, relative to the controller path. */
192
+ path: Route;
193
+ /** Whether the route streams server-sent events. */
194
+ sse: boolean;
195
+ /** The index of the method parameter receiving the request body. */
196
+ body?: number;
197
+ /** The index of the method parameter receiving the query. */
198
+ query?: number;
199
+ /** The method parameters receiving URL parameters. */
200
+ params: Array<{
201
+ slug: string;
202
+ index: number;
203
+ }>;
204
+ };
205
+ /**
206
+ * An HTTP controller, as `Http.describe()` reports it.
207
+ * @author Axel Nana <axel.nana@workbud.com>
208
+ */
209
+ export type HttpControllerDescription = {
210
+ /** The path of the controller. */
211
+ path: Route;
212
+ /** The routes of the controller. */
213
+ routes: HttpRouteDescription[];
142
214
  };
143
215
  export declare namespace Http {
216
+ /**
217
+ * Describes the routes of an HTTP controller, for tools such as the route type generator.
218
+ * Route decorators register their routes on the next tick, so call it after importing the
219
+ * controller and awaiting a tick.
220
+ * @author Axel Nana <axel.nana@workbud.com>
221
+ * @param controller The controller class.
222
+ * @returns The description, or `undefined` when the class is not an HTTP controller.
223
+ */
224
+ const describe: (controller: Class<any>) => HttpControllerDescription | undefined;
144
225
  /**
145
226
  * Marks a class as an HTTP controller.
146
227
  * @author Axel Nana <axel.nana@workbud.com>
147
- * @param props The decorator options.
228
+ * @param path The path of the controller. Defaults to `/`.
229
+ * @param options The options of the controller.
230
+ * @example
231
+ * ```ts
232
+ * @Http.controller('/wallets', { tags: ['wallets'] })
233
+ * class WalletController {}
234
+ * ```
148
235
  */
149
- const controller: (props?: HttpControllerProps) => (target: Class<any>) => void;
236
+ const controller: (path?: Route, options?: HttpControllerOptions) => (target: Class<any>) => void;
150
237
  /**
151
238
  * Marks a method as an HTTP "server-sent events" request handler.
152
239
  * @author Axel Nana <axel.nana@workbud.com>
153
- * @param props The request handler properties.
240
+ * @param path The path of the route, relative to the controller path. Defaults to `/`.
241
+ * @param options The options of the route.
154
242
  */
155
- const sse: (props?: RequestHandlerDecoratorProps) => MethodDecorator;
243
+ const sse: (path?: Route, options?: HttpRouteOptions) => MethodDecorator;
156
244
  /**
157
245
  * Marks a method as an HTTP "get" request handler.
158
246
  * @author Axel Nana <axel.nana@workbud.com>
159
- * @param props The request handler properties.
247
+ * @param path The path of the route, relative to the controller path. Defaults to `/`.
248
+ * @param options The options of the route.
249
+ * @example
250
+ * ```ts
251
+ * @Http.get('/:id', { response: WalletSchema })
252
+ * public show(@Http.param('id') id: string) {}
253
+ * ```
160
254
  */
161
- const get: (props?: RequestHandlerDecoratorProps) => MethodDecorator;
255
+ const get: (path?: Route, options?: HttpRouteOptions) => MethodDecorator;
162
256
  /**
163
257
  * Marks a method as an HTTP "post" request handler.
164
258
  * @author Axel Nana <axel.nana@workbud.com>
165
- * @param props The request handler properties.
259
+ * @param path The path of the route, relative to the controller path. Defaults to `/`.
260
+ * @param options The options of the route.
166
261
  */
167
- const post: (props?: RequestHandlerDecoratorProps) => MethodDecorator;
262
+ const post: (path?: Route, options?: HttpRouteOptions) => MethodDecorator;
168
263
  /**
169
264
  * Marks a method as an HTTP "put" request handler.
170
265
  * @author Axel Nana <axel.nana@workbud.com>
171
- * @param props The request handler properties.
266
+ * @param path The path of the route, relative to the controller path. Defaults to `/`.
267
+ * @param options The options of the route.
172
268
  */
173
- const put: (props?: RequestHandlerDecoratorProps) => MethodDecorator;
269
+ const put: (path?: Route, options?: HttpRouteOptions) => MethodDecorator;
174
270
  /**
175
271
  * Marks a method as an HTTP "delete" request handler.
176
272
  * @author Axel Nana <axel.nana@workbud.com>
177
- * @param props The request handler properties.
273
+ * @param path The path of the route, relative to the controller path. Defaults to `/`.
274
+ * @param options The options of the route.
178
275
  */
179
- const del: (props?: RequestHandlerDecoratorProps) => MethodDecorator;
276
+ const del: (path?: Route, options?: HttpRouteOptions) => MethodDecorator;
180
277
  /**
181
278
  * Marks a method as an HTTP "patch" request handler.
182
279
  * @author Axel Nana <axel.nana@workbud.com>
183
- * @param props The request handler properties.
280
+ * @param path The path of the route, relative to the controller path. Defaults to `/`.
281
+ * @param options The options of the route.
184
282
  */
185
- const patch: (props?: RequestHandlerDecoratorProps) => MethodDecorator;
283
+ const patch: (path?: Route, options?: HttpRouteOptions) => MethodDecorator;
186
284
  /**
187
285
  * Marks a method as an HTTP "head" request handler.
188
286
  * @author Axel Nana <axel.nana@workbud.com>
189
- * @param props The request handler properties.
287
+ * @param path The path of the route, relative to the controller path. Defaults to `/`.
288
+ * @param options The options of the route.
190
289
  */
191
- const head: (props?: RequestHandlerDecoratorProps) => MethodDecorator;
290
+ const head: (path?: Route, options?: HttpRouteOptions) => MethodDecorator;
192
291
  /**
193
292
  * Marks a method as an HTTP "options" request handler.
194
293
  * @author Axel Nana <axel.nana@workbud.com>
195
- * @param props The request handler properties.
294
+ * @param path The path of the route, relative to the controller path. Defaults to `/`.
295
+ * @param options The options of the route.
196
296
  */
197
- const options: (props?: RequestHandlerDecoratorProps) => MethodDecorator;
297
+ const options: (path?: Route, options?: HttpRouteOptions) => MethodDecorator;
198
298
  /**
199
299
  * Marks a method as an HTTP "trace" request handler.
200
300
  * @author Axel Nana <axel.nana@workbud.com>
201
- * @param props The request handler properties.
301
+ * @param path The path of the route, relative to the controller path. Defaults to `/`.
302
+ * @param options The options of the route.
202
303
  */
203
- const trace: (props?: RequestHandlerDecoratorProps) => MethodDecorator;
304
+ const trace: (path?: Route, options?: HttpRouteOptions) => MethodDecorator;
204
305
  /**
205
306
  * Marks a method as an HTTP request handler with a custom method.
206
307
  * @author Axel Nana <axel.nana@workbud.com>
207
- * @param props The request handler properties.
308
+ * @param method The HTTP method.
309
+ * @param path The path of the route, relative to the controller path. Defaults to `/`.
310
+ * @param options The options of the route.
208
311
  */
209
- const custom: ({ path, ...rest }: RequestHandlerDecoratorProps & {
210
- method: HTTPMethod;
211
- }) => MethodDecorator;
312
+ const custom: (method: HTTPMethod, path?: Route, options?: HttpRouteOptions) => MethodDecorator;
212
313
  /**
213
314
  * Marks a method as the callback for the request handler.
214
315
  * @author Axel Nana <axel.nana@workbud.com>
@@ -245,3 +346,4 @@ export declare namespace Http {
245
346
  */
246
347
  const decorate: (handler: Handler) => (() => ParameterDecorator);
247
348
  }
349
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,23 +1,26 @@
1
- import type { SingletonBase } from 'elysia';
2
- export { type Elysia, type TSchema, type Static, t } from 'elysia';
3
- export { type AppContext, type ElysiumPlugin, Application } from './app';
1
+ export { type Elysia, t } from 'elysia';
2
+ export { type Static, type TObject, type TOptional, type TSchema, type TString } from 'typebox';
3
+ export { type AppContext, type ElysiumPlugin, type ServerError, Application } from './app';
4
4
  export { type CommandArgumentProps, type CommandClass, Command, CommandArgumentType } from './command';
5
- export { ConsoleFormat, InteractsWithConsole } from './console';
5
+ export { ConsoleFormat, type ConsoleTag, type ConsoleTagStyle, InteractsWithConsole } from './console';
6
6
  export { Env } from './env';
7
- export { type EventData, type EventHandler, Event } from './event';
7
+ export { type LifecycleEvent, type LifecycleHandler, Lifecycle } from './lifecycle';
8
8
  export { type LoggerInterface, ConsoleLogger, getLogger, LOGGER_SERVICE } from './logger';
9
- export { type Context, type Route, DATABASE_TRANSACTION_SERVICE, HttpControllerScope, Http } from './http';
9
+ export { type Context, type HttpControllerDescription, type HttpControllerOptions, type HttpControllerProps, type HttpRouteDescription, type HttpRouteOptions, type RequestHandlerDecoratorProps, type Route, DATABASE_TRANSACTION_SERVICE, HttpControllerScope, Http } from './http';
10
10
  export { Metadata } from './metadata';
11
+ export { currentUserId, userIdOf } from './user';
12
+ export { currentEnvironment } from './utils';
11
13
  export { Middleware } from './middleware';
12
14
  export { RequestLoggerMiddleware } from './middlewares/request-logger.middleware';
13
- export { CircularDependencyError, ConflictingModuleConfigError, DuplicateCommandError, DuplicateGlobalServiceError, DuplicateModuleNameError, ElysiumError, EnvironmentSkippedModuleError, EventListenerNotSupportedError, InvalidModuleError, InvalidModuleConfigError, MissingModuleConfigError, ModuleCycleError, ServiceNotFoundError, ServiceNotReadyError, ServiceNotVisibleError } from './errors';
15
+ export { CircularDependencyError, ConflictingModuleConfigError, DuplicateCommandError, DuplicateGlobalServiceError, DuplicateModuleNameError, ElysiumError, EnvironmentSkippedModuleError, LifecycleListenerNotSupportedError, InvalidModuleError, InvalidModuleConfigError, MissingModuleConfigError, ModuleCycleError, OutsideRequestScopeError, ScopeMismatchError, InterceptorNextCalledTwiceError, ServiceNotFoundError, ServiceNotReadyError, ServiceNotVisibleError } from './errors';
14
16
  export { needsDefaultAlias, runAll } from './connections';
15
17
  export { Kernel, KERNEL_SERVICE } from './kernel';
16
18
  export { type ConfiguredModule, type ModuleClass, type ModuleProps, Module } from './module';
17
19
  export { ModuleContext } from './module-context';
18
20
  export { type ModuleGraph, type ModuleNode } from './module-graph';
19
- export { type TestingApp, type TestingOptions } from './testing';
20
- export { type RedisConnectionProps, type RedisModuleConfig, Redis, RedisModule } from './redis';
21
+ export { type TestingApp, type TestingAppOptions, type TestingHttpApp, type TestingOptions, resetContainer } from './testing';
22
+ export { type HttpTestClient, type RequestHandler, type TestResponse, createHttpClient, TestRequest, UnexpectedStatusError } from './http-client';
23
+ export { type OutputLevel, type ServeHandle, type ShutdownSignal, Runtime, RuntimeNotConfiguredError, RuntimeNotSupportedError } from './runtime';
21
24
  export { type ServiceDefinition, type ServiceFactoryDefinition, ServiceScope, Service } from './service';
22
25
  export { type WampRegistrationOptions, type WampSubscriptionOptions, type WampProps, type WampRegistrationHandlerArgs, type WampSubscriptionHandlerArgs, type WampRegistrationHandler, type WampSubscriptionHandler, type WampClientProps, type WampModuleConfig, Wamp, WampModule } from './wamp';
23
26
  export { type WS, type WSError, Websocket } from './websocket';
@@ -25,7 +28,10 @@ export { type WS, type WSError, Websocket } from './websocket';
25
28
  * The type for the context of the application.
26
29
  * @author Axel Nana <axel.nana@workbud.com>
27
30
  */
28
- export interface AppHttpContext extends SingletonBase {
31
+ export interface AppHttpContext {
32
+ decorator: Record<string, unknown>;
33
+ store: Record<string, unknown>;
34
+ derive: Record<string, unknown>;
29
35
  }
30
36
  /**
31
37
  * The type for all registered environment variables.
@@ -34,3 +40,10 @@ export interface AppHttpContext extends SingletonBase {
34
40
  export interface AppEnv {
35
41
  [key: string]: string;
36
42
  }
43
+ export { type DomainErrorOptions, type ElysiumProblemCode, type ProblemCatalogue, type ProblemCode, type ProblemContext, type ProblemDetails, type ProblemOptions, type ProblemValidationError, BadRequestError, ConflictError, DomainError, ELYSIUM_PROBLEM_CODES, ForbiddenError, isDomainError, isProblemDetails, NotFoundError, PROBLEM_CONTENT_TYPE, ProblemDetailsSchema, problemResponse, problemType, requestIdOf, statusTitle, toProblem, UnauthenticatedError, ValidationFailedError, validationErrors } from './problem';
44
+ export { type ExceptionFilter, type FilterClass, type FilterContext, Catch } from './filter';
45
+ export { type ResponseSchemas, Resource } from './resource';
46
+ export { type AppRateLimitOptions, type RateLimitHit, type RateLimitKeyContext, type RateLimitOptions, type RateLimitStore, MemoryRateLimitStore, RateLimit, TooManyRequestsError } from './rate-limit';
47
+ export { type Interceptor, type InterceptorClass, type InterceptorContext, TimingInterceptor, TransformInterceptor } from './interceptor';
48
+ export { type EnvironmentIssue, ConflictingEnvironmentSchemaError, InvalidEnvironmentError, mergeEnvironmentSchemas, Secret, validateEnvironment } from './config';
49
+ export { type CorsOptions, type HttpOptions, type SecurityHeadersOptions, clientAddress, DEFAULT_BODY_LIMIT, InvalidCorsOptionsError, PayloadTooLargeError } from './security';