import { createClient } from "../util/api/client/client.gen.js";
import { createConfig } from "../util/api/client/utils.gen.js";
import { PrismaClient } from "./api/sdk.gen.js";
export class PrismaApi extends Error {
    status;
    method;
    url;
    code;
    constructor(props) {
        const { status, error } = props.error;
        super(`A request to the Prisma API failed (${props.response.status}): ${error}`);
        this.status = props.response.status;
        this.method = props.request.method;
        this.url = props.request.url;
        this.code = status?.toString() ?? "";
        Object.assign(this, props.error);
    }
}
/**
 * Create a PrismaApi instance with environment variable fallback
 * @param options API options
 * @returns PrismaApi instance
 */
export function createPrismaApi(options = {}) {
    const serviceToken = options.serviceToken?.unencrypted ?? process.env.PRISMA_SERVICE_TOKEN;
    if (!serviceToken) {
        throw new Error("PRISMA_SERVICE_TOKEN is not set");
    }
    const client = createClient(createConfig({
        baseUrl: "https://api.prisma.io",
        throwOnError: true,
        headers: {
            Authorization: `Bearer ${serviceToken}`,
            "User-Agent": "alchemy/1.0",
        },
    }));
    client.interceptors.error.use((error, response, request, options) => {
        if (options.throwOnError !== false) {
            throw new PrismaApi({
                error: error,
                request,
                response,
            });
        }
    });
    return new PrismaClient({
        client,
    });
}
//# sourceMappingURL=api.js.map