import {z} from 'zod' export type GetApiPartnerAccountsEncodedAccountIdPlanArgs = {encoded_account_id: string}; export type GetApiPartnerAccountsEncodedAccountIdPlanOptions = {baseUrl?: string | undefined, ctctPartnerAuthorizer?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tierObject = z.object({usage_type: z.string().optional(), current_usage: z.number().optional(), tier: z.number().int().optional(), tier_min: z.number().optional(), tier_max: z.number().optional(), price: z.number().optional(), currency_code: z.string().optional()}); export const planTiersObject = z.object({plan_type: z.string().optional(), current_tiers: z.array(tierObject).optional(), billing_status: z.string().optional(), billing_day_of_month: z.number().int().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * GET Billing Plan Details for a Client Account * * GET /partner/accounts/{encoded_account_id}/plan */ export const getApiPartnerAccountsEncodedAccountIdPlan = async (args: GetApiPartnerAccountsEncodedAccountIdPlanArgs, options?: GetApiPartnerAccountsEncodedAccountIdPlanOptions) => { const {baseUrl = "https://api.cc.email/v3", fetch: fetchFn = fetch, ctctPartnerAuthorizer, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ctctPartnerAuthorizer !== undefined) headers.set('authorization', `Bearer ${ctctPartnerAuthorizer}`) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/partner/accounts/${encodeURIComponent(String(args.encoded_account_id))}/plan${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(planTiersObject, await res.json()) };