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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | import z from "zod";
import { NoThrow } from "@/utils/NoThrow";
import { ELEVEN_LABS_API_BASE_URL } from "../constants";
import { fetch as undiciFetch, Response } from "undici";
import { proxyAgentPool } from "@/lib/proxyAgentPool";
import { ProxyURLBrand } from "@/brands/proxyURL";
const SubscriptionCurrencySchema = z.enum(["usd", "eur", "inr"]).nullable();
const SubscriptionStatusSchema = z.enum(["trialing", "active", "incomplete", "past_due", "free", "free_disabled"]);
const BillingPeriodSchema = z.enum(["monthly_period", "3_month_period", "6_month_period", "annual_period"]).nullable();
const CharacterRefreshPeriodSchema = z
.enum(["monthly_period", "3_month_period", "6_month_period", "annual_period"])
.nullable();
const SubscriptionAPIResponseSchema = z
.object({
tier: z.string(),
character_count: z.number(),
character_limit: z.number(),
max_character_limit_extension: z.number().nullable(),
can_extend_character_limit: z.boolean(),
allowed_to_extend_character_limit: z.boolean(),
next_character_count_reset_unix: z.number().nullable().optional(),
voice_slots_used: z.number(),
professional_voice_slots_used: z.number(),
voice_limit: z.number(),
max_voice_add_edits: z.number().nullable().optional(),
voice_add_edit_counter: z.number(),
professional_voice_limit: z.number(),
can_extend_voice_limit: z.boolean(),
can_use_instant_voice_cloning: z.boolean(),
can_use_professional_voice_cloning: z.boolean(),
currency: SubscriptionCurrencySchema,
status: SubscriptionStatusSchema,
billing_period: BillingPeriodSchema,
character_refresh_period: CharacterRefreshPeriodSchema,
})
.transform((data) => ({
tier: data.tier,
characterCount: data.character_count,
characterLimit: data.character_limit,
maxCharacterLimitExtension: data.max_character_limit_extension,
canExtendCharacterLimit: data.can_extend_character_limit,
allowedToExtendCharacterLimit: data.allowed_to_extend_character_limit,
nextCharacterCountResetUnix: data.next_character_count_reset_unix,
voiceSlotsUsed: data.voice_slots_used,
professionalVoiceSlotsUsed: data.professional_voice_slots_used,
voiceLimit: data.voice_limit,
maxVoiceAddEdits: data.max_voice_add_edits,
voiceAddEditCounter: data.voice_add_edit_counter,
professionalVoiceLimit: data.professional_voice_limit,
canExtendVoiceLimit: data.can_extend_voice_limit,
canUseInstantVoiceCloning: data.can_use_instant_voice_cloning,
canUseProfessionalVoiceCloning: data.can_use_professional_voice_cloning,
currency: data.currency,
status: data.status,
billingPeriod: data.billing_period,
characterRefreshPeriod: data.character_refresh_period,
}));
const UserAPIResponseSchema = z
.object({
user_id: z.string(),
subscription: SubscriptionAPIResponseSchema,
is_new_user: z.boolean(),
xi_api_key: z.string().nullable().optional(),
can_use_delayed_payment_methods: z.boolean(),
is_onboarding_completed: z.boolean(),
is_onboarding_checklist_completed: z.boolean(),
first_name: z.string().nullable().optional(),
is_api_key_hashed: z.boolean().optional().default(false),
xi_api_key_preview: z.string().nullable().optional(),
referral_link_code: z.string().nullable().optional(),
partnerstack_partner_default_link: z.string().nullable().optional(),
created_at: z.number(),
})
.transform((data) => ({
userID: data.user_id,
subscription: data.subscription,
isNewUser: data.is_new_user,
xiAPIKey: data.xi_api_key,
canUseDelayedPaymentMethods: data.can_use_delayed_payment_methods,
isOnboardingCompleted: data.is_onboarding_completed,
isOnboardingChecklistCompleted: data.is_onboarding_checklist_completed,
firstName: data.first_name,
isAPIKeyHashed: data.is_api_key_hashed,
xiAPIKeyPreview: data.xi_api_key_preview,
referralLinkCode: data.referral_link_code,
partnerstackPartnerDefaultLink: data.partnerstack_partner_default_link,
createdAt: data.created_at,
}))
.transform((data) => {
if (data.userID.startsWith("user_")) {
data.userID = data.userID.slice(5);
}
return data;
});
export type SubscriptionCurrency = z.infer<typeof SubscriptionCurrencySchema>;
export type SubscriptionStatus = z.infer<typeof SubscriptionStatusSchema>;
export type BillingPeriod = z.infer<typeof BillingPeriodSchema>;
export type CharacterRefreshPeriod = z.infer<typeof CharacterRefreshPeriodSchema>;
export async function getUser({
apiKey,
proxyURL,
}: {
// apiKey: ElevenLabsAccountWithProxyDocumentType["apiKey"];
apiKey: string;
// proxyURL: ElevenLabsAccountWithProxyDocumentType["proxyURL"];
proxyURL: ProxyURLBrand;
}) {
const url = `${ELEVEN_LABS_API_BASE_URL}/user`;
let response: Response;
try {
response = await undiciFetch(url, {
headers: {
"Content-Type": "application/json",
"XI-API-KEY": apiKey,
},
dispatcher: proxyAgentPool.get(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"));
}
const validatedDataResult = await UserAPIResponseSchema.safeParseAsync(rawData);
if (!validatedDataResult.success) {
return NoThrow.error(validatedDataResult.error);
}
return NoThrow.success(validatedDataResult.data);
}
|