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/set-environment-var.ts

37 lines · 1.0 KB · TypeScript
import { assertEnvironmentId, boxRequest, failure, type Result } from './client.ts'

export type SetEnvironmentVarInput = {
	environmentId: string
	/** Variable name matching [A-Za-z_][A-Za-z0-9_]*. */
	key: string
	value: string
}

export type EnvironmentMutationOutput = {
	success: boolean
	versionId?: string
	versionNumber?: number
}

/**
 * Set or replace a single environment variable without rewriting the rest.
 * Mints a new immutable version.
 */
export default async function setEnvironmentVar(
	input: SetEnvironmentVarInput,
): Promise<Result<EnvironmentMutationOutput>> {
	const invalid = assertEnvironmentId(input?.environmentId)
	if (invalid) return failure<EnvironmentMutationOutput>(invalid)
	if (!input?.key) {
		return failure<EnvironmentMutationOutput>({
			code: 'invalid_env_key',
			message: 'key is required.',
			status: 400,
		})
	}
	return boxRequest<EnvironmentMutationOutput>({
		path: '/environments/' + input.environmentId + '/vars/' + encodeURIComponent(input.key),
		method: 'PUT',
		body: { value: input.value },
	})
}