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,405 @@
|
|
|
1
|
+
// client.go: Options, NewClient, one method per operation, their raw variants and operation specs.
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import { pagedItems, } from "@sdkloom/ir";
|
|
5
|
+
import { errorType } from "./emit-models.ts";
|
|
6
|
+
import { decoder, doc, encode, encoderFunc, exported, fieldType, go, goType, local, RUNTIME, str } from "./names.ts";
|
|
7
|
+
import { goName } from "@sdkloom/transpile";
|
|
8
|
+
|
|
9
|
+
/** Options every client has besides middleware configuration. */
|
|
10
|
+
export const CLIENT_OPTIONS = ["Server", "BaseURL", "Transport", "TimeoutMs", "Middleware", "OnEvent", "Clock", "Proxy", "TLSConfig"];
|
|
11
|
+
|
|
12
|
+
export function configFields(ir ) {
|
|
13
|
+
const fields = new Map ();
|
|
14
|
+
for (const middleware of ir.middlewares) {
|
|
15
|
+
for (const item of middleware.config) {
|
|
16
|
+
if (CLIENT_OPTIONS.includes(exported(item.name))) throw new Error(`config field ${item.name} of middleware ${middleware.name} collides with a client option`);
|
|
17
|
+
if (!fields.has(item.name)) fields.set(item.name, item);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return [...fields.values()];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export function kindOf(node ) {
|
|
26
|
+
if (node === undefined) return undefined;
|
|
27
|
+
return node.kind === "primitive" && (node.name === "bytes" || node.name === "stream" || node.name === "multipart") ? node.name : "json";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** An event stream whose data stays text: no decoder, the data is a string. */
|
|
31
|
+
export function textEvents(node ) {
|
|
32
|
+
return node.kind === "primitive" && node.name === "string";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The Go type an operation returns: struct{} for nothing, []byte, io.Reader, or the decoded type. */
|
|
36
|
+
export function resultType(operation , prefix = "") {
|
|
37
|
+
if (operation.events !== undefined) return `*${RUNTIME}.EventStream[${goType(operation.events, prefix)}]`;
|
|
38
|
+
const kind = kindOf(operation.returns);
|
|
39
|
+
if (kind === undefined) return "struct{}";
|
|
40
|
+
if (kind === "bytes") return "[]byte";
|
|
41
|
+
if (kind === "stream") return "io.Reader";
|
|
42
|
+
return goType(operation.returns , prefix);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function paramsType(operation ) {
|
|
46
|
+
return `${exported(operation.name)}Params`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function named(operation ) {
|
|
50
|
+
return operation.params.filter((param) => param.in !== "path");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function specName(operation ) {
|
|
54
|
+
return `spec${exported(operation.name)}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The parameters after ctx: path parameters, the body, then a params struct when there are other parameters. */
|
|
58
|
+
export function signature(operation , prefix = "") {
|
|
59
|
+
const path = operation.params.filter((param) => param.in === "path");
|
|
60
|
+
const parts = [
|
|
61
|
+
...path.map((param) => `${local(param.name)} ${goType(param.type, prefix)}`),
|
|
62
|
+
...(operation.body === undefined ? [] : [`body ${kindOf(operation.body) === "json" || kindOf(operation.body) === "multipart" ? goType(operation.body, prefix) : kindOf(operation.body) === "bytes" ? "[]byte" : "io.Reader"}`]),
|
|
63
|
+
...(named(operation).length === 0 ? [] : [`params ${prefix}${paramsType(operation)}`]),
|
|
64
|
+
];
|
|
65
|
+
const call = [...path.map((param) => local(param.name)), ...(operation.body === undefined ? [] : ["body"]), ...(named(operation).length === 0 ? [] : ["params"])];
|
|
66
|
+
return { declaration: ["ctx context.Context", ...parts, `opts ...${RUNTIME}.CallOption`].join(", "), call: ["ctx", ...call, "opts..."].join(", ") };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function paramValue(ir , param ) {
|
|
70
|
+
const field = `params.${exported(param.name)}`;
|
|
71
|
+
return param.optional ? `${RUNTIME}.OptEncode(${field}, ${encoderFunc(ir, param.type)})` : encode(ir, param.type, field);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function emitParams(writer , operation ) {
|
|
75
|
+
if (named(operation).length === 0) return;
|
|
76
|
+
doc(writer, [`${paramsType(operation)} are the query and header parameters of ${exported(operation.name)}.`]);
|
|
77
|
+
writer.line(`type ${paramsType(operation)} struct {`).indent((body) => {
|
|
78
|
+
named(operation).forEach((param) => {
|
|
79
|
+
doc(body, param.doc === undefined ? [] : [param.doc]);
|
|
80
|
+
body.line(`${exported(param.name)} ${fieldType({ type: param.type, optional: param.optional, nullable: false })}`);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
writer.line("}").line();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function emitSpec(writer , ir , operation ) {
|
|
87
|
+
const body = kindOf(operation.body);
|
|
88
|
+
const result = operation.events === undefined ? kindOf(operation.returns) : "events";
|
|
89
|
+
writer.line(`var ${specName(operation)} = ${RUNTIME}.OperationSpec{`).indent((spec) => {
|
|
90
|
+
spec.line(`Name: ${str(operation.name)},`);
|
|
91
|
+
spec.line(`Method: ${str(operation.method)},`);
|
|
92
|
+
spec.line(`Path: ${str(operation.path)},`);
|
|
93
|
+
if (body !== undefined) spec.line(`Body: ${str(body)},`);
|
|
94
|
+
if (result !== undefined) spec.line(`Result: ${str(result)},`);
|
|
95
|
+
spec.line(`Errors: map[int]${RUNTIME}.ErrorFactory{`).indent((errors) =>
|
|
96
|
+
operation.errors.forEach((error) =>
|
|
97
|
+
errors.line(`${error.status}: func(status int, body any, response ${RUNTIME}.Response) error { return ${errorType(error.name)}{${RUNTIME}.NewAPIError(${str(error.name)}, status, body, response)} },`),
|
|
98
|
+
),
|
|
99
|
+
);
|
|
100
|
+
spec.line("},");
|
|
101
|
+
spec.line(`Use: []string{${operation.use.map(str).join(", ")}},`);
|
|
102
|
+
if (operation.since !== undefined) spec.line(`Since: ${str(operation.since)},`);
|
|
103
|
+
});
|
|
104
|
+
writer.line("}").line();
|
|
105
|
+
void ir;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function operationDoc(operation ) {
|
|
109
|
+
return [
|
|
110
|
+
`${exported(operation.name)} ${operation.doc ?? ""}`.trim(),
|
|
111
|
+
"",
|
|
112
|
+
`HTTP: ${operation.method} ${operation.path}`,
|
|
113
|
+
...(operation.since === undefined ? [] : [`Since API version ${operation.since}.`]),
|
|
114
|
+
...(operation.events === undefined ? [] : ["", "Server-sent events, read as they arrive: the request is sent by the first Next; the stream's Err reports", "errors, including the ones listed here. Close it to stop early."]),
|
|
115
|
+
...(operation.stability === "stable" ? [] : [`Stability: ${operation.stability}`]),
|
|
116
|
+
...operation.errors.map((error) => `Returns ${errorType(error.name)} for HTTP ${error.status}.`),
|
|
117
|
+
`Returns *${RUNTIME}.APIError for any other non-2xx response.`,
|
|
118
|
+
...(operation.deprecated === undefined ? [] : ["", `Deprecated: ${operation.deprecated}`]),
|
|
119
|
+
];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function emitArgs(writer , ir , operation ) {
|
|
123
|
+
const path = operation.params.filter((param) => param.in === "path");
|
|
124
|
+
const query = operation.params.filter((param) => param.in === "query");
|
|
125
|
+
const headers = operation.params.filter((param) => param.in === "header");
|
|
126
|
+
writer.line(`args := ${RUNTIME}.Args{`).indent((args) => {
|
|
127
|
+
args.line(`Path: map[string]any{${path.map((param) => `${str(param.name)}: ${encode(ir, param.type, local(param.name))}`).join(", ")}},`);
|
|
128
|
+
if (query.length > 0) args.line(`Query: []${RUNTIME}.QueryParam{${query.map((param) => `{Name: ${str(param.name)}, Value: ${paramValue(ir, param)}}`).join(", ")}},`);
|
|
129
|
+
if (headers.length > 0) args.line(`Headers: []${RUNTIME}.QueryParam{${headers.map((param) => `{Name: ${str(param.name)}, Value: ${paramValue(ir, param)}}`).join(", ")}},`);
|
|
130
|
+
const body = kindOf(operation.body);
|
|
131
|
+
if (body === "json") args.line(`Body: ${encode(ir, operation.body , "body")},`);
|
|
132
|
+
else if (body !== undefined) args.line("Body: body,");
|
|
133
|
+
});
|
|
134
|
+
writer.line("}");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function emitRaw(writer , ir , operation ) {
|
|
138
|
+
const name = exported(operation.name);
|
|
139
|
+
const type = resultType(operation);
|
|
140
|
+
const result = `${RUNTIME}.Result[${type}]`;
|
|
141
|
+
doc(writer, [`${name} returns the decoded result with the response, so the HTTP status and headers are available.`]);
|
|
142
|
+
writer.line(`func (r *RawClient) ${name}(${signature(operation).declaration}) (${result}, error) {`).indent((fn) => {
|
|
143
|
+
emitArgs(fn, ir, operation);
|
|
144
|
+
fn.line(`response, err := ${RUNTIME}.Call(ctx, r.runtime, ${specName(operation)}, args, opts...)`);
|
|
145
|
+
fn.line("if err != nil {").indent((branch) => branch.line(`return ${result}{Response: response}, err`)).line("}");
|
|
146
|
+
const kind = kindOf(operation.returns);
|
|
147
|
+
if (operation.events !== undefined) {
|
|
148
|
+
const decode = textEvents(operation.events) ? "nil" : decoder(ir, operation.events, `operations.${operation.name}.events`);
|
|
149
|
+
fn.line(`return ${result}{Data: ${RUNTIME}.NewEventStream[${goType(operation.events)}](response, ${decode}), Response: response}, nil`);
|
|
150
|
+
} else if (kind === undefined) fn.line(`return ${result}{Response: response}, nil`);
|
|
151
|
+
else if (kind === "bytes") fn.line(`return ${result}{Data: response.Body, Response: response}, nil`);
|
|
152
|
+
else if (kind === "stream") fn.line(`return ${result}{Data: bytes.NewReader(response.Body), Response: response}, nil`);
|
|
153
|
+
else {
|
|
154
|
+
fn.line("value, err := response.JSON()");
|
|
155
|
+
fn.line("if err != nil {").indent((branch) => branch.line(`return ${result}{Response: response}, err`)).line("}");
|
|
156
|
+
fn.line(`data, err := ${decoder(ir, operation.returns , `operations.${operation.name}.returns`)}(value, "$")`);
|
|
157
|
+
fn.line("if err != nil {").indent((branch) => branch.line(`return ${result}{Response: response}, err`)).line("}");
|
|
158
|
+
fn.line(`return ${result}{Data: data, Response: response}, nil`);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
writer.line("}").line();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function emitMethod(writer , operation ) {
|
|
165
|
+
const name = exported(operation.name);
|
|
166
|
+
const { declaration, call } = signature(operation);
|
|
167
|
+
doc(writer, operationDoc(operation));
|
|
168
|
+
if (operation.events !== undefined) {
|
|
169
|
+
const type = resultType(operation);
|
|
170
|
+
writer.line(`func (c *Client) ${name}(${declaration}) ${type} {`).indent((fn) => {
|
|
171
|
+
fn.line(`return ${RUNTIME}.LazyEvents(func() (${type}, error) {`).indent((inner) => {
|
|
172
|
+
inner.line(`result, err := c.Raw().${name}(${call})`);
|
|
173
|
+
inner.line("return result.Data, err");
|
|
174
|
+
});
|
|
175
|
+
fn.line("})");
|
|
176
|
+
});
|
|
177
|
+
} else if (kindOf(operation.returns) === undefined) {
|
|
178
|
+
writer.line(`func (c *Client) ${name}(${declaration}) error {`).indent((fn) => {
|
|
179
|
+
fn.line(`_, err := c.Raw().${name}(${call})`);
|
|
180
|
+
fn.line("return err");
|
|
181
|
+
});
|
|
182
|
+
} else {
|
|
183
|
+
writer.line(`func (c *Client) ${name}(${declaration}) (${resultType(operation)}, error) {`).indent((fn) => {
|
|
184
|
+
fn.line(`result, err := c.Raw().${name}(${call})`);
|
|
185
|
+
fn.line("return result.Data, err");
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
writer.line("}").line();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** The Go statements that set `cursor` from a page field, by how the field may be absent or null. */
|
|
192
|
+
function readCursor(field , read ) {
|
|
193
|
+
if (field.optional && field.nullable) return [`if ${read}.Set && ${read}.Value != nil {`, `\tcursor = *${read}.Value`, "}"];
|
|
194
|
+
if (field.optional) return [`if ${read}.Set {`, `\tcursor = ${read}.Value`, "}"];
|
|
195
|
+
if (field.nullable) return [`if ${read} != nil {`, `\tcursor = *${read}`, "}"];
|
|
196
|
+
return [`cursor = ${read}`];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** <Name>All: a Pager over every item of every page, fetched as Next is called (kit block pagination.iterate). */
|
|
200
|
+
/** <Name>AndWait: starts the work, then polls until it is done (kit block lro.wait). */
|
|
201
|
+
function emitWait(writer , ir , operation , wait ) {
|
|
202
|
+
const name = exported(operation.name);
|
|
203
|
+
const type = resultType(operation);
|
|
204
|
+
const poll = ir.operations.find((candidate) => candidate.name === wait.poll);
|
|
205
|
+
if (poll === undefined) throw new Error(`operations.${operation.name}.wait: no operation ${wait.poll}`);
|
|
206
|
+
const { declaration, call } = signature(operation);
|
|
207
|
+
const pollArgs = ["ctx", `current.${exported(wait.from)}`, ...(named(poll).length > 0 ? [`${paramsType(poll)}{}`] : []), "opts..."];
|
|
208
|
+
doc(writer, [
|
|
209
|
+
`${name}AndWait calls ${name}, then ${exported(wait.poll)} every ${wait.intervalMs} ms until ${exported(wait.field)} is ${wait.done.join(" or ")}, and returns the finished result.`,
|
|
210
|
+
`It returns *${RUNTIME}.WaitTimeoutError when the work is not done within ${wait.timeoutMs} ms, and stops when ctx is done.`,
|
|
211
|
+
]);
|
|
212
|
+
writer.line(`func (c *Client) ${name}AndWait(${declaration}) (${type}, error) {`).indent((fn) => {
|
|
213
|
+
fn.line(`first, err := c.${name}(${call})`);
|
|
214
|
+
fn.line("if err != nil {").indent((branch) => branch.line("return first, err")).line("}");
|
|
215
|
+
fn.line(`done := map[string]bool{${wait.done.map((value) => `${str(value)}: true`).join(", ")}}`);
|
|
216
|
+
fn.line(`return ${RUNTIME}.WaitUntil(ctx, first, func(current ${type}) (${type}, error) {`).indent((inner) => inner.line(`return c.${exported(wait.poll)}(${pollArgs.join(", ")})`));
|
|
217
|
+
fn.line(`}, func(current ${type}) bool {`).indent((inner) => inner.line(`return done[string(current.${exported(wait.field)})]`));
|
|
218
|
+
fn.line(`}, c.runtime.Clock, ${wait.intervalMs}, ${wait.timeoutMs})`);
|
|
219
|
+
});
|
|
220
|
+
writer.line("}").line();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function emitIterator(writer , ir , operation , paginate ) {
|
|
224
|
+
const returns = operation.returns;
|
|
225
|
+
const page = returns?.kind === "ref" ? ir.types.find((named) => named.name === returns.name) : undefined;
|
|
226
|
+
const fields = page?.type.kind === "object" ? page.type.fields : [];
|
|
227
|
+
const { item: itemNode, field: itemsField } = pagedItems(ir, operation);
|
|
228
|
+
const name = exported(operation.name);
|
|
229
|
+
const item = goType(itemNode);
|
|
230
|
+
const pageType = resultType(operation);
|
|
231
|
+
const params = paramsType(operation);
|
|
232
|
+
const { declaration, call } = signature(operation);
|
|
233
|
+
const fetchCall = call.replace(/\bparams\b/, "current");
|
|
234
|
+
doc(writer, [`${name}All iterates over every item of every ${name} page, fetching pages as Next is called.`]);
|
|
235
|
+
if (paginate.kind === "link") {
|
|
236
|
+
// The Link header names the next page: the same operation is called at that URL.
|
|
237
|
+
const raw = `${RUNTIME}.Result[${pageType}]`;
|
|
238
|
+
writer.line(`func (c *Client) ${name}All(${declaration}) *${RUNTIME}.Pager[${item}] {`).indent((fn) => {
|
|
239
|
+
fn.line(`return ${RUNTIME}.NewPager(ctx, "", func(ctx context.Context, url string) (${raw}, error) {`).indent((inner) => {
|
|
240
|
+
inner.line("options := opts");
|
|
241
|
+
inner.line('if url != "" {').indent((branch) => branch.line(`options = append(append([]${RUNTIME}.CallOption{}, opts...), ${RUNTIME}.WithURL(url))`)).line("}");
|
|
242
|
+
inner.line(`return c.Raw().${name}(${call.replace("opts...", "options...")})`);
|
|
243
|
+
});
|
|
244
|
+
fn.line(`}, func(page ${raw}) []${item} {`).indent((inner) => inner.line(`return page.Data${itemsField === undefined ? "" : `.${exported(itemsField)}`}`));
|
|
245
|
+
fn.line(`}, func(url string, page ${raw}, items []${item}) (string, bool) {`).indent((inner) => {
|
|
246
|
+
inner.line(`next := ${RUNTIME}.NextLink(page.Response.Header("link"))`);
|
|
247
|
+
inner.line('return next, next != ""');
|
|
248
|
+
});
|
|
249
|
+
fn.line("})");
|
|
250
|
+
});
|
|
251
|
+
writer.line("}").line();
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
writer.line(`func (c *Client) ${name}All(${declaration}) *${RUNTIME}.Pager[${item}] {`).indent((fn) => {
|
|
255
|
+
fn.line(`return ${RUNTIME}.NewPager(ctx, params, func(ctx context.Context, current ${params}) (${pageType}, error) {`).indent((inner) => inner.line(`return c.${name}(${fetchCall})`));
|
|
256
|
+
fn.line(`}, func(page ${pageType}) []${item} {`).indent((inner) => inner.line(`return page.${exported(itemsField ?? "")}`));
|
|
257
|
+
fn.line(`}, func(current ${params}, page ${pageType}, items []${item}) (${params}, bool) {`).indent((inner) => {
|
|
258
|
+
const param = `current.${exported(paginate.param)}`;
|
|
259
|
+
if (paginate.kind === "cursor") {
|
|
260
|
+
const next = fields.find((entry) => entry.name === paginate.next);
|
|
261
|
+
if (next === undefined) throw new Error(`operations.${operation.name}.paginate: no field ${paginate.next}`);
|
|
262
|
+
inner.line('cursor := ""');
|
|
263
|
+
readCursor(next, `page.${exported(paginate.next)}`).forEach((line) => inner.line(line.replace(/^\t/, " ").replace(/^ /, "\t")));
|
|
264
|
+
inner.line(`${param} = ${RUNTIME}.Some(cursor)`);
|
|
265
|
+
inner.line('return current, cursor != ""');
|
|
266
|
+
} else {
|
|
267
|
+
const limit = paginate.limit === undefined ? "" : ` && !(current.${exported(paginate.limit)}.Set && int64(len(items)) < current.${exported(paginate.limit)}.Value)`;
|
|
268
|
+
inner.line(`${param} = ${RUNTIME}.Some(${param}.Or(0) + int64(len(items)))`);
|
|
269
|
+
inner.line(`return current, len(items) > 0${limit}`);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
fn.line("})");
|
|
273
|
+
});
|
|
274
|
+
writer.line("}").line();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function configValue(ir , name ) {
|
|
278
|
+
const middleware = ir.middlewares.find((candidate) => candidate.name === name);
|
|
279
|
+
if (middleware === undefined) throw new Error(`unknown middleware ${name}`);
|
|
280
|
+
const config = middleware.config.map((item) => `${str(item.name)}: ${item.optional ? `${RUNTIME}.OptValue(options.${exported(item.name)})` : `options.${exported(item.name)}`}`);
|
|
281
|
+
return `${RUNTIME}.Entry(${str(name)}, ${goName(name)}, map[string]any{${config.join(", ")}})`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function emitClient(writer , ir , sdk ) {
|
|
285
|
+
const config = configFields(ir);
|
|
286
|
+
doc(writer, ["Options configure a Client. The zero value of an option means its default."]);
|
|
287
|
+
writer.line("type Options struct {").indent((body) => {
|
|
288
|
+
config.forEach((item) => {
|
|
289
|
+
doc(body, item.doc === undefined ? [] : [item.doc]);
|
|
290
|
+
body.line(`${exported(item.name)} ${fieldType({ type: item.type, optional: item.optional, nullable: false })}`);
|
|
291
|
+
});
|
|
292
|
+
doc(body, ["Server is a key of Servers; DefaultServer when empty."]);
|
|
293
|
+
body.line("Server string");
|
|
294
|
+
doc(body, ["BaseURL replaces the server's URL, e.g. for a proxy or a test server."]);
|
|
295
|
+
body.line("BaseURL string");
|
|
296
|
+
doc(body, ["Transport sends requests; net/http when nil."]);
|
|
297
|
+
body.line(`Transport ${RUNTIME}.Transport`);
|
|
298
|
+
doc(body, ["TimeoutMs limits each call; DefaultTimeoutMs when unset."]);
|
|
299
|
+
body.line(`TimeoutMs ${RUNTIME}.Opt[int64]`);
|
|
300
|
+
doc(body, ["Middleware runs after the SDK's own, in order."]);
|
|
301
|
+
body.line(`Middleware []${RUNTIME}.MiddlewareEntry`);
|
|
302
|
+
doc(body, ["OnEvent receives every event middleware emits."]);
|
|
303
|
+
body.line(`OnEvent ${RUNTIME}.EventListener`);
|
|
304
|
+
doc(body, ["Clock measures time and waits; the system clock when nil."]);
|
|
305
|
+
body.line(`Clock ${RUNTIME}.Clock`);
|
|
306
|
+
doc(body, ["Proxy routes requests through a proxy (default transport only); HTTP_PROXY and friends when empty."]);
|
|
307
|
+
body.line("Proxy string");
|
|
308
|
+
doc(body, ["TLSConfig configures TLS (default transport only)."]);
|
|
309
|
+
body.line("TLSConfig *tls.Config");
|
|
310
|
+
const versions = ir.apiVersions;
|
|
311
|
+
if (versions !== undefined) {
|
|
312
|
+
if (configFields(ir).some((item) => exported(item.name) === "APIVersion")) throw new Error("a middleware config field apiVersion collides with the APIVersion client option of apiVersions");
|
|
313
|
+
doc(body, [`APIVersion is the API version to use, sent as ${versions.header}: ${versions.values.join(", ")}. Defaults to ${versions.values.at(-1) ?? ""}, the newest.`]);
|
|
314
|
+
body.line("APIVersion string");
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
writer.line("}").line();
|
|
318
|
+
|
|
319
|
+
const opLevel = [...new Set(ir.operations.flatMap((operation) => operation.use))].sort();
|
|
320
|
+
doc(writer, [`Client ${ir.doc ?? ir.name}`.trim(), "", "Safe for concurrent use: build one and share it."]);
|
|
321
|
+
writer.line("type Client struct {").indent((body) => body.line(`runtime *${RUNTIME}.Runtime`)).line("}").line();
|
|
322
|
+
doc(writer, ["RawClient has the same operations, returning the response too."]);
|
|
323
|
+
writer.line("type RawClient struct {").indent((body) => body.line(`runtime *${RUNTIME}.Runtime`)).line("}").line();
|
|
324
|
+
doc(writer, ["NewClient builds a client; it fails for an unknown server or an invalid proxy URL."]);
|
|
325
|
+
writer.line("func NewClient(options Options) (*Client, error) {").indent((fn) => {
|
|
326
|
+
fn.line("server := options.Server");
|
|
327
|
+
fn.line("if server == \"\" {").indent((branch) => branch.line("server = DefaultServer")).line("}");
|
|
328
|
+
fn.line("baseURL := options.BaseURL");
|
|
329
|
+
fn.line("if baseURL == \"\" {").indent((branch) => {
|
|
330
|
+
branch.line("url, ok := Servers[server]");
|
|
331
|
+
branch.line("if !ok {").indent((inner) => inner.line('return nil, fmt.Errorf("unknown server %q", server)')).line("}");
|
|
332
|
+
branch.line("baseURL = url");
|
|
333
|
+
});
|
|
334
|
+
fn.line("}");
|
|
335
|
+
fn.line("transport := options.Transport");
|
|
336
|
+
fn.line("if transport == nil {").indent((branch) => {
|
|
337
|
+
branch.line(`created, err := ${RUNTIME}.NewHTTPTransport(options.Proxy, options.TLSConfig)`);
|
|
338
|
+
branch.line("if err != nil {").indent((inner) => inner.line("return nil, err")).line("}");
|
|
339
|
+
branch.line("transport = created");
|
|
340
|
+
});
|
|
341
|
+
fn.line("}");
|
|
342
|
+
fn.line("clock := options.Clock");
|
|
343
|
+
fn.line("if clock == nil {").indent((branch) => branch.line(`clock = ${RUNTIME}.SystemClock()`)).line("}");
|
|
344
|
+
fn.line("timeout := options.TimeoutMs");
|
|
345
|
+
fn.line("if !timeout.Set {").indent((branch) => branch.line(`timeout = ${RUNTIME}.Some[int64](DefaultTimeoutMs)`)).line("}");
|
|
346
|
+
fn.line(`var listeners []${RUNTIME}.EventListener`);
|
|
347
|
+
fn.line("if options.OnEvent != nil {").indent((branch) => branch.line("listeners = append(listeners, options.OnEvent)")).line("}");
|
|
348
|
+
if (ir.apiVersions !== undefined) {
|
|
349
|
+
fn.line("apiVersion := options.APIVersion");
|
|
350
|
+
fn.line('if apiVersion == "" {').indent((branch) => branch.line(`apiVersion = ${str(ir.apiVersions?.values.at(-1) ?? "")}`)).line("}");
|
|
351
|
+
}
|
|
352
|
+
fn.line(`runtime := &${RUNTIME}.Runtime{`).indent((fields) => {
|
|
353
|
+
fields.line("BaseURL: baseURL,");
|
|
354
|
+
fields.line("Transport: transport,");
|
|
355
|
+
fields.line("Clock: clock,");
|
|
356
|
+
fields.line(`State: ${RUNTIME}.NewState(clock),`);
|
|
357
|
+
fields.line(`Middleware: []${RUNTIME}.MiddlewareEntry{${ir.use.map((name) => configValue(ir, name)).join(", ")}},`);
|
|
358
|
+
fields.line(`Registry: map[string]${RUNTIME}.MiddlewareEntry{${opLevel.map((name) => `${str(name)}: ${configValue(ir, name)}`).join(", ")}},`);
|
|
359
|
+
fields.line("UserMiddleware: options.Middleware,");
|
|
360
|
+
fields.line("Listeners: listeners,");
|
|
361
|
+
fields.line("TimeoutMs: timeout,");
|
|
362
|
+
fields.line(`SDK: ${RUNTIME}.SdkInfo{Name: ${str(sdk.name)}, Version: ${str(sdk.version)}, Language: "go"},`);
|
|
363
|
+
const versions = ir.apiVersions;
|
|
364
|
+
if (versions !== undefined) fields.line(`APIVersion: &${RUNTIME}.APIVersion{Header: ${str(versions.header)}, Value: apiVersion, Values: []string{${versions.values.map(str).join(", ")}}},`);
|
|
365
|
+
});
|
|
366
|
+
fn.line("}");
|
|
367
|
+
fn.line("return &Client{runtime: runtime}, nil");
|
|
368
|
+
});
|
|
369
|
+
writer.line("}").line();
|
|
370
|
+
doc(writer, ["Raw gives the operations that also return the response."]);
|
|
371
|
+
writer.line("func (c *Client) Raw() *RawClient {").indent((fn) => fn.line("return &RawClient{runtime: c.runtime}")).line("}").line();
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Method names a Client already has; an operation with one of them would not compile. */
|
|
375
|
+
const TAKEN = ["Raw"];
|
|
376
|
+
|
|
377
|
+
export function emitClientFile(ir , header , pkg , module , sdk ) {
|
|
378
|
+
ir.operations.forEach((operation) => {
|
|
379
|
+
if (TAKEN.includes(exported(operation.name))) throw new Error(`operation ${operation.name} would clash with the client's ${exported(operation.name)} method; rename the operation`);
|
|
380
|
+
});
|
|
381
|
+
const body = go();
|
|
382
|
+
doc(body, ["Servers are the API's servers by name."]);
|
|
383
|
+
body.line("var Servers = map[string]string{").indent((servers) => ir.servers.forEach((server) => servers.line(`${str(server.name)}: ${str(server.url)},`))).line("}").line();
|
|
384
|
+
doc(body, ["DefaultServer is used when Options.Server is empty."]);
|
|
385
|
+
body.line(`const DefaultServer = ${str(ir.defaultServer)}`).line();
|
|
386
|
+
doc(body, ["DefaultTimeoutMs limits a call when Options.TimeoutMs is unset."]);
|
|
387
|
+
body.line("const DefaultTimeoutMs = 60000").line();
|
|
388
|
+
ir.operations.forEach((operation) => emitParams(body, operation));
|
|
389
|
+
ir.operations.forEach((operation) => emitSpec(body, ir, operation));
|
|
390
|
+
emitClient(body, ir, sdk);
|
|
391
|
+
ir.operations.forEach((operation) => {
|
|
392
|
+
emitMethod(body, operation);
|
|
393
|
+
if (operation.paginate !== undefined) emitIterator(body, ir, operation, operation.paginate);
|
|
394
|
+
if (operation.wait !== undefined) emitWait(body, ir, operation, operation.wait);
|
|
395
|
+
});
|
|
396
|
+
ir.operations.forEach((operation) => emitRaw(body, ir, operation));
|
|
397
|
+
const text = body.toString();
|
|
398
|
+
|
|
399
|
+
const imports = ["context", "crypto/tls", "fmt", ...(/\bbytes\./.test(text) ? ["bytes"] : []), ...(/\bio\./.test(text) ? ["io"] : []), `${module}/${RUNTIME}`].sort();
|
|
400
|
+
const writer = go().line(header).line(`package ${pkg}`).line().line("import (");
|
|
401
|
+
writer.indent((block) => imports.forEach((path) => block.line(str(path))));
|
|
402
|
+
writer.line(")").line();
|
|
403
|
+
writer.line("var _ = context.Background").line();
|
|
404
|
+
return `${writer.toString()}${text}`;
|
|
405
|
+
}
|