Better Fetch

Plugins

Plugins are functions that can be used to modify the request, response, error and other parts of the request lifecycle and can be used to define Fetch Schema.

Init

The init function is called before the request is made and any of the internal functions are called. It takes the url and options as arguments and is expected to return the modified url and options.

fetch.ts
import { ,  } from "@better-fetch/fetch";
 
const  = {
    : "my-plugin",
    : "My Plugin",
    : async (, ) => {
       if(.("http://")) {
           const  = new ()
           const  = "http://localhost:3000"
           return {
               : `${}/${.}`,
               ,
           }
       }
        return {
            ,
            ,
        }
    },
} satisfies ;
 
const  = ({
    : "https://jsonplaceholder.typicode.com",
    : [],
});

Hooks

Hooks are functions that are called at different stages of the request lifecycle. See Hooks for more information.

fetch.ts
import { ,  } from "@better-fetch/fetch";
 
const  = {
    : "my-plugin",
    : "My Plugin",
    : {
        () {
        // do something with the context
        return ;
        },
        () {
            // do something with the context
            return ;
        },
        () {
            // do something with the context
        },
        () {
            // do something with the context
        },
    }
} satisfies ;
 
const  = ({
    : "https://jsonplaceholder.typicode.com",
    : [],
});

If more than one plugin is registered, the hooks will be called in the order they are registered.

Schema

You can define a schema for a plugin. This allows you to easily document the api usage using a schema.

fetch.ts
import { , ,  } from "@better-fetch/fetch";
import {  } from "zod";
const  = {
    : "my-plugin",
    : "My Plugin",
    : ({
            "/path": {
                : .({
                    /**
                     * You can write descriptions for the properties. Hover over the property to see 
                     * the description.
                     */
                    : .(),
                    /**
                     * The id property is required
                     */
                    : .(),
                }),
                : .({
                    : .(),
                    : .(),
                }),
            }
        },{
            : "https://jsonplaceholder.typicode.com",
        })
} satisfies ;
 
const  = ({    
    : "localhost:3000"
})
 
const { ,  } = await ("https://jsonplaceholder.typicode.com/path", {
    : {
        : "1",
        : 1,
        : "title",
        : true,
    },
});
 
 
baseURL is inferred to "https://jsonplaceholder.typicode.com"

You can also pass a prefix to the createSchema function to prefix all the routes.

Get options

The getOptions function allows you to define additional options that can be passed to the fetch function. This is useful when you want to pass options to the plugins that are not part of the BetterFetchPlugin interface.

fetch.ts
import { , ,  } from "@better-fetch/fetch";
import {  } from "zod";
 
const  = {
    : "my-plugin",
    : "My Plugin",
    () {
        return .({
            : .().(.({
                : .(),
                : .(),
            })),
        });
    },
} satisfies ;
 
const  = ({
    : "https://jsonplaceholder.typicode.com",
    : [],
});
 
const { ,  } = await ("https://jsonplaceholder.typicode.com/path", {
    ({
        ,
        ,
    }) {
        .(`Uploaded ${} of ${} bytes`);
    },
});

Properties

PropTypeDefault
id
string
-
name
string
-
description
string
-
version
string
-
hooks
FetchHooks
-
init
(url: string, options?: { cache?: RequestCache | undefined; credentials?: RequestCredentials | undefined; headers?: (HeadersInit & (HeadersInit | CommonHeaders)) | undefined; ... 32 more ...; disableValidation?: boolean | undefined; } | undefined) => Promise<...> | { ...; }
-
schema
Schema
-
getOptions
() => ZodType<any, ZodTypeDef, any>
-

On this page