import {z} from 'zod' export type CreditGrantSortOrder = 'created_at' | 'expires_at' | 'quantity' | 'quantity_remaining'; export type SortDirection = 'asc' | 'desc'; export type GetApiBillingCreditsGrantsCompanyListArgs = {company_id: string, order?: CreditGrantSortOrder | undefined, dir?: SortDirection | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiBillingCreditsGrantsCompanyListOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.enum(["aed", "afn", "all", "amd", "ang", "aoa", "ars", "aud", "awg", "azn", "bam", "bbd", "bdt", "bif", "bmd", "bnd", "bob", "brl", "bsd", "bwp", "byn", "bzd", "cad", "cdf", "chf", "clp", "cny", "cop", "crc", "cve", "czk", "djf", "dkk", "dop", "dzd", "egp", "etb", "eur", "fjd", "fkp", "gbp", "gel", "gip", "gmd", "gnf", "gtq", "gyd", "hkd", "hnl", "htg", "huf", "idr", "ils", "inr", "isk", "jmd", "jpy", "kes", "kgs", "khr", "kmf", "krw", "kyd", "kzt", "lak", "lbp", "lkr", "lrd", "lsl", "mad", "mdl", "mga", "mkd", "mmk", "mnt", "mop", "mur", "mvr", "mwk", "mxn", "myr", "mzn", "nad", "ngn", "nio", "nok", "npr", "nzd", "pab", "pen", "pgk", "php", "pkr", "pln", "pyg", "qar", "ron", "rsd", "rub", "rwf", "sar", "sbd", "scr", "sek", "sgd", "shp", "sle", "sos", "srd", "std", "szl", "thb", "tjs", "top", "try", "ttd", "twd", "tzs", "uah", "ugx", "usd", "uyu", "uzs", "vnd", "vuv", "wst", "xaf", "xcd", "xcg", "xof", "xpf", "yer", "zar", "zmw"]); export const billingCreditGrantReason = z.enum(["adjustment", "billing_credit_auto_topup", "free", "plan", "postpaid_forgiven", "postpaid_overdraft", "postpaid_settlement", "purchased", "rollover"]); export const billingProductPriceInterval = z.enum(["day", "month", "one-time", "year"]); export const billingProviderType = z.enum(["metronome", "orb", "schematic", "stripe"]); export const billingPriceScheme = z.enum(["per_unit", "tiered"]); export const billingPriceResponseData = z.object({currency: currency, external_price_id: z.string(), id: z.string(), interval: billingProductPriceInterval, interval_count: z.number().int(), nickname: z.string().nullable().optional(), price: z.number().int(), price_decimal: z.string().nullable().optional(), provider_type: billingProviderType, scheme: billingPriceScheme}); export const billingPlanCreditGrantResetCadence = z.enum(["daily", "every_6_months", "monthly", "quarterly", "weekly", "yearly"]); export const creditTransferResponseData = z.object({amount: z.number(), created_at: z.string(), direction: z.string(), id: z.string(), reason: z.string(), related_grant_id: z.string()}); export const billingCreditGrantZeroedOutReason = z.enum(["customer_archived", "expired", "integration_uninstalled", "license_retired", "manual", "plan_change", "plan_period_reset", "reconciled"]); export const billingCreditGrantResponseData = z.object({company_id: z.string(), company_license_id: z.string().nullable().optional(), company_name: z.string(), created_at: z.string(), credit_bundle_id: z.string().nullable().optional(), credit_icon: z.string().nullable().optional(), credit_id: z.string(), credit_name: z.string(), currency: currency.nullable().optional(), expires_at: z.string().nullable().optional(), grant_reason: billingCreditGrantReason, id: z.string(), license_name: z.string().nullable().optional(), plan_id: z.string().nullable().optional(), plan_name: z.string().nullable().optional(), postpaid_charge_amount: z.number().int().nullable().optional(), postpaid_charge_currency: currency.nullable().optional(), postpaid_charged_credits: z.number().nullable().optional(), price: billingPriceResponseData.optional(), quantity: z.number(), quantity_remaining: z.number(), quantity_used: z.number(), renewal_enabled: z.boolean(), renewal_period: billingPlanCreditGrantResetCadence.nullable().optional(), reserved: z.number().nullable().optional(), settled: z.number().nullable().optional(), source_grant_id: z.string().nullable().optional(), source_label: z.string(), transfers: z.array(creditTransferResponseData).optional(), updated_at: z.string(), valid_from: z.string().nullable().optional(), zeroed_out_date: z.string().nullable().optional(), zeroed_out_reason: billingCreditGrantZeroedOutReason.nullable().optional()}); export const sortDirection = z.enum(["asc", "desc"]); export const creditGrantSortOrder = z.enum(["created_at", "expires_at", "quantity", "quantity_remaining"]); export const getApiBillingCreditsGrantsCompanyListResponse = z.object({data: z.array(billingCreditGrantResponseData), params: z.object({company_id: z.string().optional(), dir: sortDirection.optional(), limit: z.number().int().gte(0).lte(250).optional(), offset: z.number().int().optional(), order: creditGrantSortOrder.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 }; /** * List company grants * * GET /billing/credits/grants/company/list */ export const getApiBillingCreditsGrantsCompanyList = async (args: GetApiBillingCreditsGrantsCompanyListArgs, options?: GetApiBillingCreditsGrantsCompanyListOptions) => { const {baseUrl = "https://api.schematichq.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.company_id != null) query.append("company_id", String(args.company_id)) if (args.order != null) query.append("order", String(args.order)) if (args.dir != null) query.append("dir", String(args.dir)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (ApiKeyAuth !== undefined) headers.set("X-Schematic-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/billing/credits/grants/company/list${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(getApiBillingCreditsGrantsCompanyListResponse, await res.json()) };