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
Request
defined inrequests.ts
(GetUserByUsername
in the example) - Use open api client created in
Client.ts
(request
function)
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.request
is used to call the requestnew GetUserByUsername
defines the request parametersApi.getUserByUsername
is 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.