Error when building static site from Strapi

The fetch code is.

interface Props {
    endpoint: string;
    query?: string;
    wrappedByKey?: string;
    wrappedByList?: boolean;
}

/**
 * Fetches data from the Strapi API
 * @param endpoint - The endpoint to fetch from
 * @param query - The query parameters to add to the URL
 * @param wrappedByKey - The key to unwrap the response from
 * @param wrappedByList - If the response is a list, unwrap it
 * @returns
 */
export default async function fetchApi<T>({
    endpoint,
    query,
    wrappedByKey,
    wrappedByList,
}: Props): Promise<T> {
    try {
        if (endpoint.startsWith('/')) {
            endpoint = endpoint.slice(1);
        }

        const url = new URL(`${import.meta.env.STRAPI_URL}/api/${endpoint}`);
        const queryString = query ? `?${query}` : '';
        const urlWithQuery = `${url.toString()}${queryString}`;
        const res = await fetch(urlWithQuery, {
            method: "GET",
            headers: {
                Authorization: `Bearer ${import.meta.env.STRAPI_KEY}`,
                "Content-Type": "application/json",
            },
        });
        if (!res.ok) {
            throw new Error(`HTTP error! Status: ${res.status}`);
        }

        let data = await res.json();
        if (wrappedByKey) {
            data = data[wrappedByKey];
        }

        if (wrappedByList) {
            data = data[0];
        }

        return data as T;
    } catch (error) {
        // Handle errors here
        console.error("Error fetching data:", error);
        throw error; // Rethrow the error
    }
}