express-model-binding 1.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.
Files changed (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +208 -0
  3. package/dist/BaseAdapter-BjvLQijd.d.mts +214 -0
  4. package/dist/BaseAdapter-BjvLQijd.d.ts +214 -0
  5. package/dist/adapters/KnexAdapter.d.mts +44 -0
  6. package/dist/adapters/KnexAdapter.d.ts +44 -0
  7. package/dist/adapters/KnexAdapter.js +257 -0
  8. package/dist/adapters/KnexAdapter.js.map +1 -0
  9. package/dist/adapters/KnexAdapter.mjs +229 -0
  10. package/dist/adapters/KnexAdapter.mjs.map +1 -0
  11. package/dist/adapters/MongooseAdapter.d.mts +30 -0
  12. package/dist/adapters/MongooseAdapter.d.ts +30 -0
  13. package/dist/adapters/MongooseAdapter.js +245 -0
  14. package/dist/adapters/MongooseAdapter.js.map +1 -0
  15. package/dist/adapters/MongooseAdapter.mjs +225 -0
  16. package/dist/adapters/MongooseAdapter.mjs.map +1 -0
  17. package/dist/adapters/PrismaAdapter.d.mts +42 -0
  18. package/dist/adapters/PrismaAdapter.d.ts +42 -0
  19. package/dist/adapters/PrismaAdapter.js +247 -0
  20. package/dist/adapters/PrismaAdapter.js.map +1 -0
  21. package/dist/adapters/PrismaAdapter.mjs +220 -0
  22. package/dist/adapters/PrismaAdapter.mjs.map +1 -0
  23. package/dist/adapters/SequelizeAdapter.d.mts +27 -0
  24. package/dist/adapters/SequelizeAdapter.d.ts +27 -0
  25. package/dist/adapters/SequelizeAdapter.js +280 -0
  26. package/dist/adapters/SequelizeAdapter.js.map +1 -0
  27. package/dist/adapters/SequelizeAdapter.mjs +260 -0
  28. package/dist/adapters/SequelizeAdapter.mjs.map +1 -0
  29. package/dist/adapters/TypeORMAdapter.d.mts +26 -0
  30. package/dist/adapters/TypeORMAdapter.d.ts +26 -0
  31. package/dist/adapters/TypeORMAdapter.js +294 -0
  32. package/dist/adapters/TypeORMAdapter.js.map +1 -0
  33. package/dist/adapters/TypeORMAdapter.mjs +267 -0
  34. package/dist/adapters/TypeORMAdapter.mjs.map +1 -0
  35. package/dist/index.d.mts +411 -0
  36. package/dist/index.d.ts +411 -0
  37. package/dist/index.js +1514 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/index.mjs +1450 -0
  40. package/dist/index.mjs.map +1 -0
  41. package/package.json +148 -0
@@ -0,0 +1,411 @@
1
+ import { Request, Response, RequestHandler } from 'express';
2
+ import { I as IORMAdapter, B as BindOptions, a as BindingResult, M as ModelBindingsConfig } from './BaseAdapter-BjvLQijd.js';
3
+ export { b as BaseAdapter, c as BindingContext, C as CacheEntry, E as ExtractModelType, g as MiddlewareFunction, e as ModelBindingGlobalConfig, d as ModelMetadata, Q as QueryOptions, T as TypedRequest, f as TypedRequestHandler } from './BaseAdapter-BjvLQijd.js';
4
+ export { KnexAdapter, KnexModel, defineKnexModel } from './adapters/KnexAdapter.js';
5
+ export { MongooseAdapter } from './adapters/MongooseAdapter.js';
6
+ export { TypeORMAdapter } from './adapters/TypeORMAdapter.js';
7
+ export { SequelizeAdapter } from './adapters/SequelizeAdapter.js';
8
+ export { PrismaAdapter } from './adapters/PrismaAdapter.js';
9
+ import 'knex';
10
+ import 'mongoose';
11
+ import 'typeorm';
12
+ import 'sequelize';
13
+
14
+ /**
15
+ * Central model binding orchestrator
16
+ */
17
+ declare class ModelBinder {
18
+ private static adapter;
19
+ private static cache;
20
+ private static debug;
21
+ static setAdapter(adapter: IORMAdapter): void;
22
+ static getAdapter(): IORMAdapter;
23
+ static hasAdapter(): boolean;
24
+ static clearAdapter(): void;
25
+ static setDebug(enabled: boolean): void;
26
+ static isDebugEnabled(): boolean;
27
+ static clearCache(): void;
28
+ static getCacheStats(): {
29
+ size: number;
30
+ maxSize: number;
31
+ };
32
+ static bind(req: Request, res: Response, paramName: string, model: unknown, options?: BindOptions): Promise<BindingResult>;
33
+ private static attachToRequest;
34
+ private static getCacheKey;
35
+ private static getModelName;
36
+ static reset(): void;
37
+ }
38
+
39
+ /**
40
+ * Create middleware that binds a model to a route parameter
41
+ *
42
+ * @param paramName - The route parameter name (without ':')
43
+ * @param model - The model class/schema/table
44
+ * @param options - Binding options
45
+ * @returns Express middleware function
46
+ *
47
+ * @example
48
+ * app.get('/users/:user', bindModel('user', User), (req, res) => {
49
+ * res.json(req.user);
50
+ * });
51
+ */
52
+ declare function bindModel(paramName: string, model: unknown, options?: BindOptions): RequestHandler;
53
+ /**
54
+ * Bind multiple models in a single middleware
55
+ *
56
+ * @param bindings - Object mapping parameter names to model configs
57
+ * @returns Express middleware function
58
+ *
59
+ * @example
60
+ * app.get('/users/:user/posts/:post',
61
+ * bindModels({
62
+ * user: { model: User },
63
+ * post: { model: Post, options: { include: ['comments'] } }
64
+ * }),
65
+ * (req, res) => {
66
+ * res.json({ user: req.user, post: req.post });
67
+ * }
68
+ * );
69
+ */
70
+ declare function bindModels(bindings: ModelBindingsConfig): RequestHandler;
71
+ /**
72
+ * Bind a model optionally (don't throw 404 if not found)
73
+ *
74
+ * @param paramName - The route parameter name
75
+ * @param model - The model class/schema/table
76
+ * @param options - Binding options
77
+ * @returns Express middleware function
78
+ *
79
+ * @example
80
+ * app.get('/posts/:post?', bindOptional('post', Post), (req, res) => {
81
+ * if (req.post) {
82
+ * res.json(req.post);
83
+ * } else {
84
+ * res.json({ message: 'No specific post' });
85
+ * }
86
+ * });
87
+ */
88
+ declare function bindOptional(paramName: string, model: unknown, options?: BindOptions): RequestHandler;
89
+ /**
90
+ * Create a middleware that binds a model using a custom key
91
+ *
92
+ * @param paramName - The route parameter name
93
+ * @param model - The model class/schema/table
94
+ * @param key - The field to search by
95
+ * @param options - Additional binding options
96
+ * @returns Express middleware function
97
+ *
98
+ * @example
99
+ * app.get('/users/by-email/:email', bindByKey('email', User, 'email'), (req, res) => {
100
+ * res.json(req.email);
101
+ * });
102
+ */
103
+ declare function bindByKey(paramName: string, model: unknown, key: string, options?: BindOptions): RequestHandler;
104
+ /**
105
+ * Create a middleware that binds a model and attaches it with a custom name
106
+ *
107
+ * @param paramName - The route parameter name
108
+ * @param model - The model class/schema/table
109
+ * @param attachAs - The name to attach the model as on the request
110
+ * @param options - Additional binding options
111
+ * @returns Express middleware function
112
+ *
113
+ * @example
114
+ * app.get('/users/:id', bindAs('id', User, 'currentUser'), (req, res) => {
115
+ * res.json(req.currentUser);
116
+ * });
117
+ */
118
+ declare function bindAs(paramName: string, model: unknown, attachAs: string, options?: BindOptions): RequestHandler;
119
+ /**
120
+ * Create a middleware that binds a model with caching
121
+ *
122
+ * @param paramName - The route parameter name
123
+ * @param model - The model class/schema/table
124
+ * @param ttl - Cache TTL in milliseconds (default: 60000)
125
+ * @param options - Additional binding options
126
+ * @returns Express middleware function
127
+ *
128
+ * @example
129
+ * app.get('/users/:user', bindCached('user', User, 300000), (req, res) => {
130
+ * res.json(req.user);
131
+ * });
132
+ */
133
+ declare function bindCached(paramName: string, model: unknown, ttl?: number, options?: BindOptions): RequestHandler;
134
+ /**
135
+ * Create a middleware that binds a model with eager-loaded relations
136
+ *
137
+ * @param paramName - The route parameter name
138
+ * @param model - The model class/schema/table
139
+ * @param relations - Relations to eager load
140
+ * @param options - Additional binding options
141
+ * @returns Express middleware function
142
+ *
143
+ * @example
144
+ * app.get('/users/:user', bindWithRelations('user', User, ['posts', 'profile']), (req, res) => {
145
+ * res.json(req.user);
146
+ * });
147
+ */
148
+ declare function bindWithRelations(paramName: string, model: unknown, relations: string[] | Record<string, unknown>, options?: BindOptions): RequestHandler;
149
+
150
+ /**
151
+ * Base error class for model binding errors
152
+ */
153
+ declare class BindingError extends Error {
154
+ readonly originalError?: Error;
155
+ constructor(message: string, originalError?: Error);
156
+ toJSON(): Record<string, unknown>;
157
+ }
158
+ /**
159
+ * Error thrown when a model is not found (404)
160
+ */
161
+ declare class ModelNotFoundError extends Error {
162
+ readonly statusCode = 404;
163
+ readonly paramName: string;
164
+ readonly paramValue: string;
165
+ readonly modelName: string;
166
+ constructor(paramName: string, paramValue: string, modelName: string, customMessage?: string);
167
+ toJSON(): Record<string, unknown>;
168
+ }
169
+ /**
170
+ * Error thrown when no adapter is configured
171
+ */
172
+ declare class AdapterNotSetError extends Error {
173
+ constructor(message?: string);
174
+ toJSON(): Record<string, unknown>;
175
+ }
176
+ /**
177
+ * Error thrown when a model is invalid for an adapter
178
+ */
179
+ declare class InvalidModelError extends Error {
180
+ readonly model: unknown;
181
+ constructor(message: string, model: unknown);
182
+ toJSON(): Record<string, unknown>;
183
+ }
184
+ /**
185
+ * Error thrown when validation fails
186
+ */
187
+ declare class ValidationError extends Error {
188
+ readonly statusCode: number;
189
+ readonly details?: unknown;
190
+ constructor(message: string, statusCode?: number, details?: unknown);
191
+ toJSON(): Record<string, unknown>;
192
+ }
193
+
194
+ /**
195
+ * Simple in-memory cache for model binding
196
+ */
197
+ declare class Cache {
198
+ private store;
199
+ private maxSize;
200
+ constructor(maxSize?: number);
201
+ /**
202
+ * Get a value from cache
203
+ */
204
+ get<T = unknown>(key: string): T | null;
205
+ /**
206
+ * Set a value in cache
207
+ */
208
+ set<T = unknown>(key: string, value: T, ttl: number): void;
209
+ /**
210
+ * Delete a value from cache
211
+ */
212
+ delete(key: string): boolean;
213
+ /**
214
+ * Clear all cached values
215
+ */
216
+ clear(): void;
217
+ /**
218
+ * Get cache size
219
+ */
220
+ get size(): number;
221
+ /**
222
+ * Check if key exists and is not expired
223
+ */
224
+ has(key: string): boolean;
225
+ /**
226
+ * Get all keys in the cache
227
+ */
228
+ keys(): string[];
229
+ /**
230
+ * Remove expired entries
231
+ */
232
+ prune(): number;
233
+ /**
234
+ * Get cache statistics
235
+ */
236
+ getStats(): {
237
+ size: number;
238
+ maxSize: number;
239
+ };
240
+ }
241
+
242
+ /**
243
+ * Log levels for the logger
244
+ */
245
+ type LogLevel = 'debug' | 'info' | 'warn' | 'error';
246
+ /**
247
+ * Logger configuration
248
+ */
249
+ interface LoggerConfig {
250
+ enabled: boolean;
251
+ level: LogLevel;
252
+ prefix: string;
253
+ }
254
+ /**
255
+ * Simple logger utility for debugging
256
+ */
257
+ declare class Logger {
258
+ private config;
259
+ private levelPriority;
260
+ /**
261
+ * Enable debug logging
262
+ */
263
+ enable(): void;
264
+ /**
265
+ * Disable debug logging
266
+ */
267
+ disable(): void;
268
+ /**
269
+ * Check if logging is enabled
270
+ */
271
+ isEnabled(): boolean;
272
+ /**
273
+ * Set log level
274
+ */
275
+ setLevel(level: LogLevel): void;
276
+ /**
277
+ * Get current log level
278
+ */
279
+ getLevel(): LogLevel;
280
+ /**
281
+ * Set custom prefix
282
+ */
283
+ setPrefix(prefix: string): void;
284
+ /**
285
+ * Check if a message at the given level should be logged
286
+ */
287
+ private shouldLog;
288
+ /**
289
+ * Format log message
290
+ */
291
+ private formatMessage;
292
+ /**
293
+ * Log a debug message
294
+ */
295
+ debug(message: string, context?: unknown): void;
296
+ /**
297
+ * Log an info message
298
+ */
299
+ info(message: string, context?: unknown): void;
300
+ /**
301
+ * Log a warning message
302
+ */
303
+ warn(message: string, context?: unknown): void;
304
+ /**
305
+ * Log an error message
306
+ */
307
+ error(message: string, error?: unknown): void;
308
+ /**
309
+ * Reset logger to default configuration
310
+ */
311
+ reset(): void;
312
+ /**
313
+ * Get current configuration
314
+ */
315
+ getConfig(): LoggerConfig;
316
+ }
317
+ declare const logger: Logger;
318
+
319
+ /**
320
+ * Check if a value is a valid UUID
321
+ */
322
+ declare function isUUID(value: string): boolean;
323
+ /**
324
+ * Check if a value is a valid MongoDB ObjectId
325
+ */
326
+ declare function isObjectId(value: string): boolean;
327
+ /**
328
+ * Check if a value is a numeric string
329
+ */
330
+ declare function isNumeric(value: string): boolean;
331
+ /**
332
+ * Check if a value is a valid positive integer
333
+ */
334
+ declare function isPositiveInteger(value: string): boolean;
335
+ /**
336
+ * Check if a value is a valid slug (lowercase alphanumeric with hyphens)
337
+ */
338
+ declare function isSlug(value: string): boolean;
339
+ /**
340
+ * Check if a value is a valid email
341
+ */
342
+ declare function isEmail(value: string): boolean;
343
+ /**
344
+ * Validate that a value is not empty
345
+ */
346
+ declare function isNotEmpty(value: unknown): boolean;
347
+ /**
348
+ * Validate that a value is a non-empty string
349
+ */
350
+ declare function isNonEmptyString(value: unknown): value is string;
351
+ /**
352
+ * Validate that a value is a plain object
353
+ */
354
+ declare function isPlainObject(value: unknown): value is Record<string, unknown>;
355
+ /**
356
+ * Validate that a value is a function
357
+ */
358
+ declare function isFunction(value: unknown): value is Function;
359
+ /**
360
+ * Validate route parameter name
361
+ */
362
+ declare function isValidParamName(value: string): boolean;
363
+
364
+ /**
365
+ * Transform a string value to a number if it's numeric
366
+ */
367
+ declare function toNumber(value: string): number | string;
368
+ /**
369
+ * Transform a string value to a float if it's a valid decimal
370
+ */
371
+ declare function toFloat(value: string): number | string;
372
+ /**
373
+ * Transform a string value to a boolean
374
+ */
375
+ declare function toBoolean(value: string): boolean;
376
+ /**
377
+ * Transform a string to lowercase
378
+ */
379
+ declare function toLowerCase(value: string): string;
380
+ /**
381
+ * Transform a string to uppercase
382
+ */
383
+ declare function toUpperCase(value: string): string;
384
+ /**
385
+ * Trim whitespace from a string
386
+ */
387
+ declare function trim(value: string): string;
388
+ /**
389
+ * Transform a slug to underscore format
390
+ */
391
+ declare function slugToUnderscore(value: string): string;
392
+ /**
393
+ * Transform underscores to dashes (slug format)
394
+ */
395
+ declare function underscoreToSlug(value: string): string;
396
+ /**
397
+ * Auto-detect and transform value based on format
398
+ */
399
+ declare function autoTransform(value: string): unknown;
400
+ /**
401
+ * Create a composed transformer from multiple transform functions
402
+ */
403
+ declare function compose(...transformers: Array<(value: string) => unknown>): (value: string) => unknown;
404
+ /**
405
+ * Identity transformer - returns the value unchanged
406
+ */
407
+ declare function identity<T>(value: T): T;
408
+
409
+ declare const VERSION = "1.0.0";
410
+
411
+ export { AdapterNotSetError, BindOptions, BindingError, BindingResult, Cache, IORMAdapter, InvalidModelError, type LogLevel, type LoggerConfig, ModelBinder, ModelBindingsConfig, ModelNotFoundError, VERSION, ValidationError, autoTransform, bindAs, bindByKey, bindCached, bindModel, bindModels, bindOptional, bindWithRelations, compose, identity, isEmail, isFunction, isNonEmptyString, isNotEmpty, isNumeric, isObjectId, isPlainObject, isPositiveInteger, isSlug, isUUID, isValidParamName, logger, slugToUnderscore, toBoolean, toFloat, toLowerCase, toNumber, toUpperCase, trim, underscoreToSlug };