import {z} from 'zod' export type RemoveCredits = {sms?: number | undefined, email?: number | undefined}; export type CreateApiResellerChildrenChildIdentifierCreditsRemoveArgs = {childIdentifier: string, body: RemoveCredits}; export type CreateApiResellerChildrenChildIdentifierCreditsRemoveOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const remainingCreditModelChild = z.object({sms: z.number(), email: z.number()}); export const remainingCreditModelReseller = z.object({sms: z.number(), email: z.number()}); export const remainingCreditModel = z.object({child: remainingCreditModelChild, reseller: remainingCreditModelReseller}); 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 }; /** * Remove Email and/or SMS credits from a specific child account * * POST /reseller/children/{childIdentifier}/credits/remove * * Also accepts auth: partner-key */ export const createApiResellerChildrenChildIdentifierCreditsRemove = async (args: CreateApiResellerChildrenChildIdentifierCreditsRemoveArgs, options?: CreateApiResellerChildrenChildIdentifierCreditsRemoveOptions) => { const {baseUrl = "https://api.brevo.com/v3", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/reseller/children/${encodeURIComponent(String(args.childIdentifier))}/credits/remove${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(remainingCreditModel, await res.json()) };