Skip to content
← Public packages

@cameronpak/box

Full Box Public API v1 client: lifecycle, prompts, files, commands, snapshots, environments, webhooks, desktop, hosting, and account.

src/update-environment.ts

56 lines · 2.2 KB · TypeScript
import { assertEnvironmentId, boxRequest, failure, type Result } from './client.ts'
import type { BoxEnvironment, SecretFile } from './types.ts'

export type UpdateEnvironmentInput = {
	environmentId: string
	name?: string
	isDefault?: boolean
	safeForThirdParties?: boolean
	passGithub?: boolean
	passSecrets?: boolean
	passBoxCredentials?: boolean
	passAgentsCredentials?: boolean
	envContents?: string
	secretFiles?: Array<SecretFile>
	/** Full replacement of the version's repository selection. */
	repositories?: Array<{
		repositoryId: string
		baseBranch?: string
		setupScript?: string
		setupBlocking?: boolean
		preCommitHooks?: Array<{ script?: string; blocking?: boolean }>
	}>
}

export type UpdateEnvironmentOutput = {
	success: boolean
	environment?: BoxEnvironment | null
}

/**
 * Rename, set as default, and/or edit flags and contents.
 * Any flag or content change mints a new immutable version; existing boxes
 * stay on their pinned version until upgradeEnvironment.
 */
export default async function updateEnvironment(
	input: UpdateEnvironmentInput,
): Promise<Result<UpdateEnvironmentOutput>> {
	const invalid = assertEnvironmentId(input?.environmentId)
	if (invalid) return failure<UpdateEnvironmentOutput>(invalid)
	const body: Record<string, unknown> = {}
	if (input.name !== undefined) body.name = input.name
	if (input.isDefault !== undefined) body.isDefault = input.isDefault
	if (input.safeForThirdParties !== undefined) body.safeForThirdParties = input.safeForThirdParties
	if (input.passGithub !== undefined) body.passGithub = input.passGithub
	if (input.passSecrets !== undefined) body.passSecrets = input.passSecrets
	if (input.passBoxCredentials !== undefined) body.passBoxCredentials = input.passBoxCredentials
	if (input.passAgentsCredentials !== undefined) body.passAgentsCredentials = input.passAgentsCredentials
	if (input.envContents !== undefined) body.envContents = input.envContents
	if (input.secretFiles !== undefined) body.secretFiles = input.secretFiles
	if (input.repositories !== undefined) body.repositories = input.repositories
	return boxRequest<UpdateEnvironmentOutput>({
		path: '/environments/' + input.environmentId,
		method: 'PUT',
		body,
	})
}