import {z} from 'zod' export type CreateApiV2PricesPriceIdArchiveArgs = {price_id: string}; export type CreateApiV2PricesPriceIdArchiveOptions = {baseUrl: string, LedgerUpApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const ledgerId = z.string(); export const billingMode = z.enum(["flat", "per_unit", "per_seat", "usage_sum", "last_metric_reported", "tiered_usage", "package", "minimum_commit"]); export const billingInterval = z.enum(["month", "quarter", "year", "one_time", "contract_term"]); export const properties = z.unknown(); export const unixTimestamp = z.number().int(); export const price = z.object({id: ledgerId, product_id: ledgerId, stripe_price_id: z.string().nullable(), billing_mode: billingMode, currency_code: z.string(), interval: billingInterval, unit_amount_minor_units: z.number().int().nullable(), unit_scale: z.string().nullable(), metadata: properties, created_at: unixTimestamp, updated_at: unixTimestamp, archived_at: unixTimestamp.nullable()}); 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 }; /** * Archive a price. * * POST /v2/prices/{price_id}/archive */ export const createApiV2PricesPriceIdArchive = async (args: CreateApiV2PricesPriceIdArchiveArgs, options: CreateApiV2PricesPriceIdArchiveOptions) => { const {baseUrl, fetch: fetchFn = fetch, LedgerUpApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (LedgerUpApiKey !== undefined) headers.set("x-api-key", LedgerUpApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/prices/${encodeURIComponent(String(args.price_id))}/archive${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(price, await res.json()) };