import {z} from 'zod' export type StackDeploymentProvider = 'aws' | 'gcp' | 'azure'; export type GetApiApiV1StackDeploymentConfigArgs = {provider: StackDeploymentProvider, stack_name: string, location?: (string | null) | undefined, terraform?: boolean | undefined}; export type GetApiApiV1StackDeploymentConfigOptions = {baseUrl: string, CookieOAuth2TokenBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const stackDeploymentConfig = z.object({deployment_url: z.string(), deployment_url_text: z.string(), configuration: z.string().nullable().optional(), instructions: z.string().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 Stack Deployment Config * * GET /api/v1/stack-deployment/config */ export const getApiApiV1StackDeploymentConfig = async (args: GetApiApiV1StackDeploymentConfigArgs, options: GetApiApiV1StackDeploymentConfigOptions) => { 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 (args.stack_name != null) query.append("stack_name", String(args.stack_name)) if (args.location != null) query.append("location", String(args.location)) if (args.terraform != null) query.append("terraform", String(args.terraform)) if (CookieOAuth2TokenBearer !== undefined) headers.set('authorization', `Bearer ${CookieOAuth2TokenBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/stack-deployment/config${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(stackDeploymentConfig, await res.json()) };