@stacksjs/bun-router 0.0.5 → 0.0.6
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/auth.d.ts +134 -0
- package/dist/cache/lru-cache.d.ts +139 -0
- package/dist/cache/middleware-memoization.d.ts +151 -0
- package/dist/cache/route-cache-warmer.d.ts +161 -0
- package/dist/cache/sqlite-cache.d.ts +209 -0
- package/dist/cache/streaming-cache.d.ts +132 -0
- package/dist/cli/colors.d.ts +15 -0
- package/dist/cli/index.d.ts +10 -0
- package/dist/cli/middleware.d.ts +31 -0
- package/dist/cli/openapi.d.ts +17 -0
- package/dist/cli/router.d.ts +15 -0
- package/dist/cli/routes.d.ts +29 -0
- package/dist/cli/utils.d.ts +74 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +1 -1
- package/dist/config.d.ts +7 -0
- package/dist/container/container.d.ts +273 -0
- package/dist/container/contextual-binding.d.ts +240 -0
- package/dist/container/decorators.d.ts +141 -0
- package/dist/container/service-provider.d.ts +285 -0
- package/dist/development/hot-reload.d.ts +187 -0
- package/dist/development/index.d.ts +167 -0
- package/dist/development/performance-profiler.d.ts +217 -0
- package/dist/development/route-debugger.d.ts +154 -0
- package/dist/development/route-inspector.d.ts +211 -0
- package/dist/development/typescript-utilities.d.ts +209 -0
- package/dist/docs.d.ts +11 -0
- package/dist/errors/circuit-breaker.d.ts +195 -0
- package/dist/errors/error-handler.d.ts +88 -0
- package/dist/errors/error-reporting.d.ts +144 -0
- package/dist/errors/exceptions.d.ts +184 -0
- package/dist/errors/graceful-degradation.d.ts +154 -0
- package/dist/errors/index.d.ts +9 -0
- package/dist/errors/router-errors.d.ts +110 -0
- package/dist/file-serving/static-files.d.ts +143 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +319 -205
- package/dist/middleware/auth.d.ts +63 -0
- package/dist/middleware/content_security_policy.d.ts +53 -0
- package/dist/middleware/cors.d.ts +5 -0
- package/dist/middleware/csrf.d.ts +7 -0
- package/dist/middleware/ddos_protection.d.ts +39 -0
- package/dist/middleware/file_security.d.ts +26 -0
- package/dist/middleware/file_upload.d.ts +38 -0
- package/dist/middleware/helmet.d.ts +54 -0
- package/dist/middleware/index.d.ts +44 -0
- package/dist/middleware/input_validation.d.ts +45 -0
- package/dist/middleware/json_body.d.ts +4 -0
- package/dist/middleware/performance_alerting.d.ts +87 -0
- package/dist/middleware/performance_dashboard.d.ts +87 -0
- package/dist/middleware/performance_monitor.d.ts +209 -0
- package/dist/middleware/pipeline.d.ts +131 -0
- package/dist/middleware/rate_limit.d.ts +36 -0
- package/dist/middleware/request_id.d.ts +4 -0
- package/dist/middleware/request_signing.d.ts +153 -0
- package/dist/middleware/request_tracer.d.ts +72 -0
- package/dist/middleware/response_cache.d.ts +97 -0
- package/dist/middleware/security.d.ts +69 -0
- package/dist/middleware/security_suite.d.ts +46 -0
- package/dist/middleware/session.d.ts +23 -0
- package/dist/model-binding/index.d.ts +2 -0
- package/dist/model-binding/model-middleware.d.ts +118 -0
- package/dist/model-binding/model-registry.d.ts +164 -0
- package/dist/model-binding.d.ts +185 -0
- package/dist/model-resolver-factory.d.ts +31 -0
- package/dist/observability/correlation.d.ts +187 -0
- package/dist/observability/health-checks.d.ts +186 -0
- package/dist/observability/index.d.ts +60 -0
- package/dist/observability/integration.d.ts +147 -0
- package/dist/observability/metrics.d.ts +184 -0
- package/dist/observability/tracing.d.ts +187 -0
- package/dist/optimization/bun-utilities.d.ts +224 -0
- package/dist/query-builder-integration.d.ts +36 -0
- package/dist/request/context.d.ts +34 -0
- package/dist/request/enhanced-request.d.ts +213 -0
- package/dist/request/macros.d.ts +300 -0
- package/dist/response/macros.d.ts +245 -0
- package/dist/response/response-factory.d.ts +133 -0
- package/dist/router/api-routes.d.ts +5 -0
- package/dist/router/file-based-routing.d.ts +118 -0
- package/dist/router/file-streaming.d.ts +5 -0
- package/dist/router/fluent-router.d.ts +315 -0
- package/dist/router/fluent-routing.d.ts +271 -0
- package/dist/router/group-organization.d.ts +5 -0
- package/dist/router/handler-resolver.d.ts +24 -0
- package/dist/router/http-methods.d.ts +10 -0
- package/dist/router/index.d.ts +113 -0
- package/dist/router/middleware-groups.d.ts +94 -0
- package/dist/router/middleware-integration.d.ts +112 -0
- package/dist/router/middleware.d.ts +5 -0
- package/dist/router/model-binding.d.ts +5 -0
- package/dist/router/optimized-route-matching.d.ts +6 -0
- package/dist/router/route-building.d.ts +5 -0
- package/dist/router/route-compiler.d.ts +153 -0
- package/dist/router/route-matching.d.ts +5 -0
- package/dist/router/route-trie.d.ts +114 -0
- package/dist/router/router.d.ts +276 -0
- package/dist/router/server.d.ts +5 -0
- package/dist/router/validation-integration.d.ts +170 -0
- package/dist/router/view-rendering.d.ts +5 -0
- package/dist/router/websocket.d.ts +5 -0
- package/dist/routing/route-caching.d.ts +129 -0
- package/dist/routing/route-throttling.d.ts +150 -0
- package/dist/routing/subdomain-routing.d.ts +206 -0
- package/dist/session/database-store.d.ts +51 -0
- package/dist/session/file-store.d.ts +22 -0
- package/dist/session/index.d.ts +106 -0
- package/dist/session/memory-store.d.ts +21 -0
- package/dist/session/redis-store.d.ts +31 -0
- package/dist/streaming/index.d.ts +3 -0
- package/dist/streaming/sse-handler.d.ts +138 -0
- package/dist/streaming/stream-handler.d.ts +114 -0
- package/dist/testing/auth-testing.d.ts +156 -0
- package/dist/testing/file-upload-testing.d.ts +190 -0
- package/dist/testing/index.d.ts +10 -0
- package/dist/testing/middleware-testing.d.ts +138 -0
- package/dist/testing/model-binding-testing.d.ts +186 -0
- package/dist/testing/performance-testing.d.ts +235 -0
- package/dist/testing/test-client.d.ts +117 -0
- package/dist/testing/test-request.d.ts +85 -0
- package/dist/testing/test-response.d.ts +90 -0
- package/dist/testing/types.d.ts +207 -0
- package/dist/testing/websocket-testing.d.ts +227 -0
- package/dist/types/controller-types.d.ts +208 -0
- package/dist/types/core.d.ts +543 -0
- package/dist/types/middleware-types.d.ts +227 -0
- package/dist/types/request-response-augmentation.d.ts +261 -0
- package/dist/types/route-inference.d.ts +168 -0
- package/dist/types.d.ts +1776 -0
- package/dist/url.d.ts +56 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/query-preservation.d.ts +48 -0
- package/dist/utils.d.ts +69 -0
- package/dist/validation/validator.d.ts +140 -0
- package/dist/websocket/clustering.d.ts +185 -0
- package/package.json +3 -2
- package/src/auth.ts +469 -0
- package/src/cache/lru-cache.ts +457 -0
- package/src/cache/middleware-memoization.ts +531 -0
- package/src/cache/route-cache-warmer.ts +486 -0
- package/src/cache/sqlite-cache.ts +783 -0
- package/src/cache/streaming-cache.ts +572 -0
- package/src/cli/colors.ts +29 -0
- package/src/cli/index.ts +287 -0
- package/src/cli/middleware.ts +291 -0
- package/src/cli/openapi.ts +407 -0
- package/src/cli/router.ts +188 -0
- package/src/cli/routes.ts +265 -0
- package/src/cli/utils.ts +531 -0
- package/src/cli.ts +5 -0
- package/src/config.ts +322 -0
- package/src/container/container.ts +740 -0
- package/src/container/contextual-binding.ts +603 -0
- package/src/container/decorators.ts +359 -0
- package/src/container/service-provider.ts +596 -0
- package/src/development/hot-reload.ts +673 -0
- package/src/development/index.ts +499 -0
- package/src/development/performance-profiler.ts +717 -0
- package/src/development/route-debugger.ts +527 -0
- package/src/development/route-inspector.ts +749 -0
- package/src/development/typescript-utilities.ts +678 -0
- package/src/docs.ts +397 -0
- package/src/errors/circuit-breaker.ts +732 -0
- package/src/errors/error-handler.ts +569 -0
- package/src/errors/error-reporting.ts +672 -0
- package/src/errors/exceptions.ts +536 -0
- package/src/errors/graceful-degradation.ts +621 -0
- package/src/errors/index.ts +21 -0
- package/src/errors/router-errors.ts +632 -0
- package/src/file-serving/static-files.ts +581 -0
- package/src/index.ts +14 -0
- package/src/middleware/auth.ts +219 -0
- package/src/middleware/content_security_policy.ts +215 -0
- package/src/middleware/cors.ts +74 -0
- package/src/middleware/csrf.ts +108 -0
- package/src/middleware/ddos_protection.ts +255 -0
- package/src/middleware/file_security.ts +191 -0
- package/src/middleware/file_upload.ts +264 -0
- package/src/middleware/helmet.ts +268 -0
- package/src/middleware/index.ts +117 -0
- package/src/middleware/input_validation.ts +449 -0
- package/src/middleware/json_body.ts +37 -0
- package/src/middleware/performance_alerting.ts +538 -0
- package/src/middleware/performance_dashboard.ts +661 -0
- package/src/middleware/performance_monitor.ts +943 -0
- package/src/middleware/pipeline.ts +489 -0
- package/src/middleware/rate_limit.ts +245 -0
- package/src/middleware/request_id.ts +36 -0
- package/src/middleware/request_signing.ts +636 -0
- package/src/middleware/request_tracer.ts +636 -0
- package/src/middleware/response_cache.ts +743 -0
- package/src/middleware/security.ts +482 -0
- package/src/middleware/security_suite.ts +257 -0
- package/src/middleware/session.ts +91 -0
- package/src/model-binding/index.ts +18 -0
- package/src/model-binding/model-middleware.ts +425 -0
- package/src/model-binding/model-registry.ts +550 -0
- package/src/model-binding.ts +370 -0
- package/src/model-resolver-factory.ts +106 -0
- package/src/observability/correlation.ts +691 -0
- package/src/observability/health-checks.ts +729 -0
- package/src/observability/index.ts +184 -0
- package/src/observability/integration.ts +548 -0
- package/src/observability/metrics.ts +753 -0
- package/src/observability/tracing.ts +638 -0
- package/src/optimization/bun-utilities.ts +778 -0
- package/src/query-builder-integration.ts +137 -0
- package/src/request/context.ts +62 -0
- package/src/request/enhanced-request.ts +857 -0
- package/src/request/macros.ts +688 -0
- package/src/response/macros.ts +612 -0
- package/src/response/response-factory.ts +588 -0
- package/src/router/api-routes.ts +243 -0
- package/src/router/file-based-routing.ts +670 -0
- package/src/router/file-streaming.ts +372 -0
- package/src/router/fluent-router.ts +927 -0
- package/src/router/fluent-routing.ts +797 -0
- package/src/router/group-organization.ts +213 -0
- package/src/router/handler-resolver.ts +291 -0
- package/src/router/http-methods.ts +377 -0
- package/src/router/index.ts +185 -0
- package/src/router/middleware-groups.ts +222 -0
- package/src/router/middleware-integration.ts +399 -0
- package/src/router/middleware.ts +231 -0
- package/src/router/model-binding.ts +215 -0
- package/src/router/optimized-route-matching.ts +253 -0
- package/src/router/route-building.ts +212 -0
- package/src/router/route-compiler.ts +702 -0
- package/src/router/route-matching.ts +349 -0
- package/src/router/route-trie.ts +464 -0
- package/src/router/router.ts +1508 -0
- package/src/router/server.ts +406 -0
- package/src/router/validation-integration.ts +443 -0
- package/src/router/view-rendering.ts +233 -0
- package/src/router/websocket.ts +100 -0
- package/src/routing/route-caching.ts +402 -0
- package/src/routing/route-throttling.ts +469 -0
- package/src/routing/subdomain-routing.ts +492 -0
- package/src/session/database-store.ts +109 -0
- package/src/session/file-store.ts +148 -0
- package/src/session/index.ts +244 -0
- package/src/session/memory-store.ts +88 -0
- package/src/session/redis-store.ts +93 -0
- package/src/streaming/index.ts +17 -0
- package/src/streaming/sse-handler.ts +482 -0
- package/src/streaming/stream-handler.ts +552 -0
- package/src/testing/auth-testing.ts +446 -0
- package/src/testing/file-upload-testing.ts +543 -0
- package/src/testing/index.ts +10 -0
- package/src/testing/middleware-testing.ts +322 -0
- package/src/testing/model-binding-testing.ts +645 -0
- package/src/testing/performance-testing.ts +738 -0
- package/src/testing/test-client.ts +310 -0
- package/src/testing/test-request.ts +315 -0
- package/src/testing/test-response.ts +332 -0
- package/src/testing/types.ts +224 -0
- package/src/testing/websocket-testing.ts +590 -0
- package/src/types/controller-types.ts +385 -0
- package/src/types/core.ts +683 -0
- package/src/types/middleware-types.ts +419 -0
- package/src/types/request-response-augmentation.ts +489 -0
- package/src/types/route-inference.ts +357 -0
- package/src/types.ts +2049 -0
- package/src/url.ts +126 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/query-preservation.ts +201 -0
- package/src/utils.ts +327 -0
- package/src/validation/validator.ts +685 -0
- package/src/websocket/clustering.ts +762 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request/Response Type Augmentation Based on Middleware
|
|
3
|
+
*
|
|
4
|
+
* Advanced TypeScript utilities for augmenting request/response types through middleware
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
AuthenticatedRequest,
|
|
9
|
+
AuthorizedRequest,
|
|
10
|
+
CachedRequest,
|
|
11
|
+
CORSRequest,
|
|
12
|
+
FileUploadRequest,
|
|
13
|
+
LoggedRequest,
|
|
14
|
+
RateLimitedRequest,
|
|
15
|
+
SessionRequest,
|
|
16
|
+
TypedMiddleware,
|
|
17
|
+
ValidatedRequest,
|
|
18
|
+
} from './middleware-types'
|
|
19
|
+
import type {
|
|
20
|
+
ExtractQueryParams,
|
|
21
|
+
ExtractTypedParams,
|
|
22
|
+
TypedRequest,
|
|
23
|
+
} from './route-inference'
|
|
24
|
+
|
|
25
|
+
// Base augmentation interface
|
|
26
|
+
export interface RequestAugmentation {
|
|
27
|
+
[key: string]: any
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ResponseAugmentation {
|
|
31
|
+
[key: string]: any
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Middleware augmentation extraction
|
|
35
|
+
export type ExtractMiddlewareAugmentation<T> =
|
|
36
|
+
T extends TypedMiddleware<infer Input, infer Output, infer Context, any>
|
|
37
|
+
? {
|
|
38
|
+
input: Input
|
|
39
|
+
output: Output
|
|
40
|
+
context: Context
|
|
41
|
+
augmentation: Output extends Input & infer Aug ? Aug : object
|
|
42
|
+
}
|
|
43
|
+
: never
|
|
44
|
+
|
|
45
|
+
// Request augmentation through middleware chain
|
|
46
|
+
export type AugmentRequestThroughChain<
|
|
47
|
+
TRequest,
|
|
48
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[],
|
|
49
|
+
> = TMiddlewares extends readonly [infer First, ...infer Rest]
|
|
50
|
+
? First extends TypedMiddleware<TRequest, infer Output, any, any>
|
|
51
|
+
? Rest extends readonly TypedMiddleware<any, any, any, any>[]
|
|
52
|
+
? AugmentRequestThroughChain<Output, Rest>
|
|
53
|
+
: Output
|
|
54
|
+
: TRequest
|
|
55
|
+
: TRequest
|
|
56
|
+
|
|
57
|
+
// Context accumulation through middleware chain
|
|
58
|
+
export type AccumulateContext<
|
|
59
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[],
|
|
60
|
+
> = TMiddlewares extends readonly [infer First, ...infer Rest]
|
|
61
|
+
? First extends TypedMiddleware<any, any, infer Context, any>
|
|
62
|
+
? Rest extends readonly TypedMiddleware<any, any, any, any>[]
|
|
63
|
+
? Context & AccumulateContext<Rest>
|
|
64
|
+
: Context
|
|
65
|
+
: object
|
|
66
|
+
: object
|
|
67
|
+
|
|
68
|
+
// Enhanced request with middleware augmentations
|
|
69
|
+
export type EnhancedRequest<
|
|
70
|
+
TPath extends string = string,
|
|
71
|
+
TQuery extends Record<string, any> = object,
|
|
72
|
+
TBody = unknown,
|
|
73
|
+
TContext = object,
|
|
74
|
+
TAugmentations = object,
|
|
75
|
+
> = TypedRequest<TPath, TQuery, TBody, TContext> & {
|
|
76
|
+
// Core request properties
|
|
77
|
+
params: ExtractTypedParams<TPath>
|
|
78
|
+
query: ExtractQueryParams<TQuery>
|
|
79
|
+
body: TBody
|
|
80
|
+
context: TContext & TAugmentations
|
|
81
|
+
|
|
82
|
+
// Middleware augmentations (conditionally added)
|
|
83
|
+
user?: TAugmentations extends { auth: any }
|
|
84
|
+
? TAugmentations['auth']['user']
|
|
85
|
+
: never
|
|
86
|
+
|
|
87
|
+
isAuthenticated?: TAugmentations extends { auth: any }
|
|
88
|
+
? boolean
|
|
89
|
+
: never
|
|
90
|
+
|
|
91
|
+
permissions?: TAugmentations extends { authz: any }
|
|
92
|
+
? TAugmentations['authz']['permissions']
|
|
93
|
+
: never
|
|
94
|
+
|
|
95
|
+
hasPermission?: TAugmentations extends { authz: any }
|
|
96
|
+
? (_permission: string) => boolean
|
|
97
|
+
: never
|
|
98
|
+
|
|
99
|
+
validatedParams?: TAugmentations extends { validation: any }
|
|
100
|
+
? TAugmentations['validation']['params']
|
|
101
|
+
: never
|
|
102
|
+
|
|
103
|
+
validatedQuery?: TAugmentations extends { validation: any }
|
|
104
|
+
? TAugmentations['validation']['query']
|
|
105
|
+
: never
|
|
106
|
+
|
|
107
|
+
validatedBody?: TAugmentations extends { validation: any }
|
|
108
|
+
? TAugmentations['validation']['body']
|
|
109
|
+
: never
|
|
110
|
+
|
|
111
|
+
rateLimit?: TAugmentations extends { rateLimit: any }
|
|
112
|
+
? TAugmentations['rateLimit']
|
|
113
|
+
: never
|
|
114
|
+
|
|
115
|
+
cache?: TAugmentations extends { cache: any }
|
|
116
|
+
? TAugmentations['cache']
|
|
117
|
+
: never
|
|
118
|
+
|
|
119
|
+
requestId?: TAugmentations extends { logging: any }
|
|
120
|
+
? string
|
|
121
|
+
: never
|
|
122
|
+
|
|
123
|
+
logger?: TAugmentations extends { logging: any }
|
|
124
|
+
? TAugmentations['logging']['logger']
|
|
125
|
+
: never
|
|
126
|
+
|
|
127
|
+
cors?: TAugmentations extends { cors: any }
|
|
128
|
+
? TAugmentations['cors']
|
|
129
|
+
: never
|
|
130
|
+
|
|
131
|
+
files?: TAugmentations extends { upload: any }
|
|
132
|
+
? TAugmentations['upload']['files']
|
|
133
|
+
: never
|
|
134
|
+
|
|
135
|
+
session?: TAugmentations extends { session: any }
|
|
136
|
+
? TAugmentations['session']
|
|
137
|
+
: never
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Enhanced response with middleware augmentations
|
|
141
|
+
export interface EnhancedResponse<TAugmentations = object> extends Response {
|
|
142
|
+
// Middleware-added properties
|
|
143
|
+
cached?: TAugmentations extends { cache: any }
|
|
144
|
+
? boolean
|
|
145
|
+
: never
|
|
146
|
+
|
|
147
|
+
rateLimited?: TAugmentations extends { rateLimit: any }
|
|
148
|
+
? boolean
|
|
149
|
+
: never
|
|
150
|
+
|
|
151
|
+
requestId?: TAugmentations extends { logging: any }
|
|
152
|
+
? string
|
|
153
|
+
: never
|
|
154
|
+
|
|
155
|
+
executionTime?: TAugmentations extends { logging: any }
|
|
156
|
+
? number
|
|
157
|
+
: never
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Middleware-aware route handler
|
|
161
|
+
export type AugmentedRouteHandler<
|
|
162
|
+
TPath extends string,
|
|
163
|
+
TQuery extends Record<string, any> = object,
|
|
164
|
+
TBody = unknown,
|
|
165
|
+
TContext = object,
|
|
166
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[] = [],
|
|
167
|
+
> = (
|
|
168
|
+
request: EnhancedRequest<
|
|
169
|
+
TPath,
|
|
170
|
+
TQuery,
|
|
171
|
+
TBody,
|
|
172
|
+
TContext,
|
|
173
|
+
AccumulateContext<TMiddlewares>
|
|
174
|
+
>
|
|
175
|
+
) => Promise<EnhancedResponse<AccumulateContext<TMiddlewares>>> | EnhancedResponse<AccumulateContext<TMiddlewares>>
|
|
176
|
+
|
|
177
|
+
// Type-safe middleware application
|
|
178
|
+
export type ApplyMiddleware<
|
|
179
|
+
TRequest,
|
|
180
|
+
TMiddleware extends TypedMiddleware<any, any, any, any>,
|
|
181
|
+
> = TMiddleware extends TypedMiddleware<TRequest, infer Output, infer Context, any>
|
|
182
|
+
? {
|
|
183
|
+
request: Output
|
|
184
|
+
context: Context
|
|
185
|
+
}
|
|
186
|
+
: never
|
|
187
|
+
|
|
188
|
+
// Sequential middleware application
|
|
189
|
+
export type ApplyMiddlewareSequence<
|
|
190
|
+
TRequest,
|
|
191
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[],
|
|
192
|
+
> = TMiddlewares extends readonly [infer First, ...infer Rest]
|
|
193
|
+
? First extends TypedMiddleware<TRequest, infer Output, infer Context, any>
|
|
194
|
+
? Rest extends readonly TypedMiddleware<any, any, any, any>[]
|
|
195
|
+
? ApplyMiddlewareSequence<Output, Rest> extends {
|
|
196
|
+
request: infer FinalRequest
|
|
197
|
+
context: infer RestContext
|
|
198
|
+
}
|
|
199
|
+
? {
|
|
200
|
+
request: FinalRequest
|
|
201
|
+
context: Context & RestContext
|
|
202
|
+
}
|
|
203
|
+
: {
|
|
204
|
+
request: Output
|
|
205
|
+
context: Context
|
|
206
|
+
}
|
|
207
|
+
: {
|
|
208
|
+
request: Output
|
|
209
|
+
context: Context
|
|
210
|
+
}
|
|
211
|
+
: {
|
|
212
|
+
request: TRequest
|
|
213
|
+
context: object
|
|
214
|
+
}
|
|
215
|
+
: {
|
|
216
|
+
request: TRequest
|
|
217
|
+
context: object
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Conditional augmentation based on middleware presence
|
|
221
|
+
export type ConditionalAugmentation<
|
|
222
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[],
|
|
223
|
+
TAugmentation,
|
|
224
|
+
> = TMiddlewares extends readonly []
|
|
225
|
+
? object
|
|
226
|
+
: TAugmentation
|
|
227
|
+
|
|
228
|
+
// Authentication augmentation
|
|
229
|
+
export type AuthAugmentation<TUser = any> = ConditionalAugmentation<
|
|
230
|
+
[TypedMiddleware<any, any, { auth: { user: TUser } }, any>],
|
|
231
|
+
AuthenticatedRequest<TUser>
|
|
232
|
+
>
|
|
233
|
+
|
|
234
|
+
// Authorization augmentation
|
|
235
|
+
export type AuthzAugmentation<TPermissions extends string = string> = ConditionalAugmentation<
|
|
236
|
+
[TypedMiddleware<any, any, { authz: { permissions: TPermissions[] } }, any>],
|
|
237
|
+
AuthorizedRequest<TPermissions>
|
|
238
|
+
>
|
|
239
|
+
|
|
240
|
+
// Validation augmentation
|
|
241
|
+
export type ValidationAugmentation<TParams = any, TQuery = any, TBody = any> = ConditionalAugmentation<
|
|
242
|
+
[TypedMiddleware<any, any, { validation: { params: TParams, query: TQuery, body: TBody } }, any>],
|
|
243
|
+
ValidatedRequest<TParams, TQuery, TBody>
|
|
244
|
+
>
|
|
245
|
+
|
|
246
|
+
// Rate limiting augmentation
|
|
247
|
+
export type RateLimitAugmentation = ConditionalAugmentation<
|
|
248
|
+
[TypedMiddleware<any, any, { rateLimit: RateLimitedRequest['rateLimit'] }, any>],
|
|
249
|
+
RateLimitedRequest
|
|
250
|
+
>
|
|
251
|
+
|
|
252
|
+
// Caching augmentation
|
|
253
|
+
export type CacheAugmentation = ConditionalAugmentation<
|
|
254
|
+
[TypedMiddleware<any, any, { cache: CachedRequest['cache'] }, any>],
|
|
255
|
+
CachedRequest
|
|
256
|
+
>
|
|
257
|
+
|
|
258
|
+
// Logging augmentation
|
|
259
|
+
export type LoggingAugmentation = ConditionalAugmentation<
|
|
260
|
+
[TypedMiddleware<any, any, { logging: { requestId: string, logger: LoggedRequest['logger'] } }, any>],
|
|
261
|
+
LoggedRequest
|
|
262
|
+
>
|
|
263
|
+
|
|
264
|
+
// CORS augmentation
|
|
265
|
+
export type CORSAugmentation = ConditionalAugmentation<
|
|
266
|
+
[TypedMiddleware<any, any, { cors: CORSRequest['cors'] }, any>],
|
|
267
|
+
CORSRequest
|
|
268
|
+
>
|
|
269
|
+
|
|
270
|
+
// File upload augmentation
|
|
271
|
+
export type FileUploadAugmentation<TFiles extends Record<string, any> = Record<string, File>> = ConditionalAugmentation<
|
|
272
|
+
[TypedMiddleware<any, any, { upload: { files: TFiles } }, any>],
|
|
273
|
+
FileUploadRequest<TFiles>
|
|
274
|
+
>
|
|
275
|
+
|
|
276
|
+
// Session augmentation
|
|
277
|
+
export type SessionAugmentation<TSession = any> = ConditionalAugmentation<
|
|
278
|
+
[TypedMiddleware<any, any, { session: SessionRequest<TSession>['session'] }, any>],
|
|
279
|
+
SessionRequest<TSession>
|
|
280
|
+
>
|
|
281
|
+
|
|
282
|
+
// Combined augmentations
|
|
283
|
+
export type CombineAugmentations<T extends Record<string, any>[]> = T extends [
|
|
284
|
+
infer First,
|
|
285
|
+
...infer Rest,
|
|
286
|
+
]
|
|
287
|
+
? First extends Record<string, any>
|
|
288
|
+
? Rest extends Record<string, any>[]
|
|
289
|
+
? First & CombineAugmentations<Rest>
|
|
290
|
+
: First
|
|
291
|
+
: object
|
|
292
|
+
: object
|
|
293
|
+
|
|
294
|
+
// Middleware type detection
|
|
295
|
+
export type HasAuthMiddleware<T extends readonly TypedMiddleware<any, any, any, any>[]> =
|
|
296
|
+
T extends readonly [infer First, ...infer Rest]
|
|
297
|
+
? First extends TypedMiddleware<any, any, { auth: any }, any>
|
|
298
|
+
? true
|
|
299
|
+
: Rest extends readonly TypedMiddleware<any, any, any, any>[]
|
|
300
|
+
? HasAuthMiddleware<Rest>
|
|
301
|
+
: false
|
|
302
|
+
: false
|
|
303
|
+
|
|
304
|
+
export type HasValidationMiddleware<T extends readonly TypedMiddleware<any, any, any, any>[]> =
|
|
305
|
+
T extends readonly [infer First, ...infer Rest]
|
|
306
|
+
? First extends TypedMiddleware<any, any, { validation: any }, any>
|
|
307
|
+
? true
|
|
308
|
+
: Rest extends readonly TypedMiddleware<any, any, any, any>[]
|
|
309
|
+
? HasValidationMiddleware<Rest>
|
|
310
|
+
: false
|
|
311
|
+
: false
|
|
312
|
+
|
|
313
|
+
export type HasRateLimitMiddleware<T extends readonly TypedMiddleware<any, any, any, any>[]> =
|
|
314
|
+
T extends readonly [infer First, ...infer Rest]
|
|
315
|
+
? First extends TypedMiddleware<any, any, { rateLimit: any }, any>
|
|
316
|
+
? true
|
|
317
|
+
: Rest extends readonly TypedMiddleware<any, any, any, any>[]
|
|
318
|
+
? HasRateLimitMiddleware<Rest>
|
|
319
|
+
: false
|
|
320
|
+
: false
|
|
321
|
+
|
|
322
|
+
// Augmentation builder
|
|
323
|
+
export interface AugmentationBuilder<TBase = Request> {
|
|
324
|
+
withAuth: <TUser>(
|
|
325
|
+
middleware: TypedMiddleware<any, any, { auth: { user: TUser } }, any>
|
|
326
|
+
) => AugmentationBuilder<TBase & AuthenticatedRequest<TUser>>
|
|
327
|
+
|
|
328
|
+
withValidation: <TParams, TQuery, TBody>(
|
|
329
|
+
middleware: TypedMiddleware<any, any, { validation: { params: TParams, query: TQuery, body: TBody } }, any>
|
|
330
|
+
) => AugmentationBuilder<TBase & ValidatedRequest<TParams, TQuery, TBody>>
|
|
331
|
+
|
|
332
|
+
withRateLimit: (
|
|
333
|
+
middleware: TypedMiddleware<any, any, { rateLimit: RateLimitedRequest['rateLimit'] }, any>
|
|
334
|
+
) => AugmentationBuilder<TBase & RateLimitedRequest>
|
|
335
|
+
|
|
336
|
+
withCache: (
|
|
337
|
+
middleware: TypedMiddleware<any, any, { cache: CachedRequest['cache'] }, any>
|
|
338
|
+
) => AugmentationBuilder<TBase & CachedRequest>
|
|
339
|
+
|
|
340
|
+
withLogging: (
|
|
341
|
+
middleware: TypedMiddleware<any, any, { logging: { requestId: string, logger: LoggedRequest['logger'] } }, any>
|
|
342
|
+
) => AugmentationBuilder<TBase & LoggedRequest>
|
|
343
|
+
|
|
344
|
+
withCORS: (
|
|
345
|
+
middleware: TypedMiddleware<any, any, { cors: CORSRequest['cors'] }, any>
|
|
346
|
+
) => AugmentationBuilder<TBase & CORSRequest>
|
|
347
|
+
|
|
348
|
+
withFileUpload: <TFiles extends Record<string, any>>(
|
|
349
|
+
middleware: TypedMiddleware<any, any, { upload: { files: TFiles } }, any>
|
|
350
|
+
) => AugmentationBuilder<TBase & FileUploadRequest<TFiles>>
|
|
351
|
+
|
|
352
|
+
withSession: <TSession>(
|
|
353
|
+
middleware: TypedMiddleware<any, any, { session: SessionRequest<TSession>['session'] }, any>
|
|
354
|
+
) => AugmentationBuilder<TBase & SessionRequest<TSession>>
|
|
355
|
+
|
|
356
|
+
build: () => TBase
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Route with augmented types
|
|
360
|
+
export interface AugmentedRoute<
|
|
361
|
+
TPath extends string,
|
|
362
|
+
TQuery extends Record<string, any> = object,
|
|
363
|
+
TBody = unknown,
|
|
364
|
+
TContext = object,
|
|
365
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[] = [],
|
|
366
|
+
> {
|
|
367
|
+
method: string
|
|
368
|
+
path: TPath
|
|
369
|
+
handler: AugmentedRouteHandler<TPath, TQuery, TBody, TContext, TMiddlewares>
|
|
370
|
+
middleware: TMiddlewares
|
|
371
|
+
augmentedRequest: EnhancedRequest<TPath, TQuery, TBody, TContext, AccumulateContext<TMiddlewares>>
|
|
372
|
+
augmentedResponse: EnhancedResponse<AccumulateContext<TMiddlewares>>
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Type-safe route builder with augmentation
|
|
376
|
+
export interface AugmentedRouteBuilder<TContext = object> {
|
|
377
|
+
get: <
|
|
378
|
+
TPath extends string,
|
|
379
|
+
TQuery extends Record<string, any> = object,
|
|
380
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[] = [],
|
|
381
|
+
>(
|
|
382
|
+
path: TPath,
|
|
383
|
+
middleware: TMiddlewares,
|
|
384
|
+
handler: AugmentedRouteHandler<TPath, TQuery, never, TContext, TMiddlewares>
|
|
385
|
+
) => AugmentedRoute<TPath, TQuery, never, TContext, TMiddlewares>
|
|
386
|
+
|
|
387
|
+
post: <
|
|
388
|
+
TPath extends string,
|
|
389
|
+
TQuery extends Record<string, any> = object,
|
|
390
|
+
TBody = unknown,
|
|
391
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[] = [],
|
|
392
|
+
>(
|
|
393
|
+
path: TPath,
|
|
394
|
+
middleware: TMiddlewares,
|
|
395
|
+
handler: AugmentedRouteHandler<TPath, TQuery, TBody, TContext, TMiddlewares>
|
|
396
|
+
) => AugmentedRoute<TPath, TQuery, TBody, TContext, TMiddlewares>
|
|
397
|
+
|
|
398
|
+
put: <
|
|
399
|
+
TPath extends string,
|
|
400
|
+
TQuery extends Record<string, any> = object,
|
|
401
|
+
TBody = unknown,
|
|
402
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[] = [],
|
|
403
|
+
>(
|
|
404
|
+
path: TPath,
|
|
405
|
+
middleware: TMiddlewares,
|
|
406
|
+
handler: AugmentedRouteHandler<TPath, TQuery, TBody, TContext, TMiddlewares>
|
|
407
|
+
) => AugmentedRoute<TPath, TQuery, TBody, TContext, TMiddlewares>
|
|
408
|
+
|
|
409
|
+
patch: <
|
|
410
|
+
TPath extends string,
|
|
411
|
+
TQuery extends Record<string, any> = object,
|
|
412
|
+
TBody = unknown,
|
|
413
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[] = [],
|
|
414
|
+
>(
|
|
415
|
+
path: TPath,
|
|
416
|
+
middleware: TMiddlewares,
|
|
417
|
+
handler: AugmentedRouteHandler<TPath, TQuery, TBody, TContext, TMiddlewares>
|
|
418
|
+
) => AugmentedRoute<TPath, TQuery, TBody, TContext, TMiddlewares>
|
|
419
|
+
|
|
420
|
+
delete: <
|
|
421
|
+
TPath extends string,
|
|
422
|
+
TQuery extends Record<string, any> = object,
|
|
423
|
+
TMiddlewares extends readonly TypedMiddleware<any, any, any, any>[] = [],
|
|
424
|
+
>(
|
|
425
|
+
path: TPath,
|
|
426
|
+
middleware: TMiddlewares,
|
|
427
|
+
handler: AugmentedRouteHandler<TPath, TQuery, never, TContext, TMiddlewares>
|
|
428
|
+
) => AugmentedRoute<TPath, TQuery, never, TContext, TMiddlewares>
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// Augmentation validation
|
|
432
|
+
export type ValidateAugmentation<
|
|
433
|
+
TRequest,
|
|
434
|
+
TExpectedAugmentation,
|
|
435
|
+
> = TRequest extends TExpectedAugmentation
|
|
436
|
+
? true
|
|
437
|
+
: false
|
|
438
|
+
|
|
439
|
+
// Augmentation compatibility checking
|
|
440
|
+
export type AugmentationCompatible<
|
|
441
|
+
TMiddleware1 extends TypedMiddleware<any, any, any, any>,
|
|
442
|
+
TMiddleware2 extends TypedMiddleware<any, any, any, any>,
|
|
443
|
+
> = TMiddleware1 extends TypedMiddleware<any, infer Output1, any, any>
|
|
444
|
+
? TMiddleware2 extends TypedMiddleware<Output1, any, any, any>
|
|
445
|
+
? true
|
|
446
|
+
: false
|
|
447
|
+
: false
|
|
448
|
+
|
|
449
|
+
// Runtime augmentation helpers
|
|
450
|
+
export interface RuntimeAugmentation {
|
|
451
|
+
augmentRequest: <T>(request: Request, augmentation: T) => Request & T
|
|
452
|
+
extractAugmentation: <T>(request: Request & T) => T
|
|
453
|
+
hasAugmentation: <T>(request: Request, key: keyof T) => boolean
|
|
454
|
+
getAugmentation: <T, K extends keyof T>(request: Request & T, key: K) => T[K]
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// Augmentation metadata
|
|
458
|
+
export interface AugmentationMetadata {
|
|
459
|
+
name: string
|
|
460
|
+
version?: string
|
|
461
|
+
description?: string
|
|
462
|
+
dependencies?: string[]
|
|
463
|
+
provides: string[]
|
|
464
|
+
requires?: string[]
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Augmentation registry
|
|
468
|
+
export interface AugmentationRegistry {
|
|
469
|
+
register: (
|
|
470
|
+
name: string,
|
|
471
|
+
middleware: TypedMiddleware<any, any, any, any>,
|
|
472
|
+
metadata: AugmentationMetadata
|
|
473
|
+
) => void
|
|
474
|
+
|
|
475
|
+
get: (name: string) => {
|
|
476
|
+
middleware: TypedMiddleware<any, any, any, any>
|
|
477
|
+
metadata: AugmentationMetadata
|
|
478
|
+
} | undefined
|
|
479
|
+
|
|
480
|
+
resolve: (requirements: string[]) => TypedMiddleware<any, any, any, any>[]
|
|
481
|
+
|
|
482
|
+
validate: (middlewares: TypedMiddleware<any, any, any, any>[]) => {
|
|
483
|
+
valid: boolean
|
|
484
|
+
errors: string[]
|
|
485
|
+
warnings: string[]
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// Types are already exported above via individual export statements
|