import {z} from 'zod' export type GetApiV1ProvidersOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const typesPredicates = z.object({availability_zones: z.array(z.string()).optional(), disk_types: z.array(z.string()).optional(), instance_types: z.array(z.string()).optional(), operating_systems: z.array(z.string()).optional(), providers: z.array(z.string()).optional(), regions: z.array(z.string()).optional(), services: z.array(z.string()).optional(), usage_types: z.array(z.string()).optional()}); export const githubComBaselinehqGolangSharedTypesProvider = z.enum(["GCP", "AWS", "DigitalOcean", "Azure", "Hetzner", "OVHCloud", "PlanetScale", "STACKIT", "Linode", "Base", "Akash", "Alibaba", "Baseten", "Beam", "Cerebrium", "CIRunners", "Civo", "Clore", "CoreWeave", "Crusoe", "CUDO", "DataCrunch", "DeepInfra", "Denvr", "E2ENetworks", "Exoscale", "Fal", "HotAisle", "Hyperbolic", "Hyperstack", "IBM", "Lambda", "Latitude", "MassedCompute", "Modal", "Nebius", "Novita", "Oblivus", "OCI", "OpenTelekom", "PrimeIntellect", "Replicate", "RunPod", "Salad", "Scaleway", "Sesterce", "Shadeform", "ThunderCompute", "UpCloud", "Vast", "Vultr"]); export const providerConfig = z.object({predicates: typesPredicates.optional(), provider: githubComBaselinehqGolangSharedTypesProvider.optional()}); export const providerConfigs = z.record(z.string(), providerConfig); 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 the providers CostGraph prices * * GET /v1/providers * * Also accepts auth: BearerAuth */ export const getApiV1Providers = async (options?: GetApiV1ProvidersOptions) => { const {baseUrl = "https://pricing.baselinehq.cloud", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/providers${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(providerConfigs, await res.json()) };