raiton 6.8.9-beta.25 → 6.10.0-beta.28
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 +44 -0
- package/README.md +46 -1
- package/benchmarks/README.md +16 -0
- package/benchmarks/latest.json +172 -0
- package/benchmarks/latest.md +66 -0
- package/build/bin/index.mjs +33550 -21725
- 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/client.command.d.ts +7 -0
- package/build/types/commands/contract.command.d.ts +8 -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/generate.command.d.ts +7 -0
- package/build/types/commands/inspect.command.d.ts +7 -0
- package/build/types/commands/mcp.command.d.ts +11 -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/routes.command.d.ts +7 -0
- package/build/types/commands/start.command.d.ts +9 -0
- package/build/types/commands/why.command.d.ts +7 -0
- package/build/types/contracts/decorators.d.ts +41 -0
- package/build/types/contracts/errors.d.ts +42 -0
- package/build/types/contracts/index.d.ts +4 -0
- package/build/types/contracts/schema.d.ts +70 -0
- package/build/types/contracts/typebox.d.ts +81 -0
- package/build/types/core/application.d.ts +37 -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/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 +42 -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 +177 -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 +62 -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 +26 -0
- package/docs/generated/config-schema.json +150 -0
- package/docs/generated/manifest-schema.json +236 -0
- package/docs/generated/manifest.json +101 -0
- package/docs/llms-full.txt +1271 -0
- package/docs/llms.txt +46 -0
- package/docs/user-guide/README.md +18 -12
- package/docs/user-guide/cli.md +21 -0
- package/docs/user-guide/config.md +14 -0
- package/docs/user-guide/contracts.md +67 -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 +40 -7
- 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 +23 -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/client.command.ts +64 -0
- package/source/commands/contract.command.ts +104 -0
- package/source/commands/docs.command.ts +263 -0
- package/source/commands/doctor.command.ts +226 -0
- package/source/commands/generate.command.ts +58 -0
- package/source/commands/inspect.command.ts +108 -0
- package/source/commands/mcp.command.ts +258 -0
- package/source/commands/new.command.ts +135 -0
- package/source/commands/openapi.command.ts +95 -0
- package/source/commands/routes.command.ts +39 -0
- package/source/commands/why.command.ts +61 -0
- package/source/contracts/decorators.ts +151 -0
- package/source/contracts/errors.ts +76 -0
- package/source/contracts/index.ts +4 -0
- package/source/contracts/schema.ts +377 -0
- package/source/contracts/typebox.ts +193 -0
- package/source/core/application.ts +215 -73
- package/source/core/builder.ts +6 -2
- package/source/core/config/config.ts +144 -26
- package/source/core/config/define.ts +35 -8
- package/source/core/config/index.ts +2 -1
- package/source/core/config/schema.ts +263 -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 +91 -31
- 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/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 +159 -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 +131 -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 +705 -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 +52 -0
- package/source/manifest/manifest.ts +341 -0
- package/source/manifest/schema.ts +149 -0
- package/source/types/builder.ts +5 -0
- package/source/types/config.ts +64 -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
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import {METADATA_KEYS} from "../framework/constants/decorators.constant.ts";
|
|
3
|
+
import {
|
|
4
|
+
type ContractSchema,
|
|
5
|
+
isContractSchema,
|
|
6
|
+
type ContractIssue,
|
|
7
|
+
validateContract,
|
|
8
|
+
} from "./schema.ts";
|
|
9
|
+
|
|
10
|
+
export interface RouteInputContract {
|
|
11
|
+
body?: ContractSchema;
|
|
12
|
+
query?: ContractSchema;
|
|
13
|
+
params?: ContractSchema;
|
|
14
|
+
headers?: ContractSchema;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface RouteContract {
|
|
18
|
+
input?: ContractSchema | RouteInputContract;
|
|
19
|
+
output?: ContractSchema;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const INPUT_PARTS = ["body", "query", "params", "headers"] as const;
|
|
23
|
+
|
|
24
|
+
export function isCompositeInput(input: any): input is RouteInputContract {
|
|
25
|
+
if (!input || typeof input !== "object") return false;
|
|
26
|
+
if (isContractSchema(input)) return false;
|
|
27
|
+
return INPUT_PARTS.some(part => part in input);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function writeContract(target: any, propertyKey: string | symbol, patch: Partial<RouteContract>) {
|
|
31
|
+
const current: RouteContract = Reflect.getOwnMetadata(
|
|
32
|
+
METADATA_KEYS.CONTRACT_INPUT,
|
|
33
|
+
target,
|
|
34
|
+
propertyKey,
|
|
35
|
+
) ?? {};
|
|
36
|
+
Reflect.defineMetadata(
|
|
37
|
+
METADATA_KEYS.CONTRACT_INPUT,
|
|
38
|
+
{...current, ...patch},
|
|
39
|
+
target,
|
|
40
|
+
propertyKey,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Declare the input contract of a route. A single schema validates the request
|
|
46
|
+
* body; an object with `body`/`query`/`params`/`headers` keys validates each
|
|
47
|
+
* part independently.
|
|
48
|
+
*/
|
|
49
|
+
export function Input(input: ContractSchema | RouteInputContract) {
|
|
50
|
+
return (target: any, propertyKey: string | symbol) => {
|
|
51
|
+
writeContract(target, propertyKey, {input});
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Declare the output contract of a route. Enforced in development only by
|
|
57
|
+
* default; see `validation.responses` configuration.
|
|
58
|
+
*/
|
|
59
|
+
export function Output(output: ContractSchema) {
|
|
60
|
+
return (target: any, propertyKey: string | symbol) => {
|
|
61
|
+
writeContract(target, propertyKey, {output});
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function getRouteContract(target: any, propertyKey?: string | symbol): RouteContract | undefined {
|
|
66
|
+
if (!target) return undefined;
|
|
67
|
+
const holder = target.prototype ?? target;
|
|
68
|
+
const contract = propertyKey !== undefined
|
|
69
|
+
? (Reflect.getOwnMetadata(METADATA_KEYS.CONTRACT_INPUT, holder, propertyKey)
|
|
70
|
+
?? Reflect.getOwnMetadata(METADATA_KEYS.CONTRACT_INPUT, target, propertyKey))
|
|
71
|
+
: Reflect.getOwnMetadata(METADATA_KEYS.CONTRACT_INPUT, holder);
|
|
72
|
+
return contract as RouteContract | undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface RouteInputValidationResult {
|
|
76
|
+
success: boolean;
|
|
77
|
+
issues: ContractIssue[];
|
|
78
|
+
body?: any;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function headersToObject(headers: any): Record<string, string> {
|
|
82
|
+
if (!headers) return {};
|
|
83
|
+
if (typeof headers.entries === "function") {
|
|
84
|
+
return Object.fromEntries([...headers.entries()].map(([key, value]: [string, any]) => [key, String(value)]));
|
|
85
|
+
}
|
|
86
|
+
return Object.fromEntries(Object.entries(headers).map(([key, value]) => [key, String(value)]));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Validate every declared part of a route input without mutating the request.
|
|
91
|
+
*/
|
|
92
|
+
export async function validateRouteInput(
|
|
93
|
+
contract: RouteContract | undefined,
|
|
94
|
+
ctx: any,
|
|
95
|
+
): Promise<RouteInputValidationResult> {
|
|
96
|
+
if (!contract?.input) return {success: true, issues: []};
|
|
97
|
+
|
|
98
|
+
const issues: ContractIssue[] = [];
|
|
99
|
+
let body = ctx?.req?.body;
|
|
100
|
+
|
|
101
|
+
if (isCompositeInput(contract.input)) {
|
|
102
|
+
for (const part of INPUT_PARTS) {
|
|
103
|
+
const schema = contract.input[part];
|
|
104
|
+
if (!schema) continue;
|
|
105
|
+
const value = part === "body"
|
|
106
|
+
? ctx?.req?.body
|
|
107
|
+
: part === "query"
|
|
108
|
+
? ctx?.req?.query
|
|
109
|
+
: part === "params"
|
|
110
|
+
? (ctx?.params ?? ctx?.req?.params)
|
|
111
|
+
: headersToObject(ctx?.req?.headers);
|
|
112
|
+
const result = await validateContract(schema, value);
|
|
113
|
+
if (!result.success) {
|
|
114
|
+
for (const issue of result.issues) {
|
|
115
|
+
issues.push({...issue, path: issue.path ? `${part}.${issue.path}` : part});
|
|
116
|
+
}
|
|
117
|
+
} else if (part === "body") {
|
|
118
|
+
body = result.value;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
const result = await validateContract(contract.input, ctx?.req?.body);
|
|
123
|
+
if (!result.success) {
|
|
124
|
+
// A single schema targets the request body: field paths stay
|
|
125
|
+
// relative (`email`, `items[0].name`), as documented.
|
|
126
|
+
issues.push(...result.issues);
|
|
127
|
+
} else {
|
|
128
|
+
body = result.value;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {success: issues.length === 0, issues, body};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export async function validateRouteOutput(
|
|
136
|
+
contract: RouteContract | undefined,
|
|
137
|
+
value: any,
|
|
138
|
+
): Promise<{success: boolean; issues: ContractIssue[]}> {
|
|
139
|
+
if (!contract?.output) return {success: true, issues: []};
|
|
140
|
+
// `@ApiResponse` style wrappers are unwrapped so the contract targets the
|
|
141
|
+
// actual payload rather than transport metadata.
|
|
142
|
+
const candidate = value && typeof value === "object" && "data" in value && "statusCode" in value
|
|
143
|
+
? (value as any).data
|
|
144
|
+
: value;
|
|
145
|
+
const result = await validateContract(contract.output, candidate);
|
|
146
|
+
return {success: result.success, issues: result.issues};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export const RouteContractMetadata = {
|
|
150
|
+
contractKey: METADATA_KEYS.CONTRACT_INPUT,
|
|
151
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type {ContractIssue} from "./schema.ts";
|
|
2
|
+
|
|
3
|
+
export const DOCS_BASE_URL = "https://github.com/protorians/raiton/blob/main/docs";
|
|
4
|
+
|
|
5
|
+
export function docsUrl(path = "user-guide/README.md"): string {
|
|
6
|
+
return `${DOCS_BASE_URL}/${path.replace(/^\//, "")}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ValidationErrorBody {
|
|
10
|
+
error: true;
|
|
11
|
+
code: "VALIDATION_ERROR";
|
|
12
|
+
message: string;
|
|
13
|
+
statusCode: number;
|
|
14
|
+
details: ContractIssue[];
|
|
15
|
+
docsUrl: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Stable validation error payload shared by runtime validation and generated
|
|
20
|
+
* clients. Never includes a stack trace or internal field.
|
|
21
|
+
*/
|
|
22
|
+
export function validationErrorBody(
|
|
23
|
+
issues: ContractIssue[],
|
|
24
|
+
message = "Invalid request",
|
|
25
|
+
): ValidationErrorBody {
|
|
26
|
+
return {
|
|
27
|
+
error: true,
|
|
28
|
+
code: "VALIDATION_ERROR",
|
|
29
|
+
message,
|
|
30
|
+
statusCode: 400,
|
|
31
|
+
details: issues,
|
|
32
|
+
docsUrl: docsUrl("user-guide/recipes/contracts.md"),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Thrown before the route handler executes when the input contract fails.
|
|
38
|
+
*/
|
|
39
|
+
export class ContractValidationError extends Error {
|
|
40
|
+
public readonly statusCode = 400;
|
|
41
|
+
|
|
42
|
+
constructor(public readonly issues: ContractIssue[], message = "Invalid request") {
|
|
43
|
+
super(message);
|
|
44
|
+
this.name = "ContractValidationError";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
render() {
|
|
48
|
+
return validationErrorBody(this.issues, this.message);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Thrown in development when a response does not match its `@Output` contract.
|
|
54
|
+
*/
|
|
55
|
+
export class OutputContractError extends Error {
|
|
56
|
+
public readonly statusCode = 500;
|
|
57
|
+
|
|
58
|
+
constructor(
|
|
59
|
+
public readonly route: string,
|
|
60
|
+
public readonly issues: ContractIssue[],
|
|
61
|
+
) {
|
|
62
|
+
super(`Response contract violation on ${route}`);
|
|
63
|
+
this.name = "OutputContractError";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
render() {
|
|
67
|
+
return {
|
|
68
|
+
error: true,
|
|
69
|
+
code: "OUTPUT_VALIDATION_ERROR",
|
|
70
|
+
statusCode: 500,
|
|
71
|
+
message: `Response contract violation on ${this.route}`,
|
|
72
|
+
details: this.issues,
|
|
73
|
+
docsUrl: docsUrl("user-guide/recipes/contracts.md"),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import {FormatRegistry, Kind, Type as TypeBox, type TSchema} from "@sinclair/typebox";
|
|
2
|
+
import {Value} from "@sinclair/typebox/value";
|
|
3
|
+
|
|
4
|
+
// TypeBox 0.34 ships an empty format registry. Register the formats documented
|
|
5
|
+
// by the framework so `t.string().email()` is validated consistently.
|
|
6
|
+
const FORMATS: Record<string, RegExp> = {
|
|
7
|
+
email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
8
|
+
hostname: /^(?=.{1,253}$)([a-zA-Z0-9-]{1,63}\.)*[a-zA-Z0-9-]{1,63}$/,
|
|
9
|
+
ipv4: /^(\d{1,3}\.){3}\d{1,3}$/,
|
|
10
|
+
ipv6: /^[0-9a-fA-F:]+$/,
|
|
11
|
+
uuid: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/,
|
|
12
|
+
uri: /^[a-zA-Z][a-zA-Z0-9+.-]*:/,
|
|
13
|
+
"date-time": /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/,
|
|
14
|
+
date: /^\d{4}-\d{2}-\d{2}$/,
|
|
15
|
+
time: /^\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?$/,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
for (const [format, pattern] of Object.entries(FORMATS)) {
|
|
19
|
+
if (!FormatRegistry.Has(format)) {
|
|
20
|
+
FormatRegistry.Set(format, value => pattern.test(value));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Minimal Standard Schema v1 surface. We only rely on the `validate` method so
|
|
26
|
+
* Zod, Valibot, ArkType and friends can be plugged without a hard dependency.
|
|
27
|
+
*/
|
|
28
|
+
export interface StandardSchemaIssue {
|
|
29
|
+
path?: ReadonlyArray<PropertyKey | {key: PropertyKey}>;
|
|
30
|
+
message?: string;
|
|
31
|
+
code?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface StandardSchemaV1<T = unknown> {
|
|
35
|
+
readonly "~standard": {
|
|
36
|
+
readonly version: number;
|
|
37
|
+
readonly vendor: string;
|
|
38
|
+
readonly validate: (value: unknown) => StandardSchemaResult<T> | Promise<StandardSchemaResult<T>>;
|
|
39
|
+
readonly jsonSchema?: Record<string, unknown>;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type StandardSchemaResult<T = unknown> =
|
|
44
|
+
| {value: T; issues?: undefined}
|
|
45
|
+
| {issues: ReadonlyArray<StandardSchemaIssue>; value?: undefined};
|
|
46
|
+
|
|
47
|
+
export type ContractSchema = TSchema | StandardSchemaV1<any>;
|
|
48
|
+
|
|
49
|
+
export interface ContractIssue {
|
|
50
|
+
path: string;
|
|
51
|
+
code: string;
|
|
52
|
+
message: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ContractValidationResult<T = unknown> {
|
|
56
|
+
success: boolean;
|
|
57
|
+
value?: T;
|
|
58
|
+
issues: ContractIssue[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function isStandardSchema(value: any): value is StandardSchemaV1 {
|
|
62
|
+
return !!value
|
|
63
|
+
&& typeof value === "object"
|
|
64
|
+
&& "~standard" in value
|
|
65
|
+
&& typeof (value as any)["~standard"]?.validate === "function";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function isTypeBoxSchema(value: any): value is TSchema {
|
|
69
|
+
if (!value || typeof value !== "object") return false;
|
|
70
|
+
if (isStandardSchema(value)) return false;
|
|
71
|
+
return Kind in value
|
|
72
|
+
|| typeof value.type === "string"
|
|
73
|
+
|| Array.isArray(value.type)
|
|
74
|
+
|| "anyOf" in value
|
|
75
|
+
|| "oneOf" in value
|
|
76
|
+
|| "allOf" in value
|
|
77
|
+
|| "$ref" in value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function isContractSchema(value: any): value is ContractSchema {
|
|
81
|
+
return isTypeBoxSchema(value) || isStandardSchema(value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function normalizePath(path: unknown): string {
|
|
85
|
+
if (Array.isArray(path)) {
|
|
86
|
+
return path
|
|
87
|
+
.map(segment => typeof segment === "object" && segment !== null && "key" in segment
|
|
88
|
+
? String((segment as any).key)
|
|
89
|
+
: String(segment))
|
|
90
|
+
.join(".");
|
|
91
|
+
}
|
|
92
|
+
if (typeof path === "string") return path.replace(/^\//, "").replace(/\//g, ".");
|
|
93
|
+
return "";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function issueCodeFromTypeBox(error: any): string {
|
|
97
|
+
const schema = error?.schema ?? {};
|
|
98
|
+
if (schema.format) return "invalid_format";
|
|
99
|
+
if (schema.enum) return "invalid_enum";
|
|
100
|
+
if (schema.type === "string") return "invalid_string";
|
|
101
|
+
if (schema.type === "number" || schema.type === "integer") return "invalid_number";
|
|
102
|
+
if (schema.type === "boolean") return "invalid_boolean";
|
|
103
|
+
if (schema.type === "array") return "invalid_array";
|
|
104
|
+
if (schema.type === "object") return "invalid_object";
|
|
105
|
+
if (error?.type !== undefined) return "invalid_value";
|
|
106
|
+
return "invalid_value";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function schemaOptions(schema: any): Record<string, any> {
|
|
110
|
+
const options: Record<string, any> = {};
|
|
111
|
+
const keys = [
|
|
112
|
+
"title", "description", "format", "default", "examples",
|
|
113
|
+
"minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf",
|
|
114
|
+
"minLength", "maxLength", "pattern",
|
|
115
|
+
"minItems", "maxItems", "uniqueItems",
|
|
116
|
+
"minProperties", "maxProperties",
|
|
117
|
+
];
|
|
118
|
+
for (const key of keys) {
|
|
119
|
+
if (schema[key] !== undefined) options[key] = schema[key];
|
|
120
|
+
}
|
|
121
|
+
return options;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Convert plain JSON Schema (no TypeBox `Kind`) to TypeBox so the value checker
|
|
126
|
+
* can validate it. Already-compiled TypeBox schemas are returned unchanged.
|
|
127
|
+
*/
|
|
128
|
+
export function toTypeBoxSchema(schema: any): TSchema {
|
|
129
|
+
if (!schema || typeof schema !== "object") return TypeBox.Any();
|
|
130
|
+
if (Kind in schema) return schema;
|
|
131
|
+
|
|
132
|
+
if (Array.isArray(schema.anyOf)) {
|
|
133
|
+
return TypeBox.Union(schema.anyOf.map((item: any) => toTypeBoxSchema(item)));
|
|
134
|
+
}
|
|
135
|
+
if (Array.isArray(schema.oneOf)) {
|
|
136
|
+
return TypeBox.Union(schema.oneOf.map((item: any) => toTypeBoxSchema(item)));
|
|
137
|
+
}
|
|
138
|
+
if (Array.isArray(schema.allOf)) {
|
|
139
|
+
return TypeBox.Intersect(schema.allOf.map((item: any) => toTypeBoxSchema(item)));
|
|
140
|
+
}
|
|
141
|
+
if (Array.isArray(schema.enum)) {
|
|
142
|
+
return TypeBox.Union(schema.enum.map((value: any) => TypeBox.Literal(value)));
|
|
143
|
+
}
|
|
144
|
+
if (schema.const !== undefined) {
|
|
145
|
+
return TypeBox.Literal(schema.const);
|
|
146
|
+
}
|
|
147
|
+
if (Array.isArray(schema.type)) {
|
|
148
|
+
return TypeBox.Union(schema.type.map((type: string) => toTypeBoxSchema({...schema, type})));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const options = schemaOptions(schema);
|
|
152
|
+
|
|
153
|
+
switch (schema.type) {
|
|
154
|
+
case "string":
|
|
155
|
+
return TypeBox.String(options);
|
|
156
|
+
case "number":
|
|
157
|
+
return TypeBox.Number(options);
|
|
158
|
+
case "integer":
|
|
159
|
+
return TypeBox.Integer(options);
|
|
160
|
+
case "boolean":
|
|
161
|
+
return TypeBox.Boolean(options);
|
|
162
|
+
case "null":
|
|
163
|
+
return TypeBox.Null(options);
|
|
164
|
+
case "array":
|
|
165
|
+
return TypeBox.Array(toTypeBoxSchema(schema.items), options);
|
|
166
|
+
case "object": {
|
|
167
|
+
const required = new Set<string>(Array.isArray(schema.required) ? schema.required : []);
|
|
168
|
+
const properties: Record<string, TSchema> = {};
|
|
169
|
+
for (const [key, child] of Object.entries(schema.properties ?? {})) {
|
|
170
|
+
const converted = toTypeBoxSchema(child);
|
|
171
|
+
properties[key] = required.has(key) ? converted : TypeBox.Optional(converted);
|
|
172
|
+
}
|
|
173
|
+
if (schema.additionalProperties !== undefined) {
|
|
174
|
+
options.additionalProperties = typeof schema.additionalProperties === "object"
|
|
175
|
+
? toTypeBoxSchema(schema.additionalProperties)
|
|
176
|
+
: schema.additionalProperties;
|
|
177
|
+
}
|
|
178
|
+
return TypeBox.Object(properties, options);
|
|
179
|
+
}
|
|
180
|
+
default:
|
|
181
|
+
return TypeBox.Any();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Validate a value against a TypeBox schema or any Standard Schema provider.
|
|
187
|
+
* Never throws for invalid user input: callers receive a stable issue list.
|
|
188
|
+
*/
|
|
189
|
+
export async function validateContract<T = unknown>(
|
|
190
|
+
schema: ContractSchema,
|
|
191
|
+
value: unknown,
|
|
192
|
+
): Promise<ContractValidationResult<T>> {
|
|
193
|
+
if (!schema) return {success: true, value: value as T, issues: []};
|
|
194
|
+
|
|
195
|
+
if (isStandardSchema(schema)) {
|
|
196
|
+
try {
|
|
197
|
+
const result = await schema["~standard"].validate(value);
|
|
198
|
+
if ("issues" in result && result.issues) {
|
|
199
|
+
return {
|
|
200
|
+
success: false,
|
|
201
|
+
issues: result.issues.map(issue => ({
|
|
202
|
+
path: normalizePath(issue.path),
|
|
203
|
+
code: issue.code || "invalid_value",
|
|
204
|
+
message: issue.message || "Invalid value",
|
|
205
|
+
})),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return {success: true, value: (result as any).value as T, issues: []};
|
|
209
|
+
} catch (error: any) {
|
|
210
|
+
return {
|
|
211
|
+
success: false,
|
|
212
|
+
issues: [{
|
|
213
|
+
path: "",
|
|
214
|
+
code: "invalid_value",
|
|
215
|
+
message: error?.message ?? "Invalid value",
|
|
216
|
+
}],
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Plain JSON Schema (MCP tool arguments, OpenAPI baselines) has no TypeBox
|
|
222
|
+
// `Kind`; compile it once into an equivalent TypeBox schema for validation.
|
|
223
|
+
const typeboxSchema = toTypeBoxSchema(schema as any);
|
|
224
|
+
if (Value.Check(typeboxSchema, value)) {
|
|
225
|
+
return {success: true, value: value as T, issues: []};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const issues: ContractIssue[] = [];
|
|
229
|
+
for (const error of Value.Errors(typeboxSchema, value)) {
|
|
230
|
+
issues.push({
|
|
231
|
+
path: normalizePath(error.path),
|
|
232
|
+
code: issueCodeFromTypeBox(error),
|
|
233
|
+
message: error.message || "Invalid value",
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (!issues.length) {
|
|
238
|
+
issues.push({path: "", code: "invalid_value", message: "Invalid value"});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return {success: false, issues};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const OMIT_KEYS = new Set([
|
|
245
|
+
Kind,
|
|
246
|
+
"~kind",
|
|
247
|
+
"~guard",
|
|
248
|
+
"~standard",
|
|
249
|
+
"transform",
|
|
250
|
+
"decode",
|
|
251
|
+
"encode",
|
|
252
|
+
"static",
|
|
253
|
+
]);
|
|
254
|
+
|
|
255
|
+
function sanitize(value: any, seen = new WeakSet<object>()): any {
|
|
256
|
+
if (value === null || typeof value !== "object") return value;
|
|
257
|
+
if (seen.has(value)) return undefined;
|
|
258
|
+
seen.add(value);
|
|
259
|
+
|
|
260
|
+
if (Array.isArray(value)) {
|
|
261
|
+
return value.map(item => sanitize(item, seen)).filter(item => item !== undefined);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const output: Record<string, any> = {};
|
|
265
|
+
for (const key of Object.keys(value)) {
|
|
266
|
+
if (OMIT_KEYS.has(key) || key.startsWith("~")) continue;
|
|
267
|
+
const sanitized = sanitize(value[key], seen);
|
|
268
|
+
if (sanitized !== undefined) output[key] = sanitized;
|
|
269
|
+
}
|
|
270
|
+
return output;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Convert any supported contract to a JSON Schema object suitable for
|
|
275
|
+
* publication in the manifest or in OpenAPI documents.
|
|
276
|
+
*/
|
|
277
|
+
export function contractToJsonSchema(schema?: ContractSchema): Record<string, any> | undefined {
|
|
278
|
+
if (!schema) return undefined;
|
|
279
|
+
if (isStandardSchema(schema)) {
|
|
280
|
+
const jsonSchema = (schema as any)["~standard"]?.jsonSchema;
|
|
281
|
+
return jsonSchema ? sanitize(jsonSchema) : {type: "object"};
|
|
282
|
+
}
|
|
283
|
+
return sanitize(schema);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* OpenAPI 3.0 does not understand every JSON Schema 2020 keyword. This adapter
|
|
288
|
+
* keeps the contract source unique while emitting a document most tools accept.
|
|
289
|
+
*/
|
|
290
|
+
export function contractToOpenApiSchema(schema?: ContractSchema): Record<string, any> | undefined {
|
|
291
|
+
const json = contractToJsonSchema(schema);
|
|
292
|
+
if (!json) return undefined;
|
|
293
|
+
return openApiSanitize(json);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function openApiSanitize(value: any, seen = new WeakSet<object>()): any {
|
|
297
|
+
if (value === null || typeof value !== "object") return value;
|
|
298
|
+
if (seen.has(value)) return {type: "object"};
|
|
299
|
+
seen.add(value);
|
|
300
|
+
|
|
301
|
+
if (Array.isArray(value)) return value.map(item => openApiSanitize(item, seen));
|
|
302
|
+
|
|
303
|
+
const output: Record<string, any> = {};
|
|
304
|
+
for (const [key, child] of Object.entries(value)) {
|
|
305
|
+
if (key === "$schema" || key === "$id") continue;
|
|
306
|
+
output[key] = openApiSanitize(child, seen);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (Array.isArray(output.type)) {
|
|
310
|
+
const types = output.type.filter((type: string) => type !== "null");
|
|
311
|
+
if (output.type.includes("null")) output.nullable = true;
|
|
312
|
+
if (types.length === 1) output.type = types[0];
|
|
313
|
+
else {
|
|
314
|
+
delete output.type;
|
|
315
|
+
output.oneOf = types.map((type: string) => ({type}));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if ("const" in output) {
|
|
320
|
+
output.enum = [output.const];
|
|
321
|
+
delete output.const;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (Array.isArray(output.anyOf)) {
|
|
325
|
+
const withoutNull = output.anyOf.filter((item: any) => item?.type !== "null");
|
|
326
|
+
if (withoutNull.length === 1 && output.anyOf.length === 2) {
|
|
327
|
+
Object.assign(output, withoutNull[0], {nullable: true});
|
|
328
|
+
delete output.anyOf;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return output;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export interface FlattenedContractField {
|
|
336
|
+
path: string;
|
|
337
|
+
type?: string;
|
|
338
|
+
required: boolean;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Flatten an object contract to a list of field paths. Used by `contract check`.
|
|
343
|
+
*/
|
|
344
|
+
export function flattenContractFields(
|
|
345
|
+
schema?: ContractSchema,
|
|
346
|
+
prefix = "",
|
|
347
|
+
): FlattenedContractField[] {
|
|
348
|
+
const json = contractToJsonSchema(schema);
|
|
349
|
+
if (!json) return [];
|
|
350
|
+
return flattenJsonSchema(json, prefix);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function flattenJsonSchema(schema: any, prefix: string): FlattenedContractField[] {
|
|
354
|
+
if (!schema || typeof schema !== "object") return [];
|
|
355
|
+
const output: FlattenedContractField[] = [];
|
|
356
|
+
|
|
357
|
+
if (schema.type === "object" || schema.properties) {
|
|
358
|
+
const required = new Set<string>(Array.isArray(schema.required) ? schema.required : []);
|
|
359
|
+
for (const [key, child] of Object.entries(schema.properties ?? {})) {
|
|
360
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
361
|
+
const childSchema = child as any;
|
|
362
|
+
const type = Array.isArray(childSchema.type)
|
|
363
|
+
? childSchema.type.join("|")
|
|
364
|
+
: childSchema.type;
|
|
365
|
+
output.push({path, type, required: required.has(key)});
|
|
366
|
+
output.push(...flattenJsonSchema(childSchema, path));
|
|
367
|
+
}
|
|
368
|
+
return output;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (schema.type === "array" && schema.items) {
|
|
372
|
+
return flattenJsonSchema(schema.items, `${prefix}[]`);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (prefix) output.push({path: prefix, type: schema.type, required: true});
|
|
376
|
+
return output;
|
|
377
|
+
}
|