sdkloom 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/README.md +239 -2
- package/kits/allowed-sources.json +24 -0
- package/kits/csharp/kit.json +6 -0
- package/kits/csharp/src/Runtime/Client.cs +295 -0
- package/kits/csharp/src/Runtime/Context.cs +216 -0
- package/kits/csharp/src/Runtime/Errors.cs +121 -0
- package/kits/csharp/src/Runtime/Http.cs +218 -0
- package/kits/csharp/src/Runtime/Json.cs +650 -0
- package/kits/csharp/src/Runtime/Lang.cs +212 -0
- package/kits/csharp/src/Runtime/Models.cs +206 -0
- package/kits/csharp/src/Runtime/Multipart.cs +108 -0
- package/kits/csharp/src/Runtime/Pagination.cs +122 -0
- package/kits/csharp/src/Runtime/Sse.cs +197 -0
- package/kits/csharp/src/Runtime/Testing.cs +375 -0
- package/kits/csharp/src/Runtime/Transport.cs +212 -0
- package/kits/csharp/src/Runtime/Wait.cs +57 -0
- package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
- package/kits/csharp/tests/ClientTests.cs +128 -0
- package/kits/csharp/tests/HttpTests.cs +44 -0
- package/kits/csharp/tests/JsonTests.cs +132 -0
- package/kits/csharp/tests/LangTests.cs +57 -0
- package/kits/csharp/tests/MultipartTests.cs +31 -0
- package/kits/csharp/tests/PaginationTests.cs +93 -0
- package/kits/csharp/tests/SseTests.cs +122 -0
- package/kits/csharp/tests/TestRunner.cs +93 -0
- package/kits/csharp/tests/TestingTests.cs +80 -0
- package/kits/csharp/tests/TransportTests.cs +145 -0
- package/kits/csharp/tests/WaitTests.cs +39 -0
- package/kits/csharp/tests/WebhooksTests.cs +46 -0
- package/kits/csharp/versions.json +12 -0
- package/kits/go/kit.json +6 -0
- package/kits/go/src/sdkruntime/client.go +322 -0
- package/kits/go/src/sdkruntime/context.go +207 -0
- package/kits/go/src/sdkruntime/errors.go +74 -0
- package/kits/go/src/sdkruntime/http.go +261 -0
- package/kits/go/src/sdkruntime/json.go +527 -0
- package/kits/go/src/sdkruntime/lang.go +462 -0
- package/kits/go/src/sdkruntime/multipart.go +75 -0
- package/kits/go/src/sdkruntime/pagination.go +150 -0
- package/kits/go/src/sdkruntime/sse.go +267 -0
- package/kits/go/src/sdkruntime/testing.go +343 -0
- package/kits/go/src/sdkruntime/transport.go +168 -0
- package/kits/go/src/sdkruntime/wait.go +49 -0
- package/kits/go/src/sdkruntime/webhooks.go +129 -0
- package/kits/go/tests/client_test.go +185 -0
- package/kits/go/tests/http_test.go +78 -0
- package/kits/go/tests/json_test.go +127 -0
- package/kits/go/tests/lang_test.go +98 -0
- package/kits/go/tests/multipart_test.go +29 -0
- package/kits/go/tests/pagination_test.go +69 -0
- package/kits/go/tests/sse_test.go +129 -0
- package/kits/go/tests/testing_test.go +121 -0
- package/kits/go/tests/transport_test.go +133 -0
- package/kits/go/tests/wait_test.go +46 -0
- package/kits/go/tests/webhooks_test.go +62 -0
- package/kits/go/versions.json +12 -0
- package/kits/java/kit.json +6 -0
- package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
- package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
- package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
- package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
- package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
- package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
- package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
- package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
- package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
- package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
- package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
- package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
- package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
- package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
- package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
- package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
- package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
- package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
- package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
- package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
- package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
- package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
- package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
- package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
- package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
- package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
- package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
- package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
- package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
- package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
- package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
- package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
- package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
- package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
- package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
- package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
- package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
- package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
- package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
- package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
- package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
- package/kits/java/versions.json +41 -0
- package/kits/python/kit.json +6 -0
- package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
- package/kits/python/src/sdkloom_runtime/client.py +232 -0
- package/kits/python/src/sdkloom_runtime/compat.py +195 -0
- package/kits/python/src/sdkloom_runtime/context.py +134 -0
- package/kits/python/src/sdkloom_runtime/errors.py +65 -0
- package/kits/python/src/sdkloom_runtime/http.py +192 -0
- package/kits/python/src/sdkloom_runtime/lang.py +207 -0
- package/kits/python/src/sdkloom_runtime/models.py +221 -0
- package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
- package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
- package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
- package/kits/python/src/sdkloom_runtime/sse.py +121 -0
- package/kits/python/src/sdkloom_runtime/testing.py +224 -0
- package/kits/python/src/sdkloom_runtime/transport.py +120 -0
- package/kits/python/src/sdkloom_runtime/wait.py +44 -0
- package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
- package/kits/python/tests/support.py +28 -0
- package/kits/python/tests/test_client.py +275 -0
- package/kits/python/tests/test_compat.py +87 -0
- package/kits/python/tests/test_http.py +94 -0
- package/kits/python/tests/test_lang.py +104 -0
- package/kits/python/tests/test_models.py +110 -0
- package/kits/python/tests/test_multipart.py +37 -0
- package/kits/python/tests/test_pagination.py +70 -0
- package/kits/python/tests/test_sse.py +61 -0
- package/kits/python/tests/test_testing.py +130 -0
- package/kits/python/tests/test_transport.py +124 -0
- package/kits/python/tests/test_wait.py +46 -0
- package/kits/python/tests/test_webhooks.py +57 -0
- package/kits/python/versions.json +33 -0
- package/kits/rust/kit.json +6 -0
- package/kits/rust/src/runtime/client.rs +212 -0
- package/kits/rust/src/runtime/context.rs +182 -0
- package/kits/rust/src/runtime/crypto.rs +84 -0
- package/kits/rust/src/runtime/errors.rs +147 -0
- package/kits/rust/src/runtime/http.rs +248 -0
- package/kits/rust/src/runtime/json.rs +424 -0
- package/kits/rust/src/runtime/lang.rs +296 -0
- package/kits/rust/src/runtime/mod.rs +30 -0
- package/kits/rust/src/runtime/models.rs +266 -0
- package/kits/rust/src/runtime/multipart.rs +81 -0
- package/kits/rust/src/runtime/pagination.rs +117 -0
- package/kits/rust/src/runtime/sse.rs +195 -0
- package/kits/rust/src/runtime/testing.rs +308 -0
- package/kits/rust/src/runtime/transport.rs +324 -0
- package/kits/rust/src/runtime/wait.rs +29 -0
- package/kits/rust/src/runtime/webhooks.rs +142 -0
- package/kits/rust/tests/client.rs +152 -0
- package/kits/rust/tests/http.rs +44 -0
- package/kits/rust/tests/json.rs +82 -0
- package/kits/rust/tests/lang.rs +74 -0
- package/kits/rust/tests/multipart.rs +21 -0
- package/kits/rust/tests/pagination.rs +61 -0
- package/kits/rust/tests/sse.rs +77 -0
- package/kits/rust/tests/testing.rs +90 -0
- package/kits/rust/tests/transport.rs +218 -0
- package/kits/rust/tests/wait.rs +32 -0
- package/kits/rust/tests/webhooks.rs +73 -0
- package/kits/rust/versions.json +6 -0
- package/kits/typescript/kit.json +6 -0
- package/kits/typescript/src/client.ts +223 -0
- package/kits/typescript/src/context.ts +108 -0
- package/kits/typescript/src/errors.ts +63 -0
- package/kits/typescript/src/helpers.ts +49 -0
- package/kits/typescript/src/http.ts +192 -0
- package/kits/typescript/src/models.ts +139 -0
- package/kits/typescript/src/multipart.ts +66 -0
- package/kits/typescript/src/pagination.ts +54 -0
- package/kits/typescript/src/pipeline.ts +25 -0
- package/kits/typescript/src/sse.ts +102 -0
- package/kits/typescript/src/testing.ts +217 -0
- package/kits/typescript/src/transport.ts +101 -0
- package/kits/typescript/src/wait.ts +36 -0
- package/kits/typescript/src/webhooks.ts +168 -0
- package/kits/typescript/tests/client.test.ts +269 -0
- package/kits/typescript/tests/helpers.test.ts +27 -0
- package/kits/typescript/tests/http.test.ts +80 -0
- package/kits/typescript/tests/models.test.ts +71 -0
- package/kits/typescript/tests/multipart.test.ts +28 -0
- package/kits/typescript/tests/pagination.test.ts +76 -0
- package/kits/typescript/tests/sse.test.ts +82 -0
- package/kits/typescript/tests/testing.test.ts +103 -0
- package/kits/typescript/tests/transport.test.ts +127 -0
- package/kits/typescript/tests/wait.test.ts +31 -0
- package/kits/typescript/tests/webhooks.test.ts +53 -0
- package/kits/typescript/versions.json +15 -0
- package/package.json +37 -4
- package/packages/cli/recipes/apiKeyHeader.ts +16 -0
- package/packages/cli/recipes/apiVersionHeader.ts +16 -0
- package/packages/cli/recipes/basic.ts +13 -0
- package/packages/cli/recipes/bearer.ts +10 -0
- package/packages/cli/recipes/hmacSign.ts +23 -0
- package/packages/cli/recipes/idempotencyKey.ts +19 -0
- package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
- package/packages/cli/recipes/retry.ts +42 -0
- package/packages/cli/recipes/timing.ts +12 -0
- package/packages/cli/recipes/userAgent.ts +8 -0
- package/packages/cli/src/build.js +93 -0
- package/packages/cli/src/commands/build.js +31 -0
- package/packages/cli/src/commands/conformance-check.js +50 -0
- package/packages/cli/src/commands/contract.js +181 -0
- package/packages/cli/src/commands/diff.js +113 -0
- package/packages/cli/src/commands/doctor.js +43 -0
- package/packages/cli/src/commands/import-openapi.js +42 -0
- package/packages/cli/src/commands/init.js +27 -0
- package/packages/cli/src/commands/kit-check.js +145 -0
- package/packages/cli/src/commands/publish.js +81 -0
- package/packages/cli/src/commands/recipe.js +49 -0
- package/packages/cli/src/commands/record.js +181 -0
- package/packages/cli/src/commands/serve.js +156 -0
- package/packages/cli/src/commands/spec-check.js +57 -0
- package/packages/cli/src/commands/test.js +177 -0
- package/packages/cli/src/commands/variants.js +32 -0
- package/packages/cli/src/commands/verify.js +57 -0
- package/packages/cli/src/commands/version.js +63 -0
- package/packages/cli/src/containers.js +82 -0
- package/packages/cli/src/coverage.js +72 -0
- package/packages/cli/src/git.js +25 -0
- package/packages/cli/src/io.js +50 -0
- package/packages/cli/src/java/ScenarioHarness.java +297 -0
- package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
- package/packages/cli/src/java.js +70 -0
- package/packages/cli/src/js/harness.mjs +120 -0
- package/packages/cli/src/kits.js +103 -0
- package/packages/cli/src/languages/csharp.js +274 -0
- package/packages/cli/src/languages/go.js +185 -0
- package/packages/cli/src/languages/index.js +18 -0
- package/packages/cli/src/languages/java.js +187 -0
- package/packages/cli/src/languages/python.js +120 -0
- package/packages/cli/src/languages/rust.js +189 -0
- package/packages/cli/src/languages/types.js +93 -0
- package/packages/cli/src/languages/typescript.js +112 -0
- package/packages/cli/src/main.js +13 -0
- package/packages/cli/src/packaging.js +39 -0
- package/packages/cli/src/portability.js +45 -0
- package/packages/cli/src/profiles.js +55 -0
- package/packages/cli/src/project.js +56 -0
- package/packages/cli/src/python/scenario_harness.py +162 -0
- package/packages/cli/src/python/schema_roundtrip.py +42 -0
- package/packages/cli/src/registries.js +214 -0
- package/packages/cli/src/resolve-core.js +39 -0
- package/packages/cli/src/run.js +133 -0
- package/packages/cli/src/runtimes.js +61 -0
- package/packages/cli/src/scenarios-java.js +82 -0
- package/packages/cli/src/scenarios-javascript.js +54 -0
- package/packages/cli/src/scenarios-python.js +90 -0
- package/packages/cli/src/scenarios.js +183 -0
- package/packages/cli/src/schema.js +84 -0
- package/packages/cli/src/templates.js +85 -0
- package/packages/cli/src/toolchains.js +68 -0
- package/packages/cli/src/transpiled-tests.js +92 -0
- package/packages/codegen/src/canonical-json.js +31 -0
- package/packages/codegen/src/capabilities.js +50 -0
- package/packages/codegen/src/code-writer.js +54 -0
- package/packages/codegen/src/file-tree.js +25 -0
- package/packages/codegen/src/hash.js +6 -0
- package/packages/codegen/src/index.js +16 -0
- package/packages/codegen/src/lock.js +106 -0
- package/packages/conformance/src/case.js +143 -0
- package/packages/conformance/src/checks.js +40 -0
- package/packages/conformance/src/index.js +3 -0
- package/packages/conformance/src/matrix.js +48 -0
- package/packages/conformance/src/suite.js +48 -0
- package/packages/differ/src/diff.js +500 -0
- package/packages/differ/src/index.js +3 -0
- package/packages/differ/src/notes.js +112 -0
- package/packages/differ/src/support.js +34 -0
- package/packages/gen-csharp/src/emit-client.js +397 -0
- package/packages/gen-csharp/src/emit-harness.js +333 -0
- package/packages/gen-csharp/src/emit-mock.js +111 -0
- package/packages/gen-csharp/src/emit-models.js +119 -0
- package/packages/gen-csharp/src/emit-webhooks.js +57 -0
- package/packages/gen-csharp/src/generate.js +245 -0
- package/packages/gen-csharp/src/index.js +5 -0
- package/packages/gen-csharp/src/names.js +284 -0
- package/packages/gen-go/src/emit-client.js +405 -0
- package/packages/gen-go/src/emit-harness.js +294 -0
- package/packages/gen-go/src/emit-mock.js +112 -0
- package/packages/gen-go/src/emit-models.js +146 -0
- package/packages/gen-go/src/emit-webhooks.js +58 -0
- package/packages/gen-go/src/generate.js +205 -0
- package/packages/gen-go/src/index.js +5 -0
- package/packages/gen-go/src/names.js +197 -0
- package/packages/gen-java/src/emit-client.js +441 -0
- package/packages/gen-java/src/emit-models.js +342 -0
- package/packages/gen-java/src/emit-testing.js +123 -0
- package/packages/gen-java/src/emit-webhooks.js +70 -0
- package/packages/gen-java/src/generate.js +244 -0
- package/packages/gen-java/src/index.js +4 -0
- package/packages/gen-java/src/names.js +197 -0
- package/packages/gen-python/src/emit-client.js +272 -0
- package/packages/gen-python/src/emit-models.js +139 -0
- package/packages/gen-python/src/emit-testing.js +93 -0
- package/packages/gen-python/src/emit-webhooks.js +48 -0
- package/packages/gen-python/src/generate.js +237 -0
- package/packages/gen-python/src/index.js +2 -0
- package/packages/gen-python/src/names.js +161 -0
- package/packages/gen-python/src/style.js +67 -0
- package/packages/gen-rust/src/emit-client.js +379 -0
- package/packages/gen-rust/src/emit-harness.js +278 -0
- package/packages/gen-rust/src/emit-mock.js +121 -0
- package/packages/gen-rust/src/emit-models.js +117 -0
- package/packages/gen-rust/src/emit-webhooks.js +58 -0
- package/packages/gen-rust/src/generate.js +243 -0
- package/packages/gen-rust/src/index.js +5 -0
- package/packages/gen-rust/src/names.js +223 -0
- package/packages/gen-typescript/src/emit-client.js +434 -0
- package/packages/gen-typescript/src/emit-models.js +93 -0
- package/packages/gen-typescript/src/emit-package.js +90 -0
- package/packages/gen-typescript/src/emit-testing.js +138 -0
- package/packages/gen-typescript/src/emit-webhooks.js +52 -0
- package/packages/gen-typescript/src/generate.js +70 -0
- package/packages/gen-typescript/src/index.js +3 -0
- package/packages/gen-typescript/src/naming.js +64 -0
- package/packages/gen-typescript/src/runtime.js +81 -0
- package/packages/gen-typescript/src/types.js +136 -0
- package/packages/ir/src/api-surface.js +22 -0
- package/packages/ir/src/checks.js +52 -0
- package/packages/ir/src/index.js +32 -0
- package/packages/ir/src/ir.js +214 -0
- package/packages/ir/src/pages.js +18 -0
- package/packages/ir/src/validate-ir.js +462 -0
- package/packages/ir/src/validate-types.js +140 -0
- package/packages/kit/src/block-parser.js +196 -0
- package/packages/kit/src/index.js +15 -0
- package/packages/kit/src/kit-check.js +203 -0
- package/packages/kit/src/sources.js +53 -0
- package/packages/kit/src/version-policy.js +133 -0
- package/packages/kit/src/version-range.js +132 -0
- package/packages/openapi/src/import.js +292 -0
- package/packages/openapi/src/index.js +1 -0
- package/packages/openapi/src/names.js +50 -0
- package/packages/openapi/src/schemas.js +264 -0
- package/packages/packager/src/archive.js +106 -0
- package/packages/packager/src/crate.js +39 -0
- package/packages/packager/src/gomodule.js +11 -0
- package/packages/packager/src/index.js +9 -0
- package/packages/packager/src/maven.js +85 -0
- package/packages/packager/src/metadata.js +34 -0
- package/packages/packager/src/npm.js +71 -0
- package/packages/packager/src/nuget.js +78 -0
- package/packages/packager/src/sbom.js +56 -0
- package/packages/packager/src/wheel.js +62 -0
- package/packages/spec/src/index.js +11 -0
- package/packages/spec/src/requirements.js +139 -0
- package/packages/transpile/src/ast.js +51 -0
- package/packages/transpile/src/check.js +669 -0
- package/packages/transpile/src/emit-csharp.js +469 -0
- package/packages/transpile/src/emit-go.js +529 -0
- package/packages/transpile/src/emit-java.js +360 -0
- package/packages/transpile/src/emit-python.js +320 -0
- package/packages/transpile/src/emit-rust.js +499 -0
- package/packages/transpile/src/errors.js +15 -0
- package/packages/transpile/src/index.js +13 -0
- package/packages/transpile/src/lexer.js +161 -0
- package/packages/transpile/src/parser.js +543 -0
- package/packages/transpile/src/tests/ast.js +46 -0
- package/packages/transpile/src/tests/parse.js +376 -0
- package/packages/transpile/src/tests/translate.js +849 -0
- package/packages/transpile/src/transpile.js +57 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/** A version as numeric segments: "3.4.3" → [3, 4, 3]. */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/** Half-open interval [min, max). `null` means unbounded on that side. */
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const SEGMENT = /^\d+$/;
|
|
16
|
+
const EVERY_VERSION = "*";
|
|
17
|
+
|
|
18
|
+
export function parseVersion(text ) {
|
|
19
|
+
const segments = text.split(".");
|
|
20
|
+
if (text === "" || !segments.every((segment) => SEGMENT.test(segment))) {
|
|
21
|
+
throw new Error(`invalid version "${text}"`);
|
|
22
|
+
}
|
|
23
|
+
return segments.map(Number);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Compares two versions; missing trailing segments count as zero (3 == 3.0.0). */
|
|
27
|
+
export function compareVersions(a , b ) {
|
|
28
|
+
const length = Math.max(a.length, b.length);
|
|
29
|
+
for (let index = 0; index < length; index += 1) {
|
|
30
|
+
const difference = (a[index] ?? 0) - (b[index] ?? 0);
|
|
31
|
+
if (difference !== 0) return Math.sign(difference);
|
|
32
|
+
}
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** "8" → 9, "2.7" → 2.8: the first version after a release line. */
|
|
37
|
+
function nextRelease(version ) {
|
|
38
|
+
const last = version.length - 1;
|
|
39
|
+
return version.map((segment, index) => (index === last ? segment + 1 : segment));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function parseToken(token ) {
|
|
43
|
+
if (token.startsWith(">=")) {
|
|
44
|
+
return [{ side: "min", version: parseVersion(token.slice(2)) }];
|
|
45
|
+
}
|
|
46
|
+
if (token.startsWith("<") && !token.startsWith("<=")) {
|
|
47
|
+
return [{ side: "max", version: parseVersion(token.slice(1)) }];
|
|
48
|
+
}
|
|
49
|
+
if (/^\d/.test(token)) {
|
|
50
|
+
const version = parseVersion(token);
|
|
51
|
+
return [
|
|
52
|
+
{ side: "min", version },
|
|
53
|
+
{ side: "max", version: nextRelease(version) },
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
throw new Error(`unsupported range token "${token}" (use ">=X", "<X", "X" or "*")`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Parses a version range. Supported forms:
|
|
61
|
+
* `>=X`, `<X`, `>=X <Y`, a bare release line `X` (means `>=X <next(X)`), and `*`.
|
|
62
|
+
*/
|
|
63
|
+
export function parseRange(text ) {
|
|
64
|
+
const tokens = text.trim().split(/\s+/).filter((token) => token !== "");
|
|
65
|
+
if (tokens.length === 0) throw new Error("empty version range");
|
|
66
|
+
if (tokens.length === 1 && tokens[0] === EVERY_VERSION) return { min: null, max: null };
|
|
67
|
+
|
|
68
|
+
const bounds = tokens.flatMap(parseToken);
|
|
69
|
+
const mins = bounds.filter((bound) => bound.side === "min");
|
|
70
|
+
const maxes = bounds.filter((bound) => bound.side === "max");
|
|
71
|
+
if (mins.length > 1) throw new Error(`"${text}" has more than one lower bound`);
|
|
72
|
+
if (maxes.length > 1) throw new Error(`"${text}" has more than one upper bound`);
|
|
73
|
+
|
|
74
|
+
const interval = { min: mins[0]?.version ?? null, max: maxes[0]?.version ?? null };
|
|
75
|
+
if (!isBelow(interval.min, interval.max)) throw new Error(`"${text}" is an empty interval`);
|
|
76
|
+
return interval;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** True when a lower bound sits strictly below an upper bound (null = unbounded). */
|
|
80
|
+
function isBelow(min , max ) {
|
|
81
|
+
if (min === null || max === null) return true;
|
|
82
|
+
return compareVersions(min, max) < 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function rangesOverlap(a , b ) {
|
|
86
|
+
return isBelow(a.min, b.max) && isBelow(b.min, a.max);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function compareByMin(a , b ) {
|
|
90
|
+
if (a.min === null) return b.min === null ? 0 : -1;
|
|
91
|
+
if (b.min === null) return 1;
|
|
92
|
+
return compareVersions(a.min, b.min);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function lowerUpper(a , b ) {
|
|
96
|
+
return b !== null && compareVersions(b, a) < 0 ? b : a;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Returns the parts of `within` that none of `ranges` cover, in ascending order. */
|
|
100
|
+
export function findGaps(ranges , within ) {
|
|
101
|
+
const sorted = [...ranges].sort(compareByMin);
|
|
102
|
+
const gaps = [];
|
|
103
|
+
let cursor = within.min;
|
|
104
|
+
let coveredToEnd = false;
|
|
105
|
+
|
|
106
|
+
for (const range of sorted) {
|
|
107
|
+
if (range.min !== null && (cursor === null || compareVersions(cursor, range.min) < 0)) {
|
|
108
|
+
const end = lowerUpper(range.min, within.max);
|
|
109
|
+
if (isBelow(cursor, end)) gaps.push({ min: cursor, max: end });
|
|
110
|
+
}
|
|
111
|
+
if (range.max === null) {
|
|
112
|
+
coveredToEnd = true;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
if (cursor === null || compareVersions(range.max, cursor) > 0) cursor = range.max;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!coveredToEnd && isBelow(cursor, within.max)) gaps.push({ min: cursor, max: within.max });
|
|
119
|
+
return gaps;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function formatVersion(version ) {
|
|
123
|
+
return version.join(".");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function formatInterval(interval ) {
|
|
127
|
+
const parts = [
|
|
128
|
+
...(interval.min === null ? [] : [`>=${formatVersion(interval.min)}`]),
|
|
129
|
+
...(interval.max === null ? [] : [`<${formatVersion(interval.max)}`]),
|
|
130
|
+
];
|
|
131
|
+
return parts.length === 0 ? EVERY_VERSION : parts.join(" ");
|
|
132
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
// An OpenAPI 3.0/3.1 document (JSON) as an sdkloom definition: the TypeScript source of sdk/index.ts.
|
|
2
|
+
|
|
3
|
+
import { camel, commentText, key, pascal, str, unique } from "./names.ts";
|
|
4
|
+
import { isRecord, Schemas, } from "./schemas.ts";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export class OpenApiError extends Error {
|
|
14
|
+
constructor(message ) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "OpenApiError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** What a definition may use from @sdkloom/core; only the used ones are imported. */
|
|
21
|
+
const CORE_NAMES = ["del", "get", "model", "patch", "post", "put", "sdk", "t", "union"] ;
|
|
22
|
+
|
|
23
|
+
const METHODS = { get: "get", post: "post", put: "put", patch: "patch", delete: "del" };
|
|
24
|
+
|
|
25
|
+
const ERROR_NAMES = {
|
|
26
|
+
"400": "BadRequest",
|
|
27
|
+
"401": "Unauthorized",
|
|
28
|
+
"403": "Forbidden",
|
|
29
|
+
"404": "NotFound",
|
|
30
|
+
"405": "MethodNotAllowed",
|
|
31
|
+
"409": "Conflict",
|
|
32
|
+
"410": "Gone",
|
|
33
|
+
"412": "PreconditionFailed",
|
|
34
|
+
"413": "PayloadTooLarge",
|
|
35
|
+
"415": "UnsupportedMediaType",
|
|
36
|
+
"422": "UnprocessableEntity",
|
|
37
|
+
"429": "TooManyRequests",
|
|
38
|
+
"500": "InternalServerError",
|
|
39
|
+
"502": "BadGateway",
|
|
40
|
+
"503": "ServiceUnavailable",
|
|
41
|
+
"504": "GatewayTimeout",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Follows a local $ref such as #/components/parameters/Limit. */
|
|
45
|
+
function resolve(document , value ) {
|
|
46
|
+
if (!isRecord(value)) return undefined;
|
|
47
|
+
const ref = value["$ref"];
|
|
48
|
+
if (typeof ref !== "string") return value;
|
|
49
|
+
if (!ref.startsWith("#/")) return undefined;
|
|
50
|
+
let current = document;
|
|
51
|
+
for (const part of ref.slice(2).split("/")) current = isRecord(current) ? current[part.replace(/~1/g, "/").replace(/~0/g, "~")] : undefined;
|
|
52
|
+
return isRecord(current) ? current : undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function text(value ) {
|
|
56
|
+
return typeof value === "string" && value.trim() !== "" ? value.trim() : undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A server URL with its {variables} set to their defaults. */
|
|
60
|
+
function serverUrl(server ) {
|
|
61
|
+
const variables = isRecord(server["variables"]) ? server["variables"] : {};
|
|
62
|
+
return String(server["url"]).replace(/\{([^}]+)\}/g, (match, name ) => {
|
|
63
|
+
const variable = variables[name];
|
|
64
|
+
return isRecord(variable) && typeof variable["default"] === "string" ? variable["default"] : match;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
class Importer {
|
|
69
|
+
#document ;
|
|
70
|
+
#schemas ;
|
|
71
|
+
#warnings = [];
|
|
72
|
+
#operationNames = new Set ();
|
|
73
|
+
|
|
74
|
+
constructor(document ) {
|
|
75
|
+
this.#document = document;
|
|
76
|
+
const components = isRecord(document["components"]) ? document["components"] : {};
|
|
77
|
+
this.#schemas = new Schemas(isRecord(components["schemas"]) ? components["schemas"] : {});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
#warn(message ) {
|
|
81
|
+
this.#warnings.push(message);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#servers() {
|
|
85
|
+
const servers = Array.isArray(this.#document["servers"]) ? this.#document["servers"].filter(isRecord) : [];
|
|
86
|
+
if (servers.length === 0) {
|
|
87
|
+
this.#warn("no servers; added production at https://api.example.com, change it");
|
|
88
|
+
return [`production: ${str("https://api.example.com")}`];
|
|
89
|
+
}
|
|
90
|
+
const taken = new Set ();
|
|
91
|
+
return servers.map((server, index) => {
|
|
92
|
+
const description = text(server["description"]);
|
|
93
|
+
const name = unique(description === undefined ? (index === 0 ? "production" : `server${index + 1}`) : camel(description), taken);
|
|
94
|
+
return `${key(name)}: ${str(serverUrl(server))}`;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Comments naming the recipe for each security scheme: sdkloom keeps authentication in middleware you own. */
|
|
99
|
+
#security() {
|
|
100
|
+
const components = isRecord(this.#document["components"]) ? this.#document["components"] : {};
|
|
101
|
+
const schemes = isRecord(components["securitySchemes"]) ? components["securitySchemes"] : {};
|
|
102
|
+
return Object.entries(schemes).flatMap(([name, value]) => {
|
|
103
|
+
const scheme = resolve(this.#document, value);
|
|
104
|
+
if (scheme === undefined) return [];
|
|
105
|
+
const flows = scheme["flows"];
|
|
106
|
+
const recipe =
|
|
107
|
+
scheme["type"] === "apiKey" && scheme["in"] === "header"
|
|
108
|
+
? "apiKeyHeader"
|
|
109
|
+
: scheme["type"] === "http" && String(scheme["scheme"]).toLowerCase() === "bearer"
|
|
110
|
+
? "bearer"
|
|
111
|
+
: scheme["type"] === "http" && String(scheme["scheme"]).toLowerCase() === "basic"
|
|
112
|
+
? "basic"
|
|
113
|
+
: scheme["type"] === "oauth2" && isRecord(flows) && isRecord(flows["clientCredentials"])
|
|
114
|
+
? "oauthClientCredentials"
|
|
115
|
+
: undefined;
|
|
116
|
+
if (recipe === undefined) {
|
|
117
|
+
this.#warn(`security scheme ${commentText(name)} (${commentText(String(scheme["type"]))}) has no recipe; write a middleware for it`);
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
const detail = recipe === "apiKeyHeader" ? ` with apiKeyHeader: ${str(String(scheme["name"]).toLowerCase())}` : "";
|
|
121
|
+
return [`// Security scheme ${commentText(name)}: run \`sdkloom recipe add ${recipe}\` and add it to \`use\`${detail}.`];
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
#operationName(operation , method , path ) {
|
|
126
|
+
const id = text(operation["operationId"]);
|
|
127
|
+
const base = id === undefined ? camel(`${method} ${path.replace(/\{([^}]+)\}/g, "by $1")}`) : camel(id);
|
|
128
|
+
return unique(base, this.#operationNames);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** `path: {…}, query: {…}, headers: {…}` from the path's and the operation's parameters (the operation's win). */
|
|
132
|
+
#parameters(path , pathItem , operation , name ) {
|
|
133
|
+
const byKey = new Map ();
|
|
134
|
+
for (const list of [pathItem["parameters"], operation["parameters"]]) {
|
|
135
|
+
if (!Array.isArray(list)) continue;
|
|
136
|
+
for (const entry of list) {
|
|
137
|
+
const parameter = resolve(this.#document, entry);
|
|
138
|
+
if (parameter === undefined) {
|
|
139
|
+
this.#warn(`${name}: a parameter ${commentText(JSON.stringify(entry))} could not be resolved; left out`);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
// Header names are case-insensitive: X-Id and x-id are one header.
|
|
143
|
+
const paramName = parameter["in"] === "header" ? String(parameter["name"]).toLowerCase() : String(parameter["name"]);
|
|
144
|
+
byKey.set(`${String(parameter["in"])}:${paramName}`, parameter);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
for (const [, template] of path.matchAll(/\{\+?([^}]+)\}/g)) {
|
|
148
|
+
if (template !== undefined && !byKey.has(`path:${template}`)) {
|
|
149
|
+
this.#warn(`${name}: path parameter ${template} is not declared; added it as a string`);
|
|
150
|
+
byKey.set(`path:${template}`, { name: template, in: "path", schema: { type: "string" } });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const groups = { path: [], query: [], headers: [] };
|
|
154
|
+
for (const parameter of byKey.values()) {
|
|
155
|
+
const where = String(parameter["in"]);
|
|
156
|
+
const paramName = String(parameter["name"]);
|
|
157
|
+
if (where === "cookie") {
|
|
158
|
+
this.#warn(`${name}: cookie parameter ${paramName} is not supported; left out`);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const group = groups[where === "header" ? "headers" : where];
|
|
162
|
+
if (group === undefined) continue;
|
|
163
|
+
const doc = text(parameter["description"]);
|
|
164
|
+
const schema = isRecord(parameter["schema"]) ? { ...parameter["schema"], ...(doc === undefined ? {} : { description: doc }) } : undefined;
|
|
165
|
+
const type = this.#schemas.type(schema, `${pascal(name)}${pascal(paramName)}`);
|
|
166
|
+
const required = where === "path" || parameter["required"] === true;
|
|
167
|
+
group.push(`${key(where === "header" ? paramName.toLowerCase() : paramName)}: ${required ? type : `t.optional(${type})`}`);
|
|
168
|
+
}
|
|
169
|
+
return Object.entries(groups)
|
|
170
|
+
.filter(([, entries]) => entries.length > 0)
|
|
171
|
+
.map(([group, entries]) => `${group}: { ${entries.join(", ")} }`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
#body(operation , name ) {
|
|
175
|
+
const body = resolve(this.#document, operation["requestBody"]);
|
|
176
|
+
if (body === undefined) return undefined;
|
|
177
|
+
const content = isRecord(body["content"]) ? body["content"] : {};
|
|
178
|
+
const types = Object.keys(content);
|
|
179
|
+
const json = types.find((type) => /^application\/([a-z.+-]*\+)?json/i.test(type));
|
|
180
|
+
if (json !== undefined) return `body: ${this.#schemas.type((content[json] )["schema"] ?? undefined, `${pascal(name)}Request`, "body")}`;
|
|
181
|
+
if (types.some((type) => type.toLowerCase() === "multipart/form-data")) return "body: t.multipart()";
|
|
182
|
+
if (types.length > 0 && !types.some((type) => /octet-stream|^image\/|^audio\/|^video\/|^application\/pdf/i.test(type))) {
|
|
183
|
+
this.#warn(`${name}: a ${types.join(", ")} body is sent as raw bytes (t.bytes()); encode it yourself or use a middleware`);
|
|
184
|
+
}
|
|
185
|
+
return types.length > 0 ? "body: t.bytes()" : undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** returns or events from the first 2xx response, and errors from the 4xx and 5xx ones. */
|
|
189
|
+
#responses(operation , name ) {
|
|
190
|
+
const responses = isRecord(operation["responses"]) ? operation["responses"] : {};
|
|
191
|
+
const parts = [];
|
|
192
|
+
const success = Object.keys(responses)
|
|
193
|
+
.filter((status) => /^2\d\d$/.test(status))
|
|
194
|
+
.sort()[0];
|
|
195
|
+
const response = success === undefined ? undefined : resolve(this.#document, responses[success]);
|
|
196
|
+
const content = response !== undefined && isRecord(response["content"]) ? response["content"] : {};
|
|
197
|
+
const types = Object.keys(content);
|
|
198
|
+
const json = types.find((type) => /^application\/([a-z.+-]*\+)?json/i.test(type));
|
|
199
|
+
if (types.some((type) => type.toLowerCase() === "text/event-stream")) {
|
|
200
|
+
const stream = content["text/event-stream"];
|
|
201
|
+
const itemSchema = isRecord(stream) ? stream["itemSchema"] : undefined;
|
|
202
|
+
const properties = isRecord(itemSchema) ? itemSchema["properties"] : undefined;
|
|
203
|
+
const data = isRecord(properties) ? properties["data"] : undefined;
|
|
204
|
+
parts.push(`events: ${data === undefined ? "t.string()" : this.#schemas.type(data, `${pascal(name)}Event`)}`);
|
|
205
|
+
} else if (json !== undefined) {
|
|
206
|
+
parts.push(`returns: ${this.#schemas.type((content[json] )["schema"] ?? undefined, `${pascal(name)}Response`, "body")}`);
|
|
207
|
+
} else if (types.length > 0) {
|
|
208
|
+
parts.push("returns: t.bytes()");
|
|
209
|
+
}
|
|
210
|
+
const errors = Object.entries(responses)
|
|
211
|
+
.filter(([status]) => /^[45]\d\d$/.test(status))
|
|
212
|
+
.map(([status, value]) => {
|
|
213
|
+
const error = resolve(this.#document, value);
|
|
214
|
+
const errorContent = error !== undefined && isRecord(error["content"]) ? error["content"] : {};
|
|
215
|
+
const errorJson = Object.keys(errorContent).find((type) => /json/i.test(type));
|
|
216
|
+
const entry = errorJson === undefined ? undefined : errorContent[errorJson];
|
|
217
|
+
const schema = isRecord(entry) ? entry["schema"] : undefined;
|
|
218
|
+
const errorName = str(ERROR_NAMES[status] ?? `Http${status}`);
|
|
219
|
+
return `${status}: ${schema === undefined || schema === null ? errorName : `{ name: ${errorName}, type: ${this.#schemas.type(schema, `${pascal(name)}Error${status}`, "body")} }`}`;
|
|
220
|
+
});
|
|
221
|
+
if (errors.length > 0) parts.push(`errors: { ${errors.join(", ")} }`);
|
|
222
|
+
return parts;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
#operations() {
|
|
226
|
+
const paths = isRecord(this.#document["paths"]) ? this.#document["paths"] : {};
|
|
227
|
+
const operations = [];
|
|
228
|
+
for (const [path, value] of Object.entries(paths)) {
|
|
229
|
+
const pathItem = resolve(this.#document, value);
|
|
230
|
+
if (pathItem === undefined) continue;
|
|
231
|
+
for (const [method, entry] of Object.entries(pathItem)) {
|
|
232
|
+
if (!isRecord(entry) || ["parameters", "summary", "description", "servers", "$ref"].includes(method)) continue;
|
|
233
|
+
const call = METHODS[method];
|
|
234
|
+
if (call === undefined) {
|
|
235
|
+
this.#warn(`${method.toUpperCase()} ${path}: sdkloom has no ${method.toUpperCase()} operations; left out`);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
const name = this.#operationName(entry, method, path);
|
|
239
|
+
const doc = text(entry["summary"]) ?? text(entry["description"]);
|
|
240
|
+
const spec = [
|
|
241
|
+
...this.#parameters(path, pathItem, entry, name),
|
|
242
|
+
...[this.#body(entry, name)].filter((part) => part !== undefined),
|
|
243
|
+
...this.#responses(entry, name),
|
|
244
|
+
...(entry["deprecated"] === true ? [`deprecated: ${str("Deprecated in the OpenAPI document.")}`] : []),
|
|
245
|
+
...(doc === undefined ? [] : [`doc: ${str(doc)}`]),
|
|
246
|
+
];
|
|
247
|
+
operations.push(`${key(name)}: ${call}(${str(path)}${spec.length === 0 ? "" : `, { ${spec.join(", ")} }`}),`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return operations;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
run() {
|
|
254
|
+
const info = isRecord(this.#document["info"]) ? this.#document["info"] : {};
|
|
255
|
+
const title = text(info["title"]) ?? "Api";
|
|
256
|
+
const servers = this.#servers();
|
|
257
|
+
const security = this.#security();
|
|
258
|
+
const operations = this.#operations();
|
|
259
|
+
const doc = text(info["description"]) ?? title;
|
|
260
|
+
const lines = [
|
|
261
|
+
`// Imported from an OpenAPI ${String(this.#document["openapi"])} document by \`sdkloom import openapi\`. It is your definition now:`,
|
|
262
|
+
"// review the names, add middleware (authentication, retries) and edit it as the API needs.",
|
|
263
|
+
...security,
|
|
264
|
+
"",
|
|
265
|
+
"",
|
|
266
|
+
...this.#schemas.declarations,
|
|
267
|
+
...(this.#schemas.declarations.length === 0 ? [] : [""]),
|
|
268
|
+
`export default sdk(${str(pascal(title))}, {`,
|
|
269
|
+
` doc: ${str(doc)},`,
|
|
270
|
+
` servers: { ${servers.join(", ")} },`,
|
|
271
|
+
" operations: {",
|
|
272
|
+
...operations.map((operation) => ` ${operation}`),
|
|
273
|
+
" },",
|
|
274
|
+
"});",
|
|
275
|
+
"",
|
|
276
|
+
];
|
|
277
|
+
const body = lines.join("\n");
|
|
278
|
+
const used = CORE_NAMES.filter((name) => name === "sdk" || new RegExp(`(^|[^.\\w])${name}[(.]`, "m").test(body));
|
|
279
|
+
const source = body.replace("\n\n", `\n\nimport { ${used.join(", ")} } from "@sdkloom/core";\n`);
|
|
280
|
+
return { source, warnings: [...this.#schemas.warnings, ...this.#warnings] };
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** Reads an OpenAPI 3.0 or 3.1 document (already parsed from JSON) into an sdkloom definition. */
|
|
285
|
+
export function importOpenApi(document ) {
|
|
286
|
+
if (!isRecord(document)) throw new OpenApiError("an OpenAPI document is a JSON object");
|
|
287
|
+
const version = document["openapi"];
|
|
288
|
+
if (typeof version !== "string" || !/^3\.[01]\.\d+$/.test(version)) {
|
|
289
|
+
throw new OpenApiError(`only OpenAPI 3.0 and 3.1 are supported; this document says ${JSON.stringify(document["openapi"] ?? document["swagger"] ?? null)}`);
|
|
290
|
+
}
|
|
291
|
+
return new Importer(document).run();
|
|
292
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { importOpenApi, OpenApiError, } from "./import.ts";
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Names for imported definitions: identifiers from free text, quoted keys, string literals.
|
|
2
|
+
|
|
3
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
4
|
+
|
|
5
|
+
/** Words of a name, split at non-alphanumerics and at lower-to-upper case changes. */
|
|
6
|
+
function words(text ) {
|
|
7
|
+
return text
|
|
8
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
9
|
+
.split(/[^A-Za-z0-9]+/)
|
|
10
|
+
.filter((word) => word !== "");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** "swagger petstore" → "SwaggerPetstore"; a leading digit gets an underscore. */
|
|
14
|
+
export function pascal(text ) {
|
|
15
|
+
const joined = words(text)
|
|
16
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
17
|
+
.join("");
|
|
18
|
+
const name = joined === "" ? "Unnamed" : joined;
|
|
19
|
+
return /^[0-9]/.test(name) ? `_${name}` : name;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** "get-pet-by-id" → "getPetById". */
|
|
23
|
+
export function camel(text ) {
|
|
24
|
+
const name = pascal(text);
|
|
25
|
+
return name.startsWith("_") ? name : name.charAt(0).toLowerCase() + name.slice(1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A JavaScript string literal. */
|
|
29
|
+
export function str(text ) {
|
|
30
|
+
return JSON.stringify(text);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** An object key: bare when it is an identifier, quoted otherwise; __proto__ is computed so it stays a field. */
|
|
34
|
+
export function key(name ) {
|
|
35
|
+
if (name === "__proto__") return `[${str(name)}]`;
|
|
36
|
+
return IDENTIFIER.test(name) ? name : str(name);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Text from the document made safe for a // comment: line breaks (including U+2028/U+2029) become spaces. */
|
|
40
|
+
export function commentText(text ) {
|
|
41
|
+
return text.replace(/[\r\n\u2028\u2029]+/g, " ");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A name not in `taken`, made unique with a number; the result is added to `taken`. */
|
|
45
|
+
export function unique(name , taken ) {
|
|
46
|
+
let candidate = name;
|
|
47
|
+
for (let index = 2; taken.has(candidate); index += 1) candidate = `${name}${index}`;
|
|
48
|
+
taken.add(candidate);
|
|
49
|
+
return candidate;
|
|
50
|
+
}
|