All files / src/services/elevenLabsAPI/voices/listVoices index.ts

0% Statements 0/41
0% Branches 0/26
0% Functions 0/2
0% Lines 0/40

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                                                                                                                                                                                                                                                                     
import { ELEVEN_LABS_API_BASE_URL } from "../../constants";
import { ListVoicesRequest, ListVoicesRequestSchema } from "./request";
import { ListVoicesResponseSchema } from "./response";
 
export async function listVoicesQuery(input: ListVoicesRequest) {
    const {
        nextPageToken,
        pageSize,
        search,
        sortBy,
        sortDirection,
        voiceType,
        category,
        fineTuningState,
        collectionID,
        includeTotalCount,
        voiceIDs,
    } = ListVoicesRequestSchema.parse(input);
 
    const queryParams = new URLSearchParams();
 
    if (nextPageToken !== undefined) {
        queryParams.append("next_page_token", nextPageToken);
    }
 
    if (pageSize !== undefined) {
        queryParams.append("page_size", pageSize.toString());
    }
 
    if (search !== undefined) {
        queryParams.append("search", search);
    }
 
    if (sortBy !== undefined) {
        const sortByMap = {
            CREATED_AT_UNIX: "created_at_unix",
            NAME: "name",
        } as const;
 
        queryParams.append("sort", sortByMap[sortBy]);
    }
 
    if (sortDirection !== undefined) {
        const sortDirectionMap = {
            ASCENDING: "asc",
            DESCENDING: "desc",
        } as const;
 
        queryParams.append("sort_direction", sortDirectionMap[sortDirection]);
    }
 
    if (voiceType) {
        const voiceTypeMap = {
            COMMUNITY: "community",
            DEFAULT: "default",
            NON_DEFAULT: "non-default",
            PERSONAL: "personal",
            WORKSPACE: "workspace",
            SAVED: "saved",
        } as const;
 
        queryParams.append("voice_type", voiceTypeMap[voiceType]);
    }
 
    if (category !== undefined) {
        queryParams.append("category", category);
    }
 
    if (fineTuningState !== undefined) {
        const fineTuningStateMap = {
            DRAFT: "draft",
            NOT_VERIFIED: "not_verified",
            NOT_STARTED: "not_started",
            QUEUED: "queued",
            FINE_TUNING: "fine_tuning",
            FINE_TUNED: "fine_tuned",
            FAILED: "failed",
            DELAYED: "delayed",
        } as const;
 
        queryParams.append("fine_tuning_state", fineTuningStateMap[fineTuningState]);
    }
 
    if (collectionID) {
        queryParams.append("collection_id", collectionID);
    }
 
    if (includeTotalCount !== undefined) {
        queryParams.append("include_total_count", includeTotalCount.toString());
    }
 
    if (voiceIDs) {
        voiceIDs.forEach((id) => queryParams.append("voice_ids", id));
    }
 
    const url = `${ELEVEN_LABS_API_BASE_URL}/shared-voices?${queryParams.toString()}`;
 
    const response = await fetch(url, {
        method: "GET",
        headers: {
            "Content-Type": "application/json",
            "XI-API-KEY": "sk_9e9df9c391690586f1d0e5404424aa7cce995b465f1dd4d3",
        },
    });
 
    if (!response.ok) {
        const errorText = await response.text();
        // return NoThrow.error(new Error(`ElevenLabs API error (${response.status}): ${errorText}`));
        throw new Error(`ElevenLabs API error (${response.status}): ${errorText}`);
    }
 
    let rawData: unknown;
 
    try {
        rawData = await response.json();
    } catch {
        // return NoThrow.error(new Error(`JSON parse error...`));
        throw new Error(`JSON parse error...`);
    }
 
    const result = await ListVoicesResponseSchema.safeParseAsync(rawData);
 
    if (!result.success) {
        // return NoThrow.error(result.error);
        throw result.error;
    }
 
    // return NoThrow.success(result.data);
    return result.data;
}