|
| 1 | +// CHANGE: Example script demonstrating createClient API usage |
| 2 | +// WHY: Verify simplified API works as requested by reviewer |
| 3 | +// QUOTE(ТЗ): "напиши для меня такой тестовый скрипт и проверь как оно работает" |
| 4 | +// REF: PR#3 comment from skulidropek |
| 5 | +// SOURCE: n/a |
| 6 | +// PURITY: SHELL |
| 7 | +// EFFECT: Demonstrates Effect-based API calls |
| 8 | + |
| 9 | +import * as HttpClient from "@effect/platform/HttpClient" |
| 10 | +import { Console, Effect, Layer } from "effect" |
| 11 | +import createClient from "../src/index.js" |
| 12 | +import { dispatcherlistPets, dispatchergetPet, dispatchercreatePet } from "../src/generated/dispatch.js" |
| 13 | +import type { paths } from "../tests/fixtures/petstore.openapi.js" |
| 14 | + |
| 15 | +/** |
| 16 | + * Example: Create API client with simplified API |
| 17 | + * |
| 18 | + * This demonstrates the ergonomic createClient API that matches |
| 19 | + * the interface requested by the reviewer. |
| 20 | + */ |
| 21 | +const apiClient = createClient<paths>({ |
| 22 | + baseUrl: "https://petstore.example.com", |
| 23 | + credentials: "include" |
| 24 | +}) |
| 25 | + |
| 26 | +/** |
| 27 | + * Example program: List all pets |
| 28 | + * |
| 29 | + * @pure false - performs HTTP request |
| 30 | + * @effect Effect<void, never, never> |
| 31 | + */ |
| 32 | +const listAllPetsExample = Effect.gen(function*() { |
| 33 | + yield* Console.log("=== Example 1: List all pets ===") |
| 34 | + |
| 35 | + // Execute request using the simplified API |
| 36 | + const result = yield* apiClient.GET( |
| 37 | + "/pets", |
| 38 | + dispatcherlistPets, |
| 39 | + { |
| 40 | + query: { limit: 10 } |
| 41 | + } |
| 42 | + ) |
| 43 | + |
| 44 | + // Pattern match on the response |
| 45 | + if (result.status === 200) { |
| 46 | + yield* Console.log(`✓ Success: Got ${result.body.length} pets`) |
| 47 | + yield* Console.log(` First pet: ${JSON.stringify(result.body[0], null, 2)}`) |
| 48 | + } else if (result.status === 500) { |
| 49 | + yield* Console.log(`✗ Server error: ${result.body.message}`) |
| 50 | + } |
| 51 | +}) |
| 52 | + |
| 53 | +/** |
| 54 | + * Example program: Get specific pet |
| 55 | + * |
| 56 | + * @pure false - performs HTTP request |
| 57 | + * @effect Effect<void, never, never> |
| 58 | + */ |
| 59 | +const getPetExample = Effect.gen(function*() { |
| 60 | + yield* Console.log("\n=== Example 2: Get specific pet ===") |
| 61 | + |
| 62 | + const result = yield* apiClient.GET( |
| 63 | + "/pets/{petId}", |
| 64 | + dispatchergetPet, |
| 65 | + { |
| 66 | + params: { petId: "123" } |
| 67 | + } |
| 68 | + ) |
| 69 | + |
| 70 | + if (result.status === 200) { |
| 71 | + yield* Console.log(`✓ Success: Got pet "${result.body.name}"`) |
| 72 | + yield* Console.log(` Tag: ${result.body.tag ?? "none"}`) |
| 73 | + } else if (result.status === 404) { |
| 74 | + yield* Console.log(`✗ Not found: ${result.body.message}`) |
| 75 | + } else if (result.status === 500) { |
| 76 | + yield* Console.log(`✗ Server error: ${result.body.message}`) |
| 77 | + } |
| 78 | +}) |
| 79 | + |
| 80 | +/** |
| 81 | + * Example program: Create new pet |
| 82 | + * |
| 83 | + * @pure false - performs HTTP request |
| 84 | + * @effect Effect<void, never, never> |
| 85 | + */ |
| 86 | +const createPetExample = Effect.gen(function*() { |
| 87 | + yield* Console.log("\n=== Example 3: Create new pet ===") |
| 88 | + |
| 89 | + const newPet = { |
| 90 | + name: "Fluffy", |
| 91 | + tag: "cat" |
| 92 | + } |
| 93 | + |
| 94 | + const result = yield* apiClient.POST( |
| 95 | + "/pets", |
| 96 | + dispatchercreatePet, |
| 97 | + { |
| 98 | + body: JSON.stringify(newPet), |
| 99 | + headers: { "Content-Type": "application/json" } |
| 100 | + } |
| 101 | + ) |
| 102 | + |
| 103 | + if (result.status === 201) { |
| 104 | + yield* Console.log(`✓ Success: Created pet with ID ${result.body.id}`) |
| 105 | + yield* Console.log(` Name: ${result.body.name}`) |
| 106 | + } else if (result.status === 400) { |
| 107 | + yield* Console.log(`✗ Validation error: ${result.body.message}`) |
| 108 | + } else if (result.status === 500) { |
| 109 | + yield* Console.log(`✗ Server error: ${result.body.message}`) |
| 110 | + } |
| 111 | +}) |
| 112 | + |
| 113 | +/** |
| 114 | + * Example program: Handle transport error |
| 115 | + * |
| 116 | + * @pure false - performs HTTP request |
| 117 | + * @effect Effect<void, never, never> |
| 118 | + */ |
| 119 | +const errorHandlingExample = Effect.gen(function*() { |
| 120 | + yield* Console.log("\n=== Example 4: Error handling ===") |
| 121 | + |
| 122 | + // Create client with invalid URL to trigger transport error |
| 123 | + const invalidClient = createClient<paths>({ |
| 124 | + baseUrl: "http://invalid.localhost:99999", |
| 125 | + credentials: "include" |
| 126 | + }) |
| 127 | + |
| 128 | + const result = yield* Effect.either( |
| 129 | + invalidClient.GET("/pets", dispatcherlistPets) |
| 130 | + ) |
| 131 | + |
| 132 | + if (result._tag === "Left") { |
| 133 | + const error = result.left |
| 134 | + if (error._tag === "TransportError") { |
| 135 | + yield* Console.log(`✓ Transport error caught: ${error.message}`) |
| 136 | + } else if (error._tag === "UnexpectedStatus") { |
| 137 | + yield* Console.log(`✓ Unexpected status: ${error.status}`) |
| 138 | + } else if (error._tag === "ParseError") { |
| 139 | + yield* Console.log(`✓ Parse error: ${error.message}`) |
| 140 | + } else { |
| 141 | + yield* Console.log(`✓ Other error: ${error._tag}`) |
| 142 | + } |
| 143 | + } else { |
| 144 | + yield* Console.log("✗ Expected error but got success") |
| 145 | + } |
| 146 | +}) |
| 147 | + |
| 148 | +/** |
| 149 | + * Main program - runs all examples |
| 150 | + * |
| 151 | + * @pure false - performs HTTP requests |
| 152 | + * @effect Effect<void, never, never> |
| 153 | + */ |
| 154 | +const mainProgram = Effect.gen(function*() { |
| 155 | + yield* Console.log("╔════════════════════════════════════════════════════╗") |
| 156 | + yield* Console.log("║ OpenAPI Effect Client - createClient() Examples ║") |
| 157 | + yield* Console.log("╚════════════════════════════════════════════════════╝\n") |
| 158 | + |
| 159 | + yield* Console.log("Demonstrating simplified API:") |
| 160 | + yield* Console.log(' import createClient from "openapi-effect"') |
| 161 | + yield* Console.log(" const client = createClient<paths>({ ... })") |
| 162 | + yield* Console.log(" client.GET(\"/path\", dispatcher, options)\n") |
| 163 | + |
| 164 | + // Note: These examples will fail with transport errors since |
| 165 | + // we're not connecting to a real server. This is intentional |
| 166 | + // to demonstrate error handling. |
| 167 | + |
| 168 | + yield* Effect.catchAll(listAllPetsExample, (error) => |
| 169 | + Console.log(`Transport error (expected): ${error._tag === "TransportError" ? "Cannot connect to example server" : error._tag}`) |
| 170 | + ) |
| 171 | + |
| 172 | + yield* Effect.catchAll(getPetExample, (error) => |
| 173 | + Console.log(`Transport error (expected): ${error._tag === "TransportError" ? "Cannot connect to example server" : error._tag}`) |
| 174 | + ) |
| 175 | + |
| 176 | + yield* Effect.catchAll(createPetExample, (error) => |
| 177 | + Console.log(`Transport error (expected): ${error._tag === "TransportError" ? "Cannot connect to example server" : error._tag}`) |
| 178 | + ) |
| 179 | + |
| 180 | + yield* errorHandlingExample |
| 181 | + |
| 182 | + yield* Console.log("\n✓ All examples completed!") |
| 183 | + yield* Console.log("\nType safety verification:") |
| 184 | + yield* Console.log(" - All paths are type-checked against OpenAPI schema") |
| 185 | + yield* Console.log(" - Path parameters validated at compile time") |
| 186 | + yield* Console.log(" - Query parameters type-safe") |
| 187 | + yield* Console.log(" - Response bodies fully typed") |
| 188 | + yield* Console.log(" - All errors explicit in Effect type") |
| 189 | +}) |
| 190 | + |
| 191 | +/** |
| 192 | + * Execute the program with HttpClient layer |
| 193 | + */ |
| 194 | +const program = mainProgram.pipe( |
| 195 | + Effect.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.fetchOk)) |
| 196 | +) |
| 197 | + |
| 198 | +Effect.runPromise(program).catch((error) => { |
| 199 | + console.error("Unexpected error:", error) |
| 200 | + process.exit(1) |
| 201 | +}) |
0 commit comments