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

72.72% Statements 8/11
50% Branches 1/2
100% Functions 1/1
72.72% Lines 8/11

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                  1x       1x 1x                           1x 1x         1x   1x       1x    
import { fetch as undiciFetch, Response } from "undici";
import { ELEVEN_LABS_INTERNAL_API_BASE_URL } from "../constants";
import { RequestType } from "./request";
import { NoThrow } from "@/utils/NoThrow";
import { proxyAgentPool } from "@/lib/proxyAgentPool";
import { requestHeaders } from "@/requestHeaders";
import { ResponseSchema } from "./response";
 
export async function user(req: RequestType) {
    const url = `${ELEVEN_LABS_INTERNAL_API_BASE_URL}/user/internal`;
 
    let response: Response;
 
    try {
        response = await undiciFetch(url, {
            headers: {
                ...requestHeaders,
                "Cache-Control": "no-cache",
                Authorization: `Bearer ${req.bearerToken}`,
            },
            dispatcher: proxyAgentPool.get(req.proxyURL),
        });
    } catch (error) {
        return NoThrow.error(error);
    }
 
    let rawData: unknown;
 
    try {
        rawData = await response.json();
    } catch (error) {
        return NoThrow.error(error);
    }
 
    const validatedDataResult = await ResponseSchema.safeParseAsync(rawData);
 
    Iif (!validatedDataResult.success) {
        return NoThrow.error(validatedDataResult.error);
    }
 
    return NoThrow.success(validatedDataResult.data);
}