@stacksjs/bun-router 0.0.14 → 0.0.15
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/README.md +261 -584
- package/bin/cli.ts +9 -0
- 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/chunk-j0e7z7hd.js +18053 -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 -0
- package/dist/cli.js +3360 -0
- 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 +14 -0
- package/dist/index.js +13968 -0
- 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 +256 -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 +114 -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 +317 -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 +1794 -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 +45 -44
- 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 +682 -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 +220 -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 +275 -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 +665 -0
- package/src/response/response-factory.ts +596 -0
- package/src/router/api-routes.ts +243 -0
- package/src/router/file-based-routing.ts +690 -0
- package/src/router/file-streaming.ts +383 -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 +187 -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 +221 -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 +1604 -0
- package/src/router/server.ts +462 -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 +2067 -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
- package/CHANGELOG.md +0 -770
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { EnhancedRequest, NextFunction } from '../types';
|
|
2
|
+
export interface SecurityOptions {
|
|
3
|
+
inputValidation?: {
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
maxBodySize?: number;
|
|
6
|
+
maxUrlLength?: number;
|
|
7
|
+
maxHeaderSize?: number;
|
|
8
|
+
allowedContentTypes?: string[];
|
|
9
|
+
sanitizeInput?: boolean;
|
|
10
|
+
validateEncoding?: boolean;
|
|
11
|
+
};
|
|
12
|
+
requestFiltering?: {
|
|
13
|
+
enabled?: boolean;
|
|
14
|
+
blockSuspiciousPatterns?: boolean;
|
|
15
|
+
maxRequestsPerMinute?: number;
|
|
16
|
+
blockUserAgents?: string[];
|
|
17
|
+
allowedMethods?: string[];
|
|
18
|
+
requireUserAgent?: boolean;
|
|
19
|
+
};
|
|
20
|
+
ipFiltering?: {
|
|
21
|
+
enabled?: boolean;
|
|
22
|
+
whitelist?: string[];
|
|
23
|
+
blacklist?: string[];
|
|
24
|
+
blockPrivateIPs?: boolean;
|
|
25
|
+
blockCloudProviders?: boolean;
|
|
26
|
+
geoBlocking?: {
|
|
27
|
+
enabled?: boolean;
|
|
28
|
+
allowedCountries?: string[];
|
|
29
|
+
blockedCountries?: string[];
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
attackPrevention?: {
|
|
33
|
+
enabled?: boolean;
|
|
34
|
+
sqlInjection?: boolean;
|
|
35
|
+
xss?: boolean;
|
|
36
|
+
pathTraversal?: boolean;
|
|
37
|
+
commandInjection?: boolean;
|
|
38
|
+
ldapInjection?: boolean;
|
|
39
|
+
xxe?: boolean;
|
|
40
|
+
customPatterns?: Array<{
|
|
41
|
+
name: string;
|
|
42
|
+
pattern: RegExp;
|
|
43
|
+
action: 'block' | 'log' | 'sanitize';
|
|
44
|
+
}>;
|
|
45
|
+
};
|
|
46
|
+
responseSecurity?: {
|
|
47
|
+
enabled?: boolean;
|
|
48
|
+
removeServerHeaders?: boolean;
|
|
49
|
+
addSecurityHeaders?: boolean;
|
|
50
|
+
sanitizeErrors?: boolean;
|
|
51
|
+
preventInfoDisclosure?: boolean;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export default class Security {
|
|
55
|
+
private options;
|
|
56
|
+
private suspiciousPatterns;
|
|
57
|
+
private sqlInjectionPatterns;
|
|
58
|
+
private xssPatterns;
|
|
59
|
+
private pathTraversalPatterns;
|
|
60
|
+
private commandInjectionPatterns;
|
|
61
|
+
constructor(options?: SecurityOptions);
|
|
62
|
+
private initializePatterns;
|
|
63
|
+
private getClientIP;
|
|
64
|
+
private isPrivateIP;
|
|
65
|
+
private validateInput;
|
|
66
|
+
private sanitizeInput;
|
|
67
|
+
handle(req: EnhancedRequest, next: NextFunction): Promise<Response>;
|
|
68
|
+
}
|
|
69
|
+
export declare function security(options?: SecurityOptions): (req: EnhancedRequest, next: NextFunction) => Promise<Response>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { EnhancedRequest, MiddlewareHandler, NextFunction } from '../types';
|
|
2
|
+
export interface SecuritySuiteOptions {
|
|
3
|
+
helmet?: boolean | object;
|
|
4
|
+
security?: boolean | object;
|
|
5
|
+
ddosProtection?: boolean | object;
|
|
6
|
+
inputValidation?: boolean | object;
|
|
7
|
+
contentSecurityPolicy?: boolean | object;
|
|
8
|
+
order?: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Comprehensive security middleware suite that combines multiple security layers
|
|
12
|
+
*/
|
|
13
|
+
export default class SecuritySuite {
|
|
14
|
+
private middlewares;
|
|
15
|
+
constructor(options?: SecuritySuiteOptions);
|
|
16
|
+
handle(req: EnhancedRequest, next: NextFunction): Promise<Response | null>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Factory function to create a security suite middleware
|
|
20
|
+
*/
|
|
21
|
+
export declare function securitySuite(options?: SecuritySuiteOptions): MiddlewareHandler;
|
|
22
|
+
/**
|
|
23
|
+
* Preset configurations for different security levels
|
|
24
|
+
*/
|
|
25
|
+
export declare const securityPresets: {
|
|
26
|
+
/**
|
|
27
|
+
* Basic security - minimal performance impact
|
|
28
|
+
*/
|
|
29
|
+
basic: () => MiddlewareHandler;
|
|
30
|
+
/**
|
|
31
|
+
* Standard security - balanced security and performance
|
|
32
|
+
*/
|
|
33
|
+
standard: () => MiddlewareHandler;
|
|
34
|
+
/**
|
|
35
|
+
* High security - maximum protection
|
|
36
|
+
*/
|
|
37
|
+
high: () => MiddlewareHandler;
|
|
38
|
+
/**
|
|
39
|
+
* API security - optimized for API endpoints
|
|
40
|
+
*/
|
|
41
|
+
api: () => MiddlewareHandler;
|
|
42
|
+
/**
|
|
43
|
+
* Development security - minimal restrictions for development
|
|
44
|
+
*/
|
|
45
|
+
development: () => MiddlewareHandler;
|
|
46
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { EnhancedRequest, NextFunction } from '../types';
|
|
2
|
+
import type { SessionStore, SessionData } from '../types/core';
|
|
3
|
+
/**
|
|
4
|
+
* Session middleware with pluggable store support.
|
|
5
|
+
*
|
|
6
|
+
* By default uses an in-memory store. Configure with a different store
|
|
7
|
+
* for persistent sessions (file, redis, database).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // Use default memory store
|
|
11
|
+
* const session = new Session()
|
|
12
|
+
*
|
|
13
|
+
* // Use a custom store
|
|
14
|
+
* import { RedisSessionStore } from 'bun-router/session'
|
|
15
|
+
* const session = new Session({ store: new RedisSessionStore(redisClient) })
|
|
16
|
+
*/
|
|
17
|
+
export default class Session {
|
|
18
|
+
private store;
|
|
19
|
+
constructor(options?: {
|
|
20
|
+
store?: SessionStore<SessionData>;
|
|
21
|
+
});
|
|
22
|
+
handle(req: EnhancedRequest, next: NextFunction): Promise<Response>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { bindModel, createModelBindingMiddleware, ModelBindingFactory, type ModelBindingMiddlewareOptions, ModelBindingUtils, TypedModelBinding, withModelBinding, } from './model-middleware';
|
|
2
|
+
export { type ModelBindingConfig, ModelFactory, ModelRegistry, modelRegistry, type ModelResolutionResult, ModelUtils, } from './model-registry';
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { EnhancedRequest, ExtractRouteParams } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Model binding middleware options
|
|
4
|
+
*/
|
|
5
|
+
export interface ModelBindingMiddlewareOptions {
|
|
6
|
+
failOnError?: boolean;
|
|
7
|
+
attachToRequest?: boolean;
|
|
8
|
+
paramMapping?: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Simplified model binding configuration for middleware
|
|
12
|
+
*/
|
|
13
|
+
export interface ModelBindingRef {
|
|
14
|
+
model: string;
|
|
15
|
+
parameter: string;
|
|
16
|
+
required?: boolean;
|
|
17
|
+
as?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create model binding middleware
|
|
21
|
+
*/
|
|
22
|
+
export declare function createModelBindingMiddleware(bindings: ModelBindingRef[], options?: ModelBindingMiddlewareOptions): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
23
|
+
/**
|
|
24
|
+
* Single model binding middleware
|
|
25
|
+
*/
|
|
26
|
+
export declare function bindModel(modelName: string, parameterName: string, options?: {
|
|
27
|
+
required?: boolean;
|
|
28
|
+
as?: string;
|
|
29
|
+
failOnError?: boolean;
|
|
30
|
+
}): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
31
|
+
/**
|
|
32
|
+
* Route parameter model binding decorator
|
|
33
|
+
*/
|
|
34
|
+
export declare function withModelBinding<TPath extends string>(bindings: ModelBindingRef[]): (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
35
|
+
/**
|
|
36
|
+
* Model binding utilities for middleware
|
|
37
|
+
*/
|
|
38
|
+
export declare const ModelBindingUtils: {
|
|
39
|
+
/**
|
|
40
|
+
* Create middleware for a specific model type
|
|
41
|
+
*/
|
|
42
|
+
forModel<T>(modelName: string, paramName: string, options?: {
|
|
43
|
+
validator?: (model: T) => boolean | Promise<boolean>;
|
|
44
|
+
transformer?: (model: T) => T | Promise<T>;
|
|
45
|
+
cache?: boolean;
|
|
46
|
+
}): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
47
|
+
/**
|
|
48
|
+
* Create middleware that binds multiple models
|
|
49
|
+
*/
|
|
50
|
+
multiple(bindings: Array<{
|
|
51
|
+
model: string;
|
|
52
|
+
param: string;
|
|
53
|
+
required?: boolean;
|
|
54
|
+
}>): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
55
|
+
/**
|
|
56
|
+
* Create middleware that validates model ownership
|
|
57
|
+
*/
|
|
58
|
+
requireOwnership<T extends {
|
|
59
|
+
userId?: string | number;
|
|
60
|
+
}>(modelName: string, userIdField?: string): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
61
|
+
/**
|
|
62
|
+
* Create middleware that caches resolved models
|
|
63
|
+
*/
|
|
64
|
+
withCache(modelName: string, paramName: string, _options?: {
|
|
65
|
+
ttl?: number;
|
|
66
|
+
keyGenerator?: (params: Record<string, string>) => string;
|
|
67
|
+
}): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
68
|
+
/**
|
|
69
|
+
* Create middleware that handles optional model binding
|
|
70
|
+
*/
|
|
71
|
+
optional(modelName: string, paramName: string): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Model binding middleware factory
|
|
75
|
+
*/
|
|
76
|
+
export declare class ModelBindingFactory {
|
|
77
|
+
/**
|
|
78
|
+
* Create binding for user model
|
|
79
|
+
*/
|
|
80
|
+
static user(paramName?: string, as?: string): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
81
|
+
/**
|
|
82
|
+
* Create binding for post model
|
|
83
|
+
*/
|
|
84
|
+
static post(paramName?: string, as?: string): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
85
|
+
/**
|
|
86
|
+
* Create binding for category model
|
|
87
|
+
*/
|
|
88
|
+
static category(paramName?: string, as?: string): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
89
|
+
/**
|
|
90
|
+
* Create binding for any resource with ownership validation
|
|
91
|
+
*/
|
|
92
|
+
static ownedResource(modelName: string, paramName: string, userIdField?: string): Array<(req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>>;
|
|
93
|
+
/**
|
|
94
|
+
* Create nested resource bindings (e.g., user -> posts)
|
|
95
|
+
*/
|
|
96
|
+
static nested(parentModel: string, parentParam: string, childModel: string, childParam: string): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Type-safe model binding helpers
|
|
100
|
+
*/
|
|
101
|
+
export declare const TypedModelBinding: {
|
|
102
|
+
/**
|
|
103
|
+
* Create a typed model binding that preserves route parameter types
|
|
104
|
+
*/
|
|
105
|
+
create<TPath extends string>(bindings: Array<{
|
|
106
|
+
model: string;
|
|
107
|
+
param: keyof ExtractRouteParams<TPath>;
|
|
108
|
+
required?: boolean;
|
|
109
|
+
as?: string;
|
|
110
|
+
}>): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
111
|
+
/**
|
|
112
|
+
* Create a single typed model binding
|
|
113
|
+
*/
|
|
114
|
+
single<TPath extends string>(modelName: string, param: keyof ExtractRouteParams<TPath>, options?: {
|
|
115
|
+
required?: boolean;
|
|
116
|
+
as?: string;
|
|
117
|
+
}): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
118
|
+
};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type { EnhancedRequest, ModelResolver, ModelTransformerFn, ModelValidatorFn } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Model resolution result
|
|
4
|
+
*/
|
|
5
|
+
export interface ModelResolutionResult<T = any> {
|
|
6
|
+
model: T | null;
|
|
7
|
+
error?: string;
|
|
8
|
+
status?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Model binding configuration for a specific model
|
|
12
|
+
*/
|
|
13
|
+
export interface ModelBindingConfig<T = any> {
|
|
14
|
+
resolver: ModelResolver<T>;
|
|
15
|
+
validator?: ModelValidatorFn<T>;
|
|
16
|
+
transformer?: ModelTransformerFn<T>;
|
|
17
|
+
cache?: {
|
|
18
|
+
enabled: boolean;
|
|
19
|
+
ttl: number;
|
|
20
|
+
key?: (params: Record<string, string>) => string;
|
|
21
|
+
};
|
|
22
|
+
errorHandling?: {
|
|
23
|
+
notFound?: (params: Record<string, string>) => Response;
|
|
24
|
+
validation?: (model: T, error: string) => Response;
|
|
25
|
+
transform?: (error: string) => Response;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Model registry for managing model bindings
|
|
30
|
+
*/
|
|
31
|
+
export declare class ModelRegistry {
|
|
32
|
+
private models;
|
|
33
|
+
private cache;
|
|
34
|
+
/**
|
|
35
|
+
* Register a model binding
|
|
36
|
+
*/
|
|
37
|
+
register<T>(name: string, config: ModelBindingConfig<T>): void;
|
|
38
|
+
/**
|
|
39
|
+
* Get model binding configuration
|
|
40
|
+
*/
|
|
41
|
+
get(name: string): ModelBindingConfig | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Check if model is registered
|
|
44
|
+
*/
|
|
45
|
+
has(name: string): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Resolve a model using its binding
|
|
48
|
+
*/
|
|
49
|
+
resolve<T>(modelName: string, params: Record<string, string>, req?: EnhancedRequest): Promise<ModelResolutionResult<T>>;
|
|
50
|
+
/**
|
|
51
|
+
* Create error response for model binding failure
|
|
52
|
+
*/
|
|
53
|
+
createErrorResponse(modelName: string, result: ModelResolutionResult, params: Record<string, string>): Response;
|
|
54
|
+
/**
|
|
55
|
+
* Clear cache for a specific model
|
|
56
|
+
*/
|
|
57
|
+
clearCache(modelName: string, params?: Record<string, string>): void;
|
|
58
|
+
/**
|
|
59
|
+
* Clear all cache
|
|
60
|
+
*/
|
|
61
|
+
clearAllCache(): void;
|
|
62
|
+
/**
|
|
63
|
+
* Get cache statistics
|
|
64
|
+
*/
|
|
65
|
+
getCacheStats(): {
|
|
66
|
+
totalEntries: number;
|
|
67
|
+
validEntries: number;
|
|
68
|
+
expiredEntries: number;
|
|
69
|
+
models: number;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Cleanup expired cache entries
|
|
73
|
+
*/
|
|
74
|
+
cleanupCache(): number;
|
|
75
|
+
/**
|
|
76
|
+
* Get cached model if available and valid
|
|
77
|
+
*/
|
|
78
|
+
private getCachedModel;
|
|
79
|
+
/**
|
|
80
|
+
* Cache a model
|
|
81
|
+
*/
|
|
82
|
+
private cacheModel;
|
|
83
|
+
/**
|
|
84
|
+
* Generate cache key for model
|
|
85
|
+
*/
|
|
86
|
+
private generateCacheKey;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Global model registry instance
|
|
90
|
+
*/
|
|
91
|
+
export declare const modelRegistry: ModelRegistry;
|
|
92
|
+
/**
|
|
93
|
+
* Model binding utilities
|
|
94
|
+
*/
|
|
95
|
+
export declare const ModelUtils: {
|
|
96
|
+
/**
|
|
97
|
+
* Create a simple resolver for database models
|
|
98
|
+
*/
|
|
99
|
+
dbResolver<T>(tableName: string, idField?: string, db?: any): ModelResolver<T>;
|
|
100
|
+
/**
|
|
101
|
+
* Create a validator that checks required fields
|
|
102
|
+
*/
|
|
103
|
+
requiredFieldsValidator<T extends Record<string, any>>(fields: (keyof T)[]): ModelValidatorFn<T>;
|
|
104
|
+
/**
|
|
105
|
+
* Create a transformer that picks specific fields
|
|
106
|
+
*/
|
|
107
|
+
pickFieldsTransformer<T, K extends keyof T>(fields: K[]): ModelTransformerFn<T>;
|
|
108
|
+
/**
|
|
109
|
+
* Create a transformer that excludes specific fields
|
|
110
|
+
*/
|
|
111
|
+
omitFieldsTransformer<T, K extends keyof T>(fields: K[]): ModelTransformerFn<T>;
|
|
112
|
+
/**
|
|
113
|
+
* Create a UUID validator
|
|
114
|
+
*/
|
|
115
|
+
uuidValidator<T extends {
|
|
116
|
+
id: string;
|
|
117
|
+
}>(): ModelValidatorFn<T>;
|
|
118
|
+
/**
|
|
119
|
+
* Create ownership validator (checks if user owns the model)
|
|
120
|
+
*/
|
|
121
|
+
ownershipValidator<T extends {
|
|
122
|
+
userId?: string | number;
|
|
123
|
+
}>(userIdField?: string): ModelValidatorFn<T>;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Common model binding factories
|
|
127
|
+
*/
|
|
128
|
+
export declare const ModelFactory: {
|
|
129
|
+
/**
|
|
130
|
+
* Create a simple model binding for database entities
|
|
131
|
+
*/
|
|
132
|
+
entity<T>(tableName: string, options?: {
|
|
133
|
+
idField?: string;
|
|
134
|
+
cache?: boolean;
|
|
135
|
+
cacheTtl?: number;
|
|
136
|
+
validator?: ModelValidatorFn<T>;
|
|
137
|
+
transformer?: ModelTransformerFn<T>;
|
|
138
|
+
db?: any;
|
|
139
|
+
}): ModelBindingConfig<T>;
|
|
140
|
+
/**
|
|
141
|
+
* Create a user model binding with ownership validation
|
|
142
|
+
*/
|
|
143
|
+
user<T extends {
|
|
144
|
+
id: string;
|
|
145
|
+
userId?: string;
|
|
146
|
+
}>(tableName?: string, options?: {
|
|
147
|
+
cache?: boolean;
|
|
148
|
+
cacheTtl?: number;
|
|
149
|
+
db?: any;
|
|
150
|
+
}): ModelBindingConfig<T>;
|
|
151
|
+
/**
|
|
152
|
+
* Create a resource model binding with ownership check
|
|
153
|
+
*/
|
|
154
|
+
ownedResource<T extends {
|
|
155
|
+
id: string;
|
|
156
|
+
userId: string;
|
|
157
|
+
}>(tableName: string, options?: {
|
|
158
|
+
idField?: string;
|
|
159
|
+
userIdField?: string;
|
|
160
|
+
cache?: boolean;
|
|
161
|
+
cacheTtl?: number;
|
|
162
|
+
db?: any;
|
|
163
|
+
}): ModelBindingConfig<T>;
|
|
164
|
+
};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { EnhancedRequest } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Laravel-style model binding that integrates with bun-query-builder models
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Base model interface compatible with bun-query-builder
|
|
7
|
+
*/
|
|
8
|
+
export interface Model {
|
|
9
|
+
id: string | number;
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
/**
|
|
12
|
+
* Get the route key for the model (Laravel-style)
|
|
13
|
+
*/
|
|
14
|
+
getRouteKeyName?: () => string;
|
|
15
|
+
/**
|
|
16
|
+
* Resolve route binding for this model instance (Laravel-style)
|
|
17
|
+
*/
|
|
18
|
+
resolveRouteBinding?: (value: string | number, field?: string) => Promise<this | null>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Query builder model resolver interface for integration layer
|
|
22
|
+
*/
|
|
23
|
+
export interface QueryBuilderModelResolver<TModel extends Model = Model> {
|
|
24
|
+
table: string;
|
|
25
|
+
queryBuilder?: any;
|
|
26
|
+
/**
|
|
27
|
+
* Find a model by ID or custom key
|
|
28
|
+
*/
|
|
29
|
+
find: (id: string | number, keyName?: string) => Promise<TModel | null>;
|
|
30
|
+
/**
|
|
31
|
+
* Find a model by ID or throw if not found
|
|
32
|
+
*/
|
|
33
|
+
findOrFail: (id: string | number, keyName?: string) => Promise<TModel>;
|
|
34
|
+
/**
|
|
35
|
+
* Get the route key name for this model
|
|
36
|
+
*/
|
|
37
|
+
getRouteKeyName?: () => string;
|
|
38
|
+
/**
|
|
39
|
+
* Apply constraints to a query (for scoped binding)
|
|
40
|
+
*/
|
|
41
|
+
applyConstraints?: (query: any, context: Record<string, Model>) => any;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Wrapper for bun-query-builder models to add Laravel-style route binding
|
|
45
|
+
*/
|
|
46
|
+
export declare class BunQueryBuilderModel<T extends Model = Model> {
|
|
47
|
+
definition: any;
|
|
48
|
+
queryBuilder: any;
|
|
49
|
+
constructor(definition: any, // bun-query-builder model definition
|
|
50
|
+
queryBuilder: any);
|
|
51
|
+
/**
|
|
52
|
+
* Get table name from bun-query-builder definition
|
|
53
|
+
*/
|
|
54
|
+
getTableName(): string;
|
|
55
|
+
/**
|
|
56
|
+
* Find a model by ID using bun-query-builder's find method
|
|
57
|
+
*/
|
|
58
|
+
find(id: string | number): Promise<T | null>;
|
|
59
|
+
/**
|
|
60
|
+
* Find a model by ID or throw using bun-query-builder's findOrFail method
|
|
61
|
+
*/
|
|
62
|
+
findOrFail(id: string | number): Promise<T>;
|
|
63
|
+
/**
|
|
64
|
+
* Find by custom field using bun-query-builder's where method
|
|
65
|
+
*/
|
|
66
|
+
where(field: string, value: any): Promise<T | null>;
|
|
67
|
+
/**
|
|
68
|
+
* Get the default route key name for this model
|
|
69
|
+
*/
|
|
70
|
+
getRouteKeyName(): string;
|
|
71
|
+
/**
|
|
72
|
+
* Custom route binding resolution (can be overridden)
|
|
73
|
+
*/
|
|
74
|
+
resolveRouteBinding(value: string | number, field?: string): Promise<T | null>;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Parse route parameter to extract model name and custom key
|
|
78
|
+
* Examples: {user} -> {name: 'user', key: undefined}
|
|
79
|
+
* {post:slug} -> {name: 'post', key: 'slug'}
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* Parse multiple route parameters from path
|
|
83
|
+
*/
|
|
84
|
+
export declare function parseRouteParameters(path: string): {
|
|
85
|
+
name: string;
|
|
86
|
+
key?: string;
|
|
87
|
+
}[];
|
|
88
|
+
export declare function parseRouteParameter(param: string): {
|
|
89
|
+
name: string;
|
|
90
|
+
key?: string;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Extract model parameters from route path
|
|
94
|
+
*/
|
|
95
|
+
export declare function extractModelParameters(path: string): {
|
|
96
|
+
name: string;
|
|
97
|
+
key?: string;
|
|
98
|
+
}[];
|
|
99
|
+
/**
|
|
100
|
+
* Model not found error
|
|
101
|
+
*/
|
|
102
|
+
export declare class ModelNotFoundError extends Error {
|
|
103
|
+
modelName: string;
|
|
104
|
+
value: string | number;
|
|
105
|
+
field: string;
|
|
106
|
+
constructor(modelName: string, value: string | number, field?: string);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Model binding registry for explicit bindings (Laravel Route::model and Route::bind)
|
|
110
|
+
*/
|
|
111
|
+
export declare class ModelBindingRegistry {
|
|
112
|
+
private bindings;
|
|
113
|
+
/**
|
|
114
|
+
* Register explicit model binding (Laravel Route::model style)
|
|
115
|
+
*/
|
|
116
|
+
model<T extends Model>(parameter: string, modelWrapper: BunQueryBuilderModel<T>): void;
|
|
117
|
+
/**
|
|
118
|
+
* Register custom binding resolver (Laravel Route::bind style)
|
|
119
|
+
*/
|
|
120
|
+
bind(parameter: string, resolver: (value: string) => Promise<Model | null>): void;
|
|
121
|
+
/**
|
|
122
|
+
* Get binding for parameter
|
|
123
|
+
*/
|
|
124
|
+
getBinding(parameter: string): BunQueryBuilderModel | ((_value: string) => Promise<Model | null>) | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* Check if parameter has explicit binding
|
|
127
|
+
*/
|
|
128
|
+
hasBinding(parameter: string): boolean;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Global model binding registry instance
|
|
132
|
+
*/
|
|
133
|
+
export declare const modelRegistry: ModelBindingRegistry;
|
|
134
|
+
/**
|
|
135
|
+
* Model binding configuration interface
|
|
136
|
+
*/
|
|
137
|
+
export interface ModelBindingConfig {
|
|
138
|
+
model: string;
|
|
139
|
+
key?: string;
|
|
140
|
+
constraints?: Record<string, any>;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Model class interface
|
|
144
|
+
*/
|
|
145
|
+
export interface ModelClass<T extends Model = Model> {
|
|
146
|
+
new (...args: any[]): T;
|
|
147
|
+
find: (id: string | number) => Promise<T | null>;
|
|
148
|
+
findOrFail: (id: string | number) => Promise<T>;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Model resolver interface
|
|
152
|
+
*/
|
|
153
|
+
export interface ModelResolver<T extends Model = Model> {
|
|
154
|
+
find: (id: string | number, keyName?: string) => Promise<T | null>;
|
|
155
|
+
findOrFail: (id: string | number, keyName?: string) => Promise<T>;
|
|
156
|
+
getRouteKeyName?: () => string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Route binding options interface
|
|
160
|
+
*/
|
|
161
|
+
export interface RouteBindingOptions {
|
|
162
|
+
model?: string;
|
|
163
|
+
key?: string;
|
|
164
|
+
constraints?: Record<string, any>;
|
|
165
|
+
middleware?: string[];
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Resolve model from route parameter using bun-query-builder
|
|
169
|
+
*/
|
|
170
|
+
export declare function resolveModel<T extends Model>(_paramName: string, value: string | number, modelWrapper: BunQueryBuilderModel<T>, customKey?: string, _context?: Record<string, Model>): Promise<T | null>;
|
|
171
|
+
/**
|
|
172
|
+
* Route model binding middleware factory for bun-query-builder models
|
|
173
|
+
*/
|
|
174
|
+
export declare function createModelBindingMiddleware(modelParameters: {
|
|
175
|
+
name: string;
|
|
176
|
+
key?: string;
|
|
177
|
+
}[], modelWrappers: Record<string, BunQueryBuilderModel>): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
178
|
+
/**
|
|
179
|
+
* Create a model wrapper from bun-query-builder model definition
|
|
180
|
+
*/
|
|
181
|
+
export declare function createModelWrapper<T extends Model>(modelDefinition: any, queryBuilder: any): BunQueryBuilderModel<T>;
|
|
182
|
+
/**
|
|
183
|
+
* Apply model bindings to a route
|
|
184
|
+
*/
|
|
185
|
+
export declare function applyModelBindings(path: string, modelWrappers: Record<string, BunQueryBuilderModel>, _options?: RouteBindingOptions): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Model, QueryBuilderModelResolver } from './model-binding';
|
|
2
|
+
/**
|
|
3
|
+
* Factory function to create model resolvers with bun-query-builder integration
|
|
4
|
+
*/
|
|
5
|
+
export declare function createModelResolver<TModel extends Model = Model>(tableName: string, options?: {
|
|
6
|
+
keyName?: string;
|
|
7
|
+
applyConstraints?: (query: any, context: Record<string, Model>) => any;
|
|
8
|
+
}): QueryBuilderModelResolver<TModel>;
|
|
9
|
+
/**
|
|
10
|
+
* Create a scoped model resolver that applies constraints based on parent models
|
|
11
|
+
*/
|
|
12
|
+
export declare function createScopedModelResolver<TModel extends Model = Model>(tableName: string, constraintFn: (query: any, context: Record<string, Model>) => any, options?: {
|
|
13
|
+
keyName?: string;
|
|
14
|
+
}): QueryBuilderModelResolver<TModel>;
|
|
15
|
+
/**
|
|
16
|
+
* Common constraint functions for scoped bindings
|
|
17
|
+
*/
|
|
18
|
+
export declare const constraints: {
|
|
19
|
+
/**
|
|
20
|
+
* Ensure a model belongs to a specific parent model
|
|
21
|
+
*/
|
|
22
|
+
belongsTo: (parentKey: string, foreignKey: string) => ((_query: any, _context: Record<string, Model>) => any);
|
|
23
|
+
/**
|
|
24
|
+
* Ensure a model belongs to the authenticated user
|
|
25
|
+
*/
|
|
26
|
+
belongsToUser: (userKey?: string, foreignKey?: string) => ((_query: any, _context: Record<string, Model>) => any);
|
|
27
|
+
/**
|
|
28
|
+
* Apply multiple constraints
|
|
29
|
+
*/
|
|
30
|
+
combine: (...constraintFns: Array<(query: any, context: Record<string, Model>) => any>) => ((query: any, context: Record<string, Model>) => any);
|
|
31
|
+
};
|