import {z} from 'zod' export type PricingEstimationSource = 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 | 1301 | 1302 | 1303 | 1304 | 1305 | 1306 | 1401 | 1402 | 1403 | 1404 | 1405 | 1406 | 1501 | 1502 | 1503 | 1504 | 1505 | 1506 | 1511 | 1512 | 1513 | 1514 | 1515 | 1516 | 1630 | 1701 | 1702 | 1703 | 1704 | 1705 | 1706 | 1711 | 1712 | 1713 | 1714 | 1715 | 1716 | 1801 | 1802 | 1803 | 1804 | 1805 | 1806 | 1811 | 1812 | 1813 | 1814 | 1815 | 1816 | 2100 | 2200 | 2300 | 2400 | 4100 | 4200 | 4300 | 4400 | 4501 | 4502 | 4503 | 5200 | 5300; export type GetApiV1PricingSourceResourceIdArgs = {source: PricingEstimationSource, resourceId: number, units?: (number | null) | undefined}; export type GetApiV1PricingSourceResourceIdOptions = {baseUrl?: string | undefined, AccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const appliedPricingType = z.union([z.literal(0), z.literal(1), z.literal(2)]); export const priceEstimationBreakdownLine = z.object({Description: z.string().nullable().optional(), Units: z.number().int().optional(), PricePerUnit: z.number().optional(), Cost: z.number().optional()}); export const priceEstimationModel = z.object({PricingType: appliedPricingType.optional(), PricePerUnit: z.number().optional(), UnitDescription: z.string().nullable().optional(), EstimatedCost: z.number().nullable().optional(), Breakdown: z.array(priceEstimationBreakdownLine).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 }; /** * Get active price for a resource and optionally cost estimate if usage amount provided * * GET /v1/pricing/{source}/{resourceId} * * Also accepts auth: bearer */ export const getApiV1PricingSourceResourceId = async (args: GetApiV1PricingSourceResourceIdArgs, options?: GetApiV1PricingSourceResourceIdOptions) => { const {baseUrl = "https://api.bunny.net", fetch: fetchFn = fetch, AccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.units != null) query.append("units", String(args.units)) if (AccessKey !== undefined) headers.set("AccessKey", AccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/pricing/${encodeURIComponent(String(args.source))}/${encodeURIComponent(String(args.resourceId))}${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(priceEstimationModel, await res.json()) };