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

63.63% Statements 14/22
33.33% Branches 2/6
100% Functions 2/2
63.63% Lines 14/22

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 69 70 71 72 73                    1x 1x   1x                                     1x       1x   1x       1x   1x     1x   1x 1x                           1x 1x         1x    
import { fetch as undiciFetch, Response } from "undici";
import z from "zod";
import { ELEVEN_LABS_INTERNAL_API_BASE_URL } from "../constants";
import { RequestSchema, RequestType } from "./request";
import { NoThrow } from "@/utils/NoThrow";
import { proxyAgentPool } from "@/lib/proxyAgentPool";
import { requestHeaders } from "@/requestHeaders";
import { ResponseSchema } from "./response";
 
function prepareQueryParams(validatedInput: z.output<typeof RequestSchema>) {
    const queryParams = new URLSearchParams();
    queryParams.append("page_size", validatedInput.pageSize.toString());
 
    Iif (validatedInput.filters) {
        const filters = validatedInput.filters;
 
        const optionalFilters: Record<string, string | number | undefined> = {
            start_after_history_item_id: filters.startAfterHistoryItemID,
            date_before_unix: filters.dateBeforeUnix,
            date_after_unix: filters.dateAfterUnix,
            sort_direction: filters.sortDirection,
            search: filters.search,
            source: filters.source,
        };
 
        for (const [key, value] of Object.entries(optionalFilters)) {
            if (value !== undefined) {
                queryParams.append(key, value.toString());
            }
        }
    }
 
    return queryParams;
}
 
export async function history(input: RequestType) {
    const inputValidationResult = await RequestSchema.safeDecodeAsync(input);
 
    Iif (!inputValidationResult.success) {
        return NoThrow.error(inputValidationResult.error);
    }
 
    const validatedInput = inputValidationResult.data;
 
    const url = `${ELEVEN_LABS_INTERNAL_API_BASE_URL}/history`;
    let response: Response;
 
    const queryParams = prepareQueryParams(validatedInput);
 
    try {
        response = await undiciFetch(`${url}?${queryParams.toString()}`, {
            headers: {
                ...requestHeaders,
                "Cache-Control": "no-cache",
                Authorization: `Bearer ${validatedInput.bearerToken}`,
            },
            dispatcher: proxyAgentPool.get(validatedInput.proxyURL),
        });
    } catch (error) {
        return NoThrow.error(error);
    }
 
    let rawData: unknown;
 
    try {
        rawData = await response.json();
    } catch (error) {
        return NoThrow.error(error);
    }
 
    return NoThrow.fromZodResult(await ResponseSchema.safeParseAsync(rawData));
}