import { safeFetch } from "../util/safe-fetch.js";
/**
 * Minimal API client using raw fetch
 */
export class UpstashApi {
    /** Base URL for API */
    baseUrl;
    /** API key */
    apiKey;
    /** Email */
    email;
    /**
     * Create a new API client
     *
     * @param options API options
     */
    constructor(options = {}) {
        this.baseUrl = "https://api.upstash.com/v2";
        this.apiKey =
            options.apiKey?.unencrypted ?? process.env.UPSTASH_API_KEY ?? "";
        this.email = options.email ?? process.env.UPSTASH_EMAIL ?? "";
        if (!this.apiKey) {
            throw new Error("UPSTASH_API_KEY environment variable is required");
        }
        if (!this.email) {
            throw new Error("UPSTASH_EMAIL environment variable is required");
        }
    }
    /**
     * 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 = {}) {
        const headers = {
            "Content-Type": "application/json",
            Authorization: `Basic ${btoa(`${this.email}:${this.apiKey}`)}`,
        };
        if (init.headers) {
            const initHeaders = init.headers;
            Object.keys(initHeaders).forEach((key) => {
                headers[key] = initHeaders[key];
            });
        }
        return safeFetch(`${this.baseUrl}${path}`, {
            ...init,
            headers,
        });
    }
    /**
     * Helper for GET requests
     */
    async get(path, init = {}) {
        return this.fetch(path, { ...init, method: "GET" });
    }
    /**
     * Helper for POST requests
     */
    async post(path, body, init = {}) {
        return this.fetch(path, {
            ...init,
            method: "POST",
            body: JSON.stringify(body),
        });
    }
    /**
     * Helper for DELETE requests
     */
    async delete(path, init = {}) {
        return this.fetch(path, { ...init, method: "DELETE" });
    }
}
//# sourceMappingURL=api.js.map