You can generate a type safe client using openapi-fetch and effect.
The typescript client is generated from an openapi definition file (example inside schema.yaml). We use openapi-typescript to do this, you can see an example from package.json.
Each request is defined inside requests.ts using Request.TaggedClass:
path: path of the endpoint for the requestschema: the schema of the response (using@effect/schema)- Parameters of the request (path, query, body)
Inside Api.ts we define a function for each request:
- Use a
Requestdefined inrequests.ts(GetUserByUsernamein the example) - Use open api client created in
Client.ts(requestfunction)
config.ts contains the Config definition for baseUrl (environmental variables) that exports a Layer for the client.
main.ts is the entry point of the application:
Effect.requestis used to call the requestnew GetUserByUsernamedefines the request parametersApi.getUserByUsernameis the request resolver
Finally, we use Effect.provide to inject the required dependencies and run the program.
You can see and run the full example on stackblitz.com.