1- // CHANGE: Example script demonstrating createClient API usage
2- // WHY: Verify simplified API works as requested by reviewer
3- // QUOTE(TZ): "napishi dlya menya takoi testovyi skript i prover' kak ono rabotaet "
1+ // CHANGE: Example script demonstrating createClient API usage with automatic type inference
2+ // WHY: Verify simplified API works as requested by reviewer without explicit type annotations
3+ // QUOTE(TZ): "А почему он заставляет явно описать тип? apiClient.GET и так должен вернуть тип "
44// REF: PR#3 comment from skulidropek
55// SOURCE: n/a
66// PURITY: SHELL
7- // EFFECT: Demonstrates Effect-based API calls
7+ // EFFECT: Demonstrates Effect-based API calls with automatic type inference
88
99import * as FetchHttpClient from "@effect/platform/FetchHttpClient"
1010import { Console , Effect , Exit } from "effect"
1111import { createClient , type ClientOptions } from "../src/shell/api-client/create-client.js"
1212import { dispatchercreatePet , dispatchergetPet , dispatcherlistPets } from "../src/generated/dispatch.js"
13- import type { Operations , Paths } from "../tests/fixtures/petstore.openapi.js"
14- import type { ApiSuccess , ResponsesFor } from "../src/core/api-client/strict-types.js"
15-
16- // Type aliases for operation responses
17- type ListPetsResponses = ResponsesFor < Operations [ "listPets" ] >
18- type GetPetResponses = ResponsesFor < Operations [ "getPet" ] >
19- type CreatePetResponses = ResponsesFor < Operations [ "createPet" ] >
20-
21- // Success types for pattern matching
22- type ListPetsSuccess = ApiSuccess < ListPetsResponses >
23- type GetPetSuccess = ApiSuccess < GetPetResponses >
24- type CreatePetSuccess = ApiSuccess < CreatePetResponses >
13+ import type { Paths } from "../tests/fixtures/petstore.openapi.js"
2514
2615// Helper type for Error schema body
2716type ErrorBody = { readonly code : number ; readonly message : string }
@@ -49,22 +38,25 @@ const apiClient = createClient<Paths>(clientOptions)
4938/**
5039 * Example program: List all pets
5140 *
41+ * NOTE: Types are now automatically inferred from the dispatcher!
42+ * No explicit type annotation needed on the result variable.
43+ *
5244 * @pure false - performs HTTP request
53- * @effect Effect<void, ListPetsFailure, HttpClient>
5445 */
5546const listAllPetsExample = Effect . gen ( function * ( ) {
5647 yield * Console . log ( "=== Example 1: List all pets ===" )
5748
58- // Execute request using the simplified API
59- const result : ListPetsSuccess = yield * apiClient . GET (
49+ // Execute request - type is automatically inferred from dispatcherlistPets
50+ // No need for explicit type annotation!
51+ const result = yield * apiClient . GET (
6052 "/pets" ,
6153 dispatcherlistPets ,
6254 {
6355 query : { limit : 10 }
6456 }
6557 )
6658
67- // Pattern match on the response
59+ // Pattern match on the response - TypeScript knows the possible statuses
6860 if ( result . status === 200 ) {
6961 const pets = result . body as Array < { id : string ; name : string ; tag ?: string } >
7062 yield * Console . log ( `Success: Got ${ pets . length } pets` )
@@ -77,13 +69,15 @@ const listAllPetsExample = Effect.gen(function*() {
7769/**
7870 * Example program: Get specific pet
7971 *
72+ * Demonstrates path parameters with automatic type inference.
73+ *
8074 * @pure false - performs HTTP request
81- * @effect Effect<void, GetPetFailure, HttpClient>
8275 */
8376const getPetExample = Effect . gen ( function * ( ) {
8477 yield * Console . log ( "\n=== Example 2: Get specific pet ===" )
8578
86- const result : GetPetSuccess = yield * apiClient . GET (
79+ // Type is inferred from dispatchergetPet - no annotation needed!
80+ const result = yield * apiClient . GET (
8781 "/pets/{petId}" ,
8882 dispatchergetPet ,
8983 {
@@ -105,8 +99,9 @@ const getPetExample = Effect.gen(function*() {
10599/**
106100 * Example program: Create new pet
107101 *
102+ * Demonstrates POST requests with body.
103+ *
108104 * @pure false - performs HTTP request
109- * @effect Effect<void, CreatePetFailure, HttpClient>
110105 */
111106const createPetExample = Effect . gen ( function * ( ) {
112107 yield * Console . log ( "\n=== Example 3: Create new pet ===" )
@@ -116,7 +111,8 @@ const createPetExample = Effect.gen(function*() {
116111 tag : "cat"
117112 }
118113
119- const result : CreatePetSuccess = yield * apiClient . POST (
114+ // Type is inferred from dispatchercreatePet - no annotation needed!
115+ const result = yield * apiClient . POST (
120116 "/pets" ,
121117 dispatchercreatePet ,
122118 {
@@ -139,8 +135,9 @@ const createPetExample = Effect.gen(function*() {
139135/**
140136 * Example program: Handle transport error
141137 *
138+ * Demonstrates error handling with Effect.either.
139+ *
142140 * @pure false - performs HTTP request
143- * @effect Effect<void, never, HttpClient>
144141 */
145142const errorHandlingExample = Effect . gen ( function * ( ) {
146143 yield * Console . log ( "\n=== Example 4: Error handling ===" )
@@ -180,17 +177,17 @@ type ApiError = { readonly _tag: string }
180177 * Main program - runs all examples
181178 *
182179 * @pure false - performs HTTP requests
183- * @effect Effect<void, never, HttpClient>
184180 */
185181const mainProgram = Effect . gen ( function * ( ) {
186182 yield * Console . log ( "========================================" )
187183 yield * Console . log ( " OpenAPI Effect Client - Examples" )
188184 yield * Console . log ( "========================================\n" )
189185
190- yield * Console . log ( "Demonstrating simplified API:" )
186+ yield * Console . log ( "Demonstrating simplified API with automatic type inference :" )
191187 yield * Console . log ( ' import createClient from "openapi-effect"' )
192188 yield * Console . log ( " const client = createClient<Paths>({ ... })" )
193- yield * Console . log ( " client.GET(\"/path\", dispatcher, options)\n" )
189+ yield * Console . log ( " const result = yield* client.GET(\"/path\", dispatcher)" )
190+ yield * Console . log ( " // result is automatically typed!\n" )
194191
195192 // Note: These examples will fail with transport errors since
196193 // we're not connecting to a real server. This is intentional
@@ -212,10 +209,9 @@ const mainProgram = Effect.gen(function*() {
212209
213210 yield * Console . log ( "\nAll examples completed!" )
214211 yield * Console . log ( "\nType safety verification:" )
215- yield * Console . log ( " - All paths are type-checked against OpenAPI schema" )
216- yield * Console . log ( " - Path parameters validated at compile time" )
217- yield * Console . log ( " - Query parameters type-safe" )
218- yield * Console . log ( " - Response bodies fully typed" )
212+ yield * Console . log ( " - Response types automatically inferred from dispatcher" )
213+ yield * Console . log ( " - No explicit type annotations required" )
214+ yield * Console . log ( " - All paths type-checked against OpenAPI schema" )
219215 yield * Console . log ( " - All errors explicit in Effect type" )
220216} )
221217
0 commit comments