Skip to content
← Public packages

@kody/browser-run

Call Cloudflare Browser Run Quick Actions and reuse shared sessions with createBrowserContext.

src/pdf.ts

48 lines · 1.5 KB · TypeScript
import { any, object, optional, parse, string } from 'remix/data-schema'
import quickAction from './quick-action.ts'

const inputSchema = object(
	{
		accountId: string(),
		apiTokenSecret: optional(string()),
		body: optional(any()),
		url: optional(string()),
		html: optional(string()),
	},
	{ unknownKeys: 'error' },
)

/**
 * Cloudflare Browser Run `/pdf` Quick Action.
 * Prefer this over a bare fetch when you only need pdf.
 * Pass `url` or `html` (or a full `body` object from CF docs).
 *
 * @param raw.accountId - Cloudflare account id
 * @param raw.url - Page URL (merged into body when set)
 * @param raw.html - Raw HTML (merged into body when set)
 * @param raw.body - Extra CF request fields
 * @param raw.apiTokenSecret - Optional alternate secret name
 * @returns Structured Quick Action result (binary base64 for screenshot/pdf)
 *
 * @example
 * import pdf from 'kody:@kody/browser-run/pdf'
 * const result = await pdf({
 *   accountId: 'YOUR_ACCOUNT_ID',
 *   url: 'https://example.com',
 * })
 */
export default async function pdf(raw: unknown = {}) {
	const input = parse(inputSchema, raw ?? {})
	const body: Record<string, unknown> =
		input.body && typeof input.body === 'object' && !Array.isArray(input.body)
			? { ...(input.body as Record<string, unknown>) }
			: {}
	if (input.url) body.url = input.url
	if (input.html) body.html = input.html
	return await quickAction({
		accountId: input.accountId,
		apiTokenSecret: input.apiTokenSecret,
		action: 'pdf',
		body,
	})
}