import {z} from 'zod' export type GetApiProjectsProjectIdEnvironmentsArgs = {projectId: string}; export type GetApiProjectsProjectIdEnvironmentsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const environment = z.object({id: z.string(), created_at: z.string().nullable(), updated_at: z.string().nullable(), name: z.string(), machine_name: z.string(), title: z.string(), attributes: z.record(z.string(), z.string()), type: z.enum(["development", "production", "staging"]), parent: z.string().nullable(), default_domain: z.string().nullable(), has_domains: z.boolean(), clone_parent_on_create: z.boolean(), deployment_target: z.string().nullable(), is_pr: z.boolean(), has_remote: z.boolean(), status: z.enum(["active", "deleting", "dirty", "inactive", "paused"]), http_access: z.object({is_enabled: z.boolean(), addresses: z.array(z.object({permission: z.enum(["allow", "deny"]), address: z.string()})), basic_auth: z.record(z.string(), z.string())}), enable_smtp: z.boolean(), restrict_robots: z.boolean(), edge_hostname: z.string(), deployment_state: z.object({last_deployment_successful: z.boolean(), last_deployment_at: z.string().nullable(), last_autoscale_up_at: z.string().nullable(), last_autoscale_down_at: z.string().nullable(), crons: z.object({enabled: z.boolean(), status: z.enum(["paused", "running", "sleeping"])})}).nullable(), sizing: z.object({services: z.record(z.string(), z.object({resources: z.object({profile_size: z.string().nullable()}).nullable(), instance_count: z.number().int().nullable(), disk: z.number().int().nullable()})), webapps: z.record(z.string(), z.object({resources: z.object({profile_size: z.string().nullable()}).nullable(), instance_count: z.number().int().nullable(), disk: z.number().int().nullable()})), workers: z.record(z.string(), z.object({resources: z.object({profile_size: z.string().nullable()}).nullable(), instance_count: z.number().int().nullable(), disk: z.number().int().nullable()}))}).nullable(), resources_overrides: z.record(z.string(), z.object({services: z.record(z.string(), z.object({cpu: z.number().nullable(), memory: z.number().int().nullable(), disk: z.number().int().nullable()})), starts_at: z.string().nullable(), ends_at: z.string().nullable(), redeployed_start: z.boolean(), redeployed_end: z.boolean()})), max_instance_count: z.number().int().nullable(), last_active_at: z.string().nullable(), last_backup_at: z.string().nullable(), project: z.string(), is_main: z.boolean(), is_dirty: z.boolean(), has_staged_activities: z.boolean(), can_rolling_deploy: z.boolean(), supports_rolling_deployments: z.boolean(), has_code: z.boolean(), head_commit: z.string().nullable(), merge_info: z.object({commits_ahead: z.number().int().nullable(), commits_behind: z.number().int().nullable(), parent_ref: z.string().nullable()}), has_deployment: z.boolean(), supports_restrict_robots: z.boolean()}); export const environmentCollection = z.array(environment); 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 list of project environments * * GET /projects/{projectId}/environments */ export const getApiProjectsProjectIdEnvironments = async (args: GetApiProjectsProjectIdEnvironmentsArgs, options?: GetApiProjectsProjectIdEnvironmentsOptions) => { const {baseUrl = "https://api.upsun.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/environments${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(environmentCollection, await res.json()) };