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,434 @@
|
|
|
1
|
+
// src/client.ts, src/errors.ts and src/middleware.ts.
|
|
2
|
+
|
|
3
|
+
import { createCodeWriter, } from "@sdkloom/codegen";
|
|
4
|
+
import { pagedItems, } from "@sdkloom/ir";
|
|
5
|
+
import { MODEL_HELPERS } from "./emit-models.ts";
|
|
6
|
+
import { constantName, docComment, errorClassName, importLine, memberDoc, pascal, propertyKey, quote, usedNames } from "./naming.ts";
|
|
7
|
+
import { bodyKind, bodyType, decoderExpr, operationResultType, resultType, textEvents, tsType } from "./types.ts";
|
|
8
|
+
|
|
9
|
+
const CLIENT_OPTIONS = ["server", "baseUrl", "transport", "timeoutMs", "middleware", "onEvent", "clock"];
|
|
10
|
+
|
|
11
|
+
export function clientName(ir ) {
|
|
12
|
+
return `${ir.name}Client`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function specName(operation ) {
|
|
16
|
+
return `${constantName(operation.name)}_SPEC`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function argsName(operation ) {
|
|
20
|
+
return `${pascal(operation.name)}Args`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function hasArgs(operation ) {
|
|
24
|
+
return operation.params.length > 0 || operation.body !== undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function fieldType(field ) {
|
|
28
|
+
return `${tsType(field.type)}${field.nullable ? " | null" : ""}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Config fields of every middleware become client options; names must not clash with built-in options. */
|
|
32
|
+
export function configFields(ir ) {
|
|
33
|
+
const fields = new Map ();
|
|
34
|
+
for (const middleware of ir.middlewares) {
|
|
35
|
+
for (const field of middleware.config) {
|
|
36
|
+
if (CLIENT_OPTIONS.includes(field.name)) {
|
|
37
|
+
throw new Error(`config field ${field.name} of middleware ${middleware.name} collides with a client option`);
|
|
38
|
+
}
|
|
39
|
+
if (!fields.has(field.name)) fields.set(field.name, field);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return [...fields.values()];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The result type a caller sees: void for operations without a result. */
|
|
46
|
+
function returnType(operation ) {
|
|
47
|
+
const type = resultType(operation.returns);
|
|
48
|
+
return type === "undefined" ? "void" : type;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function emitErrors(ir , header ) {
|
|
52
|
+
const statuses = new Map ();
|
|
53
|
+
ir.operations.forEach((operation) =>
|
|
54
|
+
operation.errors.forEach((error) => statuses.set(error.name, [...(statuses.get(error.name) ?? []), error.status])),
|
|
55
|
+
);
|
|
56
|
+
const writer = createCodeWriter().line(header).line();
|
|
57
|
+
if (statuses.size === 0) return writer.line("export {};").toString();
|
|
58
|
+
|
|
59
|
+
writer.line('import { ApiError } from "./runtime/errors.ts";').line();
|
|
60
|
+
[...statuses.entries()]
|
|
61
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
62
|
+
.forEach(([name, codes]) => {
|
|
63
|
+
docComment(writer, [`Thrown for HTTP ${[...new Set(codes)].sort().join(", ")} (${name}).`]);
|
|
64
|
+
writer.line(`export class ${errorClassName(name)} extends ApiError {}`).line();
|
|
65
|
+
});
|
|
66
|
+
return writer.toString();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function configType(middleware ) {
|
|
70
|
+
if (middleware.config.length === 0) return "Record<string, never>";
|
|
71
|
+
return `{ ${middleware.config.map((field) => `readonly ${propertyKey(field.name)}${field.optional ? "?" : ""}: ${fieldType(field)}`).join("; ")} }`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A function's source keeps the indentation it had in the author's file; shift it back to column zero. */
|
|
75
|
+
function dedent(lines ) {
|
|
76
|
+
const closing = /^(\s*)[}\])]/.exec(lines[lines.length - 1] ?? "");
|
|
77
|
+
if (lines.length < 2 || closing === null) return [...lines];
|
|
78
|
+
const shift = closing[1]?.length ?? 0;
|
|
79
|
+
return lines.map((line, index) => (index === 0 ? line : line.slice(Math.min(shift, line.length - line.trimStart().length))));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function emitMiddleware(ir , sources , header ) {
|
|
83
|
+
const writer = createCodeWriter().line(header).line();
|
|
84
|
+
if (ir.middlewares.length === 0) return writer.line("export {};").toString();
|
|
85
|
+
|
|
86
|
+
writer.line('import type { Middleware } from "./runtime/client.ts";');
|
|
87
|
+
// Helpers a middleware imported from @sdkloom/core come from the vendored kit.
|
|
88
|
+
const helpers = usedNames([...sources.values()].join("\n"), ["hmacSha256Base64", "hmacSha256Hex", "randomUUID"]);
|
|
89
|
+
if (helpers.length > 0) writer.line(`import { ${helpers.join(", ")} } from "./runtime/helpers.ts";`);
|
|
90
|
+
// Middleware may call the project's logic functions.
|
|
91
|
+
const logic = usedNames([...sources.values()].join("\n"), (ir.logic ?? []).map((entry) => entry.name));
|
|
92
|
+
if (logic.length > 0) writer.line(`import { ${logic.join(", ")} } from "./logic.ts";`);
|
|
93
|
+
writer.line();
|
|
94
|
+
for (const middleware of ir.middlewares) {
|
|
95
|
+
const source = sources.get(middleware.name);
|
|
96
|
+
if (source === undefined) throw new Error(`middleware ${middleware.name} has no source`);
|
|
97
|
+
const lines = dedent(source.split("\n"));
|
|
98
|
+
docComment(writer, memberDoc(middleware.doc, undefined));
|
|
99
|
+
lines.forEach((line, index) => {
|
|
100
|
+
const prefix = index === 0 ? `export const ${middleware.name}: Middleware<${configType(middleware)}> = ` : "";
|
|
101
|
+
writer.line(`${prefix}${line}${index === lines.length - 1 ? ";" : ""}`);
|
|
102
|
+
});
|
|
103
|
+
writer.line();
|
|
104
|
+
}
|
|
105
|
+
return writer.toString();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** logic.ts: each logic function as the author wrote it, typed and exported. */
|
|
109
|
+
export function emitLogic(ir , sources , header ) {
|
|
110
|
+
const writer = createCodeWriter().line(header).line();
|
|
111
|
+
const helpers = usedNames([...sources.values()].join("\n"), ["hmacSha256Base64", "hmacSha256Hex", "randomUUID"]);
|
|
112
|
+
if (helpers.length > 0) writer.line(`import { ${helpers.join(", ")} } from "./runtime/helpers.ts";`).line();
|
|
113
|
+
for (const logic of ir.logic ?? []) {
|
|
114
|
+
const source = sources.get(logic.name);
|
|
115
|
+
if (source === undefined) throw new Error(`logic function ${logic.name} has no source`);
|
|
116
|
+
const lines = dedent(source.split("\n"));
|
|
117
|
+
const input = `{ ${logic.input.map((field) => `readonly ${propertyKey(field.name)}: ${fieldType(field)}`).join("; ")} }`;
|
|
118
|
+
docComment(writer, memberDoc(logic.doc, undefined));
|
|
119
|
+
lines.forEach((line, index) => {
|
|
120
|
+
const prefix = index === 0 ? `export const ${logic.name}: (input: ${input}) => ${tsType(logic.returns)} = ` : "";
|
|
121
|
+
writer.line(`${prefix}${line}${index === lines.length - 1 ? ";" : ""}`);
|
|
122
|
+
});
|
|
123
|
+
writer.line();
|
|
124
|
+
}
|
|
125
|
+
return writer.toString();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function configValue(middleware ) {
|
|
129
|
+
if (middleware.config.length === 0) return "{}";
|
|
130
|
+
const parts = middleware.config.map((field) =>
|
|
131
|
+
field.optional
|
|
132
|
+
? `...(options.${field.name} === undefined ? {} : { ${propertyKey(field.name)}: options.${field.name} })`
|
|
133
|
+
: `${propertyKey(field.name)}: options.${field.name}`,
|
|
134
|
+
);
|
|
135
|
+
return `{ ${parts.join(", ")} }`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function entryExpr(ir , name ) {
|
|
139
|
+
const middleware = ir.middlewares.find((candidate) => candidate.name === name);
|
|
140
|
+
if (middleware === undefined) throw new Error(`unknown middleware ${name}`);
|
|
141
|
+
return `entry(${quote(name)}, middleware.${name}, ${configValue(middleware)})`;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function emitOptions(writer , ir ) {
|
|
145
|
+
docComment(writer, [`Options for ${clientName(ir)}.`]);
|
|
146
|
+
writer
|
|
147
|
+
.line(`export interface ${ir.name}Options {`)
|
|
148
|
+
.indent((body) => {
|
|
149
|
+
configFields(ir).forEach((field) => {
|
|
150
|
+
docComment(body, memberDoc(field.doc, field.deprecated));
|
|
151
|
+
body.line(`readonly ${propertyKey(field.name)}${field.optional ? "?" : ""}: ${fieldType(field)};`);
|
|
152
|
+
});
|
|
153
|
+
docComment(body, [`Named server. Defaults to ${quote(ir.defaultServer)}.`]);
|
|
154
|
+
body.line("readonly server?: ServerName;");
|
|
155
|
+
docComment(body, ["Overrides the server URL, e.g. for a proxy or a local mock server."]);
|
|
156
|
+
body.line("readonly baseUrl?: string;");
|
|
157
|
+
docComment(body, ["Sends requests. Defaults to fetch()."]);
|
|
158
|
+
body.line("readonly transport?: Transport;");
|
|
159
|
+
docComment(body, ["Per-request timeout in milliseconds. Defaults to 60000."]);
|
|
160
|
+
body.line("readonly timeoutMs?: number;");
|
|
161
|
+
docComment(body, ["Your own middleware, run after the SDK's middleware and before the transport."]);
|
|
162
|
+
body.line("readonly middleware?: readonly MiddlewareEntry[];");
|
|
163
|
+
docComment(body, ["Receives events emitted by middleware. A listener that throws never breaks a call."]);
|
|
164
|
+
body.line("readonly onEvent?: EventListener;");
|
|
165
|
+
body.line("readonly clock?: Clock;");
|
|
166
|
+
const versions = ir.apiVersions;
|
|
167
|
+
if (versions !== undefined) {
|
|
168
|
+
if (configFields(ir).some((field) => field.name === "apiVersion")) throw new Error("a middleware config field apiVersion collides with the apiVersion client option of apiVersions");
|
|
169
|
+
docComment(body, [`The API version to use, sent as ${versions.header}. Defaults to ${quote(versions.values.at(-1) ?? "")}, the newest.`]);
|
|
170
|
+
body.line(`readonly apiVersion?: ${versions.values.map(quote).join(" | ")};`);
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
.line("}")
|
|
174
|
+
.line();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function emitArgs(writer , operation ) {
|
|
178
|
+
if (!hasArgs(operation)) return;
|
|
179
|
+
docComment(writer, [`Arguments of ${operation.name}.`]);
|
|
180
|
+
writer
|
|
181
|
+
.line(`export type ${argsName(operation)} = {`)
|
|
182
|
+
.indent((body) => {
|
|
183
|
+
operation.params.forEach((param) => {
|
|
184
|
+
docComment(body, memberDoc(param.doc, undefined));
|
|
185
|
+
body.line(`readonly ${propertyKey(param.name)}${param.optional ? "?" : ""}: ${tsType(param.type)};`);
|
|
186
|
+
});
|
|
187
|
+
if (operation.body !== undefined) body.line(`readonly body: ${bodyType(operation.body)};`);
|
|
188
|
+
})
|
|
189
|
+
.line("};")
|
|
190
|
+
.line();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function emitSpec(writer , operation ) {
|
|
194
|
+
const result = operation.events === undefined ? bodyKind(operation.returns) : "events";
|
|
195
|
+
writer
|
|
196
|
+
.line(`const ${specName(operation)}: OperationSpec<${operationResultType(operation)}> = {`)
|
|
197
|
+
.indent((body) => {
|
|
198
|
+
body.line(`name: ${quote(operation.name)},`);
|
|
199
|
+
body.line(`method: ${quote(operation.method)},`);
|
|
200
|
+
body.line(`path: ${quote(operation.path)},`);
|
|
201
|
+
body.line(`params: [${operation.params.map((param) => `{ name: ${quote(param.name)}, in: ${quote(param.in)} }`).join(", ")}],`);
|
|
202
|
+
const kind = bodyKind(operation.body);
|
|
203
|
+
if (kind !== undefined) body.line(`body: ${quote(kind)},`);
|
|
204
|
+
if (result !== undefined) body.line(`result: ${quote(result)},`);
|
|
205
|
+
if (result === "json" && operation.returns !== undefined) body.line(`decode: ${decoderExpr(operation.returns, `operations.${operation.name}.returns`)},`);
|
|
206
|
+
if (operation.events !== undefined && !textEvents(operation.events)) body.line(`decodeEvent: ${decoderExpr(operation.events, `operations.${operation.name}.events`)},`);
|
|
207
|
+
body.line("errors: {").indent((errors) =>
|
|
208
|
+
operation.errors.forEach((error) =>
|
|
209
|
+
errors.line(`${error.status}: (status, body, response) => new ${errorClassName(error.name)}(status, body, response, ${quote(error.name)}),`),
|
|
210
|
+
),
|
|
211
|
+
);
|
|
212
|
+
body.line("},");
|
|
213
|
+
body.line(`use: [${operation.use.map(quote).join(", ")}],`);
|
|
214
|
+
if (operation.since !== undefined) body.line(`since: ${quote(operation.since)},`);
|
|
215
|
+
})
|
|
216
|
+
.line("};")
|
|
217
|
+
.line();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function operationDoc(operation ) {
|
|
221
|
+
return [
|
|
222
|
+
...(operation.doc === undefined ? [] : [operation.doc, ""]),
|
|
223
|
+
`HTTP: ${operation.method} ${operation.path}`,
|
|
224
|
+
...(operation.events === undefined ? [] : [`Server-sent events, read as they arrive: \`for await (const event of client.${operation.name}(…))\`.`]),
|
|
225
|
+
...(operation.since === undefined ? [] : [`Since API version ${operation.since}.`]),
|
|
226
|
+
...(operation.stability === "stable" ? [] : [`Stability: ${operation.stability}`]),
|
|
227
|
+
...operation.errors.map((error) => `@throws {${errorClassName(error.name)}} ${error.status}`),
|
|
228
|
+
"@throws {ApiError} for any other non-2xx response",
|
|
229
|
+
...(operation.deprecated === undefined ? [] : [`@deprecated ${operation.deprecated}`]),
|
|
230
|
+
];
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function signature(operation ) {
|
|
234
|
+
return hasArgs(operation) ? `args: ${argsName(operation)}, options?: CallOptions` : "options?: CallOptions";
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Like Python's DeprecationWarning: deprecated operations warn once per process when first called. */
|
|
238
|
+
function emitDeprecationHelper(writer ) {
|
|
239
|
+
writer.line("const warnedDeprecated = new Set<string>();").line();
|
|
240
|
+
docComment(writer, ["Warns once per operation that it is deprecated."]);
|
|
241
|
+
writer.line("function warnDeprecated(operation: string, message: string): void {").indent((fn) => {
|
|
242
|
+
fn.line("if (warnedDeprecated.has(operation)) return;");
|
|
243
|
+
fn.line("warnedDeprecated.add(operation);");
|
|
244
|
+
fn.line("console.warn(`${operation} is deprecated: ${message}`);");
|
|
245
|
+
});
|
|
246
|
+
writer.line("}").line();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function emitClass(writer , ir , sdk ) {
|
|
250
|
+
if (ir.operations.some((operation) => operation.deprecated !== undefined)) emitDeprecationHelper(writer);
|
|
251
|
+
const opLevel = [...new Set(ir.operations.flatMap((operation) => operation.use))].sort();
|
|
252
|
+
writer.line(`export interface ${ir.name}Raw {`).indent((body) =>
|
|
253
|
+
ir.operations.forEach((operation) => body.line(`${operation.name}(${signature(operation)}): Promise<RawResult<${operationResultType(operation)}>>;`)),
|
|
254
|
+
);
|
|
255
|
+
writer.line("}").line();
|
|
256
|
+
|
|
257
|
+
docComment(writer, memberDoc(ir.doc, undefined));
|
|
258
|
+
writer.line(`export class ${clientName(ir)} {`).indent((body) => {
|
|
259
|
+
docComment(body, ["The same operations, resolving to { data, response } so the HTTP status and headers are available."]);
|
|
260
|
+
body.line(`readonly raw: ${ir.name}Raw;`).line();
|
|
261
|
+
const waits = ir.operations.some((operation) => operation.wait !== undefined);
|
|
262
|
+
if (waits) body.line("readonly #clock: Clock;").line();
|
|
263
|
+
body.line(`constructor(options: ${ir.name}Options) {`).indent((ctor) => {
|
|
264
|
+
if (ir.operations.length === 0) {
|
|
265
|
+
ctor.line("this.raw = {};");
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
ctor.line("const clock = options.clock ?? systemClock;");
|
|
269
|
+
if (waits) ctor.line("this.#clock = clock;");
|
|
270
|
+
ctor.line("const runtime: ClientRuntime = {").indent((runtime) => {
|
|
271
|
+
runtime.line("baseUrl: options.baseUrl ?? SERVERS[options.server ?? DEFAULT_SERVER],");
|
|
272
|
+
runtime.line("transport: options.transport ?? fetchTransport(),");
|
|
273
|
+
runtime.line(`middleware: [${ir.use.map((name) => entryExpr(ir, name)).join(", ")}],`);
|
|
274
|
+
runtime.line("registry: {").indent((registry) => opLevel.forEach((name) => registry.line(`${propertyKey(name)}: ${entryExpr(ir, name)},`)));
|
|
275
|
+
runtime.line("},");
|
|
276
|
+
runtime.line("userMiddleware: options.middleware ?? [],");
|
|
277
|
+
runtime.line("clock,");
|
|
278
|
+
runtime.line("state: createState(clock),");
|
|
279
|
+
runtime.line("listeners: options.onEvent === undefined ? [] : [options.onEvent],");
|
|
280
|
+
runtime.line("timeoutMs: options.timeoutMs ?? DEFAULT_TIMEOUT_MS,");
|
|
281
|
+
runtime.line(`sdk: { name: ${quote(sdk.name)}, version: ${quote(sdk.version)}, language: "typescript" },`);
|
|
282
|
+
const versions = ir.apiVersions;
|
|
283
|
+
if (versions !== undefined) {
|
|
284
|
+
runtime.line(`apiVersion: { header: ${quote(versions.header)}, value: options.apiVersion ?? ${quote(versions.values.at(-1) ?? "")}, values: [${versions.values.map(quote).join(", ")}] },`);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
ctor.line("};");
|
|
288
|
+
ctor.line("this.raw = {").indent((raw) =>
|
|
289
|
+
ir.operations.forEach((operation) =>
|
|
290
|
+
raw.line(
|
|
291
|
+
hasArgs(operation)
|
|
292
|
+
? `${operation.name}: (args, callOptions) => callOperation(runtime, ${specName(operation)}, args, callOptions),`
|
|
293
|
+
: `${operation.name}: (callOptions) => callOperation(runtime, ${specName(operation)}, {}, callOptions),`,
|
|
294
|
+
),
|
|
295
|
+
),
|
|
296
|
+
);
|
|
297
|
+
ctor.line("};");
|
|
298
|
+
});
|
|
299
|
+
body.line("}");
|
|
300
|
+
|
|
301
|
+
ir.operations.forEach((operation) => {
|
|
302
|
+
body.line();
|
|
303
|
+
docComment(body, operationDoc(operation));
|
|
304
|
+
const call = `this.raw.${operation.name}(${hasArgs(operation) ? "args, options" : "options"})`;
|
|
305
|
+
if (operation.events !== undefined) {
|
|
306
|
+
// The request is sent when iteration starts; events arrive as the server sends them.
|
|
307
|
+
body.line(`async *${operation.name}(${signature(operation)}): AsyncGenerator<ServerSentEvent<${tsType(operation.events)}>, void, undefined> {`).indent((method) => {
|
|
308
|
+
if (operation.deprecated !== undefined) method.line(`warnDeprecated(${quote(operation.name)}, ${quote(operation.deprecated)});`);
|
|
309
|
+
method.line(`yield* (await ${call}).data;`);
|
|
310
|
+
});
|
|
311
|
+
body.line("}");
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
body.line(`async ${operation.name}(${signature(operation)}): Promise<${returnType(operation)}> {`).indent((method) => {
|
|
315
|
+
if (operation.deprecated !== undefined) method.line(`warnDeprecated(${quote(operation.name)}, ${quote(operation.deprecated)});`);
|
|
316
|
+
method.line(returnType(operation) === "void" ? `await ${call};` : `return (await ${call}).data;`);
|
|
317
|
+
});
|
|
318
|
+
body.line("}");
|
|
319
|
+
if (operation.paginate !== undefined) emitIterator(body, ir, operation, operation.paginate);
|
|
320
|
+
if (operation.wait !== undefined) emitWait(body, operation, operation.wait);
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
writer.line("}");
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** <name>All: every item of every page, fetched as the caller iterates (kit block pagination.iterate). */
|
|
327
|
+
function emitIterator(writer , ir , operation , paginate ) {
|
|
328
|
+
const { item, field } = pagedItems(ir, operation);
|
|
329
|
+
const read = (object , name ) => `${object}[${quote(name)}]`;
|
|
330
|
+
const args = argsName(operation);
|
|
331
|
+
writer.line();
|
|
332
|
+
docComment(writer, [`Every item of every ${operation.name} page, fetched as you iterate: \`for await (const item of client.${operation.name}All(…))\`.`]);
|
|
333
|
+
writer.line(`${operation.name}All(${signature(operation)}): AsyncGenerator<${tsType(item)}, void, undefined> {`).indent((method) => {
|
|
334
|
+
if (paginate.kind === "link") {
|
|
335
|
+
// The Link header names the next page: the same operation is called with that URL.
|
|
336
|
+
const call = (callOptions ) => (hasArgs(operation) ? `this.raw.${operation.name}(args, ${callOptions})` : `this.raw.${operation.name}(${callOptions})`);
|
|
337
|
+
method.line("return paginate(").indent((inner) => {
|
|
338
|
+
inner.line("{} as { readonly url?: string },");
|
|
339
|
+
inner.line(`(state) => ${call("state.url === undefined ? options : { ...options, url: state.url }")},`);
|
|
340
|
+
inner.line(`(page) => ${field === undefined ? "page.data" : read("page.data", field)},`);
|
|
341
|
+
inner.line('(_state, page) => {').indent((fn) => {
|
|
342
|
+
fn.line('const url = nextLink(page.response.header("link"));');
|
|
343
|
+
fn.line("return url === undefined ? undefined : { url };");
|
|
344
|
+
});
|
|
345
|
+
inner.line("},");
|
|
346
|
+
});
|
|
347
|
+
method.line(");");
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
method.line("return paginate(").indent((call) => {
|
|
351
|
+
call.line(hasArgs(operation) ? "args," : "{},");
|
|
352
|
+
call.line(hasArgs(operation) ? `(params: ${args}) => this.${operation.name}(params, options),` : `() => this.${operation.name}(options),`);
|
|
353
|
+
call.line(`(page) => ${read("page", paginate.items)},`);
|
|
354
|
+
if (paginate.kind === "cursor") {
|
|
355
|
+
call.line("(params, page) => {").indent((fn) => {
|
|
356
|
+
fn.line(`const cursor = ${read("page", paginate.next)};`);
|
|
357
|
+
fn.line(`return cursor === undefined || cursor === null || cursor === "" ? undefined : { ...params, ${propertyKey(paginate.param)}: cursor };`);
|
|
358
|
+
});
|
|
359
|
+
call.line("},");
|
|
360
|
+
} else {
|
|
361
|
+
const short = paginate.limit === undefined ? "" : ` || items.length < (${read("params", paginate.limit)} ?? 0)`;
|
|
362
|
+
call.line(`(params, _page, items) => (items.length === 0${short} ? undefined : { ...params, ${propertyKey(paginate.param)}: (${read("params", paginate.param)} ?? 0) + items.length }),`);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
method.line(");");
|
|
366
|
+
});
|
|
367
|
+
writer.line("}");
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** <name>AndWait: starts the work, then polls until it is done (kit block lro.wait). */
|
|
371
|
+
function emitWait(writer , operation , wait ) {
|
|
372
|
+
const read = (object , name ) => `${object}[${quote(name)}]`;
|
|
373
|
+
writer.line();
|
|
374
|
+
docComment(writer, [
|
|
375
|
+
`${operation.name}, then ${wait.poll} every ${wait.intervalMs} ms until ${wait.field} is ${wait.done.join(" or ")}; the finished result.`,
|
|
376
|
+
`@throws {WaitTimeoutError} when the work is not done within ${wait.timeoutMs} ms`,
|
|
377
|
+
]);
|
|
378
|
+
writer.line(`async ${operation.name}AndWait(${signature(operation)}): Promise<${returnType(operation)}> {`).indent((method) => {
|
|
379
|
+
method.line(`const first = await this.${operation.name}(${hasArgs(operation) ? "args, options" : "options"});`);
|
|
380
|
+
method.line(`const done: readonly string[] = [${wait.done.map(quote).join(", ")}];`);
|
|
381
|
+
method.line("return waitUntil(").indent((call) => {
|
|
382
|
+
call.line("first,");
|
|
383
|
+
call.line(`(current) => this.${wait.poll}({ ${propertyKey(wait.param)}: ${read("current", wait.from)} }, options),`);
|
|
384
|
+
call.line(`(current) => done.includes(${read("current", wait.field)}),`);
|
|
385
|
+
call.line("this.#clock,");
|
|
386
|
+
call.line(`${wait.intervalMs},`);
|
|
387
|
+
call.line(`${wait.timeoutMs},`);
|
|
388
|
+
});
|
|
389
|
+
method.line(");");
|
|
390
|
+
});
|
|
391
|
+
writer.line("}");
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const RUNTIME_IMPORTS = [
|
|
395
|
+
["./runtime/client.ts", ["callOperation", "entry"], ["CallOptions", "ClientRuntime", "MiddlewareEntry", "OperationSpec", "RawResult"]],
|
|
396
|
+
["./runtime/context.ts", ["createState", "systemClock"], ["Clock", "EventListener"]],
|
|
397
|
+
["./runtime/transport.ts", ["fetchTransport"], ["Transport"]],
|
|
398
|
+
["./runtime/models.ts", MODEL_HELPERS, []],
|
|
399
|
+
["./runtime/pagination.ts", ["nextLink", "paginate"], []],
|
|
400
|
+
["./runtime/sse.ts", [], ["ServerSentEvent"]],
|
|
401
|
+
["./runtime/wait.ts", ["waitUntil"], []],
|
|
402
|
+
["./runtime/multipart.ts", [], ["MultipartForm"]],
|
|
403
|
+
];
|
|
404
|
+
|
|
405
|
+
/** Identity of the generated SDK, shown to middleware as ctx.sdk. */
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
export function emitClient(ir , header , sdk ) {
|
|
412
|
+
const body = createCodeWriter();
|
|
413
|
+
body.line("export const SERVERS = {").indent((servers) => ir.servers.forEach((server) => servers.line(`${propertyKey(server.name)}: ${quote(server.url)},`)));
|
|
414
|
+
body.line("} as const;").line();
|
|
415
|
+
body.line("export type ServerName = keyof typeof SERVERS;").line();
|
|
416
|
+
body.line(`const DEFAULT_SERVER: ServerName = ${quote(ir.defaultServer)};`);
|
|
417
|
+
body.line("const DEFAULT_TIMEOUT_MS = 60_000;").line();
|
|
418
|
+
emitOptions(body, ir);
|
|
419
|
+
ir.operations.forEach((operation) => emitArgs(body, operation));
|
|
420
|
+
ir.operations.forEach((operation) => emitSpec(body, operation));
|
|
421
|
+
emitClass(body, ir, sdk);
|
|
422
|
+
const text = body.toString();
|
|
423
|
+
|
|
424
|
+
const writer = createCodeWriter().line(header).line();
|
|
425
|
+
RUNTIME_IMPORTS.forEach(([from, values, types]) => importLine(writer, from, usedNames(text, values), usedNames(text, types)));
|
|
426
|
+
const typeNames = ir.types.map((named) => named.name);
|
|
427
|
+
importLine(writer, "./models.ts", usedNames(text, typeNames.map((name) => `decode${name}`)), usedNames(text, typeNames));
|
|
428
|
+
const errorNames = [...new Set(ir.operations.flatMap((operation) => operation.errors.map((error) => errorClassName(error.name))))].sort();
|
|
429
|
+
importLine(writer, "./errors.ts", usedNames(text, errorNames), []);
|
|
430
|
+
if (ir.middlewares.length > 0 && text.includes("middleware.")) writer.line('import * as middleware from "./middleware.ts";');
|
|
431
|
+
writer.line();
|
|
432
|
+
text.split("\n").forEach((line) => writer.line(line));
|
|
433
|
+
return writer.toString();
|
|
434
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// src/models.ts: one interface and one forward-compatible decoder per named type.
|
|
2
|
+
|
|
3
|
+
import { createCodeWriter, } from "@sdkloom/codegen";
|
|
4
|
+
|
|
5
|
+
import { docComment, importLine, memberDoc, propertyKey, quote, usedNames } from "./naming.ts";
|
|
6
|
+
import { decoderExpr, tsType, UNKNOWN_VARIANT } from "./types.ts";
|
|
7
|
+
|
|
8
|
+
export const MODEL_HELPERS = ["asArray", "asBoolean", "asDecimal", "asEnum", "asInteger", "asMap", "asNumber", "asObject", "asString", "field", "omit"];
|
|
9
|
+
|
|
10
|
+
function emitObject(writer , named , node ) {
|
|
11
|
+
docComment(writer, memberDoc(named.doc, named.deprecated));
|
|
12
|
+
writer
|
|
13
|
+
.line(`export interface ${named.name} {`)
|
|
14
|
+
.indent((body) => {
|
|
15
|
+
node.fields.forEach((field) => {
|
|
16
|
+
docComment(body, memberDoc(field.doc, field.deprecated));
|
|
17
|
+
body.line(`readonly ${propertyKey(field.name)}${field.optional ? "?" : ""}: ${tsType(field.type)}${field.nullable ? " | null" : ""};`);
|
|
18
|
+
});
|
|
19
|
+
})
|
|
20
|
+
.line("}")
|
|
21
|
+
.line();
|
|
22
|
+
|
|
23
|
+
writer
|
|
24
|
+
.line(`export function decode${named.name}(value: unknown, path = "$"): ${named.name} {`)
|
|
25
|
+
.indent((body) => {
|
|
26
|
+
body.line("const object = asObject(value, path);");
|
|
27
|
+
node.fields.forEach((field, index) => {
|
|
28
|
+
const decoder = decoderExpr(field.type, `types.${named.name}.${field.name}`);
|
|
29
|
+
const flags = `{ optional: ${field.optional}, nullable: ${field.nullable} }`;
|
|
30
|
+
body.line(`const f${index} = field(object, ${quote(field.name)}, path, ${decoder}, ${flags}) as ${named.name}[${quote(field.name)}];`);
|
|
31
|
+
});
|
|
32
|
+
body
|
|
33
|
+
.line("return {")
|
|
34
|
+
.indent((result) => {
|
|
35
|
+
// Unknown fields come back through the spread; optional fields that cannot be null are left out of it, so a
|
|
36
|
+
// null the decoder read as absent does not return.
|
|
37
|
+
const absentWhenNull = node.fields.filter((field) => field.optional && !field.nullable).map((field) => quote(field.name));
|
|
38
|
+
result.line(absentWhenNull.length === 0 ? "...object," : `...(omit(object, [${absentWhenNull.join(", ")}])),`);
|
|
39
|
+
node.fields.forEach((field, index) => {
|
|
40
|
+
const key = propertyKey(field.name);
|
|
41
|
+
result.line(field.optional ? `...(f${index} === undefined ? {} : { ${key}: f${index} }),` : `${key}: f${index},`);
|
|
42
|
+
});
|
|
43
|
+
})
|
|
44
|
+
.line("};");
|
|
45
|
+
})
|
|
46
|
+
.line("}")
|
|
47
|
+
.line();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function emitUnion(writer , named , node ) {
|
|
51
|
+
const key = propertyKey(node.discriminator);
|
|
52
|
+
docComment(writer, [...memberDoc(named.doc, named.deprecated), `Unknown variants decode to { ${node.discriminator}: "${UNKNOWN_VARIANT}" } and keep the raw value.`]);
|
|
53
|
+
writer.line(`export type ${named.name} =`).indent((body) => {
|
|
54
|
+
node.variants.forEach((variant) => body.line(`| (${variant.type} & { readonly ${key}: ${quote(variant.tag)} })`));
|
|
55
|
+
body.line(`| { readonly ${key}: "${UNKNOWN_VARIANT}"; readonly unknownTag: string; readonly raw: JsonObject };`);
|
|
56
|
+
});
|
|
57
|
+
writer.line();
|
|
58
|
+
|
|
59
|
+
writer
|
|
60
|
+
.line(`export function decode${named.name}(value: unknown, path = "$"): ${named.name} {`)
|
|
61
|
+
.indent((body) => {
|
|
62
|
+
body.line("const object = asObject(value, path);");
|
|
63
|
+
body.line(`const tag = asString(object[${quote(node.discriminator)}], path + ${quote(`.${node.discriminator}`)});`);
|
|
64
|
+
body
|
|
65
|
+
.line("switch (tag) {")
|
|
66
|
+
.indent((cases) => {
|
|
67
|
+
node.variants.forEach((variant) =>
|
|
68
|
+
cases.line(`case ${quote(variant.tag)}:`).indent((item) => item.line(`return { ...decode${variant.type}(object, path), ${key}: ${quote(variant.tag)} };`)),
|
|
69
|
+
);
|
|
70
|
+
cases.line("default:").indent((item) => item.line(`return { ${key}: "${UNKNOWN_VARIANT}", unknownTag: tag, raw: object };`));
|
|
71
|
+
})
|
|
72
|
+
.line("}");
|
|
73
|
+
})
|
|
74
|
+
.line("}")
|
|
75
|
+
.line();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function emitModels(ir , header ) {
|
|
79
|
+
const body = createCodeWriter();
|
|
80
|
+
for (const named of ir.types) {
|
|
81
|
+
if (named.type.kind === "object") emitObject(body, named, named.type);
|
|
82
|
+
else if (named.type.kind === "union") emitUnion(body, named, named.type);
|
|
83
|
+
else throw new Error(`types.${named.name}: only model() and union() can be named`);
|
|
84
|
+
}
|
|
85
|
+
const text = body.toString();
|
|
86
|
+
|
|
87
|
+
const writer = createCodeWriter().line(header).line();
|
|
88
|
+
importLine(writer, "./runtime/models.ts", usedNames(text, MODEL_HELPERS), usedNames(text, ["JsonObject"]));
|
|
89
|
+
writer.line();
|
|
90
|
+
text.split("\n").forEach((line) => writer.line(line));
|
|
91
|
+
if (text === "") writer.line("export {};");
|
|
92
|
+
return writer.toString();
|
|
93
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// package.json, README.md and src/index.ts.
|
|
2
|
+
|
|
3
|
+
import { canonicalJson, createCodeWriter } from "@sdkloom/codegen";
|
|
4
|
+
|
|
5
|
+
import { clientName } from "./emit-client.ts";
|
|
6
|
+
import { bodyKind } from "./types.ts";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export function emitPackageJson(ir , info ) {
|
|
17
|
+
return canonicalJson({
|
|
18
|
+
name: info.packageName,
|
|
19
|
+
version: info.version,
|
|
20
|
+
...(ir.doc === undefined ? {} : { description: ir.doc }),
|
|
21
|
+
type: "module",
|
|
22
|
+
engines: { node: info.nodeVersions },
|
|
23
|
+
exports: { ".": "./src/index.ts", "./testing": "./src/testing.ts" },
|
|
24
|
+
files: ["src", "README.md"],
|
|
25
|
+
sideEffects: false,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function emitIndex(header , webhooks = false, logic = false) {
|
|
30
|
+
const writer = createCodeWriter()
|
|
31
|
+
.line(header)
|
|
32
|
+
.line()
|
|
33
|
+
.line('export * from "./client.ts";')
|
|
34
|
+
.line('export * from "./errors.ts";')
|
|
35
|
+
.line('export * from "./models.ts";')
|
|
36
|
+
.line('export { ApiError, TransportError, type TransportFailure } from "./runtime/errors.ts";')
|
|
37
|
+
.line('export { DecodeError, type JsonObject } from "./runtime/models.ts";')
|
|
38
|
+
.line('export { entry, type CallOptions, type Middleware, type MiddlewareEntry, type RawResult } from "./runtime/client.ts";')
|
|
39
|
+
.line('export { fetchTransport, type SendOptions, type Transport } from "./runtime/transport.ts";')
|
|
40
|
+
.line('export { HttpRequest, HttpResponse, type HeaderList } from "./runtime/http.ts";')
|
|
41
|
+
.line('export type { ServerSentEvent } from "./runtime/sse.ts";')
|
|
42
|
+
.line('export { WaitTimeoutError } from "./runtime/wait.ts";')
|
|
43
|
+
.line('export { MultipartForm } from "./runtime/multipart.ts";')
|
|
44
|
+
.line('export type { Clock, Context, EventFields, EventListener } from "./runtime/context.ts";');
|
|
45
|
+
if (logic) writer.line('export * from "./logic.ts";');
|
|
46
|
+
if (webhooks) writer.line('export * from "./webhooks.ts";').line('export { WebhookSignatureError, type WebhookHeaders } from "./runtime/webhooks.ts";');
|
|
47
|
+
return writer.toString();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function cell(text ) {
|
|
51
|
+
return text.replaceAll("|", "\\|").replaceAll("\n", " ");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function emitReadme(ir , info ) {
|
|
55
|
+
const client = clientName(ir);
|
|
56
|
+
const example = ir.operations.find((operation) => bodyKind(operation.returns) === "json" && operation.returns?.kind === "ref");
|
|
57
|
+
const writer = createCodeWriter()
|
|
58
|
+
.line(`# ${info.packageName}`)
|
|
59
|
+
.line()
|
|
60
|
+
.line(ir.doc ?? "")
|
|
61
|
+
.line()
|
|
62
|
+
.line(`> Generated by sdkloom ${info.sdkloomVersion} from \`${info.source}\`. Do not edit by hand: run \`sdkloom build\`.`)
|
|
63
|
+
.line()
|
|
64
|
+
.line("## Usage")
|
|
65
|
+
.line()
|
|
66
|
+
.line("```ts")
|
|
67
|
+
.line(`import { ${client} } from "${info.packageName}";`)
|
|
68
|
+
.line()
|
|
69
|
+
.line(`const client = new ${client}({ /* options */ });`)
|
|
70
|
+
.line("```")
|
|
71
|
+
.line()
|
|
72
|
+
.line("## Servers")
|
|
73
|
+
.line()
|
|
74
|
+
.line("| Name | URL |")
|
|
75
|
+
.line("|---|---|");
|
|
76
|
+
ir.servers.forEach((server) => writer.line(`| ${server.name}${server.name === ir.defaultServer ? " (default)" : ""} | ${server.url} |`));
|
|
77
|
+
writer.line().line("## Operations").line().line("| Method | HTTP | Description |").line("|---|---|---|");
|
|
78
|
+
ir.operations.forEach((operation) =>
|
|
79
|
+
writer.line(`| \`${operation.name}\` | \`${operation.method} ${operation.path}\` | ${cell(operation.deprecated === undefined ? (operation.doc ?? "") : `Deprecated: ${operation.deprecated}`)} |`),
|
|
80
|
+
);
|
|
81
|
+
writer.line().line("## Testing").line().line("```ts").line(`import { mockClient, examples } from "${info.packageName}/testing";`).line();
|
|
82
|
+
writer.line("const { client, mock } = mockClient();");
|
|
83
|
+
if (example?.returns?.kind === "ref") writer.line(`mock.${example.name}.returns(examples.${example.returns.name}());`);
|
|
84
|
+
writer.line("```").line();
|
|
85
|
+
writer
|
|
86
|
+
.line("## Runtime")
|
|
87
|
+
.line()
|
|
88
|
+
.line(`TypeScript sources with no dependencies. Needs a runtime with \`fetch\`: Node.js ${info.nodeVersions}, Deno, Bun or a browser.`);
|
|
89
|
+
return writer.toString();
|
|
90
|
+
}
|