import {z} from 'zod' export type GetApiUsageTenantsArgs = {period?: string | undefined, page?: number | undefined, perPage?: number | undefined, sort?: ('sent' | '-sent' | 'delivered' | '-delivered' | 'bounce_rate' | '-bounce_rate' | 'delivery_rate' | '-delivery_rate' | 'tenant_name' | '-tenant_name') | undefined, status?: ('active' | 'suspended' | 'archived') | undefined, minSent?: number | undefined, timezone?: string | undefined}; export type GetApiUsageTenantsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const emailCounts = z.object({sent: z.number().int(), delivered: z.number().int(), soft_failed: z.number().int(), hard_failed: z.number().int(), bounced: z.number().int(), held: z.number().int()}); export const emailRates = z.object({delivery_rate: z.number(), bounce_rate: z.number()}); export const tenantUsageItem = z.object({tenantId: z.string(), tenantName: z.string(), externalId: z.string().nullable().optional(), status: z.enum(["active", "suspended", "archived"]), emails: emailCounts, rates: emailRates}); export const usagePeriod = z.object({start: z.string(), end: z.string()}); export const apiMeta = z.object({requestId: z.string()}); export const listTenantUsageResponse = z.object({data: z.array(tenantUsageItem), page: z.number().int(), perPage: z.number().int(), total: z.number().int(), totalPages: z.number().int(), period: usagePeriod, summary: z.object({totalTenants: z.number().int(), totalSent: z.number().int(), totalDelivered: z.number().int()}), meta: apiMeta}); 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 }; /** * List usage for all tenants * * GET /usage/tenants */ export const getApiUsageTenants = async (args: GetApiUsageTenantsArgs, options?: GetApiUsageTenantsOptions) => { const {baseUrl = "https://api.arkhq.io/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period != null) query.append("period", String(args.period)) if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.status != null) query.append("status", String(args.status)) if (args.minSent != null) query.append("minSent", String(args.minSent)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/usage/tenants${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(listTenantUsageResponse, await res.json()) };