$npx -y skills add Amplicode/spring-skills --skill connekt-script-writerWrite .connekt.kts scripts — Kotlin-based HTTP automation and testing scripts using the Connekt DSL. Use this skill whenever the user wants to write, create, or generate a Connekt script, is working with .connekt.kts files, describes an HTTP workflow to automate, wants to tes
| 1 | # Connekt Script Writer |
| 2 | |
| 3 | Connekt is an HTTP client driven by Kotlin scripts. Scripts use the `.connekt.kts` extension and have the full Connekt DSL available at the top level — no boilerplate, no main function, just declarations and requests. |
| 4 | |
| 5 | When generating scripts, always read from `connekt.env.json` for base URLs and secrets using `val x: String by env` rather than hardcoding values. Save scripts with the `.connekt.kts` extension. |
| 6 | |
| 7 | ## Execution Model |
| 8 | |
| 9 | **A script only registers requests — the runner decides which request to execute.** The script is NOT run top-to-bottom like a regular program. This means: |
| 10 | |
| 11 | - **No imperative code between requests.** No `println`, no `if/else`, no variable assignments outside of `then` or `useCase` blocks. |
| 12 | - **Assertions are only allowed inside `then { }` blocks or `useCase { }` blocks** — never at the top level between requests. |
| 13 | - **Never interpolate results from other requests directly into a URL.** Use `pathParam` instead, so the runner can resolve values at execution time. |
| 14 | - **Allowed at top level:** `val x by env`, `data class`, `configureClient`, `val x by oauth(...)`, extension functions (e.g. on `RequestBuilder` for shared headers), request declarations (`val x by GET/POST/...`), and `useCase` blocks. |
| 15 | |
| 16 | ```kotlin |
| 17 | // ✅ CORRECT — pass result via pathParam |
| 18 | val petId by POST("$host/api/pets") { |
| 19 | contentType("application/json") |
| 20 | body("""{"name": "Fido"}""") |
| 21 | } then { |
| 22 | decode<Long>("$.id") |
| 23 | } |
| 24 | |
| 25 | GET("$host/api/pets/{petId}") { |
| 26 | pathParam("petId", petId) |
| 27 | } then { |
| 28 | assert(code == 200) |
| 29 | } |
| 30 | |
| 31 | // ❌ WRONG — never interpolate request results in URL |
| 32 | GET("$host/api/pets/$petId") then { |
| 33 | assert(code == 200) |
| 34 | } |
| 35 | ``` |
| 36 | |
| 37 | **Extension functions** on `RequestBuilder` are a good way to apply shared configuration: |
| 38 | |
| 39 | ```kotlin |
| 40 | fun RequestBuilder.withApiKey() { |
| 41 | header("X-Api-Key", apiKey) |
| 42 | header("X-Request-Id", java.util.UUID.randomUUID().toString()) |
| 43 | } |
| 44 | |
| 45 | GET("$host/api/pets") { |
| 46 | withApiKey() |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | ## Script Structure |
| 51 | |
| 52 | A `.connekt.kts` file follows this conventional ordering: |
| 53 | |
| 54 | ```kotlin |
| 55 | // 1. File-level annotations (imports, dependencies) |
| 56 | @file:Import("shared_auth.connekt.kts") |
| 57 | @file:DependsOn("com.example:my-lib:1.0") |
| 58 | |
| 59 | // 2. Third-party imports (if needed) |
| 60 | import org.assertj.core.api.Assertions.assertThat |
| 61 | |
| 62 | // 3. Environment variables from connekt.env.json |
| 63 | val host: String by env |
| 64 | val apiKey: String by env |
| 65 | |
| 66 | // 4. Data classes for typed deserialization |
| 67 | data class Pet(val id: Long, val name: String, val status: String) |
| 68 | |
| 69 | // 5. Global client configuration |
| 70 | configureClient { |
| 71 | insecure() // for local dev only |
| 72 | } |
| 73 | |
| 74 | // 6. OAuth setup (if needed) |
| 75 | val auth by oauth( |
| 76 | authorizeEndpoint = "...", |
| 77 | clientId = "...", |
| 78 | clientSecret = "...", |
| 79 | scope = "openid", |
| 80 | tokenEndpoint = "...", |
| 81 | redirectUri = "http://localhost:8080/callback" |
| 82 | ) |
| 83 | |
| 84 | // 7. HTTP requests |
| 85 | val pets by GET("$host/api/pets") then { |
| 86 | decode<List<Pet>>("$.content") |
| 87 | } |
| 88 | |
| 89 | // 8. Use cases for grouping related requests |
| 90 | val result by useCase("Create and verify") { |
| 91 | val created by POST("$host/api/pets") { |
| 92 | contentType("application/json") |
| 93 | body("""{"name": "Fido"}""") |
| 94 | } then { |
| 95 | decode<Pet>() |
| 96 | } |
| 97 | created |
| 98 | } |
| 99 | ``` |
| 100 | |
| 101 | Key conventions: |
| 102 | - `val x by env` reads from `connekt.env.json` — the property name is the lookup key |
| 103 | - `val response by GET(...)` delegates execution and binds the result |
| 104 | - `then { ... }` chains response handling — inside the block, `this` is the OkHttp `Response` |
| 105 | - `val result by GET(...) then { expr }` captures the `then` block's return value |
| 106 | - `decode<T>(jsonPath)` deserializes JSON from the response body |
| 107 | - `useCase("name") { ... }` groups requests; the last expression is the return value |
| 108 | - `assertSoftly { assert(...) }` collects all assertion failures before reporting |
| 109 | - **No code between requests** — assertions and logic go inside `then` or `useCase` only |
| 110 | - **Pass request results via `pathParam`**, never interpolate them into the URL string |
| 111 | |
| 112 | ## DSL Reference |
| 113 | |
| 114 | ### HTTP Methods |
| 115 | |
| 116 | ```kotlin |
| 117 | GET("$host/api/resource") |
| 118 | POST("$host/api/resource") |
| 119 | PUT("$host/api/resource") |
| 120 | PATCH("$host/api/resource") |
| 121 | DELETE("$host/api/resource") |
| 122 | HEAD("$host/api/resource") |
| 123 | OPTIONS("$host/api/resource") |
| 124 | TRACE("$host/api/resource") |
| 125 | ``` |
| 126 | |
| 127 | Each method accepts an optional `name` parameter and a configuration lambda: |
| 128 | |
| 129 | ```kotlin |
| 130 | GET("$host/api/pets", name = "List all pets") { |
| 131 | header("Accept", "application/json") |
| 132 | quer |