import {z} from 'zod' export type CreateApiV1BalancesBatchTrackArgs = {'x-api-version': string, body: Array<{customer_id: string, feature_id?: string | undefined, entity_id?: string | undefined, event_name?: string | undefined, value?: number | undefined, properties?: {[key: string]: unknown} | undefined, timestamp?: number | undefined, overage_behavior?: ('cap' | 'overflow') | undefined, async?: boolean | undefined, lock?: ({lock_id: string, enabled: unknown, expires_at?: number | undefined}) | undefined}>}; export type CreateApiV1BalancesBatchTrackOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1BalancesBatchTrackResponse = z.object({success: z.unknown()}); 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 }; /** * Enqueue up to 1000 usage events for asynchronous processing. Items are validated synchronously up front; validated items are then enqueued via SQS for background deduction by workers. The response returns 202 immediately and does not include balance information. On partial enqueue failure (some items fail to enqueue, others succeed), the endpoint still returns 202 and logs the failures server-side; clients should NOT retry, because retrying re-enqueues the already-succeeded items. A 503 is returned only when zero items were successfully enqueued (queue entirely unavailable) — that case is safe to retry. * * POST /v1/balances.batch_track */ export const createApiV1BalancesBatchTrack = async (args: CreateApiV1BalancesBatchTrackArgs, options?: CreateApiV1BalancesBatchTrackOptions) => { const {baseUrl = "https://api.useautumn.com", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-version'] != null) headers.set("x-api-version", String(args['x-api-version'])) if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/balances.batch_track${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(createApiV1BalancesBatchTrackResponse, await res.json()) };