Skip to content

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

Package listing

@kody/zoom

src/meetings/create.ts

59 lines · 1.5 KB · TypeScript
import { isConfirmed, isDryRun, normalizeUserLocator, pickAuthInput, request } from '../core.ts'
import { asRecord, buildMeetingBody, slimMeeting } from '../shape.ts'

/**
 * Create a Zoom meeting.
 *
 * Pass `dryRun: true` to preview the request. Live creates require `confirm: true`.
 * Do not create a live meeting from a smoke test.
 *
 * @example
 * import createZoomMeeting from 'kody:@kody/zoom/meetings/create'
 * const preview = await createZoomMeeting({
 *   topic: 'Example meeting',
 *   type: 2,
 *   start_time: '2030-01-15T17:00:00',
 *   duration: 30,
 *   timezone: 'UTC',
 *   dryRun: true,
 * })
 */
export default async function createZoomMeeting(params: Record<string, unknown> = {}) {
	const auth = pickAuthInput(params)
	const { userId } = normalizeUserLocator(params)
	const body = buildMeetingBody(params)
	if (!body.topic || typeof body.topic !== 'string') {
		throw new Error('topic is required to create a meeting.')
	}

	if (isDryRun(params)) {
		return {
			dryRun: true,
			wouldCreate: true,
			userId,
			path: `/users/${userId}/meetings`,
			body,
			auth,
		}
	}

	if (!isConfirmed(params)) {
		throw new Error(
			`Creating a meeting for ${userId} requires dryRun: true (preview) or confirm: true (live write).`,
		)
	}

	const response = await request<Record<string, unknown>>({
		...auth,
		method: 'POST',
		path: `/users/${userId}/meetings`,
		body,
		throwOnError: true,
		confirm: true,
	})

	return {
		...slimMeeting(asRecord(response.data)),
		auth: response.auth,
	}
}