raiton 6.8.9-beta.25 → 6.11.0-beta.29
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/CHANGELOG.md +67 -0
- package/README.md +65 -1
- package/benchmarks/README.md +31 -0
- package/benchmarks/ai-debug.json +145 -0
- package/benchmarks/ai-debug.md +64 -0
- package/benchmarks/latest.json +172 -0
- package/benchmarks/latest.md +66 -0
- package/build/bin/index.mjs +37621 -21866
- package/build/types/bin/bootstrapper.d.ts +2 -0
- package/build/types/bin/cli-output.d.ts +46 -0
- package/build/types/bin/cli-tools.d.ts +13 -0
- package/build/types/bin/cli.d.ts +3 -0
- package/build/types/bin/commands.registry.d.ts +4 -0
- package/build/types/bin/constants.d.ts +3 -0
- package/build/types/bin/file-utils.d.ts +21 -0
- package/build/types/bin/index.d.ts +2 -0
- package/build/types/bin/name-utils.d.ts +4 -0
- package/build/types/bin/project-utils.d.ts +12 -0
- package/build/types/commands/artifact.command.d.ts +7 -0
- package/build/types/commands/build.command.d.ts +8 -0
- package/build/types/commands/call.command.d.ts +10 -0
- package/build/types/commands/client.command.d.ts +7 -0
- package/build/types/commands/contract.command.d.ts +8 -0
- package/build/types/commands/debug.shared.d.ts +16 -0
- package/build/types/commands/develop.command.d.ts +9 -0
- package/build/types/commands/docs.command.d.ts +8 -0
- package/build/types/commands/doctor.command.d.ts +9 -0
- package/build/types/commands/explain.command.d.ts +8 -0
- package/build/types/commands/generate.command.d.ts +7 -0
- package/build/types/commands/inspect.command.d.ts +7 -0
- package/build/types/commands/mcp.command.d.ts +12 -0
- package/build/types/commands/new.command.d.ts +7 -0
- package/build/types/commands/openapi.command.d.ts +7 -0
- package/build/types/commands/requests.command.d.ts +8 -0
- package/build/types/commands/routes.command.d.ts +7 -0
- package/build/types/commands/start.command.d.ts +9 -0
- package/build/types/commands/verify.command.d.ts +8 -0
- package/build/types/commands/why.command.d.ts +7 -0
- package/build/types/contracts/decorators.d.ts +59 -0
- package/build/types/contracts/errors.d.ts +68 -0
- package/build/types/contracts/index.d.ts +4 -0
- package/build/types/contracts/schema.d.ts +83 -0
- package/build/types/contracts/typebox.d.ts +81 -0
- package/build/types/core/application.d.ts +45 -0
- package/build/types/core/artifacts/artifact.d.ts +3 -0
- package/build/types/core/artifacts/index.d.ts +1 -0
- package/build/types/core/artifacts/runner.d.ts +1 -0
- package/build/types/core/builder.d.ts +42 -0
- package/build/types/core/bytes.util.d.ts +3 -0
- package/build/types/core/command.d.ts +13 -0
- package/build/types/core/commands.d.ts +10 -0
- package/build/types/core/config/config.d.ts +27 -0
- package/build/types/core/config/define.d.ts +8 -0
- package/build/types/core/config/index.d.ts +3 -0
- package/build/types/core/config/schema.d.ts +24 -0
- package/build/types/core/context.d.ts +11 -0
- package/build/types/core/controller/builder.d.ts +26 -0
- package/build/types/core/controller/compiler.d.ts +2 -0
- package/build/types/core/controller/index.d.ts +4 -0
- package/build/types/core/controller/metadata.d.ts +3 -0
- package/build/types/core/controller/tracker.d.ts +9 -0
- package/build/types/core/directories.d.ts +10 -0
- package/build/types/core/guards.d.ts +11 -0
- package/build/types/core/helpers/index.d.ts +1 -0
- package/build/types/core/helpers/raiton.d.ts +1 -0
- package/build/types/core/hooks.d.ts +14 -0
- package/build/types/core/index.d.ts +17 -0
- package/build/types/core/injection/index.d.ts +1 -0
- package/build/types/core/injection/injection.d.ts +37 -0
- package/build/types/core/mcp/builder.d.ts +2 -0
- package/build/types/core/mcp/index.d.ts +3 -0
- package/build/types/core/mcp/metadata.d.ts +9 -0
- package/build/types/core/mcp/tracker.d.ts +9 -0
- package/build/types/core/middleware/compose.d.ts +8 -0
- package/build/types/core/middleware/index.d.ts +2 -0
- package/build/types/core/middleware/pipeline.d.ts +12 -0
- package/build/types/core/plugins/index.d.ts +2 -0
- package/build/types/core/plugins/plugin.d.ts +2 -0
- package/build/types/core/plugins/scope.d.ts +15 -0
- package/build/types/core/process.util.d.ts +1 -0
- package/build/types/core/raiton.d.ts +15 -0
- package/build/types/core/router/handler.d.ts +2 -0
- package/build/types/core/router/handler.utils.d.ts +37 -0
- package/build/types/core/router/index.d.ts +4 -0
- package/build/types/core/router/matcher.d.ts +50 -0
- package/build/types/core/router/route.d.ts +52 -0
- package/build/types/core/router/router.d.ts +23 -0
- package/build/types/core/scheduler/builder.d.ts +2 -0
- package/build/types/core/scheduler/cron.d.ts +32 -0
- package/build/types/core/scheduler/duration.d.ts +7 -0
- package/build/types/core/scheduler/index.d.ts +6 -0
- package/build/types/core/scheduler/manager.d.ts +47 -0
- package/build/types/core/scheduler/metadata.d.ts +8 -0
- package/build/types/core/scheduler/queue/backend.d.ts +42 -0
- package/build/types/core/scheduler/queue/index.d.ts +3 -0
- package/build/types/core/scheduler/queue/rabbitmq.backend.d.ts +37 -0
- package/build/types/core/scheduler/queue/redis.backend.d.ts +38 -0
- package/build/types/core/server.d.ts +9 -0
- package/build/types/core/socket/builder.d.ts +1 -0
- package/build/types/core/socket/index.d.ts +2 -0
- package/build/types/core/socket/metadata.d.ts +14 -0
- package/build/types/core/thread.d.ts +21 -0
- package/build/types/debug/cli.d.ts +33 -0
- package/build/types/debug/collector.d.ts +41 -0
- package/build/types/debug/collector.internal.d.ts +27 -0
- package/build/types/debug/config.d.ts +19 -0
- package/build/types/debug/examples.d.ts +43 -0
- package/build/types/debug/explain.d.ts +9 -0
- package/build/types/debug/index.d.ts +15 -0
- package/build/types/debug/invocation.d.ts +53 -0
- package/build/types/debug/journal.d.ts +49 -0
- package/build/types/debug/mcp-http.d.ts +17 -0
- package/build/types/debug/mcp.d.ts +14 -0
- package/build/types/debug/redaction.d.ts +48 -0
- package/build/types/debug/requests.d.ts +37 -0
- package/build/types/debug/runtime.d.ts +52 -0
- package/build/types/debug/session.d.ts +21 -0
- package/build/types/debug/source.d.ts +17 -0
- package/build/types/debug/surface.d.ts +10 -0
- package/build/types/debug/types.d.ts +271 -0
- package/build/types/debug/verify.d.ts +23 -0
- package/build/types/framework/artifacts.d.ts +20 -0
- package/build/types/framework/cache/cache.d.ts +84 -0
- package/build/types/framework/cache/cache.decorator.d.ts +21 -0
- package/build/types/framework/cache/etag.d.ts +62 -0
- package/build/types/framework/cache/index.d.ts +4 -0
- package/build/types/framework/cache/types.d.ts +89 -0
- package/build/types/framework/constants/decorators.constant.d.ts +28 -0
- package/build/types/framework/constants/index.d.ts +2 -0
- package/build/types/framework/constants/microservices.constant.d.ts +3 -0
- package/build/types/framework/controllers.d.ts +2 -0
- package/build/types/framework/data-transfer-object.d.ts +5 -0
- package/build/types/framework/decorators/access-guard.decorator.d.ts +1 -0
- package/build/types/framework/decorators/api-response.decorator.d.ts +66 -0
- package/build/types/framework/decorators/api.decorator.d.ts +160 -0
- package/build/types/framework/decorators/controllable.decorator.d.ts +3 -0
- package/build/types/framework/decorators/csrf-guard.decorator.d.ts +2 -0
- package/build/types/framework/decorators/csrf-token.decorator.d.ts +1 -0
- package/build/types/framework/decorators/guard.decorator.d.ts +2 -0
- package/build/types/framework/decorators/health-check.decorator.d.ts +17 -0
- package/build/types/framework/decorators/index.d.ts +15 -0
- package/build/types/framework/decorators/injection.decorator.d.ts +5 -0
- package/build/types/framework/decorators/mcp.decorator.d.ts +68 -0
- package/build/types/framework/decorators/middleware.decorator.d.ts +3 -0
- package/build/types/framework/decorators/parametrable.decorator.d.ts +9 -0
- package/build/types/framework/decorators/routable.decorator.d.ts +12 -0
- package/build/types/framework/decorators/route-interaction.decorator.d.ts +81 -0
- package/build/types/framework/decorators/scheduler.decorator.d.ts +54 -0
- package/build/types/framework/decorators/socket.decorator.d.ts +5 -0
- package/build/types/framework/encryption.d.ts +25 -0
- package/build/types/framework/enums/csrf.enum.d.ts +4 -0
- package/build/types/framework/enums/encrypted.enum.d.ts +20 -0
- package/build/types/framework/enums/event.message.enum.d.ts +4 -0
- package/build/types/framework/enums/http-method.enum.d.ts +10 -0
- package/build/types/framework/enums/http-parameters.enum.d.ts +11 -0
- package/build/types/framework/enums/http-status.enum.d.ts +64 -0
- package/build/types/framework/enums/index.d.ts +9 -0
- package/build/types/framework/enums/runtime.enum.d.ts +5 -0
- package/build/types/framework/enums/scheduler.enum.d.ts +21 -0
- package/build/types/framework/enums/timestamp.enum.d.ts +6 -0
- package/build/types/framework/env.d.ts +3 -0
- package/build/types/framework/errors/catalog.d.ts +57 -0
- package/build/types/framework/exceptions/http-exception.d.ts +18 -0
- package/build/types/framework/exceptions/index.d.ts +2 -0
- package/build/types/framework/exceptions/throwable.d.ts +76 -0
- package/build/types/framework/health-check.d.ts +42 -0
- package/build/types/framework/index.d.ts +20 -0
- package/build/types/framework/mcp/audit.d.ts +12 -0
- package/build/types/framework/mcp/conformance.d.ts +31 -0
- package/build/types/framework/mcp/handler.d.ts +11 -0
- package/build/types/framework/mcp/index.d.ts +5 -0
- package/build/types/framework/mcp/protocol.d.ts +17 -0
- package/build/types/framework/mcp/rate-limit.d.ts +10 -0
- package/build/types/framework/mcp/stdio.d.ts +40 -0
- package/build/types/framework/mcp/streamable-http.d.ts +17 -0
- package/build/types/framework/observability/index.d.ts +6 -0
- package/build/types/framework/observability/metrics.d.ts +23 -0
- package/build/types/framework/observability/plugin.d.ts +20 -0
- package/build/types/framework/observability/readiness.d.ts +9 -0
- package/build/types/framework/observability/request.d.ts +7 -0
- package/build/types/framework/observability/shutdown.d.ts +21 -0
- package/build/types/framework/observability/trace.d.ts +96 -0
- package/build/types/framework/parameter-bag.d.ts +14 -0
- package/build/types/framework/plugins/body-parser.plugin.d.ts +3 -0
- package/build/types/framework/plugins/dev.plugin.d.ts +12 -0
- package/build/types/framework/plugins/index.d.ts +5 -0
- package/build/types/framework/plugins/mcp.plugin.d.ts +15 -0
- package/build/types/framework/plugins/openapi.plugin.d.ts +58 -0
- package/build/types/framework/plugins/security/body-limit.d.ts +1 -0
- package/build/types/framework/plugins/security/cors.d.ts +10 -0
- package/build/types/framework/plugins/security/csrf.d.ts +2 -0
- package/build/types/framework/plugins/security/headers.d.ts +1 -0
- package/build/types/framework/plugins/security/index.d.ts +13 -0
- package/build/types/framework/plugins/security/method-guard.d.ts +1 -0
- package/build/types/framework/plugins/security/rate-limit.d.ts +5 -0
- package/build/types/framework/repositories.d.ts +5 -0
- package/build/types/framework/responses/error.d.ts +13 -0
- package/build/types/framework/responses/helpers.d.ts +7 -0
- package/build/types/framework/responses/http-throwable.d.ts +14 -0
- package/build/types/framework/responses/http.d.ts +13 -0
- package/build/types/framework/responses/index.d.ts +4 -0
- package/build/types/framework/runtime/bun/server.d.ts +2 -0
- package/build/types/framework/runtime/deno/server.d.ts +2 -0
- package/build/types/framework/runtime/index.d.ts +17 -0
- package/build/types/framework/runtime/node/reply.d.ts +12 -0
- package/build/types/framework/runtime/node/request.d.ts +33 -0
- package/build/types/framework/runtime/node/server.d.ts +2 -0
- package/build/types/framework/runtime/web/server.d.ts +1 -0
- package/build/types/framework/services.d.ts +5 -0
- package/build/types/framework/utilities/alias-path.util.d.ts +1 -0
- package/build/types/framework/utilities/artifact.util.d.ts +7 -0
- package/build/types/framework/utilities/callable.util.d.ts +1 -0
- package/build/types/framework/utilities/cookie.util.d.ts +1 -0
- package/build/types/framework/utilities/csrf.util.d.ts +26 -0
- package/build/types/framework/utilities/https.util.d.ts +49 -0
- package/build/types/framework/utilities/index.d.ts +12 -0
- package/build/types/framework/utilities/ip.util.d.ts +15 -0
- package/build/types/framework/utilities/json.util.d.ts +10 -0
- package/build/types/framework/utilities/openapi.utils.d.ts +7 -0
- package/build/types/framework/utilities/openapi.utils.impl.d.ts +18 -0
- package/build/types/framework/utilities/path.util.d.ts +3 -0
- package/build/types/framework/utilities/route-suggestion.util.d.ts +10 -0
- package/build/types/framework/utilities/url.util.d.ts +1 -0
- package/build/types/framework/utilities/utilities.util.d.ts +3 -0
- package/build/types/framework/utilities/validation.util.d.ts +8 -0
- package/build/types/framework/view-model.d.ts +4 -0
- package/build/types/generators/artifact.templates.d.ts +7 -0
- package/build/types/generators/index.d.ts +2 -0
- package/build/types/generators/project.templates.d.ts +21 -0
- package/build/types/manifest/client.d.ts +12 -0
- package/build/types/manifest/contract.d.ts +25 -0
- package/build/types/manifest/index.d.ts +5 -0
- package/build/types/manifest/introspect.d.ts +18 -0
- package/build/types/manifest/manifest.d.ts +18 -0
- package/build/types/manifest/schema.d.ts +241 -0
- package/build/types/requirements.d.ts +1 -0
- package/build/types/types/access-guards.d.ts +4 -0
- package/build/types/types/application.d.ts +32 -0
- package/build/types/types/artifact.d.ts +8 -0
- package/build/types/types/builder.d.ts +37 -0
- package/build/types/types/config.d.ts +112 -0
- package/build/types/types/controller.d.ts +27 -0
- package/build/types/types/contruct.d.ts +1 -0
- package/build/types/types/core.d.ts +11 -0
- package/build/types/types/csrf.d.ts +33 -0
- package/build/types/types/directory.d.ts +6 -0
- package/build/types/types/encryption.d.ts +14 -0
- package/build/types/types/generic.d.ts +2 -0
- package/build/types/types/guard.d.ts +9 -0
- package/build/types/types/http-responses.d.ts +7 -0
- package/build/types/types/index.d.ts +27 -0
- package/build/types/types/injection.d.ts +11 -0
- package/build/types/types/lifecycle.d.ts +9 -0
- package/build/types/types/mcp.d.ts +148 -0
- package/build/types/types/middleware.d.ts +12 -0
- package/build/types/types/parseable.d.ts +3 -0
- package/build/types/types/plugin.d.ts +6 -0
- package/build/types/types/raiton.d.ts +13 -0
- package/build/types/types/responses.d.ts +20 -0
- package/build/types/types/router.d.ts +13 -0
- package/build/types/types/runtime.d.ts +48 -0
- package/build/types/types/scheduler.d.ts +140 -0
- package/build/types/types/scheme.d.ts +125 -0
- package/build/types/types/server.d.ts +10 -0
- package/build/types/types/socket.d.ts +11 -0
- package/build/types/types/thread.d.ts +25 -0
- package/build/types/types/utilities.d.ts +4 -0
- package/build/types/types/values.d.ts +2 -0
- package/docs/AGENTS.md +55 -0
- package/docs/CLAUDE.md +55 -0
- package/docs/README.md +5 -0
- package/docs/contributors/cli/README.md +17 -3
- package/docs/contributors/cli/commands.md +28 -17
- package/docs/contributors/framework/plugins/openapi.md +12 -3
- package/docs/generated/cli/commands.md +31 -0
- package/docs/generated/config-schema.json +243 -0
- package/docs/generated/manifest-schema.json +312 -0
- package/docs/generated/manifest.json +118 -0
- package/docs/llms-full.txt +1450 -0
- package/docs/llms.txt +51 -0
- package/docs/user-guide/README.md +19 -12
- package/docs/user-guide/cli.md +26 -0
- package/docs/user-guide/config.md +32 -0
- package/docs/user-guide/contracts.md +67 -0
- package/docs/user-guide/debug.md +130 -0
- package/docs/user-guide/en/README.md +83 -0
- package/docs/user-guide/getting-started.md +13 -1
- package/docs/user-guide/mcp.md +45 -2
- package/docs/user-guide/observability.md +74 -0
- package/docs/user-guide/openapi/plugin.md +10 -0
- package/docs/user-guide/recipes/README.md +32 -0
- package/docs/user-guide/recipes/contracts.md +52 -0
- package/docs/user-guide/recipes/doctor.md +16 -0
- package/docs/user-guide/recipes/generate.md +28 -0
- package/docs/user-guide/recipes/manifest.md +17 -0
- package/docs/user-guide/recipes/mcp-stdio.md +35 -0
- package/docs/user-guide/recipes/new-project.md +34 -0
- package/docs/user-guide/recipes/observability.md +17 -0
- package/package.json +42 -7
- package/scripts/bench-ai-debug.ts +572 -0
- package/scripts/bench.ts +586 -0
- package/scripts/postinstall.mjs +15 -0
- package/source/bin/cli-output.ts +139 -0
- package/source/bin/commands.registry.ts +31 -3
- package/source/bin/file-utils.ts +95 -0
- package/source/bin/name-utils.ts +26 -0
- package/source/bin/project-utils.ts +78 -0
- package/source/commands/artifact.command.ts +30 -15
- package/source/commands/call.command.ts +247 -0
- package/source/commands/client.command.ts +64 -0
- package/source/commands/contract.command.ts +104 -0
- package/source/commands/debug.shared.ts +90 -0
- package/source/commands/docs.command.ts +269 -0
- package/source/commands/doctor.command.ts +226 -0
- package/source/commands/explain.command.ts +119 -0
- package/source/commands/generate.command.ts +58 -0
- package/source/commands/inspect.command.ts +108 -0
- package/source/commands/mcp.command.ts +322 -0
- package/source/commands/new.command.ts +135 -0
- package/source/commands/openapi.command.ts +95 -0
- package/source/commands/requests.command.ts +137 -0
- package/source/commands/routes.command.ts +39 -0
- package/source/commands/verify.command.ts +173 -0
- package/source/commands/why.command.ts +61 -0
- package/source/contracts/decorators.ts +216 -0
- package/source/contracts/errors.ts +135 -0
- package/source/contracts/index.ts +4 -0
- package/source/contracts/schema.ts +438 -0
- package/source/contracts/typebox.ts +193 -0
- package/source/core/application.ts +322 -72
- package/source/core/builder.ts +10 -2
- package/source/core/config/config.ts +155 -26
- package/source/core/config/define.ts +35 -8
- package/source/core/config/index.ts +2 -1
- package/source/core/config/schema.ts +406 -0
- package/source/core/controller/builder.ts +38 -13
- package/source/core/controller/compiler.ts +14 -7
- package/source/core/controller/metadata.ts +1 -1
- package/source/core/hooks.ts +13 -1
- package/source/core/injection/injection.ts +47 -9
- package/source/core/mcp/builder.ts +13 -2
- package/source/core/mcp/metadata.ts +2 -1
- package/source/core/middleware/compose.ts +48 -0
- package/source/core/middleware/pipeline.ts +69 -46
- package/source/core/raiton.ts +8 -0
- package/source/core/router/handler.ts +246 -37
- package/source/core/router/handler.utils.ts +107 -51
- package/source/core/router/matcher.ts +195 -24
- package/source/core/router/route.ts +100 -31
- package/source/core/router/router.ts +27 -2
- package/source/core/scheduler/metadata.ts +1 -1
- package/source/core/socket/metadata.ts +1 -1
- package/source/core/thread.ts +37 -2
- package/source/debug/cli.ts +206 -0
- package/source/debug/collector.internal.ts +27 -0
- package/source/debug/collector.ts +294 -0
- package/source/debug/config.ts +142 -0
- package/source/debug/examples.ts +188 -0
- package/source/debug/explain.ts +187 -0
- package/source/debug/index.ts +15 -0
- package/source/debug/invocation.ts +695 -0
- package/source/debug/journal.ts +249 -0
- package/source/debug/mcp-http.ts +145 -0
- package/source/debug/mcp.ts +410 -0
- package/source/debug/redaction.ts +225 -0
- package/source/debug/requests.ts +136 -0
- package/source/debug/runtime.ts +181 -0
- package/source/debug/session.ts +107 -0
- package/source/debug/source.ts +95 -0
- package/source/debug/surface.ts +375 -0
- package/source/debug/types.ts +311 -0
- package/source/debug/verify.ts +186 -0
- package/source/framework/artifacts.ts +47 -15
- package/source/framework/cache/cache.ts +48 -3
- package/source/framework/constants/decorators.constant.ts +5 -0
- package/source/framework/data-transfer-object.ts +4 -3
- package/source/framework/decorators/controllable.decorator.ts +3 -3
- package/source/framework/decorators/csrf-guard.decorator.ts +3 -2
- package/source/framework/decorators/csrf-token.decorator.ts +1 -1
- package/source/framework/decorators/guard.decorator.ts +3 -2
- package/source/framework/decorators/health-check.decorator.ts +1 -1
- package/source/framework/decorators/mcp.decorator.ts +38 -7
- package/source/framework/decorators/middleware.decorator.ts +2 -2
- package/source/framework/decorators/parametrable.decorator.ts +2 -1
- package/source/framework/decorators/routable.decorator.ts +3 -3
- package/source/framework/decorators/route-interaction.decorator.ts +2 -2
- package/source/framework/decorators/scheduler.decorator.ts +3 -3
- package/source/framework/decorators/socket.decorator.ts +3 -3
- package/source/framework/encryption.ts +46 -7
- package/source/framework/errors/catalog.ts +205 -0
- package/source/framework/exceptions/http-exception.ts +1 -1
- package/source/framework/health-check.ts +18 -0
- package/source/framework/index.ts +2 -0
- package/source/framework/mcp/audit.ts +57 -0
- package/source/framework/mcp/conformance.ts +195 -0
- package/source/framework/mcp/handler.ts +139 -9
- package/source/framework/mcp/index.ts +6 -1
- package/source/framework/mcp/protocol.ts +6 -0
- package/source/framework/mcp/rate-limit.ts +33 -0
- package/source/framework/mcp/stdio.ts +150 -0
- package/source/framework/mcp/streamable-http.ts +36 -5
- package/source/framework/observability/index.ts +6 -0
- package/source/framework/observability/metrics.ts +162 -0
- package/source/framework/observability/plugin.ts +151 -0
- package/source/framework/observability/readiness.ts +22 -0
- package/source/framework/observability/request.ts +31 -0
- package/source/framework/observability/shutdown.ts +68 -0
- package/source/framework/observability/trace.ts +241 -0
- package/source/framework/plugins/body-parser.plugin.ts +88 -85
- package/source/framework/plugins/dev.plugin.ts +227 -0
- package/source/framework/plugins/index.ts +2 -1
- package/source/framework/plugins/mcp.plugin.ts +14 -3
- package/source/framework/plugins/openapi.plugin.ts +56 -13
- package/source/framework/responses/helpers.ts +4 -4
- package/source/framework/responses/http-throwable.ts +1 -1
- package/source/framework/runtime/bun/server.ts +2 -1
- package/source/framework/runtime/deno/server.ts +2 -1
- package/source/framework/runtime/index.ts +45 -13
- package/source/framework/runtime/node/request.ts +59 -13
- package/source/framework/utilities/index.ts +3 -1
- package/source/framework/utilities/openapi.utils.impl.ts +733 -0
- package/source/framework/utilities/openapi.utils.ts +6 -636
- package/source/framework/utilities/route-suggestion.util.ts +59 -0
- package/source/framework/utilities/validation.util.ts +20 -0
- package/source/generators/artifact.templates.ts +230 -0
- package/source/generators/index.ts +2 -0
- package/source/generators/project.templates.ts +392 -0
- package/source/manifest/client.ts +299 -0
- package/source/manifest/contract.ts +138 -0
- package/source/manifest/index.ts +5 -0
- package/source/manifest/introspect.ts +57 -0
- package/source/manifest/manifest.ts +364 -0
- package/source/manifest/schema.ts +200 -0
- package/source/types/builder.ts +5 -0
- package/source/types/config.ts +120 -2
- package/source/types/mcp.ts +63 -1
- package/tsconfig.build.json +13 -0
- package/source/commands/grafts.command.ts +0 -27
- package/tests/artifact-resolution.test.ts +0 -60
|
@@ -1,17 +1,29 @@
|
|
|
1
|
-
import {MiddlewareCallable, ParamMetaInterface, RouteMetaInterface} from "../../types/index.ts";
|
|
2
|
-
import {middlewareCompose} from "../
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import type {MiddlewareCallable, ParamMetaInterface, RouteMetaInterface} from "../../types/index.ts";
|
|
2
|
+
import {middlewareCompose} from "../middleware/compose.ts";
|
|
3
|
+
import {DataTransferObject} from "../../framework/data-transfer-object.ts";
|
|
4
|
+
import {METADATA_KEYS} from "../../framework/constants/decorators.constant.ts";
|
|
5
|
+
import {Parametrable} from "../../framework/enums/http-parameters.enum.ts";
|
|
6
|
+
import {parseCookie} from "../../framework/utilities/cookie.util.ts";
|
|
7
|
+
import {Throwable} from "../../framework/exceptions/throwable.ts";
|
|
8
|
+
import {ViewModel} from "../../framework/view-model.ts";
|
|
9
|
+
import {loadClassValidator} from "../../framework/utilities/validation.util.ts";
|
|
5
10
|
|
|
6
11
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @param routeMeta
|
|
10
|
-
* @param ctx
|
|
12
|
+
* Récupère (une seule fois, à la compilation du handler) les métadonnées de
|
|
13
|
+
* paramètres d'une route.
|
|
11
14
|
*/
|
|
12
|
-
export function
|
|
15
|
+
export function getRouteParameters(instance: any, routeMeta: RouteMetaInterface): ParamMetaInterface[] {
|
|
16
|
+
return Reflect.getMetadata(METADATA_KEYS.ROUTE_PARAMETERS, instance.constructor)?.[routeMeta.propertyKey] || []
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Collecte les arguments d'un handler à partir de métadonnées déjà résolues.
|
|
21
|
+
*
|
|
22
|
+
* Cette fonction ne fait aucune réflexion : elle est appelée à chaque requête
|
|
23
|
+
* avec le tableau figé au moment de la compilation du handler.
|
|
24
|
+
*/
|
|
25
|
+
export function collectArguments(params: readonly ParamMetaInterface[], ctx: any): any[] {
|
|
13
26
|
const args: any[] = [];
|
|
14
|
-
const params: ParamMetaInterface[] = Reflect.getMetadata(METADATA_KEYS.ROUTE_PARAMETERS, instance.constructor)?.[routeMeta.propertyKey] || [];
|
|
15
27
|
|
|
16
28
|
for (const p of params) {
|
|
17
29
|
switch (p.type) {
|
|
@@ -19,7 +31,7 @@ export function collectRouteArguments(instance: any, routeMeta: RouteMetaInterfa
|
|
|
19
31
|
args[p.index] = ctx.req.query?.[p.key!]
|
|
20
32
|
break;
|
|
21
33
|
case Parametrable.PARAM:
|
|
22
|
-
args[p.index] = ctx.
|
|
34
|
+
args[p.index] = ctx.params?.[p.key!] ?? ctx.req.params?.[p.key!]
|
|
23
35
|
break;
|
|
24
36
|
case Parametrable.BODY:
|
|
25
37
|
if (p.metatype && 'prototype' in p.metatype) {
|
|
@@ -53,6 +65,13 @@ export function collectRouteArguments(instance: any, routeMeta: RouteMetaInterfa
|
|
|
53
65
|
return args;
|
|
54
66
|
}
|
|
55
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Compatibilité : récupère les métadonnées puis collecte les arguments.
|
|
70
|
+
*/
|
|
71
|
+
export function collectRouteArguments(instance: any, routeMeta: RouteMetaInterface, ctx: any): any[] {
|
|
72
|
+
return collectArguments(getRouteParameters(instance, routeMeta), ctx);
|
|
73
|
+
}
|
|
74
|
+
|
|
56
75
|
/**
|
|
57
76
|
* Helper function for validating DTO arguments
|
|
58
77
|
* @param args
|
|
@@ -69,40 +88,78 @@ export async function validateDtoArguments(args: any[]): Promise<void> {
|
|
|
69
88
|
}
|
|
70
89
|
}
|
|
71
90
|
|
|
91
|
+
export type ResponseValidator = (responses: any) => Promise<void>
|
|
92
|
+
|
|
72
93
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* @param routeMeta
|
|
94
|
+
* Compile le validateur de réponse d'une route : métadonnées, cible et mode
|
|
95
|
+
* tableau sont résolus une fois. Le validateur n'est créé par `createHandler`
|
|
96
|
+
* que si la validation est active (dev/HMR ou configuration explicite).
|
|
77
97
|
*/
|
|
78
|
-
export
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
98
|
+
export function createResponseValidator(instance: any, routeMeta: RouteMetaInterface): ResponseValidator {
|
|
99
|
+
// @ApiResponse() stores metadata on the class prototype.
|
|
100
|
+
const holder = instance.constructor?.prototype ?? instance.constructor
|
|
101
|
+
const apiResponses = Reflect.getMetadata(METADATA_KEYS.API_RESPONSES, holder)
|
|
102
|
+
?? Reflect.getMetadata(METADATA_KEYS.API_RESPONSES, instance.constructor)
|
|
103
|
+
?? [];
|
|
104
|
+
const responses = apiResponses[routeMeta.propertyKey] || [];
|
|
105
|
+
const successResponse = responses.find((r: any) => r.status === 200) || responses[0];
|
|
106
|
+
const validationType = successResponse?.type;
|
|
107
|
+
const isArray = Boolean(successResponse?.isArray);
|
|
108
|
+
|
|
109
|
+
return async (responses: any) => {
|
|
110
|
+
const {validate} = await loadClassValidator()
|
|
111
|
+
|
|
112
|
+
// Special handling for ViewModel response wrapping: { data: viewModelInstance }
|
|
113
|
+
if (responses && typeof responses === 'object' && 'data' in responses) {
|
|
114
|
+
const data = (responses as any).data;
|
|
115
|
+
if (data instanceof ViewModel) {
|
|
116
|
+
const ViewModelClass = data.constructor as new (init: any) => ViewModel;
|
|
117
|
+
const validationTarget = new ViewModelClass(data['__initial__']);
|
|
118
|
+
const errors = await validate(validationTarget);
|
|
119
|
+
if (errors.length > 0) {
|
|
120
|
+
const errorMessages = errors.map(err =>
|
|
121
|
+
Object.values(err.constraints || {}).join(', ')
|
|
122
|
+
).join('; ');
|
|
123
|
+
throw new Throwable(`Response validation failed: ${errorMessages}`, 500);
|
|
124
|
+
}
|
|
91
125
|
}
|
|
92
126
|
}
|
|
93
|
-
}
|
|
94
127
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
128
|
+
if (validationType) {
|
|
129
|
+
const buildTarget = (payload: any) => {
|
|
130
|
+
if (typeof validationType === 'function' && 'prototype' in validationType) {
|
|
131
|
+
const target = new validationType(payload)
|
|
132
|
+
// Plain decorated classes often have no constructor that
|
|
133
|
+
// copies the payload; assign own fields as a fallback.
|
|
134
|
+
if (payload && typeof payload === 'object') {
|
|
135
|
+
for (const [key, value] of Object.entries(payload)) {
|
|
136
|
+
if (!(key in target) || target[key] === undefined) {
|
|
137
|
+
try {
|
|
138
|
+
target[key] = value
|
|
139
|
+
} catch {
|
|
140
|
+
// Read-only property: keep the constructor's value.
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return target
|
|
146
|
+
}
|
|
147
|
+
return payload
|
|
148
|
+
}
|
|
98
149
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
150
|
+
if (isArray && Array.isArray(responses)) {
|
|
151
|
+
for (const item of responses) {
|
|
152
|
+
const validationTarget = buildTarget(item);
|
|
153
|
+
const errors = await validate(validationTarget);
|
|
154
|
+
if (errors.length > 0) {
|
|
155
|
+
const errorMessages = errors.map(err =>
|
|
156
|
+
Object.values(err.constraints || {}).join(', ')
|
|
157
|
+
).join('; ');
|
|
158
|
+
throw new Throwable(`Response validation failed: ${errorMessages}`, 500);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
const validationTarget = buildTarget(responses);
|
|
106
163
|
const errors = await validate(validationTarget);
|
|
107
164
|
if (errors.length > 0) {
|
|
108
165
|
const errorMessages = errors.map(err =>
|
|
@@ -111,24 +168,23 @@ export async function validateResponse(responses: any, instance: any, routeMeta:
|
|
|
111
168
|
throw new Throwable(`Response validation failed: ${errorMessages}`, 500);
|
|
112
169
|
}
|
|
113
170
|
}
|
|
114
|
-
} else {
|
|
115
|
-
const validationTarget = typeof successResponse.type === 'function' && 'prototype' in successResponse.type
|
|
116
|
-
? new successResponse.type(responses)
|
|
117
|
-
: responses;
|
|
118
|
-
const errors = await validate(validationTarget);
|
|
119
|
-
if (errors.length > 0) {
|
|
120
|
-
const errorMessages = errors.map(err =>
|
|
121
|
-
Object.values(err.constraints || {}).join(', ')
|
|
122
|
-
).join('; ');
|
|
123
|
-
throw new Throwable(`Response validation failed: ${errorMessages}`, 500);
|
|
124
|
-
}
|
|
125
171
|
}
|
|
126
172
|
}
|
|
127
173
|
}
|
|
128
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Compatibilité : compile puis exécute le validateur de réponse.
|
|
177
|
+
*
|
|
178
|
+
* @deprecated Utilisez `createResponseValidator()` à la compilation du handler
|
|
179
|
+
* pour éviter de relire les métadonnées à chaque requête.
|
|
180
|
+
*/
|
|
181
|
+
export async function validateResponse(responses: any, instance: any, routeMeta: RouteMetaInterface): Promise<void> {
|
|
182
|
+
await createResponseValidator(instance, routeMeta)(responses)
|
|
183
|
+
}
|
|
184
|
+
|
|
129
185
|
// Helper function for running middlewares
|
|
130
186
|
export async function runMiddlewares(middlewares: MiddlewareCallable[], ctx: any): Promise<void> {
|
|
131
187
|
if (middlewares.length > 0) {
|
|
132
188
|
await middlewareCompose(middlewares)(ctx);
|
|
133
189
|
}
|
|
134
|
-
}
|
|
190
|
+
}
|
|
@@ -1,10 +1,44 @@
|
|
|
1
|
-
import {Route} from './
|
|
1
|
+
import {normalizeRoutePath, Route} from './route.ts'
|
|
2
2
|
|
|
3
|
+
export interface RouteResolution {
|
|
4
|
+
route: Route
|
|
5
|
+
params: Record<string, string>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface TrieNode {
|
|
9
|
+
static: Map<string, TrieNode>
|
|
10
|
+
param?: TrieNode
|
|
11
|
+
routes: Route[]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface RouteCandidate {
|
|
15
|
+
route: Route
|
|
16
|
+
values: string[]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function createNode(): TrieNode {
|
|
20
|
+
return {static: new Map(), routes: []}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Index de routes à coût prévisible.
|
|
25
|
+
*
|
|
26
|
+
* - Les routes statiques sont résolues par un `Map` (O(1)).
|
|
27
|
+
* - Les routes paramétriques utilisent un trie par segment (O(segments)).
|
|
28
|
+
* - Les motifs sont compilés par `Route` à l'enregistrement.
|
|
29
|
+
*
|
|
30
|
+
* Toutes les opérations (`add`, `remove`, `replace`, `clear`) maintiennent les
|
|
31
|
+
* trois index (toutes les routes, statiques, trie) de façon cohérente, ce qui
|
|
32
|
+
* rend le rechargement HMR sûr.
|
|
33
|
+
*/
|
|
3
34
|
export class RouteMatcher {
|
|
4
35
|
protected _routes = new Map<string, Route>()
|
|
36
|
+
protected _static = new Map<string, Route>()
|
|
37
|
+
protected _tries = new Map<string, TrieNode>()
|
|
38
|
+
protected _order = 0
|
|
5
39
|
|
|
6
40
|
protected getKey(route: Route): string {
|
|
7
|
-
return `${route.method}:${route.
|
|
41
|
+
return `${route.method}:${route.fullPath}`
|
|
8
42
|
}
|
|
9
43
|
|
|
10
44
|
get routes(): Map<string, Route> {
|
|
@@ -12,8 +46,46 @@ export class RouteMatcher {
|
|
|
12
46
|
}
|
|
13
47
|
|
|
14
48
|
add(route: Route): this {
|
|
15
|
-
this.
|
|
16
|
-
|
|
49
|
+
const key = this.getKey(route)
|
|
50
|
+
const previous = this._routes.get(key)
|
|
51
|
+
if (previous) this.remove(previous)
|
|
52
|
+
|
|
53
|
+
if (!route.order || route.order === 0) {
|
|
54
|
+
// Routes created outside Router (legacy callers) still receive a
|
|
55
|
+
// stable insertion order for tie-breaking.
|
|
56
|
+
(route as any).order = ++this._order
|
|
57
|
+
} else {
|
|
58
|
+
this._order = Math.max(this._order, route.order)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
this._routes.set(key, route)
|
|
62
|
+
|
|
63
|
+
if (route.isStatic) {
|
|
64
|
+
this._static.set(key, route)
|
|
65
|
+
return this
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let node = this._tries.get(route.method)
|
|
69
|
+
if (!node) {
|
|
70
|
+
node = createNode()
|
|
71
|
+
this._tries.set(route.method, node)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const segment of route.segments) {
|
|
75
|
+
if (segment.startsWith(':') && segment.length > 1) {
|
|
76
|
+
node = node.param ??= createNode()
|
|
77
|
+
} else {
|
|
78
|
+
let child = node.static.get(segment)
|
|
79
|
+
if (!child) {
|
|
80
|
+
child = createNode()
|
|
81
|
+
node.static.set(segment, child)
|
|
82
|
+
}
|
|
83
|
+
node = child
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
node.routes.push(route)
|
|
88
|
+
return this
|
|
17
89
|
}
|
|
18
90
|
|
|
19
91
|
has(route: Route): boolean {
|
|
@@ -25,43 +97,142 @@ export class RouteMatcher {
|
|
|
25
97
|
}
|
|
26
98
|
|
|
27
99
|
replace(route: Route): this {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return this
|
|
100
|
+
const key = this.getKey(route)
|
|
101
|
+
const previous = this._routes.get(key)
|
|
102
|
+
if (previous) this.remove(previous)
|
|
103
|
+
return this.add(route)
|
|
32
104
|
}
|
|
33
105
|
|
|
34
106
|
upsert(route: Route): this {
|
|
35
|
-
this.
|
|
36
|
-
return this
|
|
107
|
+
return this.replace(route)
|
|
37
108
|
}
|
|
38
109
|
|
|
39
110
|
remove(route: Route): this {
|
|
40
|
-
this.
|
|
41
|
-
|
|
111
|
+
const key = this.getKey(route)
|
|
112
|
+
const registered = this._routes.get(key)
|
|
113
|
+
if (!registered) return this
|
|
114
|
+
|
|
115
|
+
this._routes.delete(key)
|
|
116
|
+
this._static.delete(key)
|
|
117
|
+
|
|
118
|
+
if (registered.isStatic || !registered.segments.length) return this
|
|
119
|
+
|
|
120
|
+
const root = this._tries.get(registered.method)
|
|
121
|
+
if (!root) return this
|
|
122
|
+
|
|
123
|
+
this.removeFromTrie(root, registered.segments, 0, registered)
|
|
124
|
+
|
|
125
|
+
if (!root.routes.length && !root.param && !root.static.size) {
|
|
126
|
+
this._tries.delete(registered.method)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return this
|
|
42
130
|
}
|
|
43
131
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
132
|
+
protected removeFromTrie(node: TrieNode, segments: readonly string[], index: number, route: Route): boolean {
|
|
133
|
+
if (index === segments.length) {
|
|
134
|
+
const position = node.routes.indexOf(route)
|
|
135
|
+
if (position !== -1) node.routes.splice(position, 1)
|
|
136
|
+
} else {
|
|
137
|
+
const segment = segments[index]
|
|
138
|
+
if (segment.startsWith(':') && segment.length > 1) {
|
|
139
|
+
if (node.param && this.removeFromTrie(node.param, segments, index + 1, route)) {
|
|
140
|
+
if (!node.param.routes.length && !node.param.param && !node.param.static.size) {
|
|
141
|
+
node.param = undefined
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
} else {
|
|
145
|
+
const child = node.static.get(segment)
|
|
146
|
+
if (child && this.removeFromTrie(child, segments, index + 1, route)) {
|
|
147
|
+
if (!child.routes.length && !child.param && !child.static.size) {
|
|
148
|
+
node.static.delete(segment)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return !node.routes.length && !node.param && !node.static.size
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Résout une requête sans muter la `Route` : les paramètres sont propres à
|
|
159
|
+
* l'appel et peuvent être conservés par le contexte de la requête.
|
|
160
|
+
*/
|
|
161
|
+
resolve(method: string, url: string): RouteResolution | null {
|
|
162
|
+
const path = normalizeRoutePath(url)
|
|
163
|
+
const staticRoute = this._static.get(`${method}:${path}`)
|
|
164
|
+
if (staticRoute) return {route: staticRoute, params: EMPTY_PARAMS}
|
|
165
|
+
|
|
166
|
+
const root = this._tries.get(method)
|
|
167
|
+
if (!root) return null
|
|
168
|
+
|
|
169
|
+
const segments = path === '/' ? EMPTY_SEGMENTS : path.slice(1).split('/')
|
|
170
|
+
const candidates: RouteCandidate[] = []
|
|
171
|
+
const values: string[] = []
|
|
172
|
+
this.collect(root, segments, 0, values, candidates)
|
|
173
|
+
|
|
174
|
+
if (!candidates.length) return null
|
|
47
175
|
|
|
48
|
-
|
|
49
|
-
|
|
176
|
+
let best = candidates[0]
|
|
177
|
+
for (let index = 1; index < candidates.length; index++) {
|
|
178
|
+
const candidate = candidates[index]
|
|
179
|
+
const candidateCount = candidate.route.paramNames.length
|
|
180
|
+
const bestCount = best.route.paramNames.length
|
|
181
|
+
if (candidateCount < bestCount) {
|
|
182
|
+
best = candidate
|
|
183
|
+
} else if (candidateCount === bestCount && candidate.route.order < best.route.order) {
|
|
184
|
+
best = candidate
|
|
185
|
+
}
|
|
186
|
+
}
|
|
50
187
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
188
|
+
if (!best.route.paramNames.length) return {route: best.route, params: EMPTY_PARAMS}
|
|
189
|
+
|
|
190
|
+
const params: Record<string, string> = {}
|
|
191
|
+
for (let index = 0; index < best.route.paramNames.length; index++) {
|
|
192
|
+
params[best.route.paramNames[index]] = best.values[index] ?? ''
|
|
193
|
+
}
|
|
55
194
|
|
|
56
|
-
|
|
195
|
+
return {route: best.route, params}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
protected collect(
|
|
199
|
+
node: TrieNode,
|
|
200
|
+
segments: readonly string[],
|
|
201
|
+
index: number,
|
|
202
|
+
values: string[],
|
|
203
|
+
candidates: RouteCandidate[],
|
|
204
|
+
): void {
|
|
205
|
+
if (index === segments.length) {
|
|
206
|
+
for (const route of node.routes) {
|
|
207
|
+
candidates.push({route, values: [...values]})
|
|
57
208
|
}
|
|
209
|
+
return
|
|
58
210
|
}
|
|
59
211
|
|
|
60
|
-
|
|
212
|
+
const segment = segments[index]
|
|
213
|
+
const staticChild = node.static.get(segment)
|
|
214
|
+
if (staticChild) this.collect(staticChild, segments, index + 1, values, candidates)
|
|
215
|
+
|
|
216
|
+
if (node.param) {
|
|
217
|
+
values.push(segment)
|
|
218
|
+
this.collect(node.param, segments, index + 1, values, candidates)
|
|
219
|
+
values.pop()
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Compatibilité : renvoie la route sans exposer les paramètres. */
|
|
224
|
+
match(method: string, url: string): Route | null {
|
|
225
|
+
return this.resolve(method, url)?.route ?? null
|
|
61
226
|
}
|
|
62
227
|
|
|
63
228
|
clear(): this {
|
|
64
229
|
this._routes.clear()
|
|
65
|
-
|
|
230
|
+
this._static.clear()
|
|
231
|
+
this._tries.clear()
|
|
232
|
+
this._order = 0
|
|
233
|
+
return this
|
|
66
234
|
}
|
|
67
235
|
}
|
|
236
|
+
|
|
237
|
+
const EMPTY_PARAMS: Record<string, string> = Object.freeze({}) as Record<string, string>
|
|
238
|
+
const EMPTY_SEGMENTS: readonly string[] = Object.freeze([])
|
|
@@ -1,63 +1,132 @@
|
|
|
1
1
|
import type {RouteDefinitionInterface} from '../../types/router.ts'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Normalise un chemin de route : slash initial, pas de slashs doublés, pas de
|
|
5
|
+
* slash final (sauf pour la racine).
|
|
6
|
+
*/
|
|
7
|
+
export function normalizeRoutePath(value: string): string {
|
|
8
|
+
let path = value || '/'
|
|
9
|
+
if (!path.startsWith('/')) path = `/${path}`
|
|
10
|
+
path = path.replace(/\/+/g, '/')
|
|
11
|
+
if (path.length > 1) path = path.replace(/\/+$/, '')
|
|
12
|
+
return path || '/'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function escapeRegExp(value: string): string {
|
|
16
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Une route compilée une seule fois à l'enregistrement.
|
|
21
|
+
*
|
|
22
|
+
* Les motifs (`regex`, segments, noms de paramètres) sont calculés dans le
|
|
23
|
+
* constructeur : aucun travail de compilation n'est effectué pendant le
|
|
24
|
+
* traitement d'une requête. Les paramètres extraits sont renvoyés dans un objet
|
|
25
|
+
* neuf, jamais stockés sur l'instance partagée.
|
|
26
|
+
*/
|
|
3
27
|
export class Route {
|
|
4
28
|
method: string
|
|
5
29
|
path: string
|
|
6
30
|
version?: string
|
|
7
31
|
handler: RouteDefinitionInterface['handler']
|
|
32
|
+
|
|
33
|
+
/** Chemin complet normalisé (version incluse). */
|
|
34
|
+
readonly fullPath: string
|
|
35
|
+
/** Segments du chemin complet, sans segment vide. */
|
|
36
|
+
readonly segments: readonly string[]
|
|
37
|
+
/** Noms des paramètres, dans l'ordre du chemin. */
|
|
38
|
+
readonly paramNames: readonly string[]
|
|
39
|
+
/** Vrai lorsqu'aucun segment paramétrique n'est présent. */
|
|
40
|
+
readonly isStatic: boolean
|
|
41
|
+
/** Motif compilé pour les routes paramétriques. */
|
|
42
|
+
readonly pattern?: RegExp
|
|
43
|
+
/** Ordre d'enregistrement (départage à spécificité égale). */
|
|
44
|
+
readonly order: number
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Conservé pour compatibilité : plus jamais muté par le matching.
|
|
48
|
+
* Utilisez `Router.resolve()` / `Route.extractParams()` pour obtenir les
|
|
49
|
+
* paramètres propres à une requête.
|
|
50
|
+
*/
|
|
8
51
|
parameters: Record<string, string> = {}
|
|
9
52
|
|
|
10
|
-
constructor(def: RouteDefinitionInterface) {
|
|
53
|
+
constructor(def: RouteDefinitionInterface, order = 0) {
|
|
11
54
|
this.method = def.method
|
|
12
55
|
this.path = def.path
|
|
13
56
|
this.version = def.version
|
|
14
57
|
this.handler = def.handler
|
|
58
|
+
this.order = order
|
|
59
|
+
|
|
60
|
+
const fullPath = normalizeRoutePath(this.version ? `/${this.version}${this.path}` : this.path)
|
|
61
|
+
this.fullPath = fullPath
|
|
62
|
+
this.segments = fullPath === '/' ? [] : fullPath.slice(1).split('/')
|
|
63
|
+
|
|
64
|
+
const names: string[] = []
|
|
65
|
+
let isStatic = true
|
|
66
|
+
let pattern = ''
|
|
67
|
+
|
|
68
|
+
for (const segment of this.segments) {
|
|
69
|
+
if (segment.startsWith(':') && segment.length > 1) {
|
|
70
|
+
names.push(segment.slice(1))
|
|
71
|
+
pattern += '/([^/]+)'
|
|
72
|
+
isStatic = false
|
|
73
|
+
} else {
|
|
74
|
+
pattern += `/${escapeRegExp(segment)}`
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
this.paramNames = names
|
|
79
|
+
this.isStatic = isStatic
|
|
80
|
+
this.pattern = isStatic ? undefined : new RegExp(`^${pattern || '/'}$`)
|
|
15
81
|
}
|
|
16
82
|
|
|
83
|
+
/** @deprecated Conservé pour compatibilité ; les noms sont précompilés. */
|
|
17
84
|
get params(): string[] {
|
|
18
|
-
|
|
19
|
-
const matches = fullPath.match(/:[a-zA-Z0-9_]+/g)
|
|
20
|
-
return matches ? matches.map(m => m.substring(1)) : []
|
|
85
|
+
return [...this.paramNames]
|
|
21
86
|
}
|
|
22
87
|
|
|
23
88
|
get name(): string {
|
|
24
89
|
return this.handler.name
|
|
25
90
|
}
|
|
26
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Compatibilité : renvoie une vue de la route portant les paramètres
|
|
94
|
+
* extraits. L'instance partagée n'est jamais mutée.
|
|
95
|
+
*/
|
|
27
96
|
extractParams(url: string): this {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
const regexPath = fullPath.replace(/:[a-zA-Z0-9_]+/g, '([^/]+)')
|
|
34
|
-
const regex = new RegExp(`^${regexPath || '/'}/?$`)
|
|
35
|
-
const matches = url.match(regex)
|
|
36
|
-
|
|
37
|
-
if (matches) {
|
|
38
|
-
const params: Record<string, string> = {}
|
|
39
|
-
paramNames.forEach((name, index) => {
|
|
40
|
-
params[name] = matches[index + 1]
|
|
41
|
-
})
|
|
42
|
-
this.parameters = params
|
|
43
|
-
}
|
|
97
|
+
if (this.isStatic) return this
|
|
98
|
+
|
|
99
|
+
const params = this.extractParamsFromPath(normalizeRoutePath(url))
|
|
100
|
+
if (!params) return this
|
|
44
101
|
|
|
45
|
-
return this
|
|
102
|
+
return Object.assign(Object.create(this), {parameters: params}) as this
|
|
46
103
|
}
|
|
47
104
|
|
|
48
|
-
|
|
49
|
-
|
|
105
|
+
/** Extrait les paramètres d'un chemin déjà normalisé. */
|
|
106
|
+
extractParamsFromPath(path: string): Record<string, string> | null {
|
|
107
|
+
if (this.isStatic || !this.pattern) {
|
|
108
|
+
return path === this.fullPath ? {} : null
|
|
109
|
+
}
|
|
50
110
|
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
const cleanUrl = url.replace(/\/+$/, '')
|
|
111
|
+
const matches = this.pattern.exec(path)
|
|
112
|
+
if (!matches) return null
|
|
54
113
|
|
|
55
|
-
|
|
56
|
-
|
|
114
|
+
const params: Record<string, string> = {}
|
|
115
|
+
for (let index = 0; index < this.paramNames.length; index++) {
|
|
116
|
+
params[this.paramNames[index]] = matches[index + 1]
|
|
57
117
|
}
|
|
118
|
+
return params
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Vrai si la route correspond au chemin, quelle que soit la méthode. */
|
|
122
|
+
matchesPath(url: string): boolean {
|
|
123
|
+
const path = normalizeRoutePath(url)
|
|
124
|
+
if (this.isStatic) return this.fullPath === path
|
|
125
|
+
return Boolean(this.pattern?.test(path))
|
|
126
|
+
}
|
|
58
127
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return
|
|
128
|
+
match(method: string, url: string): boolean {
|
|
129
|
+
if (method !== this.method) return false
|
|
130
|
+
return this.matchesPath(url)
|
|
62
131
|
}
|
|
63
132
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {RouteHandlerCallable} from '../../types/index.ts'
|
|
2
|
-
import {HttpMethod} from "../../framework/enums/
|
|
2
|
+
import {HttpMethod} from "../../framework/enums/http-method.enum.ts";
|
|
3
3
|
import {Route} from './route.ts'
|
|
4
4
|
import {RouteMatcher} from './matcher.ts'
|
|
5
5
|
|
|
@@ -21,10 +21,35 @@ export class Router {
|
|
|
21
21
|
this.matcher.remove(route)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
replace(route: Route) {
|
|
25
|
+
this.matcher.replace(route)
|
|
26
|
+
return route
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Compatibilité : renvoie la `Route` correspondante. Les paramètres ne sont
|
|
31
|
+
* plus stockés sur l'instance ; utilisez `resolve()` pour les obtenir.
|
|
32
|
+
*/
|
|
24
33
|
match(method: string, url: string) {
|
|
25
34
|
return this.matcher.match(method, url)
|
|
26
35
|
}
|
|
27
36
|
|
|
37
|
+
/** Résout la route et les paramètres propres à cette requête. */
|
|
38
|
+
resolve(method: string, url: string) {
|
|
39
|
+
return this.matcher.resolve(method, url)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Return a route matching the URL regardless of method. Used to answer 405
|
|
44
|
+
* instead of 404 when the path exists.
|
|
45
|
+
*/
|
|
46
|
+
matchPath(url: string) {
|
|
47
|
+
for (const route of this.matcher.routes.values()) {
|
|
48
|
+
if (route.matchesPath(url)) return route;
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
28
53
|
reset() {
|
|
29
54
|
this.matcher.clear()
|
|
30
55
|
}
|
|
@@ -32,4 +57,4 @@ export class Router {
|
|
|
32
57
|
getRoutes(): Route[] {
|
|
33
58
|
return Array.from(this.matcher.routes.values())
|
|
34
59
|
}
|
|
35
|
-
}
|
|
60
|
+
}
|