All files / src/services/elevenLabsInternalAPI/authAccount index.ts

37.5% Statements 9/24
7.14% Branches 1/14
100% Functions 1/1
37.5% Lines 9/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                  1x     1x 1x                                                     1x 1x                                 1x 1x   1x       1x    
import { requestHeaders } from "@/requestHeaders";
import { ELEVEN_LABS_INTERNAL_API_BASE_URL } from "../constants";
import { RequestType } from "./request";
import { Response, fetch as undiciFetch } from "undici";
import { proxyAgentPool } from "@/lib/proxyAgentPool";
import { NoThrow } from "@/utils/NoThrow";
import { ResponseSchema } from "./response";
 
export async function authAccount(req: RequestType) {
    const url = `${ELEVEN_LABS_INTERNAL_API_BASE_URL}/auth-account`;
    let response: Response;
 
    try {
        response = await undiciFetch(url, {
            headers: {
                ...requestHeaders,
                "Content-Type": "application/json",
                "Cache-Control": "no-cache",
                Authorization: `Bearer ${req.bearerToken}`,
            },
            dispatcher: proxyAgentPool.get(req.proxyURL),
        });
    } catch (error) {
        if (error instanceof TypeError) {
            return NoThrow.error(error);
        }
 
        if (error instanceof SyntaxError) {
            return NoThrow.error(error);
        }
 
        if (error instanceof DOMException) {
            return NoThrow.error(error);
        }
 
        return NoThrow.error(new Error("Unknown error"));
    }
 
    let rawData: unknown;
 
    try {
        rawData = await response.json();
    } catch (error) {
        if (error instanceof TypeError) {
            return NoThrow.error(error);
        }
 
        if (error instanceof SyntaxError) {
            return NoThrow.error(error);
        }
 
        if (error instanceof DOMException) {
            return NoThrow.error(error);
        }
 
        return NoThrow.error(new Error("Unknown error"));
    }
 
    console.log("JSON:", JSON.stringify(rawData, null, 4));
    const validatedDataResult = await ResponseSchema.safeParseAsync(rawData);
 
    Iif (!validatedDataResult.success) {
        return NoThrow.error(validatedDataResult.error);
    }
 
    return NoThrow.success(validatedDataResult.data);
}