import { alchemy } from "../alchemy.js";
import { safeFetch } from "../util/safe-fetch.js";
/**
 * Creates a VercelApi instance with automatic token validation
 *
 * @param options API options
 * @returns Promise resolving to a VercelApi instance
 */
export async function createVercelApi(options) {
    const { accessToken = alchemy.secret.env.VERCEL_ACCESS_TOKEN, baseUrl } = options;
    return new VercelApi({
        baseUrl,
        accessToken,
    });
}
/**
 * Vercel API client using raw fetch
 */
export class VercelApi {
    /** Base URL for API */
    baseUrl;
    /** API access token */
    accessToken;
    /**
     * Create a new API client
     * Use createVercelApi factory function instead of direct constructor
     * for automatic token validation.
     *
     * @param options API options
     */
    constructor(options) {
        this.baseUrl = options.baseUrl;
        this.accessToken = options.accessToken;
    }
    /**
     * Make a request to the API
     *
     * @param path API path (without base URL)
     * @param init Fetch init options
     * @returns Raw Response object from fetch
     */
    async fetch(path, init = {}) {
        // Set up authentication headers
        const headers = {
            "Content-Type": "application/json",
            Authorization: `Bearer ${this.accessToken.unencrypted}`,
        };
        // Add headers from init if provided
        if (init.headers) {
            const initHeaders = init.headers;
            Object.keys(initHeaders).forEach((key) => {
                headers[key] = initHeaders[key];
            });
        }
        // For FormData, remove Content-Type
        if (init.body instanceof FormData) {
            delete headers["Content-Type"];
        }
        const response = await safeFetch(`${this.baseUrl}${path}`, {
            ...init,
            headers,
        });
        if (!response.ok) {
            let error = new Error(`API error: ${response.statusText}`, {
                cause: response,
            });
            try {
                error = Object.assign(new Error(response.statusText, { cause: response }), (await response.json())?.error);
            }
            finally {
                throw error;
            }
        }
        return response;
    }
    /**
     * Helper for GET requests
     */
    async get(path, init = {}) {
        return this.fetch(path, { ...init, method: "GET" });
    }
    /**
     * Helper for POST requests
     */
    async post(path, body, init = {}) {
        const requestBody = body instanceof FormData ? body : JSON.stringify(body);
        return this.fetch(path, { ...init, method: "POST", body: requestBody });
    }
    /**
     * Helper for PUT requests
     */
    async put(path, body, init = {}) {
        const requestBody = body instanceof FormData ? body : JSON.stringify(body);
        return this.fetch(path, { ...init, method: "PUT", body: requestBody });
    }
    /**
     * Helper for PATCH requests
     */
    async patch(path, body, init = {}) {
        return this.fetch(path, {
            ...init,
            method: "PATCH",
            body: JSON.stringify(body),
        });
    }
    /**
     * Helper for DELETE requests
     */
    async delete(path, init = {}) {
        return this.fetch(path, { ...init, method: "DELETE" });
    }
}
//# sourceMappingURL=api.js.map