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/write-file.ts

33 lines · 863 B · TypeScript
import { assertBoxId, boxRequest, failure, type Result } from './client.ts'

export type WriteFileInput = {
	boxId: string
	/** Path relative to the box work directory. */
	path: string
	content: string
	encoding?: 'utf8' | 'base64'
}

export type WriteFileOutput = {
	success: boolean
	path: string
	encoding: 'utf8' | 'base64'
	size: number
}

/** Write one file into the box work directory. */
export default async function writeFile(
	input: WriteFileInput,
): Promise<Result<WriteFileOutput>> {
	const invalid = assertBoxId(input?.boxId)
	if (invalid) return failure<WriteFileOutput>(invalid)

	const body: Record<string, unknown> = { path: input.path, content: input.content }
	if (input.encoding !== undefined) body.encoding = input.encoding

	return boxRequest<WriteFileOutput>({
		path: '/boxes/' + input.boxId + '/files',
		method: 'PUT',
		body,
	})
}