Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/zoom

src/webinars/update.ts

59 lines · 1.5 KB · TypeScript
import { isConfirmed, isDryRun, normalizeWebinarLocator, pickAuthInput, request } from '../core.ts'
import { buildWebinarBody } from '../shape.ts'

/**
 * Update a Zoom webinar.
 *
 * Pass `dryRun: true` to preview the request. Live updates require `confirm: true`.
 *
 * @example
 * import updateZoomWebinar from 'kody:@kody/zoom/webinars/update'
 * const preview = await updateZoomWebinar({
 *   webinarId: '22222222222',
 *   topic: 'Updated example webinar',
 *   dryRun: true,
 * })
 */
export default async function updateZoomWebinar(params: Record<string, unknown> = {}) {
	const auth = pickAuthInput(params)
	const { webinarId, occurrenceId } = normalizeWebinarLocator(params)
	const body = buildWebinarBody(params)
	if (Object.keys(body).length === 0) {
		throw new Error('Provide at least one webinar field to update (topic, start_time, duration, …).')
	}

	if (isDryRun(params)) {
		return {
			dryRun: true,
			wouldUpdate: true,
			webinarId,
			occurrenceId: occurrenceId ?? null,
			path: `/webinars/${webinarId}`,
			body,
			auth,
		}
	}

	if (!isConfirmed(params)) {
		throw new Error(
			`Updating webinar ${webinarId} requires dryRun: true (preview) or confirm: true (live write).`,
		)
	}

	const response = await request<Record<string, unknown>>({
		...auth,
		method: 'PATCH',
		path: `/webinars/${webinarId}`,
		query: occurrenceId ? { occurrence_id: occurrenceId } : undefined,
		body,
		throwOnError: true,
		confirm: true,
	})

	return {
		ok: true,
		webinarId,
		status: response.status,
		auth: response.auth,
	}
}