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/host-port.ts

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

export type HostPortInput = {
	boxId: string
	/** Port the service listens on inside the box. */
	port: number
	/** Return an ungated URL instead of one that requires the `_token` query parameter. */
	public?: boolean
	title?: string
}

export type HostPortOutput = {
	success?: boolean
	port: number
	/** Public HTTPS URL. May include `_token`. Redact from logs. */
	url: string
	isProtected?: boolean
	access?: 'private' | 'public'
}

/**
 * Expose a box port on a stable public HTTPS URL
 * (`https://<box-subdomain>-<port>.on.ascii.dev`).
 * Idempotent for the same box and port. The URL may include `_token`; redact it.
 */
export default async function hostPort(input: HostPortInput): Promise<Result<HostPortOutput>> {
	const invalid = assertBoxId(input?.boxId)
	if (invalid) return failure<HostPortOutput>(invalid)
	const body: Record<string, unknown> = { port: input.port }
	if (input.public !== undefined) body.public = input.public
	if (input.title !== undefined) body.title = input.title
	return boxRequest<HostPortOutput>({
		path: '/boxes/' + input.boxId + '/host',
		method: 'POST',
		body,
	})
}