@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,407 +0,0 @@
1
- import fs from 'node:fs/promises'
2
- import process from 'node:process'
3
- import { chalk } from './colors'
4
-
5
- /**
6
- * Options for the openapi command
7
- */
8
- export interface OpenAPIOptions {
9
- output?: string
10
- title?: string
11
- version?: string
12
- description?: string
13
- basePath?: string
14
- servers?: string
15
- tags?: boolean
16
- security?: boolean
17
- }
18
-
19
- /**
20
- * Generate OpenAPI specification for API routes
21
- */
22
- export async function generateOpenAPISpec(options: OpenAPIOptions): Promise<void> {
23
- try {
24
- // Try to load router instance from the application
25
- const routesFile = `${process.cwd()}/routes/index.ts`
26
- const { router } = await import(routesFile)
27
-
28
- if (!router) {
29
- console.error(chalk.red(`Could not find router instance in ${routesFile}.`))
30
- process.exit(1)
31
- }
32
-
33
- // Get routes from router
34
- const routes = (router as any).routes || []
35
-
36
- if (routes.length === 0) {
37
- console.log(chalk.yellow('No routes defined. Creating empty OpenAPI specification.'))
38
- }
39
-
40
- // Filter API routes - typically these start with /api or have type 'api'
41
- const apiRoutes = routes.filter((route: any) => {
42
- return route.type === 'api' || route.path.startsWith('/api')
43
- })
44
-
45
- // Define base structure for OpenAPI spec
46
- const openapi: any = {
47
- openapi: '3.0.3',
48
- info: {
49
- title: options.title || 'API Documentation',
50
- version: options.version || '1.0.0',
51
- description: options.description || 'Generated API documentation',
52
- },
53
- servers: generateServers(options),
54
- paths: {},
55
- components: {
56
- schemas: {},
57
- securitySchemes: {},
58
- },
59
- tags: [],
60
- }
61
-
62
- // Collect unique tags if enabled
63
- const uniqueTags = new Set<string>()
64
-
65
- // Process routes and add them to the paths
66
- for (const route of apiRoutes) {
67
- const pathItem: any = openapi.paths[route.path] || {}
68
-
69
- // Extract tag from route path or route metadata
70
- let tag = ''
71
- if (options.tags !== false) {
72
- tag = route.tag || extractTagFromPath(route.path)
73
- uniqueTags.add(tag)
74
- }
75
-
76
- // Convert route to OpenAPI operation
77
- const operation: any = {
78
- summary: route.summary || `${route.method.toUpperCase()} ${route.path}`,
79
- description: route.description || '',
80
- operationId: route.name || generateOperationId(route.method, route.path),
81
- parameters: generateParameters(route),
82
- responses: generateResponses(route),
83
- }
84
-
85
- // Add tag if available
86
- if (tag) {
87
- operation.tags = [tag]
88
- }
89
-
90
- // Add request body for applicable methods
91
- if (['post', 'put', 'patch'].includes(route.method.toLowerCase())) {
92
- operation.requestBody = generateRequestBody(route)
93
- }
94
-
95
- // Add security if enabled and available in route
96
- if (options.security !== false && route.middleware) {
97
- const securityItems = extractSecurityFromMiddleware(route.middleware)
98
- if (securityItems.length > 0) {
99
- operation.security = securityItems
100
- }
101
- }
102
-
103
- // Add operation to path
104
- pathItem[route.method.toLowerCase()] = operation
105
- openapi.paths[route.path] = pathItem
106
- }
107
-
108
- // Add tags to OpenAPI spec if enabled
109
- if (options.tags !== false) {
110
- openapi.tags = Array.from(uniqueTags).map(tag => ({
111
- name: tag,
112
- description: `Operations related to ${tag}`,
113
- }))
114
- }
115
-
116
- // Write the OpenAPI spec to the output file
117
- const outputPath = options.output || 'openapi.json'
118
- await fs.writeFile(outputPath, JSON.stringify(openapi, null, 2))
119
- console.log(chalk.green(`✨ OpenAPI specification generated at ${outputPath}`))
120
- console.log(chalk.blue(`Import this file into Postman, Swagger UI, or other API tools to explore your API.`))
121
- }
122
- catch (error: any) {
123
- if (error.code === 'ERR_MODULE_NOT_FOUND') {
124
- console.error(chalk.red(`Routes file not found at ${process.cwd()}/routes/index.ts`))
125
- console.error(chalk.yellow('Make sure your routes are defined and exported as "router" in routes/index.ts'))
126
- }
127
- else {
128
- console.error(chalk.red(`Error generating OpenAPI specification: ${error.message}`))
129
- }
130
- throw error
131
- }
132
- }
133
-
134
- /**
135
- * Generate server objects for OpenAPI spec
136
- */
137
- function generateServers(options: OpenAPIOptions): any[] {
138
- const servers = []
139
-
140
- // Add servers from options
141
- if (options.servers) {
142
- const serverUrls = options.servers.split(',').map((url: string) => url.trim())
143
- for (const url of serverUrls) {
144
- servers.push({
145
- url,
146
- description: `Server ${servers.length + 1}`,
147
- })
148
- }
149
- }
150
-
151
- // If no servers provided, add default
152
- if (servers.length === 0) {
153
- servers.push({
154
- url: options.basePath || '/',
155
- description: 'Default server',
156
- })
157
- }
158
-
159
- return servers
160
- }
161
-
162
- /**
163
- * Extract tag from route path
164
- */
165
- function extractTagFromPath(path: string): string {
166
- // Extract first meaningful segment from path
167
- const segments = path.split('/').filter(Boolean)
168
- if (segments.length > 0) {
169
- // If first segment is 'api', use second segment
170
- if (segments[0].toLowerCase() === 'api' && segments.length > 1) {
171
- return segments[1]
172
- }
173
- return segments[0]
174
- }
175
- return 'default'
176
- }
177
-
178
- /**
179
- * Generate a camelCase operation ID from method and path
180
- */
181
- function generateOperationId(method: string, path: string): string {
182
- const pathWithoutParams = path.replace(/\{([^}]+)\}/g, '_$1')
183
- const segments = pathWithoutParams.split('/').filter(Boolean)
184
-
185
- if (segments.length === 0) {
186
- return method.toLowerCase()
187
- }
188
-
189
- let operationId = method.toLowerCase()
190
-
191
- for (const segment of segments) {
192
- operationId += segment.charAt(0).toUpperCase() + segment.slice(1)
193
- }
194
-
195
- return operationId
196
- }
197
-
198
- /**
199
- * Generate OpenAPI parameters from route path
200
- */
201
- function generateParameters(route: any): any[] {
202
- const parameters = []
203
-
204
- // Path parameters
205
- const pathParams = route.path.match(/\{([^}]+)\}/g)
206
- if (pathParams) {
207
- for (const param of pathParams) {
208
- const paramName = param.slice(1, -1)
209
- parameters.push({
210
- name: paramName,
211
- in: 'path',
212
- required: true,
213
- schema: {
214
- type: 'string',
215
- },
216
- description: `Path parameter: ${paramName}`,
217
- })
218
- }
219
- }
220
-
221
- // Add query parameters if defined in the route
222
- if (route.params && route.params.query) {
223
- for (const [name, schema] of Object.entries(route.params.query)) {
224
- parameters.push({
225
- name,
226
- in: 'query',
227
- schema: mapSchemaType(schema as any),
228
- required: (schema as any).required === true,
229
- description: (schema as any).description || `Query parameter: ${name}`,
230
- })
231
- }
232
- }
233
-
234
- return parameters
235
- }
236
-
237
- /**
238
- * Map schema type to OpenAPI schema
239
- */
240
- function mapSchemaType(schema: any): any {
241
- if (!schema || typeof schema !== 'object') {
242
- return { type: 'string' }
243
- }
244
-
245
- const openApiSchema: any = {}
246
-
247
- if (schema.type) {
248
- openApiSchema.type = schema.type
249
- }
250
-
251
- if (schema.format) {
252
- openApiSchema.format = schema.format
253
- }
254
-
255
- if (schema.enum) {
256
- openApiSchema.enum = schema.enum
257
- }
258
-
259
- if (schema.default !== undefined) {
260
- openApiSchema.default = schema.default
261
- }
262
-
263
- return openApiSchema
264
- }
265
-
266
- /**
267
- * Generate responses object for OpenAPI
268
- */
269
- function generateResponses(route: any): any {
270
- const responses: any = {
271
- 200: {
272
- description: 'Successful response',
273
- content: {
274
- 'application/json': {
275
- schema: {},
276
- },
277
- },
278
- },
279
- }
280
-
281
- // Add common error responses
282
- responses['400'] = {
283
- description: 'Bad request',
284
- content: {
285
- 'application/json': {
286
- schema: {
287
- type: 'object',
288
- properties: {
289
- error: { type: 'string' },
290
- },
291
- },
292
- },
293
- },
294
- }
295
-
296
- responses['401'] = {
297
- description: 'Unauthorized',
298
- content: {
299
- 'application/json': {
300
- schema: {
301
- type: 'object',
302
- properties: {
303
- error: { type: 'string' },
304
- },
305
- },
306
- },
307
- },
308
- }
309
-
310
- responses['404'] = {
311
- description: 'Not found',
312
- content: {
313
- 'application/json': {
314
- schema: {
315
- type: 'object',
316
- properties: {
317
- error: { type: 'string' },
318
- },
319
- },
320
- },
321
- },
322
- }
323
-
324
- responses['500'] = {
325
- description: 'Server error',
326
- content: {
327
- 'application/json': {
328
- schema: {
329
- type: 'object',
330
- properties: {
331
- error: { type: 'string' },
332
- },
333
- },
334
- },
335
- },
336
- }
337
-
338
- // Add custom responses from route if available
339
- if (route.responses) {
340
- for (const [status, response] of Object.entries(route.responses)) {
341
- responses[status] = {
342
- description: (response as any).description || `Response with status ${status}`,
343
- content: {
344
- 'application/json': {
345
- schema: (response as any).schema || {},
346
- },
347
- },
348
- }
349
- }
350
- }
351
-
352
- return responses
353
- }
354
-
355
- /**
356
- * Generate request body object for OpenAPI
357
- */
358
- function generateRequestBody(route: any): any {
359
- // Default empty request body
360
- const requestBody: any = {
361
- required: true,
362
- content: {
363
- 'application/json': {
364
- schema: {
365
- type: 'object',
366
- },
367
- },
368
- },
369
- }
370
-
371
- // Add schema from route if available
372
- if (route.params && route.params.body) {
373
- requestBody.content['application/json'].schema = route.params.body
374
- }
375
-
376
- return requestBody
377
- }
378
-
379
- /**
380
- * Extract security requirements from middleware
381
- */
382
- function extractSecurityFromMiddleware(middleware: any[]): any[] {
383
- const securityItems = []
384
-
385
- // Check for common auth middleware like jwt, oauth, apiKey, etc.
386
- for (const mw of middleware) {
387
- const name = typeof mw === 'string' ? mw : mw.name
388
-
389
- if (name && name.toLowerCase().includes('auth')) {
390
- // Add security scheme based on middleware type
391
- if (name.toLowerCase().includes('jwt')) {
392
- securityItems.push({ BearerAuth: [] })
393
- }
394
- else if (name.toLowerCase().includes('apikey')) {
395
- securityItems.push({ ApiKeyAuth: [] })
396
- }
397
- else if (name.toLowerCase().includes('basic')) {
398
- securityItems.push({ BasicAuth: [] })
399
- }
400
- else {
401
- securityItems.push({ [name]: [] })
402
- }
403
- }
404
- }
405
-
406
- return securityItems
407
- }
package/src/cli/router.ts DELETED
@@ -1,188 +0,0 @@
1
- import fs from 'node:fs/promises'
2
- import path from 'node:path'
3
- import process from 'node:process'
4
- import { chalk } from './colors'
5
-
6
- /**
7
- * Options for the router:types command
8
- */
9
- export interface GenerateRouterTypesOptions {
10
- output?: string
11
- watch?: boolean
12
- }
13
-
14
- /**
15
- * Generate TypeScript types for router extensions
16
- */
17
- export async function generateRouterTypes(outputPath: string): Promise<void> {
18
- try {
19
- // Try to load router instance from the application
20
- const routesFile = `${process.cwd()}/routes/index.ts`
21
- const { router } = await import(routesFile)
22
-
23
- if (!router) {
24
- console.error(chalk.red(`Could not find router instance in ${routesFile}.`))
25
- process.exit(1)
26
- }
27
-
28
- // Get router methods excluding internal properties
29
- const routerMethods = Object.getOwnPropertyNames(Object.getPrototypeOf(router))
30
- .filter(method =>
31
- !method.startsWith('_')
32
- && method !== 'constructor'
33
- && typeof (router as any)[method] === 'function',
34
- )
35
-
36
- // Get custom properties that were added to router
37
- const extendedProperties = Object.keys(router)
38
- .filter(prop =>
39
- !['routes', 'middleware', 'hooks', 'config'].includes(prop)
40
- && typeof prop === 'string'
41
- && !prop.startsWith('_'),
42
- )
43
-
44
- // Generate type definition
45
- const typeContent = `/**
46
- * This file is auto-generated.
47
- * DO NOT EDIT THIS FILE DIRECTLY.
48
- * To update, run 'bun router router:types'
49
- */
50
- import type { Router, RouteHandler, MiddlewareHandler, RouteDefinition } from '../src/types'
51
-
52
- /**
53
- * Extended Router interface with all available methods and properties
54
- */
55
- export interface ExtendedRouter extends Router {
56
- ${routerMethods.map((method) => {
57
- // Provide basic type definitions for common router methods
58
- if (['get', 'post', 'put', 'delete', 'patch', 'options', 'head'].includes(method)) {
59
- return `${method}(path: string, handler: RouteHandler, options?: any): Router`
60
- }
61
- else if (method === 'use') {
62
- return `${method}(middleware: MiddlewareHandler | MiddlewareHandler[]): Router`
63
- }
64
- else if (method === 'group') {
65
- return `${method}(prefix: string, callback: (router: Router) => void): Router`
66
- }
67
- else {
68
- return `${method}: Function`
69
- }
70
- }).join('\n ')}
71
-
72
- ${extendedProperties.map((prop) => {
73
- const propType = typeof (router as any)[prop]
74
- return `${prop}: ${propType}`
75
- }).join('\n ')}
76
- }
77
-
78
- /**
79
- * Type for extending the router with custom methods
80
- */
81
- export type RouterExtension = {
82
- [key: string]: Function | any
83
- }
84
-
85
- /**
86
- * Helper type for correctly typing router extensions
87
- *
88
- * Example usage:
89
- * \`\`\`typescript
90
- * import { Router, ExtendRouter } from './router-types'
91
- *
92
- * // Define extension type
93
- * interface MyCustomRouter extends ExtendRouter<{
94
- * customMethod(param: string): void
95
- * }> {}
96
- *
97
- * // Create and extend the router
98
- * const router = new Router() as MyCustomRouter
99
- * router.customMethod = (param: string) => {
100
- * console.log(param)
101
- * }
102
- * \`\`\`
103
- */
104
- export type ExtendRouter<T extends RouterExtension> = Router & T
105
- `
106
-
107
- // Write to file
108
- await fs.writeFile(outputPath, typeContent)
109
- console.log(chalk.green(`✨ Router types generated at ${outputPath}`))
110
- }
111
- catch (error: any) {
112
- if (error.code === 'ERR_MODULE_NOT_FOUND') {
113
- console.error(chalk.red(`Routes file not found at ${process.cwd()}/routes/index.ts`))
114
- console.error(chalk.yellow('Make sure your routes are defined and exported as "router" in routes/index.ts'))
115
- }
116
- else {
117
- console.error(chalk.red(`Error generating router types: ${error.message}`))
118
- }
119
- throw error
120
- }
121
- }
122
-
123
- /**
124
- * Watch for changes in router files and regenerate types
125
- */
126
- export async function watchRouterFiles(outputPath: string): Promise<void> {
127
- const routerFiles = [
128
- path.join(process.cwd(), 'routes'),
129
- path.join(process.cwd(), 'src'),
130
- ]
131
-
132
- // Check if at least one directory exists
133
- let validPath = false
134
- for (const dirPath of routerFiles) {
135
- try {
136
- await fs.access(dirPath)
137
- validPath = true
138
- break
139
- }
140
- catch {
141
- // Continue checking other paths
142
- }
143
- }
144
-
145
- if (!validPath) {
146
- console.error(chalk.red(`No valid source directories found to watch.`))
147
- process.exit(1)
148
- }
149
-
150
- // Initial generation
151
- await generateRouterTypes(outputPath)
152
- console.log(chalk.green(`✨ Router types generated at ${outputPath}`))
153
- console.log(chalk.blue(`Watching for changes in router files...`))
154
-
155
- // Setup file watchers for all paths
156
- let timeoutId: ReturnType<typeof setTimeout> | null = null
157
-
158
- for (const dirPath of routerFiles) {
159
- try {
160
- const watcher = fs.watch(dirPath, { recursive: true })
161
-
162
- // Using IIFE to create separate async context for each watcher
163
- ;(async () => {
164
- for await (const _event of watcher) {
165
- // Debounce to avoid multiple regenerations when multiple files change
166
- if (timeoutId) {
167
- clearTimeout(timeoutId)
168
- }
169
-
170
- timeoutId = setTimeout(async () => {
171
- console.log(chalk.yellow(`Changes detected in router files. Regenerating types...`))
172
- try {
173
- await generateRouterTypes(outputPath)
174
- console.log(chalk.green(`✨ Router types regenerated at ${outputPath}`))
175
- }
176
- catch (error: any) {
177
- console.error(chalk.red(`Error regenerating types: ${error.message}`))
178
- }
179
- timeoutId = null
180
- }, 500)
181
- }
182
- })()
183
- }
184
- catch {
185
- // Skip if this path doesn't exist
186
- }
187
- }
188
- }