Quick start
feTS Client is a fully type safe HTTP Client that uses OpenAPI (opens in a new tab) specification. It automatically infers types from the document and gives you a type safe API to interact with your API.
Installation
Terminal
yarn add fets
Usage
First, you need to create a TypeScript file that exports your OpenAPI document. Due to the
limitations in TypeScript, we cannot import types from JSON files directly.
See this issue (opens in a new tab). Just copy and paste the
content of the OpenAPI file inside the TypeScript file, and export it with as const
modifier.
openapi.ts
export default { openapi: '3.0.0' /* ... */ } as const
Then, you can create a client instance by passing the OpenAPI document to the createClient
function.
client.ts
import { createClient, Mutable } from 'fets'
import openapi from './openapi'
const client = createClient<Mutable<typeof openapi>>()
const response = await client['/pets'].get()
const pets = await response.json()
console.log(pets)
Infer types from feTS Server
If you are using feTS Server and share the types of your router instance. You can infer types from there directly.
client.ts
import { createClient } from 'fets'
// Don't forget to add `type` to get the types only.
import type { router } from './router'
const client = createClient<typeof router>({
endpoint: 'http://localhost:3000'
})
const response = await client['/pets'].get()
const pets = await response.json()
console.log(pets)