@veloxts/core 0.1.0 → 0.2.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/context.d.ts DELETED
@@ -1,88 +0,0 @@
1
- /**
2
- * Context system for request-scoped state
3
- * Supports TypeScript declaration merging for plugin extensions
4
- * @module context
5
- */
6
- import type { FastifyReply, FastifyRequest } from 'fastify';
7
- /**
8
- * Base context interface available in all request handlers
9
- *
10
- * Plugins extend this via TypeScript declaration merging to add
11
- * their own properties (e.g., database clients, user sessions, etc.)
12
- *
13
- * @example
14
- * ```typescript
15
- * // In a plugin file:
16
- * declare module '@veloxts/core' {
17
- * interface BaseContext {
18
- * db: PrismaClient;
19
- * user?: User;
20
- * }
21
- * }
22
- * ```
23
- *
24
- * @example
25
- * ```typescript
26
- * // In a route handler:
27
- * server.get('/users', async (request) => {
28
- * const users = await request.context.db.user.findMany();
29
- * return users;
30
- * });
31
- * ```
32
- */
33
- export interface BaseContext {
34
- /**
35
- * Fastify request object
36
- * Provides access to request data (params, query, body, headers, etc.)
37
- */
38
- request: FastifyRequest;
39
- /**
40
- * Fastify reply object
41
- * Provides access to response methods (send, status, header, etc.)
42
- */
43
- reply: FastifyReply;
44
- }
45
- /**
46
- * Creates a context object for the current request
47
- *
48
- * This function is called automatically by the framework for each request
49
- * via the `onRequest` hook. Users typically don't need to call this directly.
50
- *
51
- * @param request - Fastify request object
52
- * @param reply - Fastify reply object
53
- * @returns Context object with request and reply
54
- *
55
- * @internal
56
- */
57
- export declare function createContext(request: FastifyRequest, reply: FastifyReply): BaseContext;
58
- /**
59
- * Type guard to check if a value is a valid context object
60
- *
61
- * Uses the `in` operator for safe property access with proper null checks.
62
- *
63
- * @param value - Value to check
64
- * @returns true if value is a valid BaseContext
65
- *
66
- * @example
67
- * ```typescript
68
- * if (isContext(someValue)) {
69
- * // TypeScript knows someValue has request and reply properties
70
- * console.log(someValue.request.url);
71
- * }
72
- * ```
73
- */
74
- export declare function isContext(value: unknown): value is BaseContext;
75
- /**
76
- * Augment Fastify's Request interface to include context
77
- * This enables `request.context` in route handlers
78
- */
79
- declare module 'fastify' {
80
- interface FastifyRequest {
81
- /**
82
- * Request-scoped context object
83
- * Contains request/reply and plugin-provided properties
84
- */
85
- context: BaseContext;
86
- }
87
- }
88
- //# sourceMappingURL=context.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,OAAO,EAAE,cAAc,CAAC;IAExB;;;OAGG;IACH,KAAK,EAAE,YAAY,CAAC;CACrB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,YAAY,GAAG,WAAW,CAKvF;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAqB9D;AAED;;;GAGG;AACH,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,cAAc;QACtB;;;WAGG;QACH,OAAO,EAAE,WAAW,CAAC;KACtB;CACF"}
package/dist/context.js DELETED
@@ -1,57 +0,0 @@
1
- /**
2
- * Context system for request-scoped state
3
- * Supports TypeScript declaration merging for plugin extensions
4
- * @module context
5
- */
6
- /**
7
- * Creates a context object for the current request
8
- *
9
- * This function is called automatically by the framework for each request
10
- * via the `onRequest` hook. Users typically don't need to call this directly.
11
- *
12
- * @param request - Fastify request object
13
- * @param reply - Fastify reply object
14
- * @returns Context object with request and reply
15
- *
16
- * @internal
17
- */
18
- export function createContext(request, reply) {
19
- return {
20
- request,
21
- reply,
22
- };
23
- }
24
- /**
25
- * Type guard to check if a value is a valid context object
26
- *
27
- * Uses the `in` operator for safe property access with proper null checks.
28
- *
29
- * @param value - Value to check
30
- * @returns true if value is a valid BaseContext
31
- *
32
- * @example
33
- * ```typescript
34
- * if (isContext(someValue)) {
35
- * // TypeScript knows someValue has request and reply properties
36
- * console.log(someValue.request.url);
37
- * }
38
- * ```
39
- */
40
- export function isContext(value) {
41
- // Early return for non-objects
42
- if (typeof value !== 'object' || value === null) {
43
- return false;
44
- }
45
- // Check properties exist using 'in' operator
46
- if (!('request' in value) || !('reply' in value)) {
47
- return false;
48
- }
49
- // After 'in' checks, safely access properties
50
- const ctx = value;
51
- // Verify request and reply are non-null objects
52
- return (typeof ctx.request === 'object' &&
53
- ctx.request !== null &&
54
- typeof ctx.reply === 'object' &&
55
- ctx.reply !== null);
56
- }
57
- //# sourceMappingURL=context.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA4CH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa,CAAC,OAAuB,EAAE,KAAmB;IACxE,OAAO;QACL,OAAO;QACP,KAAK;KACN,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,+BAA+B;IAC/B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,6CAA6C;IAC7C,IAAI,CAAC,CAAC,SAAS,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,IAAI,KAAK,CAAC,EAAE,CAAC;QACjD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,8CAA8C;IAC9C,MAAM,GAAG,GAAG,KAA6C,CAAC;IAE1D,gDAAgD;IAChD,OAAO,CACL,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ;QAC/B,GAAG,CAAC,OAAO,KAAK,IAAI;QACpB,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ;QAC7B,GAAG,CAAC,KAAK,KAAK,IAAI,CACnB,CAAC;AACJ,CAAC"}
package/dist/errors.d.ts DELETED
@@ -1,298 +0,0 @@
1
- /**
2
- * Error handling foundation for VeloxTS framework
3
- * Provides base error classes with HTTP status codes and discriminated unions
4
- * @module errors
5
- */
6
- /**
7
- * Known error codes in the VeloxTS framework core
8
- * Can be extended via declaration merging by plugins
9
- */
10
- export type VeloxCoreErrorCode = 'VALIDATION_ERROR' | 'NOT_FOUND' | 'CONFIGURATION_ERROR' | 'PLUGIN_REGISTRATION_ERROR' | 'SERVER_ALREADY_RUNNING' | 'SERVER_NOT_RUNNING' | 'SERVER_START_ERROR' | 'SERVER_STOP_ERROR' | 'INVALID_PLUGIN_METADATA';
11
- /**
12
- * Registry for error codes - allows plugins to extend via declaration merging
13
- *
14
- * @example
15
- * ```typescript
16
- * // In your plugin
17
- * declare module '@veloxts/core' {
18
- * interface VeloxErrorCodeRegistry {
19
- * myPlugin: 'MY_PLUGIN_ERROR' | 'ANOTHER_ERROR';
20
- * }
21
- * }
22
- * ```
23
- */
24
- export interface VeloxErrorCodeRegistry {
25
- core: VeloxCoreErrorCode;
26
- }
27
- /**
28
- * Union of all registered error codes from all packages
29
- */
30
- export type VeloxErrorCode = VeloxErrorCodeRegistry[keyof VeloxErrorCodeRegistry];
31
- /**
32
- * Base error response fields common to all errors
33
- */
34
- interface BaseErrorResponse {
35
- /** Error class name */
36
- error: string;
37
- /** Human-readable error message */
38
- message: string;
39
- /** HTTP status code */
40
- statusCode: number;
41
- /** Error code for programmatic handling */
42
- code?: string;
43
- }
44
- /**
45
- * Validation error response with field-specific errors
46
- */
47
- export interface ValidationErrorResponse extends BaseErrorResponse {
48
- error: 'ValidationError';
49
- statusCode: 400;
50
- code: 'VALIDATION_ERROR';
51
- /** Field-specific validation errors */
52
- fields?: Record<string, string>;
53
- }
54
- /**
55
- * Not found error response with resource details
56
- */
57
- export interface NotFoundErrorResponse extends BaseErrorResponse {
58
- error: 'NotFoundError';
59
- statusCode: 404;
60
- code: 'NOT_FOUND';
61
- /** Type of resource that was not found */
62
- resource: string;
63
- /** Optional identifier of the resource */
64
- resourceId?: string;
65
- }
66
- /**
67
- * Generic VeloxTS error response for all other errors
68
- */
69
- export interface GenericErrorResponse extends BaseErrorResponse {
70
- error: string;
71
- statusCode: number;
72
- code?: string;
73
- }
74
- /**
75
- * Discriminated union of all error response types
76
- * Enables type-safe error handling based on the 'error' field
77
- *
78
- * @example
79
- * ```typescript
80
- * function handleError(response: ErrorResponse) {
81
- * if (response.error === 'ValidationError') {
82
- * // TypeScript knows response.fields exists here
83
- * console.log(response.fields);
84
- * }
85
- * }
86
- * ```
87
- */
88
- export type ErrorResponse = ValidationErrorResponse | NotFoundErrorResponse | GenericErrorResponse;
89
- /**
90
- * Type guard for validation error responses
91
- */
92
- export declare function isValidationErrorResponse(response: ErrorResponse): response is ValidationErrorResponse;
93
- /**
94
- * Type guard for not found error responses
95
- */
96
- export declare function isNotFoundErrorResponse(response: ErrorResponse): response is NotFoundErrorResponse;
97
- /**
98
- * Base error class for all VeloxTS framework errors
99
- *
100
- * Extends the standard Error class with HTTP status code and optional error code.
101
- * All framework errors should extend this class for consistent error handling.
102
- *
103
- * @template TCode - The error code type (defaults to VeloxErrorCode)
104
- *
105
- * @example
106
- * ```typescript
107
- * throw new VeloxError('Something went wrong', 500);
108
- * ```
109
- *
110
- * @example
111
- * ```typescript
112
- * throw new VeloxError('Database connection failed', 503, 'DB_CONNECTION_ERROR');
113
- * ```
114
- */
115
- export declare class VeloxError<TCode extends string = string> extends Error {
116
- /**
117
- * HTTP status code for the error
118
- */
119
- readonly statusCode: number;
120
- /**
121
- * Optional error code for programmatic error handling
122
- */
123
- readonly code?: TCode;
124
- /**
125
- * Creates a new VeloxError instance
126
- *
127
- * @param message - Human-readable error message
128
- * @param statusCode - HTTP status code (default: 500)
129
- * @param code - Optional error code for programmatic handling
130
- */
131
- constructor(message: string, statusCode?: number, code?: TCode);
132
- /**
133
- * Converts error to JSON format for API responses
134
- *
135
- * @returns Error response object
136
- */
137
- toJSON(): GenericErrorResponse;
138
- }
139
- /**
140
- * Validation error for invalid user input
141
- *
142
- * Used when request data fails validation (e.g., missing required fields,
143
- * invalid format, type mismatches, etc.)
144
- *
145
- * @example
146
- * ```typescript
147
- * throw new ValidationError('Invalid email format', {
148
- * email: 'Must be a valid email address'
149
- * });
150
- * ```
151
- *
152
- * @example
153
- * ```typescript
154
- * throw new ValidationError('Missing required fields', {
155
- * name: 'Name is required',
156
- * email: 'Email is required'
157
- * });
158
- * ```
159
- */
160
- export declare class ValidationError extends VeloxError<'VALIDATION_ERROR'> {
161
- /**
162
- * Field-specific validation errors
163
- * Maps field names to error messages
164
- */
165
- readonly fields?: Record<string, string>;
166
- /**
167
- * Creates a new ValidationError instance
168
- *
169
- * @param message - General validation error message
170
- * @param fields - Optional object mapping field names to specific error messages
171
- */
172
- constructor(message: string, fields?: Record<string, string>);
173
- /**
174
- * Converts validation error to JSON format with field details
175
- *
176
- * @returns Validation error response with field-specific errors
177
- */
178
- toJSON(): ValidationErrorResponse;
179
- }
180
- /**
181
- * Configuration error for framework setup issues
182
- *
183
- * Used when the framework is misconfigured or used incorrectly.
184
- * These errors indicate developer mistakes rather than runtime issues.
185
- *
186
- * @example
187
- * ```typescript
188
- * throw new ConfigurationError('VeloxApp must be started before registering routes');
189
- * ```
190
- *
191
- * @example
192
- * ```typescript
193
- * throw new ConfigurationError('Missing required plugin: @veloxts/orm');
194
- * ```
195
- */
196
- export declare class ConfigurationError extends VeloxError<'CONFIGURATION_ERROR'> {
197
- /**
198
- * Creates a new ConfigurationError instance
199
- *
200
- * @param message - Human-readable error message explaining the configuration issue
201
- */
202
- constructor(message: string);
203
- }
204
- /**
205
- * Not found error for missing resources
206
- *
207
- * Used when a requested resource (user, post, file, etc.) doesn't exist
208
- *
209
- * @example
210
- * ```typescript
211
- * throw new NotFoundError('User');
212
- * // Message: "User not found"
213
- * ```
214
- *
215
- * @example
216
- * ```typescript
217
- * throw new NotFoundError('Post', '123');
218
- * // Message: "Post with id 123 not found"
219
- * ```
220
- */
221
- export declare class NotFoundError extends VeloxError<'NOT_FOUND'> {
222
- /**
223
- * Type of resource that was not found
224
- */
225
- readonly resource: string;
226
- /**
227
- * Optional identifier of the resource that was not found
228
- */
229
- readonly resourceId?: string;
230
- /**
231
- * Creates a new NotFoundError instance
232
- *
233
- * @param resource - Type of resource (e.g., "User", "Post", "File")
234
- * @param resourceId - Optional identifier of the specific resource
235
- */
236
- constructor(resource: string, resourceId?: string);
237
- /**
238
- * Converts not found error to JSON format with resource details
239
- *
240
- * @returns Not found error response with resource information
241
- */
242
- toJSON(): NotFoundErrorResponse;
243
- }
244
- /**
245
- * Type guard to check if an error is a VeloxError
246
- *
247
- * @param error - Error to check
248
- * @returns true if error is a VeloxError instance
249
- *
250
- * @example
251
- * ```typescript
252
- * try {
253
- * // ... some code
254
- * } catch (error) {
255
- * if (isVeloxError(error)) {
256
- * console.log('Status code:', error.statusCode);
257
- * }
258
- * }
259
- * ```
260
- */
261
- export declare function isVeloxError(error: unknown): error is VeloxError;
262
- /**
263
- * Type guard to check if an error is a ValidationError
264
- *
265
- * @param error - Error to check
266
- * @returns true if error is a ValidationError instance
267
- */
268
- export declare function isValidationError(error: unknown): error is ValidationError;
269
- /**
270
- * Type guard to check if an error is a NotFoundError
271
- *
272
- * @param error - Error to check
273
- * @returns true if error is a NotFoundError instance
274
- */
275
- export declare function isNotFoundError(error: unknown): error is NotFoundError;
276
- /**
277
- * Type guard to check if an error is a ConfigurationError
278
- *
279
- * @param error - Error to check
280
- * @returns true if error is a ConfigurationError instance
281
- */
282
- export declare function isConfigurationError(error: unknown): error is ConfigurationError;
283
- /**
284
- * Helper to ensure exhaustive handling of error types
285
- * Throws at compile time if a case is not handled
286
- *
287
- * @example
288
- * ```typescript
289
- * function handleError(error: VeloxError): string {
290
- * if (isValidationError(error)) return 'validation';
291
- * if (isNotFoundError(error)) return 'not found';
292
- * return 'generic';
293
- * }
294
- * ```
295
- */
296
- export declare function assertNever(value: never): never;
297
- export {};
298
- //# sourceMappingURL=errors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAC1B,kBAAkB,GAClB,WAAW,GACX,qBAAqB,GACrB,2BAA2B,GAC3B,wBAAwB,GACxB,oBAAoB,GACpB,oBAAoB,GACpB,mBAAmB,GACnB,yBAAyB,CAAC;AAE9B;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,kBAAkB,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,sBAAsB,CAAC,MAAM,sBAAsB,CAAC,CAAC;AAMlF;;GAEG;AACH,UAAU,iBAAiB;IACzB,uBAAuB;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,mCAAmC;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,uBAAuB;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,2CAA2C;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,uBAAwB,SAAQ,iBAAiB;IAChE,KAAK,EAAE,iBAAiB,CAAC;IACzB,UAAU,EAAE,GAAG,CAAC;IAChB,IAAI,EAAE,kBAAkB,CAAC;IACzB,uCAAuC;IACvC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjC;AAED;;GAEG;AACH,MAAM,WAAW,qBAAsB,SAAQ,iBAAiB;IAC9D,KAAK,EAAE,eAAe,CAAC;IACvB,UAAU,EAAE,GAAG,CAAC;IAChB,IAAI,EAAE,WAAW,CAAC;IAClB,0CAA0C;IAC1C,QAAQ,EAAE,MAAM,CAAC;IACjB,0CAA0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