@stacksjs/bun-router 0.0.16 → 0.0.18

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 (176) hide show
  1. package/dist/auth.d.ts +22 -8
  2. package/dist/chunk-1ahs68ys.js +18 -0
  3. package/dist/{chunk-0145g1me.js → chunk-cgptvjdf.js} +147 -75
  4. package/dist/chunk-g3ybefhg.js +1923 -0
  5. package/dist/cli.js +3 -2
  6. package/dist/container/container.d.ts +1 -0
  7. package/dist/container/contextual-binding.d.ts +0 -1
  8. package/dist/container/decorators.d.ts +2 -5
  9. package/dist/container/index.d.ts +16 -0
  10. package/dist/container/index.js +322 -0
  11. package/dist/container/service-provider.d.ts +3 -3
  12. package/dist/development/performance-profiler.d.ts +3 -3
  13. package/dist/development/route-debugger.d.ts +1 -1
  14. package/dist/development/route-inspector.d.ts +2 -2
  15. package/dist/file-serving/static-files.d.ts +16 -0
  16. package/dist/index.d.ts +17 -1
  17. package/dist/index.js +1159 -1380
  18. package/dist/middleware/cors.d.ts +9 -0
  19. package/dist/middleware/csrf.d.ts +7 -0
  20. package/dist/middleware/ddos_protection.d.ts +16 -1
  21. package/dist/middleware/rate_limit.d.ts +7 -1
  22. package/dist/middleware/request_tracer.d.ts +15 -9
  23. package/dist/middleware/response_cache.d.ts +2 -1
  24. package/dist/observability/integration.d.ts +1 -1
  25. package/dist/request/context.d.ts +12 -1
  26. package/dist/request/macros.d.ts +40 -2
  27. package/dist/response/macros.d.ts +4 -4
  28. package/dist/router/fluent-routing.d.ts +50 -13
  29. package/dist/router/handler-resolver.d.ts +8 -0
  30. package/dist/router/index.d.ts +9 -7
  31. package/dist/router/route-compiler.d.ts +8 -10
  32. package/dist/router/route-trie.d.ts +7 -1
  33. package/dist/router/router.d.ts +13 -34
  34. package/dist/router/validation-integration.d.ts +10 -4
  35. package/dist/testing/performance-testing.d.ts +2 -2
  36. package/dist/testing/test-client.d.ts +1 -1
  37. package/dist/testing/websocket-testing.d.ts +1 -1
  38. package/dist/types/middleware-types.d.ts +1 -1
  39. package/dist/types/route-inference.d.ts +9 -2
  40. package/dist/types.d.ts +62 -9
  41. package/dist/utils.d.ts +1 -1
  42. package/package.json +12 -3
  43. package/dist/router/fluent-router.d.ts +0 -315
  44. package/src/auth.ts +0 -469
  45. package/src/cache/lru-cache.ts +0 -457
  46. package/src/cache/middleware-memoization.ts +0 -531
  47. package/src/cache/route-cache-warmer.ts +0 -486
  48. package/src/cache/sqlite-cache.ts +0 -783
  49. package/src/cache/streaming-cache.ts +0 -572
  50. package/src/cli/colors.ts +0 -29
  51. package/src/cli/index.ts +0 -287
  52. package/src/cli/middleware.ts +0 -291
  53. package/src/cli/openapi.ts +0 -407
  54. package/src/cli/router.ts +0 -188
  55. package/src/cli/routes.ts +0 -265
  56. package/src/cli/utils.ts +0 -531
  57. package/src/cli.ts +0 -5
  58. package/src/config.ts +0 -322
  59. package/src/container/container.ts +0 -740
  60. package/src/container/contextual-binding.ts +0 -603
  61. package/src/container/decorators.ts +0 -359
  62. package/src/container/service-provider.ts +0 -596
  63. package/src/development/hot-reload.ts +0 -673
  64. package/src/development/index.ts +0 -499
  65. package/src/development/performance-profiler.ts +0 -717
  66. package/src/development/route-debugger.ts +0 -527
  67. package/src/development/route-inspector.ts +0 -749
  68. package/src/development/typescript-utilities.ts +0 -682
  69. package/src/docs.ts +0 -397
  70. package/src/errors/circuit-breaker.ts +0 -732
  71. package/src/errors/error-handler.ts +0 -569
  72. package/src/errors/error-reporting.ts +0 -672
  73. package/src/errors/exceptions.ts +0 -536
  74. package/src/errors/graceful-degradation.ts +0 -621
  75. package/src/errors/index.ts +0 -21
  76. package/src/errors/router-errors.ts +0 -632
  77. package/src/file-serving/static-files.ts +0 -581
  78. package/src/index.ts +0 -14
  79. package/src/middleware/auth.ts +0 -220
  80. package/src/middleware/content_security_policy.ts +0 -215
  81. package/src/middleware/cors.ts +0 -74
  82. package/src/middleware/csrf.ts +0 -118
  83. package/src/middleware/ddos_protection.ts +0 -255
  84. package/src/middleware/file_security.ts +0 -191
  85. package/src/middleware/file_upload.ts +0 -275
  86. package/src/middleware/helmet.ts +0 -268
  87. package/src/middleware/index.ts +0 -117
  88. package/src/middleware/input_validation.ts +0 -449
  89. package/src/middleware/json_body.ts +0 -37
  90. package/src/middleware/performance_alerting.ts +0 -538
  91. package/src/middleware/performance_dashboard.ts +0 -661
  92. package/src/middleware/performance_monitor.ts +0 -943
  93. package/src/middleware/pipeline.ts +0 -489
  94. package/src/middleware/rate_limit.ts +0 -245
  95. package/src/middleware/request_id.ts +0 -36
  96. package/src/middleware/request_signing.ts +0 -636
  97. package/src/middleware/request_tracer.ts +0 -636
  98. package/src/middleware/response_cache.ts +0 -743
  99. package/src/middleware/security.ts +0 -482
  100. package/src/middleware/security_suite.ts +0 -257
  101. package/src/middleware/session.ts +0 -91
  102. package/src/model-binding/index.ts +0 -18
  103. package/src/model-binding/model-middleware.ts +0 -425
  104. package/src/model-binding/model-registry.ts +0 -550
  105. package/src/model-binding.ts +0 -370
  106. package/src/model-resolver-factory.ts +0 -106
  107. package/src/observability/correlation.ts +0 -691
  108. package/src/observability/health-checks.ts +0 -729
  109. package/src/observability/index.ts +0 -184
  110. package/src/observability/integration.ts +0 -548
  111. package/src/observability/metrics.ts +0 -753
  112. package/src/observability/tracing.ts +0 -638
  113. package/src/optimization/bun-utilities.ts +0 -778
  114. package/src/query-builder-integration.ts +0 -137
  115. package/src/request/context.ts +0 -62
  116. package/src/request/enhanced-request.ts +0 -857
  117. package/src/request/macros.ts +0 -688
  118. package/src/response/macros.ts +0 -665
  119. package/src/response/response-factory.ts +0 -596
  120. package/src/router/api-routes.ts +0 -243
  121. package/src/router/file-based-routing.ts +0 -690
  122. package/src/router/file-streaming.ts +0 -383
  123. package/src/router/fluent-router.ts +0 -927
  124. package/src/router/fluent-routing.ts +0 -797
  125. package/src/router/group-organization.ts +0 -213
  126. package/src/router/handler-resolver.ts +0 -291
  127. package/src/router/http-methods.ts +0 -377
  128. package/src/router/index.ts +0 -187
  129. package/src/router/middleware-groups.ts +0 -222
  130. package/src/router/middleware-integration.ts +0 -399
  131. package/src/router/middleware.ts +0 -231
  132. package/src/router/model-binding.ts +0 -215
  133. package/src/router/optimized-route-matching.ts +0 -253
  134. package/src/router/route-building.ts +0 -221
  135. package/src/router/route-compiler.ts +0 -702
  136. package/src/router/route-matching.ts +0 -349
  137. package/src/router/route-trie.ts +0 -464
  138. package/src/router/router.ts +0 -1604
  139. package/src/router/server.ts +0 -462
  140. package/src/router/validation-integration.ts +0 -443
  141. package/src/router/view-rendering.ts +0 -233
  142. package/src/router/websocket.ts +0 -100
  143. package/src/routing/route-caching.ts +0 -402
  144. package/src/routing/route-throttling.ts +0 -469
  145. package/src/routing/subdomain-routing.ts +0 -492
  146. package/src/session/database-store.ts +0 -109
  147. package/src/session/file-store.ts +0 -148
  148. package/src/session/index.ts +0 -244
  149. package/src/session/memory-store.ts +0 -88
  150. package/src/session/redis-store.ts +0 -93
  151. package/src/streaming/index.ts +0 -17
  152. package/src/streaming/sse-handler.ts +0 -482
  153. package/src/streaming/stream-handler.ts +0 -552
  154. package/src/testing/auth-testing.ts +0 -446
  155. package/src/testing/file-upload-testing.ts +0 -543
  156. package/src/testing/index.ts +0 -10
  157. package/src/testing/middleware-testing.ts +0 -322
  158. package/src/testing/model-binding-testing.ts +0 -645
  159. package/src/testing/performance-testing.ts +0 -738
  160. package/src/testing/test-client.ts +0 -310
  161. package/src/testing/test-request.ts +0 -315
  162. package/src/testing/test-response.ts +0 -332
  163. package/src/testing/types.ts +0 -224
  164. package/src/testing/websocket-testing.ts +0 -590
  165. package/src/types/controller-types.ts +0 -385
  166. package/src/types/core.ts +0 -683
  167. package/src/types/middleware-types.ts +0 -419
  168. package/src/types/request-response-augmentation.ts +0 -489
  169. package/src/types/route-inference.ts +0 -357
  170. package/src/types.ts +0 -2067
  171. package/src/url.ts +0 -126
  172. package/src/utils/index.ts +0 -1
  173. package/src/utils/query-preservation.ts +0 -201
  174. package/src/utils.ts +0 -392
  175. package/src/validation/validator.ts +0 -685
  176. package/src/websocket/clustering.ts +0 -762
