Skip to content

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

Package listing

@kody/calendly

src/get-event-type.ts

51 lines · 2.0 KB · TypeScript
import { calendlyRequest, uuidFromUri } from './client.ts'
import { mapEventType } from './models.ts'
import type { CalendlyAuthInput, CalendlyEventTypeSummary } from './types.ts'
import { optionalString, requireRecord } from './types.ts'
import { requireAuthMode } from './auth.ts'

export type GetEventTypeInput = CalendlyAuthInput & {
	/** Event type uuid or full `https://api.calendly.com/event_types/{uuid}` URI. */
	eventType?: string
	/** Alias of `eventType`. */
	uuid?: string
}

/**
 * Read one Calendly event type by uuid or URI.
 * @example
 * import getEventType from 'kody:@kody/calendly/get-event-type'
 * const eventType = await getEventType({ eventType: 'AAAAAAAAAAAAAAAA' })
 */
export async function getEventType(input: GetEventTypeInput): Promise<CalendlyEventTypeSummary> {
	const eventType =
		optionalString(input.eventType, 'eventType') ?? optionalString(input.uuid, 'uuid')
	if (!eventType) throw new Error('get-event-type requires eventType (uuid is accepted as an alias).')
	const result = await calendlyRequest<unknown>({
		...input,
		operation: 'eventTypes.read',
		path: `/event_types/${uuidFromUri(eventType)}`,
	})
	return mapEventType(result.data)
}

/**
 * Read one Calendly event type by uuid or URI.
 * @example
 * import getEventType from 'kody:@kody/calendly/get-event-type'
 * const eventType = await getEventType({ uuid: 'AAAAAAAAAAAAAAAA' })
 */
export default async function getEventTypeEntrypoint(
	params: Partial<GetEventTypeInput> & Record<string, unknown> = {},
) {
	const input = requireRecord(params, 'get-event-type')
	return getEventType({
		eventType: optionalString(input.eventType, 'eventType') ?? optionalString(input.uuid, 'uuid'),
		uuid: optionalString(input.uuid, 'uuid'),
		integrationName: optionalString(input.integrationName, 'integrationName'),
		integration: optionalString(input.integration, 'integration'),
		account: optionalString(input.account, 'account'),
		secretName: optionalString(input.secretName, 'secretName'),
		auth: requireAuthMode(input.auth),
	})
}