import {z} from 'zod' export type EstimateItem = {mime_type: string, count?: (number | null) | undefined, minutes?: (number | null) | undefined, pages?: (number | null) | undefined, tokens?: (number | null) | undefined, units?: (number | null) | undefined}; export type EstimateRequest = {items?: Array | undefined, features?: Array | undefined, full_res?: boolean | undefined, manifest?: (string | {[key: string]: unknown} | null) | undefined, inventory?: {[key: string]: {[key: string]: number}} | undefined, queries_per_month?: number | undefined}; export type CreateApiV1OrganizationsBillingEstimateArgs = {body: EstimateRequest}; export type CreateApiV1OrganizationsBillingEstimateOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const manifestQuote = z.object({index_usd: z.number(), monthly_usd: z.number(), storage_usd: z.number(), queries_usd: z.number(), unit_prices: z.array(z.record(z.string(), z.unknown())), collections: z.array(z.record(z.string(), z.unknown())), unpriced_collections: z.array(z.string()), assumptions: z.array(z.string())}); export const estimateResponse = z.object({pricing_model: z.string().optional(), placeholder_rates: z.boolean(), line_items: z.array(z.record(z.string(), z.unknown())), total_usd: z.number(), batch_minimum_applied: z.boolean(), allowance_pool_usd: z.number().nullable().optional(), allowance_covered_usd: z.number(), estimated_overage_usd: z.number(), overage_discount_pct: z.number().optional(), overage_discount_usd: z.number().optional(), manifest_quote: manifestQuote.nullable().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 }; /** * Estimate Cost * * POST /v1/organizations/billing/estimate */ export const createApiV1OrganizationsBillingEstimate = async (args: CreateApiV1OrganizationsBillingEstimateArgs, options?: CreateApiV1OrganizationsBillingEstimateOptions) => { const {baseUrl = "https://api.mixpeek.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/organizations/billing/estimate${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(estimateResponse, await res.json()) };