import {z} from 'zod' export type StackDeploymentProvider = 'aws' | 'gcp' | 'azure'; export type GetApiApiV1StackDeploymentInfoArgs = {provider: StackDeploymentProvider}; export type GetApiApiV1StackDeploymentInfoOptions = {baseUrl: string, CookieOAuth2TokenBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const stackDeploymentProvider = z.enum(["aws", "gcp", "azure"]); export const stackDeploymentInfo = z.object({provider: stackDeploymentProvider, description: z.string(), instructions: z.string(), post_deploy_instructions: z.string(), integrations: z.array(z.string()), permissions: z.record(z.string(), z.array(z.string())), locations: z.record(z.string(), z.string()), skypilot_default_regions: z.record(z.string(), z.string())}); 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 Stack Deployment Info * * GET /api/v1/stack-deployment/info */ export const getApiApiV1StackDeploymentInfo = async (args: GetApiApiV1StackDeploymentInfoArgs, options: GetApiApiV1StackDeploymentInfoOptions) => { const {baseUrl, fetch: fetchFn = fetch, CookieOAuth2TokenBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.provider != null) query.append("provider", String(args.provider)) if (CookieOAuth2TokenBearer !== undefined) headers.set('authorization', `Bearer ${CookieOAuth2TokenBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/stack-deployment/info${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(stackDeploymentInfo, await res.json()) };