Better Fetch

Getting Started

Installation

npm i @better-fetch/fetch

You also need to have zod installed for schema validations. If you don't plan to use zod for schema validation, you can install it as a dev dependency.

npm i zod

Quick Start

The fastest way to start using better fetch is to import the betterFetch function and start making requests.

You can define the response type using generics or zod schema (recommended).

fetch.ts
import {  } from '@better-fetch/fetch';
import {  } from 'zod';
 
 
//Using generic type
const { ,  } = await <{
    : string;
    : number;
    : string;
    : boolean;
}>("https://jsonplaceholder.typicode.com/todos/1");
 
 
//Using zod schema
const { : , :  } = await ("https://jsonplaceholder.typicodei.com/todos/1", {
    : .({
        : .(),
        : .(),
        : .(),
        : .(),
    })  
});
 
 
Hover over the data object to see the type

Make sure strict mode is enabled in your tsconfig when using zod schema validations.

Better fetch by default returns a Promise that resolves to an object of data and error but if you pass the throw option, it will return the parsed response data only.

Create Fetch

Create Fetch allows you to create a better fetch instance with custom configurations.

fetch.ts
import {  } from "@better-fetch/fetch";
 
export const  = ({
  : "https://jsonplaceholder.typicode.com",
  : {
      : "linear",
      : 3,
      : 1000 
  }
});
 
const { ,  } = await <{
  : number;
  : number;
  : string;
  : boolean;
}>("/todos/1");

You can pass more options see the Fetch Options section for more details.

Throwing Errors

You can throw errors instead of returning them by passing the throw option.

If you pass the throw option, the betterFetch function will throw an error. And instead of returning data and error object it'll only the response data as it is.

fetch.ts
import {  } from '@better-fetch/fetch';
import {  } from 'zod';
 
const  = ({
    : "https://jsonplaceholder.typicode.com",
    : true,
});
 
const  = await <{
    : number;
}>("https://jsonplaceholder.typicode.com/todos/1");

Learn more about handling errors Handling Errors section.

Fetch Schema

Fetch schema enables you to pre-define the URL path and the shape of request and response data. This makes it easy to document your API.

Plugins can also define fetch schemas. See Plugins section for more details.

The output of the scheam will be validated using zod and if the validation fails, it'll throw an error.

fetch.ts
import { ,  } from "@better-fetch/fetch";
import {  } from "zod";
 
export const  = ({ 
    "/path": { 
        : .({ 
            : .(), 
            : .(), 
            : .(), 
            : .(), 
        }), 
        : .({ 
            : .(), 
            : .(), 
            : .(), 
            : .(), 
        }), 
    } 
}) 
 
const  = ({
    : "https://jsonplaceholder.typicode.com",
    :  
});
 
const { ,  } = await ("/path", {
    : {
        : "1",
        : 1,
        : "title",
        : true,
    },
});

Learn more about fetch schema Fetch Schema section.

On this page