@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/errors.js DELETED
@@ -1,294 +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
- * Type guard for validation error responses
8
- */
9
- export function isValidationErrorResponse(response) {
10
- return response.error === 'ValidationError';
11
- }
12
- /**
13
- * Type guard for not found error responses
14
- */
15
- export function isNotFoundErrorResponse(response) {
16
- return response.error === 'NotFoundError';
17
- }
18
- // ============================================================================
19
- // Error Classes
20
- // ============================================================================
21
- /**
22
- * Base error class for all VeloxTS framework errors
23
- *
24
- * Extends the standard Error class with HTTP status code and optional error code.
25
- * All framework errors should extend this class for consistent error handling.
26
- *
27
- * @template TCode - The error code type (defaults to VeloxErrorCode)
28
- *
29
- * @example
30
- * ```typescript
31
- * throw new VeloxError('Something went wrong', 500);
32
- * ```
33
- *
34
- * @example
35
- * ```typescript
36
- * throw new VeloxError('Database connection failed', 503, 'DB_CONNECTION_ERROR');
37
- * ```
38
- */
39
- export class VeloxError extends Error {
40
- /**
41
- * HTTP status code for the error
42
- */
43
- statusCode;
44
- /**
45
- * Optional error code for programmatic error handling
46
- */
47
- code;
48
- /**
49
- * Creates a new VeloxError instance
50
- *
51
- * @param message - Human-readable error message
52
- * @param statusCode - HTTP status code (default: 500)
53
- * @param code - Optional error code for programmatic handling
54
- */
55
- constructor(message, statusCode = 500, code) {
56
- super(message);
57
- this.name = 'VeloxError';
58
- this.statusCode = statusCode;
59
- this.code = code;
60
- // Maintains proper stack trace for where error was thrown (V8 only)
61
- if (Error.captureStackTrace) {
62
- Error.captureStackTrace(this, VeloxError);
63
- }
64
- }
65
- /**
66
- * Converts error to JSON format for API responses
67
- *
68
- * @returns Error response object
69
- */
70
- toJSON() {
71
- return {
72
- error: this.name,
73
- message: this.message,
74
- statusCode: this.statusCode,
75
- code: this.code,
76
- };
77
- }
78
- }
79
- /**
80
- * Validation error for invalid user input
81
- *
82
- * Used when request data fails validation (e.g., missing required fields,
83
- * invalid format, type mismatches, etc.)
84
- *
85
- * @example
86
- * ```typescript
87
- * throw new ValidationError('Invalid email format', {
88
- * email: 'Must be a valid email address'
89
- * });
90
- * ```
91
- *
92
- * @example
93
- * ```typescript
94
- * throw new ValidationError('Missing required fields', {
95
- * name: 'Name is required',
96
- * email: 'Email is required'
97
- * });
98
- * ```
99
- */
100
- export class ValidationError extends VeloxError {
101
- /**
102
- * Field-specific validation errors
103
- * Maps field names to error messages
104
- */
105
- fields;
106
- /**
107
- * Creates a new ValidationError instance
108
- *
109
- * @param message - General validation error message
110
- * @param fields - Optional object mapping field names to specific error messages
111
- */
112
- constructor(message, fields) {
113
- super(message, 400, 'VALIDATION_ERROR');
114
- this.name = 'ValidationError';
115
- this.fields = fields;
116
- if (Error.captureStackTrace) {
117
- Error.captureStackTrace(this, ValidationError);
118
- }
119
- }
120
- /**
121
- * Converts validation error to JSON format with field details
122
- *
123
- * @returns Validation error response with field-specific errors
124
- */
125
- toJSON() {
126
- return {
127
- error: 'ValidationError',
128
- message: this.message,
129
- statusCode: 400,
130
- code: 'VALIDATION_ERROR',
131
- fields: this.fields,
132
- };
133
- }
134
- }
135
- /**
136
- * Configuration error for framework setup issues
137
- *
138
- * Used when the framework is misconfigured or used incorrectly.
139
- * These errors indicate developer mistakes rather than runtime issues.
140
- *
141
- * @example
142
- * ```typescript
143
- * throw new ConfigurationError('VeloxApp must be started before registering routes');
144
- * ```
145
- *
146
- * @example
147
- * ```typescript
148
- * throw new ConfigurationError('Missing required plugin: @veloxts/orm');
149
- * ```
150
- */
151
- export class ConfigurationError extends VeloxError {
152
- /**
153
- * Creates a new ConfigurationError instance
154
- *
155
- * @param message - Human-readable error message explaining the configuration issue
156
- */
157
- constructor(message) {
158
- super(message, 500, 'CONFIGURATION_ERROR');
159
- this.name = 'ConfigurationError';
160
- if (Error.captureStackTrace) {
161
- Error.captureStackTrace(this, ConfigurationError);
162
- }
163
- }
164
- }
165
- /**
166
- * Not found error for missing resources
167
- *
168
- * Used when a requested resource (user, post, file, etc.) doesn't exist
169
- *
170
- * @example
171
- * ```typescript
172
- * throw new NotFoundError('User');
173
- * // Message: "User not found"
174
- * ```
175
- *
176
- * @example
177
- * ```typescript
178
- * throw new NotFoundError('Post', '123');
179
- * // Message: "Post with id 123 not found"
180
- * ```
181
- */
182
- export class NotFoundError extends VeloxError {
183
- /**
184
- * Type of resource that was not found
185
- */
186
- resource;
187
- /**
188
- * Optional identifier of the resource that was not found
189
- */
190
- resourceId;
191
- /**
192
- * Creates a new NotFoundError instance
193
- *
194
- * @param resource - Type of resource (e.g., "User", "Post", "File")
195
- * @param resourceId - Optional identifier of the specific resource
196
- */
197
- constructor(resource, resourceId) {
198
- const message = resourceId
199
- ? `${resource} with id ${resourceId} not found`
200
- : `${resource} not found`;
201
- super(message, 404, 'NOT_FOUND');
202
- this.name = 'NotFoundError';
203
- this.resource = resource;
204
- this.resourceId = resourceId;
205
- if (Error.captureStackTrace) {
206
- Error.captureStackTrace(this, NotFoundError);
207
- }
208
- }
209
- /**
210
- * Converts not found error to JSON format with resource details
211
- *
212
- * @returns Not found error response with resource information
213
- */
214
- toJSON() {
215
- return {
216
- error: 'NotFoundError',
217
- message: this.message,
218
- statusCode: 404,
219
- code: 'NOT_FOUND',
220
- resource: this.resource,
221
- resourceId: this.resourceId,
222
- };
223
- }
224
- }
225
- // ============================================================================
226
- // Type Guards
227
- // ============================================================================
228
- /**
229
- * Type guard to check if an error is a VeloxError
230
- *
231
- * @param error - Error to check
232
- * @returns true if error is a VeloxError instance
233
- *
234
- * @example
235
- * ```typescript
236
- * try {
237
- * // ... some code
238
- * } catch (error) {
239
- * if (isVeloxError(error)) {
240
- * console.log('Status code:', error.statusCode);
241
- * }
242
- * }
243
- * ```
244
- */
245
- export function isVeloxError(error) {
246
- return error != null && error instanceof VeloxError;
247
- }
248
- /**
249
- * Type guard to check if an error is a ValidationError
250
- *
251
- * @param error - Error to check
252
- * @returns true if error is a ValidationError instance
253
- */
254
- export function isValidationError(error) {
255
- return error instanceof ValidationError;
256
- }
257
- /**
258
- * Type guard to check if an error is a NotFoundError
259
- *
260
- * @param error - Error to check
261
- * @returns true if error is a NotFoundError instance
262
- */
263
- export function isNotFoundError(error) {
264
- return error instanceof NotFoundError;
265
- }
266
- /**
267
- * Type guard to check if an error is a ConfigurationError
268
- *
269
- * @param error - Error to check
270
- * @returns true if error is a ConfigurationError instance
271
- */
272
- export function isConfigurationError(error) {
273
- return error instanceof ConfigurationError;
274
- }
275
- // ============================================================================
276
- // Utility Types
277
- // ============================================================================
278
- /**
279
- * Helper to ensure exhaustive handling of error types
280
- * Throws at compile time if a case is not handled
281
- *
282
- * @example
283
- * ```typescript
284
- * function handleError(error: VeloxError): string {
285
- * if (isValidationError(error)) return 'validation';
286
- * if (isNotFoundError(error)) return 'not found';
287
- * return 'generic';
288
- * }
289
- * ```
290
- */
291
- export function assertNever(value) {
292
- throw new Error(`Unhandled value: ${JSON.stringify(value)}`);
293
- }
294
- //# sourceMappingURL=errors.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA8GH;;GAEG;AACH,MAAM,UAAU,yBAAyB,CACvC,QAAuB;IAEvB,OAAO,QAAQ,CAAC,KAAK,KAAK,iBAAiB,CAAC;AAC9C,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB,CACrC,QAAuB;IAEvB,OAAO,QAAQ,CAAC,KAAK,KAAK,eAAe,CAAC;AAC5C,CAAC;AAED,+EAA+E;AAC/E,gBAAgB;AAChB,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,UAA0C,SAAQ,KAAK;IAClE;;OAEG;IACa,UAAU,CAAS;IAEnC;;OAEG;IACa,IAAI,CAAS;IAE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AA+E;AAC/E,cAAc;AACd,+EAA+E;AAE/E;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,KAAK,IAAI,IAAI,IAAI,KAAK,YAAY,UAAU,CAAC;AACtD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,OAAO,KAAK,YAAY,eAAe,CAAC;AAC1C,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,OAAO,KAAK,YAAY,aAAa,CAAC;AACxC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAc;IACjD,OAAO,KAAK,YAAY,kBAAkB,CAAC;AAC7C,CAAC;AAED,+EAA+E;AAC/E,gBAAgB;AAChB,+EAA+E;AAE/E;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW,CAAC,KAAY;IACtC,MAAM,IAAI,KAAK,CAAC,oBAAoB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;AAC/D,CAAC"}
package/dist/index.d.ts DELETED
@@ -1,28 +0,0 @@
1
- /**
2
- * @veloxts/core - Foundation package for the VeloxTS framework
3
- *
4
- * Provides the core Fastify wrapper, plugin system, and base context
5
- * that all other framework packages build upon.
6
- *
7
- * @example
8
- * ```typescript
9
- * import { createVeloxApp, definePlugin } from '@veloxts/core';
10
- *
11
- * const app = await createVeloxApp({ port: 3000 });
12
- * await app.start();
13
- * ```
14
- *
15
- * @module @veloxts/core
16
- */
17
- export declare const VELOX_VERSION: "0.1.0";
18
- export { createVeloxApp, VeloxApp } from './app.js';
19
- export type { BaseContext } from './context.js';
20
- export { createContext, isContext } from './context.js';
21
- export type { ErrorResponse, GenericErrorResponse, NotFoundErrorResponse, ValidationErrorResponse, VeloxCoreErrorCode, VeloxErrorCode, VeloxErrorCodeRegistry, } from './errors.js';
22
- export { assertNever, ConfigurationError, isConfigurationError, isNotFoundError, isNotFoundErrorResponse, isValidationError, isValidationErrorResponse, isVeloxError, NotFoundError, ValidationError, VeloxError, } from './errors.js';
23
- export type { InferPluginOptions, PluginMetadata, PluginOptions, VeloxPlugin } from './plugin.js';
24
- export { definePlugin, isVeloxPlugin, validatePluginMetadata } from './plugin.js';
25
- export type { AsyncHandler, JsonArray, JsonObject, JsonPrimitive, JsonValue, LifecycleHook, ShutdownHandler, SyncHandler, } from './types.js';
26
- export type { FrozenVeloxAppConfig, ValidHost, ValidPort, VeloxAppConfig, VeloxFastifyOptions, } from './utils/config.js';
27
- export { isValidHost, isValidPort } from './utils/config.js';
28
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,eAAO,MAAM,aAAa,EAAG,OAAgB,CAAC;AAG9C,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpD,YAAY,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAExD,YAAY,EACV,aAAa,EACb,oBAAoB,EACpB,qBAAqB,EACrB,uBAAuB,EACvB,kBAAkB,EAClB,cAAc,EACd,sBAAsB,GACvB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,oBAAoB,EACpB,eAAe,EACf,uBAAuB,EACvB,iBAAiB,EACjB,yBAAyB,EACzB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,UAAU,GACX,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,kBAAkB,EAAE,cAAc,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAElG,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAElF,YAAY,EACV,YAAY,EACZ,SAAS,EACT,UAAU,EACV,aAAa,EACb,SAAS,EACT,aAAa,EACb,eAAe,EACf,WAAW,GACZ,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,oBAAoB,EACpB,SAAS,EACT,SAAS,EACT,cAAc,EACd,mBAAmB,GACpB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js DELETED
@@ -1,26 +0,0 @@
1
- /**
2
- * @veloxts/core - Foundation package for the VeloxTS framework
3
- *
4
- * Provides the core Fastify wrapper, plugin system, and base context
5
- * that all other framework packages build upon.
6
- *
7
- * @example
8
- * ```typescript
9
- * import { createVeloxApp, definePlugin } from '@veloxts/core';
10
- *
11
- * const app = await createVeloxApp({ port: 3000 });
12
- * await app.start();
13
- * ```
14
- *
15
- * @module @veloxts/core
16
- */
17
- // Version constant
18
- export const VELOX_VERSION = '0.1.0';
19
- // App creation and types
20
- export { createVeloxApp, VeloxApp } from './app.js';
21
- export { createContext, isContext } from './context.js';
22
- export { assertNever, ConfigurationError, isConfigurationError, isNotFoundError, isNotFoundErrorResponse, isValidationError, isValidationErrorResponse, isVeloxError, NotFoundError, ValidationError, VeloxError, } from './errors.js';
23
- // Plugin system
24
- export { definePlugin, isVeloxPlugin, validatePluginMetadata } from './plugin.js';
25
- export { isValidHost, isValidPort } from './utils/config.js';
26
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,mBAAmB;AACnB,MAAM,CAAC,MAAM,aAAa,GAAG,OAAgB,CAAC;AAE9C,yBAAyB;AACzB,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAGpD,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAWxD,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,oBAAoB,EACpB,eAAe,EACf,uBAAuB,EACvB,iBAAiB,EACjB,yBAAyB,EACzB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,UAAU,GACX,MAAM,aAAa,CAAC;AAErB,gBAAgB;AAChB,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAoBlF,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/plugin.d.ts DELETED
@@ -1,182 +0,0 @@
1
- /**
2
- * Plugin system for VeloxTS framework
3
- * Wraps Fastify's plugin architecture with metadata and validation
4
- * @module plugin
5
- */
6
- import type { FastifyPluginAsync, FastifyPluginOptions } from 'fastify';
7
- /**
8
- * Plugin options type
9
- * Generic record allowing plugins to define custom options
10
- */
11
- export type PluginOptions = FastifyPluginOptions;
12
- /**
13
- * Plugin metadata interface
14
- *
15
- * Provides information about the plugin for registration and dependency resolution
16
- */
17
- export interface PluginMetadata {
18
- /**
19
- * Unique plugin name
20
- * Used for identification and dependency resolution
21
- */
22
- name: string;
23
- /**
24
- * Plugin version (semantic versioning recommended)
25
- */
26
- version: string;
27
- /**
28
- * Optional list of plugin names this plugin depends on
29
- *
30
- * Fastify handles the ordering - plugins listed here should be
31
- * registered before this plugin
32
- *
33
- * @example
34
- * ```typescript
35
- * dependencies: ['@veloxts/orm', '@veloxts/validation']
36
- * ```
37
- */
38
- dependencies?: string[];
39
- }
40
- /**
41
- * VeloxTS plugin definition
42
- *
43
- * Wraps a Fastify plugin with metadata for better developer experience
44
- *
45
- * @template Options - Type of options the plugin accepts
46
- *
47
- * @example
48
- * ```typescript
49
- * interface MyPluginOptions {
50
- * apiKey: string;
51
- * timeout?: number;
52
- * }
53
- *
54
- * const myPlugin: VeloxPlugin<MyPluginOptions> = {
55
- * name: 'my-plugin',
56
- * version: '1.0.0',
57
- * async register(server, options) {
58
- * // Plugin implementation
59
- * server.get('/api', async () => {
60
- * return { message: 'Hello from plugin' };
61
- * });
62
- * }
63
- * };
64
- * ```
65
- */
66
- export interface VeloxPlugin<Options extends PluginOptions = PluginOptions> extends PluginMetadata {
67
- /**
68
- * Plugin registration function
69
- *
70
- * Called when the plugin is registered with the VeloxTS app.
71
- * This is a standard Fastify plugin function.
72
- *
73
- * @param server - Fastify server instance
74
- * @param options - Plugin options provided during registration
75
- */
76
- register: FastifyPluginAsync<Options>;
77
- }
78
- /**
79
- * Defines a VeloxTS plugin with metadata and type-safe options
80
- *
81
- * This is a helper function that provides better TypeScript inference
82
- * and validates plugin metadata at definition time.
83
- *
84
- * @template Options - Type of options the plugin accepts
85
- * @param plugin - Plugin definition with metadata and register function
86
- * @returns The same plugin object with proper types
87
- *
88
- * @throws {VeloxError} If plugin metadata is invalid
89
- *
90
- * @example
91
- * ```typescript
92
- * export const databasePlugin = definePlugin({
93
- * name: '@myapp/database',
94
- * version: '1.0.0',
95
- * dependencies: ['@veloxts/orm'],
96
- * async register(server, options) {
97
- * const db = await createDatabase(options.connectionString);
98
- *
99
- * // Decorate server with database client
100
- * server.decorate('db', db);
101
- *
102
- * // Add shutdown hook
103
- * server.addHook('onClose', async () => {
104
- * await db.disconnect();
105
- * });
106
- * }
107
- * });
108
- * ```
109
- *
110
- * @example
111
- * ```typescript
112
- * // Plugin with typed options
113
- * interface AuthOptions {
114
- * secret: string;
115
- * expiresIn?: string;
116
- * }
117
- *
118
- * export const authPlugin = definePlugin<AuthOptions>({
119
- * name: '@myapp/auth',
120
- * version: '1.0.0',
121
- * async register(server, options) {
122
- * // options is properly typed as AuthOptions
123
- * const { secret, expiresIn = '1h' } = options;
124
- * // ... implementation
125
- * }
126
- * });
127
- * ```
128
- */
129
- export declare function definePlugin<Options extends PluginOptions = PluginOptions>(plugin: VeloxPlugin<Options>): VeloxPlugin<Options>;
130
- /**
131
- * Validates plugin metadata
132
- *
133
- * @param plugin - Plugin to validate
134
- * @throws {VeloxError} If metadata is invalid
135
- *
136
- * @internal
137
- */
138
- export declare function validatePluginMetadata(plugin: Partial<PluginMetadata> & {
139
- register?: unknown;
140
- }): void;
141
- /**
142
- * Type guard to check if a value is a valid VeloxPlugin
143
- *
144
- * Uses the `in` operator for safe property access without type assertions.
145
- *
146
- * @param value - Value to check
147
- * @returns true if value is a valid VeloxPlugin
148
- *
149
- * @example
150
- * ```typescript
151
- * if (isVeloxPlugin(someValue)) {
152
- * console.log('Plugin name:', someValue.name);
153
- * await app.register(someValue);
154
- * }
155
- * ```
156
- */
157
- export declare function isVeloxPlugin(value: unknown): value is VeloxPlugin;
158
- /**
159
- * Infers the options type from a VeloxPlugin
160
- *
161
- * Useful for extracting the options type when you have a plugin instance
162
- * and need to reference its options type elsewhere.
163
- *
164
- * @template T - Plugin type to extract options from
165
- *
166
- * @example
167
- * ```typescript
168
- * const myPlugin = definePlugin<{ apiKey: string }>({
169
- * name: 'my-plugin',
170
- * version: '1.0.0',
171
- * async register(server, options) {
172
- * // options is { apiKey: string }
173
- * }
174
- * });
175
- *
176
- * // Extract the options type
177
- * type MyPluginOptions = InferPluginOptions<typeof myPlugin>;
178
- * // MyPluginOptions = { apiKey: string }
179
- * ```
180
- */
181
- export type InferPluginOptions<T> = T extends VeloxPlugin<infer O> ? O : never;
182
- //# sourceMappingURL=plugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAIxE;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,oBAAoB,CAAC;AAEjD;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;;;;;;;OAUG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,WAAW,WAAW,CAAC,OAAO,SAAS,aAAa,GAAG,aAAa,CAAE,SAAQ,cAAc;IAChG;;;;;;;;OAQG;IACH,QAAQ,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;CACvC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,wBAAgB,YAAY,CAAC,OAAO,SAAS,aAAa,GAAG,aAAa,EACxE,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,GAC3B,WAAW,CAAC,OAAO,CAAC,CAKtB;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,OAAO,CAAC,cAAc,CAAC,GAAG;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,GACvD,IAAI,CAyCN;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAclE;AAMD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,kBAAkB,CAAC,CAAC,IAAI,CAAC,SAAS,WAAW,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC"}
package/dist/plugin.js DELETED
@@ -1,121 +0,0 @@
1
- /**
2
- * Plugin system for VeloxTS framework
3
- * Wraps Fastify's plugin architecture with metadata and validation
4
- * @module plugin
5
- */
6
- import { VeloxError } from './errors.js';
7
- /**
8
- * Defines a VeloxTS plugin with metadata and type-safe options
9
- *
10
- * This is a helper function that provides better TypeScript inference
11
- * and validates plugin metadata at definition time.
12
- *
13
- * @template Options - Type of options the plugin accepts
14
- * @param plugin - Plugin definition with metadata and register function
15
- * @returns The same plugin object with proper types
16
- *
17
- * @throws {VeloxError} If plugin metadata is invalid
18
- *
19
- * @example
20
- * ```typescript
21
- * export const databasePlugin = definePlugin({
22
- * name: '@myapp/database',
23
- * version: '1.0.0',
24
- * dependencies: ['@veloxts/orm'],
25
- * async register(server, options) {
26
- * const db = await createDatabase(options.connectionString);
27
- *
28
- * // Decorate server with database client
29
- * server.decorate('db', db);
30
- *
31
- * // Add shutdown hook
32
- * server.addHook('onClose', async () => {
33
- * await db.disconnect();
34
- * });
35
- * }
36
- * });
37
- * ```
38
- *
39
- * @example
40
- * ```typescript
41
- * // Plugin with typed options
42
- * interface AuthOptions {
43
- * secret: string;
44
- * expiresIn?: string;
45
- * }
46
- *
47
- * export const authPlugin = definePlugin<AuthOptions>({
48
- * name: '@myapp/auth',
49
- * version: '1.0.0',
50
- * async register(server, options) {
51
- * // options is properly typed as AuthOptions
52
- * const { secret, expiresIn = '1h' } = options;
53
- * // ... implementation
54
- * }
55
- * });
56
- * ```
57
- */
58
- export function definePlugin(plugin) {
59
- // Validate plugin metadata
60
- validatePluginMetadata(plugin);
61
- return plugin;
62
- }
63
- /**
64
- * Validates plugin metadata
65
- *
66
- * @param plugin - Plugin to validate
67
- * @throws {VeloxError} If metadata is invalid
68
- *
69
- * @internal
70
- */
71
- export function validatePluginMetadata(plugin) {
72
- if (!plugin.name || typeof plugin.name !== 'string' || plugin.name.trim() === '') {
73
- throw new VeloxError('Plugin must have a non-empty name', 500, 'INVALID_PLUGIN_METADATA');
74
- }
75
- if (!plugin.version || typeof plugin.version !== 'string' || plugin.version.trim() === '') {
76
- throw new VeloxError(`Plugin "${plugin.name}" must have a version`, 500, 'INVALID_PLUGIN_METADATA');
77
- }
78
- if (!plugin.register || typeof plugin.register !== 'function') {
79
- throw new VeloxError(`Plugin "${plugin.name}" must have a register function`, 500, 'INVALID_PLUGIN_METADATA');
80
- }
81
- // Validate dependencies array if provided
82
- if (plugin.dependencies !== undefined) {
83
- if (!Array.isArray(plugin.dependencies)) {
84
- throw new VeloxError(`Plugin "${plugin.name}" dependencies must be an array`, 500, 'INVALID_PLUGIN_METADATA');
85
- }
86
- for (const dep of plugin.dependencies) {
87
- if (typeof dep !== 'string' || dep.trim() === '') {
88
- throw new VeloxError(`Plugin "${plugin.name}" has invalid dependency: dependencies must be non-empty strings`, 500, 'INVALID_PLUGIN_METADATA');
89
- }
90
- }
91
- }
92
- }
93
- /**
94
- * Type guard to check if a value is a valid VeloxPlugin
95
- *
96
- * Uses the `in` operator for safe property access without type assertions.
97
- *
98
- * @param value - Value to check
99
- * @returns true if value is a valid VeloxPlugin
100
- *
101
- * @example
102
- * ```typescript
103
- * if (isVeloxPlugin(someValue)) {
104
- * console.log('Plugin name:', someValue.name);
105
- * await app.register(someValue);
106
- * }
107
- * ```
108
- */
109
- export function isVeloxPlugin(value) {
110
- if (typeof value !== 'object' || value === null) {
111
- return false;
112
- }
113
- // Use 'in' operator for safe property access without type assertions
114
- return ('name' in value &&
115
- 'version' in value &&
116
- 'register' in value &&
117
- typeof value.name === 'string' &&
118
- typeof value.version === 'string' &&
119
- typeof value.register === 'function');
120
- }
121
- //# sourceMappingURL=plugin.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AA8EzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,MAAM,UAAU,YAAY,CAC1B,MAA4B;IAE5B,2BAA2B;IAC3B,sBAAsB,CAAC,MAAM,CAAC,CAAC;IAE/B,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CACpC,MAAwD;IAExD,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACjF,MAAM,IAAI,UAAU,CAAC,mCAAmC,EAAE,GAAG,EAAE,yBAAyB,CAA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