import {z} from 'zod' export type GetApiResellerChildrenArgs = {limit?: number | undefined, offset?: number | undefined}; export type GetApiResellerChildrenOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getChildInfoCredits = z.object({emailCredits: z.number().int().optional(), smsCredits: z.number().int().optional()}); export const getChildInfoStatistics = z.object({previousMonthTotalSent: z.number().int().optional(), currentMonthTotalSent: z.number().int().optional(), totalSent: z.number().int().optional()}); export const getChildInfoApiKeysV2 = z.object({name: z.string(), key: z.string()}); export const getChildInfoApiKeysV3 = z.object({name: z.string(), key: z.string()}); export const getChildInfoApiKeys = z.object({v2: z.array(getChildInfoApiKeysV2), v3: z.array(getChildInfoApiKeysV3).optional()}); export const getChildrenList = z.object({children: z.array(z.object({email: z.string(), firstName: z.string(), lastName: z.string(), companyName: z.string(), credits: getChildInfoCredits.optional(), statistics: getChildInfoStatistics.optional(), password: z.string(), ips: z.array(z.string()).optional(), apiKeys: getChildInfoApiKeys.optional(), id: z.number().int().optional()})).optional(), count: 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 the list of all children accounts * * GET /reseller/children * * Also accepts auth: partner-key */ export const getApiResellerChildren = async (args: GetApiResellerChildrenArgs, options?: GetApiResellerChildrenOptions) => { const {baseUrl = "https://api.brevo.com/v3", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (apiKey !== undefined) headers.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/reseller/children${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(getChildrenList, await res.json()) };