import {z} from 'zod' export type TypesRunnerCurrent = {cost?: number | undefined, provider?: string | undefined, sku?: string | undefined}; export type TypesRunnerSelfHosted = {github_self_hosted_fee?: boolean | undefined, provider?: string | undefined, region?: string | undefined}; export type TypesRunnerShape = {arch?: string | undefined, cpu_cores?: number | undefined, gpu_count?: number | undefined, os?: string | undefined, ram_gb?: number | undefined}; export type RunnerRecommendationRequest = {billed_minutes?: number | undefined, current?: TypesRunnerCurrent | undefined, job_count?: number | undefined, jobs_seconds?: Array | undefined, monthly_minutes?: number | undefined, public_repo?: boolean | undefined, self_hosted?: TypesRunnerSelfHosted | undefined, shape?: TypesRunnerShape | undefined}; export type CreateApiV1RecommendationsRunnerArgs = {body: RunnerRecommendationRequest}; export type CreateApiV1RecommendationsRunnerOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const typesRunnerPlanFee = z.object({fee: z.number().optional(), per: z.string().optional(), period: z.string().optional(), plan: z.string().optional()}); export const runnerRecommendation = z.object({billed_seconds: z.number().optional(), compute_source: z.string().optional(), cost: z.number().optional(), cost_per_month: z.number().optional(), fetched_on: z.string().optional(), flags: z.array(z.string()).optional(), plan_fees: z.array(typesRunnerPlanFee).optional(), pricing_model: z.string().optional(), provider: z.string().optional(), runtime_seconds: z.number().optional(), savings_vs_current: z.number().optional(), sku: z.string().optional(), source_url: z.string().optional(), vm_instance_type: z.string().optional()}); export const createApiV1RecommendationsRunnerResponse = z.array(runnerRecommendation); 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 }; /** * Find cheaper CI runners * * POST /v1/recommendations/runner * * Also accepts auth: BearerAuth */ export const createApiV1RecommendationsRunner = async (args: CreateApiV1RecommendationsRunnerArgs, options?: CreateApiV1RecommendationsRunnerOptions) => { const {baseUrl = "https://pricing.baselinehq.cloud", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/recommendations/runner${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(createApiV1RecommendationsRunnerResponse, await res.json()) };