@@ -1,857 +0,0 @@
1
- /**
2
- * Enhanced Request Methods
3
- *
4
- * Implements fluent request methods with full TypeScript support
5
- */
6
-
7
- import type {
8
- EnhancedRequest,
9
- InputValue,
10
- RequestInput,
11
- ValidatedData,
12
- } from '../types'
13
- import { Validator } from '../validation/validator'
14
-
15
- // ============================================================================
16
- // Types
17
- // ============================================================================
18
-
19
- /**
20
- * Validation rules type
21
- */
22
- export type ValidationRules = Record<string, string | ValidationRule[]>
23
-
24
- /**
25
- * Validation rule interface
26
- */
27
- export interface ValidationRule {
28
- name: string
29
- validate: (value: unknown, parameters: string[], field: string, data: Record<string, unknown>) => Promise<boolean> | boolean
30
- message: string | ((_field: string, _parameters: string[]) => string)
31
- }
32
-
33
- /**
34
- * Safe validated data wrapper
35
- */
36
- export interface SafeData<T extends Record<string, unknown> = Record<string, unknown>> {
37
- only: <K extends keyof T>(keys: K[]) => Pick<T, K>
38
- except: <K extends keyof T>(keys: K[]) => Omit<T, K>
39
- merge: <U extends Record<string, unknown>>(data: U) => T & U
40
- all: () => T
41
- has: (key: keyof T) => boolean
42
- get: <K extends keyof T>(key: K) => T[K]
43
- }
44
-
45
- /**
46
- * Collection interface for array data
47
- */
48
- export interface Collection<T> {
49
- items: T[]
50
- count: () => number
51
- first: () => T | undefined
52
- last: () => T | undefined
53
- map: <U>(fn: (item: T, index: number) => U) => Collection<U>
54
- filter: (fn: (item: T, index: number) => boolean) => Collection<T>
55
- find: (fn: (item: T, index: number) => boolean) => T | undefined
56
- reduce: <U>(fn: (acc: U, item: T, index: number) => U, initial: U) => U
57
- forEach: (fn: (item: T, index: number) => void) => void
58
- toArray: () => T[]
59
- isEmpty: () => boolean
60
- isNotEmpty: () => boolean
61
- pluck: <K extends keyof T>(key: K) => Collection<T[K]>
62
- unique: () => Collection<T>
63
- sortBy: <K extends keyof T>(key: K, direction?: 'asc' | 'desc') => Collection<T>
64
- groupBy: <K extends keyof T>(key: K) => Map<T[K], Collection<T>>
65
- chunk: (size: number) => Collection<Collection<T>>
66
- take: (count: number) => Collection<T>
67
- skip: (count: number) => Collection<T>
68
- reverse: () => Collection<T>
69
- sum: (key?: keyof T) => number
70
- avg: (key?: keyof T) => number
71
- min: (key?: keyof T) => T | undefined
72
- max: (key?: keyof T) => T | undefined
73
- }
74
-
75
- // ============================================================================
76
- // Collection Implementation
77
- // ============================================================================
78
-
79
- /**
80
- * Collection class for array manipulation
81
- */
82
- export class CollectionImpl<T> implements Collection<T> {
83
- items: T[]
84
-
85
- constructor(items: T[]) {
86
- this.items = [...items]
87
- }
88
-
89
- count(): number {
90
- return this.items.length
91
- }
92
-
93
- first(): T | undefined {
94
- return this.items[0]
95
- }
96
-
97
- last(): T | undefined {
98
- return this.items[this.items.length - 1]
99
- }
100
-
101
- map<U>(fn: (item: T, index: number) => U): Collection<U> {
102
- return new CollectionImpl(this.items.map(fn))
103
- }
104
-
105
- filter(fn: (item: T, index: number) => boolean): Collection<T> {
106
- return new CollectionImpl(this.items.filter(fn))
107
- }
108
-
109
- find(fn: (item: T, index: number) => boolean): T | undefined {
110
- return this.items.find(fn)
111
- }
112
-
113
- reduce<U>(fn: (acc: U, item: T, index: number) => U, initial: U): U {
114
- return this.items.reduce(fn, initial)
115
- }
116
-
117
- forEach(fn: (item: T, index: number) => void): void {
118
- this.items.forEach(fn)
119
- }
120
-
121
- toArray(): T[] {
122
- return [...this.items]
123
- }
124
-
125
- isEmpty(): boolean {
126
- return this.items.length === 0
127
- }
128
-
129
- isNotEmpty(): boolean {
130
- return this.items.length > 0
131
- }
132
-
133
- pluck<K extends keyof T>(key: K): Collection<T[K]> {
134
- return new CollectionImpl(this.items.map(item => item[key]))
135
- }
136
-
137
- unique(): Collection<T> {
138
- return new CollectionImpl([...new Set(this.items)])
139
- }
140
-
141
- sortBy<K extends keyof T>(key: K, direction: 'asc' | 'desc' = 'asc'): Collection<T> {
142
- const sorted = [...this.items].sort((a, b) => {
143
- const aVal = a[key]
144
- const bVal = b[key]
145
- if (aVal < bVal)
146
- return direction === 'asc' ? -1 : 1
147
- if (aVal > bVal)
148
- return direction === 'asc' ? 1 : -1
149
- return 0
150
- })
151
- return new CollectionImpl(sorted)
152
- }
153
-
154
- groupBy<K extends keyof T>(key: K): Map<T[K], Collection<T>> {
155
- const groups = new Map<T[K], T[]>()
156
- for (const item of this.items) {
157
- const groupKey = item[key]
158
- if (!groups.has(groupKey)) {
159
- groups.set(groupKey, [])
160
- }
161
- groups.get(groupKey)!.push(item)
162
- }
163
- const result = new Map<T[K], Collection<T>>()
164
- for (const [k, v] of groups) {
165
- result.set(k, new CollectionImpl(v))
166
- }
167
- return result
168
- }
169
-
170
- chunk(size: number): Collection<Collection<T>> {
171
- const chunks: Collection<T>[] = []
172
- for (let i = 0; i < this.items.length; i += size) {
173
- chunks.push(new CollectionImpl(this.items.slice(i, i + size)))
174
- }
175
- return new CollectionImpl(chunks)
176
- }
177
-
178
- take(count: number): Collection<T> {
179
- return new CollectionImpl(this.items.slice(0, count))
180
- }
181
-
182
- skip(count: number): Collection<T> {
183
- return new CollectionImpl(this.items.slice(count))
184
- }
185
-
186
- reverse(): Collection<T> {
187
- return new CollectionImpl([...this.items].reverse())
188
- }
189
-
190
- sum(key?: keyof T): number {
191
- if (key) {
192
- return this.items.reduce((acc, item) => acc + (Number(item[key]) || 0), 0)
193
- }
194
- return this.items.reduce((acc, item) => acc + (Number(item) || 0), 0)
195
- }
196
-
197
- avg(key?: keyof T): number {
198
- if (this.items.length === 0)
199
- return 0
200
- return this.sum(key) / this.items.length
201
- }
202
-
203
- min(key?: keyof T): T | undefined {
204
- if (this.items.length === 0)
205
- return undefined
206
- if (key) {
207
- return this.items.reduce((min, item) =>
208
- item[key] < min[key] ? item : min,
209
- )
210
- }
211
- return this.items.reduce((min, item) => (item < min ? item : min))
212
- }
213
-
214
- max(key?: keyof T): T | undefined {
215
- if (this.items.length === 0)
216
- return undefined
217
- if (key) {
218
- return this.items.reduce((max, item) =>
219
- item[key] > max[key] ? item : max,
220
- )
221
- }
222
- return this.items.reduce((max, item) => (item > max ? item : max))
223
- }
224
- }
225
-
226
- // ============================================================================
227
- // Safe Data Implementation
228
- // ============================================================================
229
-
230
- /**
231
- * Create a safe data wrapper
232
- */
233
- export function createSafeData<T extends Record<string, unknown>>(data: T): SafeData<T> {
234
- return {
235
- only: <K extends keyof T>(keys: K[]): Pick<T, K> => {
236
- const result = {} as Pick<T, K>
237
- for (const key of keys) {
238
- if (key in data) {
239
- result[key] = data[key]
240
- }
241
- }
242
- return result
243
- },
244
-
245
- except: <K extends keyof T>(keys: K[]): Omit<T, K> => {
246
- const result = { ...data }
247
- for (const key of keys) {
248
- delete result[key]
249
- }
250
- return result as Omit<T, K>
251
- },
252
-
253
- // eslint-disable-next-line pickier/no-unused-vars
254
- merge: <U extends Record<string, unknown>>(mergeData: U): T & U => {
255
- return { ...data, ...mergeData }
256
- },
257
-
258
- all: (): T => ({ ...data }),
259
-
260
- has: (key: keyof T): boolean => key in data,
261
-
262
- get: <K extends keyof T>(key: K): T[K] => data[key],
263
- }
264
- }
265
-
266
- // ============================================================================
267
- // Enhanced Request Methods
268
- // ============================================================================
269
-
270
- /**
271
- * Enhanced request methods interface
272
- */
273
- export interface EnhancedRequestMethods {
274
- /**
275
- * Validate the request data
276
- */
277
- validate: <T extends Record<string, unknown> = Record<string, unknown>>(
278
- rules: ValidationRules,
279
- messages?: Record<string, string>,
280
- ) => Promise<T>
281
-
282
- /**
283
- * Get validated data as typed object
284
- */
285
- getValidated: <T extends Record<string, unknown> = Record<string, unknown>>() => T
286
-
287
- /**
288
- * Get safe validated data wrapper
289
- */
290
- safe: <T extends Record<string, unknown> = Record<string, unknown>>() => SafeData<T>
291
-
292
- /**
293
- * Execute callback when input has a value
294
- */
295
- whenHas: <T>(
296
- key: string,
297
- callback: (value: T) => void,
298
- defaultCallback?: () => void,
299
- ) => void
300
-
301
- /**
302
- * Execute callback when input is filled (not empty)
303
- */
304
- whenFilled: <T>(
305
- key: string,
306
- callback: (value: T) => void,
307
- defaultCallback?: () => void,
308
- ) => void
309
-
310
- /**
311
- * Parse date input
312
- */
313
- date: (key: string, format?: string) => Date | null
314
-
315
- /**
316
- * Parse enum input
317
- */
318
- enum: <T extends Record<string, string | number>>(
319
- key: string,
320
- enumType: T,
321
- ) => T[keyof T] | null
322
-
323
- /**
324
- * Get input as collection
325
- */
326
- collect: <T = unknown>(key: string) => Collection<T>
327
-
328
- /**
329
- * Get all input keys
330
- */
331
- keys: () => string[]
332
-
333
- /**
334
- * Get input with type casting
335
- */
336
- string: (key: string, defaultValue?: string) => string
337
- integer: (key: string, defaultValue?: number) => number
338
- float: (key: string, defaultValue?: number) => number
339
- boolean: (key: string, defaultValue?: boolean) => boolean
340
-
341
- /**
342
- * Check if input matches a value
343
- */
344
- isValue: (key: string, value: unknown) => boolean
345
-
346
- /**
347
- * Get old input (for form repopulation)
348
- */
349
- old: <T = unknown>(key: string, defaultValue?: T) => T
350
-
351
- /**
352
- * Flash input to session for form repopulation
353
- */
354
- flashInput: (keys?: string[]) => void
355
-
356
- /**
357
- * Flash only specific keys
358
- */
359
- flashInputOnly: (keys: string[]) => void
360
-
361
- /**
362
- * Flash except specific keys
363
- */
364
- flashInputExcept: (keys: string[]) => void
365
-
366
- /**
367
- * Get input as array
368
- */
369
- array: <T = unknown>(key: string) => T[]
370
-
371
- /**
372
- * Get input value from any source (query, body, params)
373
- * Laravel-style request.get()
374
- */
375
- get: <T = unknown>(key: string, defaultValue?: T) => T
376
-
377
- /**
378
- * Get all input data
379
- */
380
- all: () => RequestInput
381
-
382
- /**
383
- * Get only specified keys
384
- */
385
- only: <T extends Record<string, unknown> = Record<string, unknown>>(keys: string[]) => T
386
-
387
- /**
388
- * Get all except specified keys
389
- */
390
- except: <T extends Record<string, unknown> = Record<string, unknown>>(keys: string[]) => T
391
-
392
- /**
393
- * Check if input has a key
394
- */
395
- has: (key: string | string[]) => boolean
396
-
397
- /**
398
- * Check if input has any of the keys
399
- */
400
- hasAny: (keys: string[]) => boolean
401
-
402
- /**
403
- * Check if input is filled (not empty)
404
- */
405
- filled: (key: string | string[]) => boolean
406
-
407
- /**
408
- * Check if input is missing
409
- */
410
- missing: (key: string | string[]) => boolean
411
-
412
- /**
413
- * Merge additional data into input
414
- */
415
- merge: (data: Record<string, unknown>) => void
416
-
417
- /**
418
- * Get input value or fail
419
- */
420
- input: <T = unknown>(key: string, defaultValue?: T) => T
421
- }
422
-
423
- // ============================================================================
424
- // Request Enhancement
425
- // ============================================================================
426
-
427
- /**
428
- * Enhance request with additional methods
429
- */
430
- export function enhanceRequestWithMethods(request: EnhancedRequest): EnhancedRequest & EnhancedRequestMethods {
431
- const enhanced = request as EnhancedRequest & EnhancedRequestMethods & {
432
- _validatedData?: ValidatedData
433
- _oldInput?: RequestInput
434
- }
435
-
436
- // Get all input data
437
- const getAllInput = (): RequestInput => {
438
- const input: RequestInput = {}
439
-
440
- // Query parameters
441
- if (request.query) {
442
- for (const [key, value] of Object.entries(request.query)) {
443
- input[key] = value as InputValue
444
- }
445
- }
446
-
447
- // JSON body
448
- if (request.jsonBody) {
449
- for (const [key, value] of Object.entries(request.jsonBody as Record<string, unknown>)) {
450
- input[key] = value as InputValue
451
- }
452
- }
453
-
454
- // Form body
455
- if (request.formBody) {
456
- for (const [key, value] of Object.entries(request.formBody)) {
457
- input[key] = value as InputValue
458
- }
459
- }
460
-
461
- // Route params
462
- if (request.params) {
463
- for (const [key, value] of Object.entries(request.params)) {
464
- input[key] = value
465
- }
466
- }
467
-
468
- return input
469
- }
470
-
471
- /**
472
- * Validate the request
473
- */
474
- enhanced.validate = async <T extends Record<string, unknown>>(
475
- rules: ValidationRules,
476
- messages?: Record<string, string>,
477
- ): Promise<T> => {
478
- const input = getAllInput()
479
- const validator = new Validator({
480
- customMessages: messages,
481
- })
482
-
483
- const errors = await validator.validate(input as Record<string, unknown>, rules as Record<string, string>)
484
-
485
- if (Object.keys(errors).length > 0) {
486
- const error = new Error('Validation failed') as Error & { errors: Record<string, string[]> }
487
- error.errors = errors
488
- throw error
489
- }
490
-
491
- enhanced._validatedData = input as ValidatedData
492
- return input as T
493
- }
494
-
495
- /**
496
- * Get validated data as typed object
497
- */
498
- enhanced.getValidated = <T extends Record<string, unknown>>(): T => {
499
- return (enhanced._validatedData || {}) as T
500
- }
501
-
502
- /**
503
- * Get safe validated data wrapper
504
- */
505
- enhanced.safe = <T extends Record<string, unknown>>(): SafeData<T> => {
506
- return createSafeData(enhanced.getValidated<T>())
507
- }
508
-
509
- /**
510
- * Execute callback when input has a value
511
- */
512
- enhanced.whenHas = <T>(
513
- key: string,
514
- callback: (value: T) => void,
515
- defaultCallback?: () => void,
516
- ): void => {
517
- const input = getAllInput()
518
- if (key in input && input[key] !== undefined) {
519
- callback(input[key] as T)
520
- }
521
- else if (defaultCallback) {
522
- defaultCallback()
523
- }
524
- }
525
-
526
- /**
527
- * Execute callback when input is filled
528
- */
529
- enhanced.whenFilled = <T>(
530
- key: string,
531
- callback: (value: T) => void,
532
- defaultCallback?: () => void,
533
- ): void => {
534
- const input = getAllInput()
535
- const value = input[key]
536
- const isFilled = value !== undefined
537
- && value !== null
538
- && value !== ''
539
- && !(Array.isArray(value) && value.length === 0)
540
-
541
- if (isFilled) {
542
- callback(value as T)
543
- }
544
- else if (defaultCallback) {
545
- defaultCallback()
546
- }
547
- }
548
-
549
- /**
550
- * Parse date input
551
- */
552
- enhanced.date = (key: string, _format?: string): Date | null => {
553
- const input = getAllInput()
554
- const value = input[key]
555
-
556
- if (!value)
557
- return null
558
-
559
- const date = new Date(String(value))
560
- return Number.isNaN(date.getTime()) ? null : date
561
- }
562
-
563
- /**
564
- * Parse enum input
565
- */
566
- enhanced.enum = <T extends Record<string, string | number>>(
567
- key: string,
568
- enumType: T,
569
- ): T[keyof T] | null => {
570
- const input = getAllInput()
571
- const value = input[key]
572
-
573
- if (value === undefined || value === null)
574
- return null
575
-
576
- const enumValues = Object.values(enumType)
577
- if (enumValues.includes(value as T[keyof T])) {
578
- return value as T[keyof T]
579
- }
580
-
581
- // Try to match by key
582
- const stringValue = String(value)
583
- if (stringValue in enumType) {
584
- return enumType[stringValue as keyof T]
585
- }
586
-
587
- return null
588
- }
589
-
590
- /**
591
- * Get input as collection
592
- */
593
- enhanced.collect = <T = unknown>(key: string): Collection<T> => {
594
- const input = getAllInput()
595
- const value = input[key]
596
-
597
- if (Array.isArray(value)) {
598
- return new CollectionImpl(value as T[])
599
- }
600
-
601
- return new CollectionImpl(value ? [value as T] : [])
602
- }
603
-
604
- /**
605
- * Get all input keys
606
- */
607
- enhanced.keys = (): string[] => {
608
- return Object.keys(getAllInput())
609
- }
610
-
611
- /**
612
- * Get string input
613
- */
614
- enhanced.string = (key: string, defaultValue: string = ''): string => {
615
- const input = getAllInput()
616
- const value = input[key]
617
- return value !== undefined && value !== null ? String(value) : defaultValue
618
- }
619
-
620
- /**
621
- * Get integer input
622
- */
623
- enhanced.integer = (key: string, defaultValue: number = 0): number => {
624
- const input = getAllInput()
625
- const value = input[key]
626
- const parsed = Number.parseInt(String(value), 10)
627
- return Number.isNaN(parsed) ? defaultValue : parsed
628
- }
629
-
630
- /**
631
- * Get float input
632
- */
633
- enhanced.float = (key: string, defaultValue: number = 0): number => {
634
- const input = getAllInput()
635
- const value = input[key]
636
- const parsed = Number.parseFloat(String(value))
637
- return Number.isNaN(parsed) ? defaultValue : parsed
638
- }
639
-
640
- /**
641
- * Get boolean input
642
- */
643
- enhanced.boolean = (key: string, defaultValue: boolean = false): boolean => {
644
- const input = getAllInput()
645
- const value = input[key]
646
-
647
- if (value === undefined || value === null)
648
- return defaultValue
649
- if (typeof value === 'boolean')
650
- return value
651
- if (value === 'true' || value === '1' || value === 1)
652
- return true
653
- if (value === 'false' || value === '0' || value === 0)
654
- return false
655
-
656
- return defaultValue
657
- }
658
-
659
- /**
660
- * Check if input matches a value
661
- */
662
- enhanced.isValue = (key: string, value: unknown): boolean => {
663
- const input = getAllInput()
664
- return input[key] === value
665
- }
666
-
667
- /**
668
- * Get old input
669
- */
670
- enhanced.old = <T = unknown>(key: string, defaultValue?: T): T => {
671
- const oldInput = enhanced._oldInput || {}
672
- return (oldInput[key] as T) ?? (defaultValue as T)
673
- }
674
-
675
- /**
676
- * Flash input to session for form repopulation
677
- */
678
- enhanced.flashInput = (keys?: string[]): void => {
679
- const input = getAllInput()
680
- if (keys) {
681
- const filtered: RequestInput = {}
682
- for (const key of keys) {
683
- if (key in input) {
684
- filtered[key] = input[key]
685
- }
686
- }
687
- enhanced._oldInput = filtered
688
- }
689
- else {
690
- enhanced._oldInput = input
691
- }
692
- }
693
-
694
- /**
695
- * Flash only specific keys
696
- */
697
- enhanced.flashInputOnly = (keys: string[]): void => {
698
- enhanced.flashInput(keys)
699
- }
700
-
701
- /**
702
- * Flash except specific keys
703
- */
704
- enhanced.flashInputExcept = (keys: string[]): void => {
705
- const input = getAllInput()
706
- const filtered: RequestInput = {}
707
- for (const [key, value] of Object.entries(input)) {
708
- if (!keys.includes(key)) {
709
- filtered[key] = value
710
- }
711
- }
712
- enhanced._oldInput = filtered
713
- }
714
-
715
- /**
716
- * Get input as array
717
- */
718
- enhanced.array = <T = unknown>(key: string): T[] => {
719
- const input = getAllInput()
720
- const value = input[key]
721
-
722
- if (Array.isArray(value)) {
723
- return value as T[]
724
- }
725
-
726
- return value !== undefined && value !== null ? [value as T] : []
727
- }
728
-
729
- /**
730
- * Get input value from any source (query, body, params) - Laravel style
731
- */
732
- enhanced.get = <T = unknown>(key: string, defaultValue?: T): T => {
733
- const input = getAllInput()
734
- const value = input[key]
735
- return (value !== undefined ? value : defaultValue) as T
736
- }
737
-
738
- /**
739
- * Get all input data
740
- */
741
- enhanced.all = (): RequestInput => {
742
- return getAllInput()
743
- }
744
-
745
- /**
746
- * Get only specified keys
747
- */
748
- enhanced.only = <T extends Record<string, unknown> = Record<string, unknown>>(keys: string[]): T => {
749
- const input = getAllInput()
750
- const result = {} as T
751
- for (const key of keys) {
752
- if (key in input) {
753
- (result as Record<string, unknown>)[key] = input[key]
754
- }
755
- }
756
- return result
757
- }
758
-
759
- /**
760
- * Get all except specified keys
761
- */
762
- enhanced.except = <T extends Record<string, unknown> = Record<string, unknown>>(keys: string[]): T => {
763
- const input = getAllInput()
764
- const result = { ...input } as T
765
- for (const key of keys) {
766
- delete (result as Record<string, unknown>)[key]
767
- }
768
- return result
769
- }
770
-
771
- /**
772
- * Check if input has a key (or all keys if array)
773
- */
774
- enhanced.has = (key: string | string[]): boolean => {
775
- const input = getAllInput()
776
- if (Array.isArray(key)) {
777
- return key.every(k => k in input && input[k] !== undefined)
778
- }
779
- return key in input && input[key] !== undefined
780
- }
781
-
782
- /**
783
- * Check if input has any of the keys
784
- */
785
- enhanced.hasAny = (keys: string[]): boolean => {
786
- const input = getAllInput()
787
- return keys.some(k => k in input && input[k] !== undefined)
788
- }
789
-
790
- /**
791
- * Check if input is filled (not empty)
792
- */
793
- enhanced.filled = (key: string | string[]): boolean => {
794
- const input = getAllInput()
795
- const isFilled = (k: string): boolean => {
796
- const value = input[k]
797
- return value !== undefined
798
- && value !== null
799
- && value !== ''
800
- && !(Array.isArray(value) && value.length === 0)
801
- }
802
-
803
- if (Array.isArray(key)) {
804
- return key.every(isFilled)
805
- }
806
- return isFilled(key)
807
- }
808
-
809
- /**
810
- * Check if input is missing
811
- */
812
- enhanced.missing = (key: string | string[]): boolean => {
813
- const input = getAllInput()
814
- if (Array.isArray(key)) {
815
- return key.every(k => !(k in input) || input[k] === undefined)
816
- }
817
- return !(key in input) || input[key] === undefined
818
- }
819
-
820
- /**
821
- * Merge additional data into input
822
- */
823
- enhanced.merge = (data: Record<string, unknown>): void => {
824
- if (request.jsonBody && typeof request.jsonBody === 'object') {
825
- Object.assign(request.jsonBody, data)
826
- }
827
- else {
828
- (request as any).jsonBody = data
829
- }
830
- }
831
-
832
- /**
833
- * Get input value - alias for get() for Laravel compatibility
834
- */
835
- enhanced.input = <T = unknown>(key: string, defaultValue?: T): T => {
836
- return enhanced.get(key, defaultValue)
837
- }
838
-
839
- return enhanced
840
- }
841
-
842
- // ============================================================================
843
- // Middleware for Request Enhancement
844
- // ============================================================================
845
-
846
- /**
847
- * Middleware to enhance request with additional methods
848
- */
849
- export function enhancedRequestMiddleware() {
850
- return async (req: EnhancedRequest, next: () => Promise<Response | null>): Promise<Response | null> => {
851
- enhanceRequestWithMethods(req)
852
- return next()
853
- }
854
- }
855
-
856
- // Note: Collection interface and CollectionImpl class are already exported above
857
- // enhanceRequestWithMethods is already exported above