Quickstart Guide
Welcome to the feTS Client, a fully type-safe HTTP Client that leverages the OpenAPI specification. It automatically infers types from the document, providing you with a type-safe interface for seamless API interaction.
Installation
To install the feTS Client, use the following command:
npm i fets
Usage with Existing REST API
Before diving in, make sure that you have an OpenAPI document, which is a language-agnostic specification for HTTP APIs. You can usually retrieve it from your server, or manually create it if you’re familiar with the OpenAPI schema. Check out the OpenAPI docs to learn more.
Start by creating a TypeScript file that exports your OpenAPI document. Due to certain limitations
in TypeScript, importing types directly from JSON files isn’t currently supported
(see this issue). To work around this,
simply copy and paste the content of your OpenAPI file into the TypeScript file and then export it
with the as const
modifier.
export default { openapi: '3.0.0' /* ... */ } as const
Next, create a client instance by passing the OpenAPI document to the createClient
function.
import { createClient, type NormalizeOAS } from 'fets'
import type openapi from './openapi'
const client = createClient<NormalizeOAS<typeof openapi>>({})
const response = await client['/pets'].get()
const pets = await response.json()
console.log(pets)
If you get This node exceeds the maximum length error from TypeScript
, you need to add
disableSizeLimit: true
to compilerOptions
in your tsconfig.json
file.
Usage with feTS Server
If you’re using feTS Server, and you’re sharing the types of your router instance, you can infer types directly from there.
import { createClient } from 'fets'
// Remember to add `type` to import 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)
This quick start guide should provide you with a solid foundation for using feTS Client. Enjoy exploring its full potential!