Better Fetch

Authorization

Authorization is a way that allows you to add authentication headers to the request. Currently, supports Bearer and Basic authorization.

Bearer

The bearer authorization is used to add a bearer token to the request. The token is added to the Authorization header.

fetch.ts
import {  } from "@better-fetch/fetch";
 
const  = ({
    : "http://localhost:3000",
    : {
        : "Bearer",
        : "my-token",
    },
})

You can also pass a function that returns a string.

fetch.ts
const  = ({
    : "http://localhost:3000",
    : {
        : "Bearer",
        : () => .(),
    },
})

The function will be called only once when the request is made. If it returns undefined, the header will not be added to the request.

Basic

The basic authorization is used to add a basic authentication to the request. The username and password are added to the Authorization header.

fetch.ts
import {  } from "@better-fetch/fetch";
 
const  = ({
    : "http://localhost:3000",
    : {
        : "Basic",
        : "my-username",
        : "my-password",
    },
})

On this page