Create an HTTP Client
io-ts-http
API specifications are not coupled to any particular HTTP client. api-ts
provides helper libraries that wrap your favorite HTTP client and use the TypeScript
type-checker to ensure type-safe communication with a server implementing an
io-ts-http
specification.
Create a type-safe HTTP Client from an API specification
As before, first edit your package.json
file to add our new dependencies
(highlighted):
And install them by running:
$ npm install
Next, create a new file client.ts
:
Notice the inferred type of response.body
.
Compile and run client.ts
(make sure your server is still running!):
$ npm run build$ node ./client.js
You will see output like
Response is: Hello, world!