Skip to content

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

Package listing

@kody/cal-com

src/setup.js

49 lines · 2.5 KB · JavaScript
export const API_BASE_URL = 'https://api.cal.com'
export const DEFAULT_BOOKING_BASE_URL = 'https://cal.com'
export const API_KEY_SECRET = 'calComApiKey'
export const DEFAULT_INTEGRATION_NAME = 'cal-com'
export const API_HOSTS = ['api.cal.com', 'app.cal.com']

export const SECRET_URL =
	'https://kody.codes/account/secrets/new?name=calComApiKey&description=Cal.com%20API%20key%20(Bearer%20token%20for%20api.cal.com)&allowedHosts=api.cal.com&scope=user'

export const OAUTH_URL =
	'https://kody.codes/connect/oauth?provider=cal-com&authorizeUrl=https%3A%2F%2Fapp.cal.com%2Fauth%2Foauth2%2Fauthorize&tokenUrl=https%3A%2F%2Fapi.cal.com%2Fv2%2Fauth%2Foauth2%2Ftoken&flow=confidential&scopes=PROFILE_READ%20EVENT_TYPE_READ%20BOOKING_READ%20SCHEDULE_READ%20WEBHOOK_READ%20WEBHOOK_WRITE%20BOOKING_WRITE&allowedHosts=api.cal.com%2Capp.cal.com&apiBaseUrl=https%3A%2F%2Fapi.cal.com&dashboardUrl=https%3A%2F%2Fapp.cal.com%2Fsettings%2Fdeveloper%2Foauth&providerSetupInstructions=Create%20an%20OAuth%20client%20at%20https%3A%2F%2Fapp.cal.com%2Fsettings%2Fdeveloper%2Foauth.%20Register%20redirect%20URI%20exactly%20https%3A%2F%2Fkody.codes%2Fconnect%2Foauth.%20Cal.com%20must%20approve%20the%20client%20before%20authorize%20works.%20Paste%20the%20client%20id%20and%20client%20secret%20into%20this%20form.'

export const API_KEY_DASHBOARD_URL = 'https://app.cal.com/settings/developer/api-keys'
export const OAUTH_DASHBOARD_URL = 'https://app.cal.com/settings/developer/oauth'
export const REDIRECT_URI = 'https://kody.codes/connect/oauth'

const RETIRED_ALIASES = new Set([
	'kent',
	'kentcdodds',
	'kent-booking',
	'kent-explicit-only',
	'bot',
])

export function setupUrls() {
	return {
		apiKeyUrl: SECRET_URL,
		oauthUrl: OAUTH_URL,
		apiKeyDashboardUrl: API_KEY_DASHBOARD_URL,
		oauthDashboardUrl: OAUTH_DASHBOARD_URL,
		redirectUri: REDIRECT_URI,
		hosts: [...API_HOSTS],
		apiKeySecret: API_KEY_SECRET,
		integrationName: DEFAULT_INTEGRATION_NAME,
	}
}

export function assertNoRetiredAlias(value, fieldName) {
	if (typeof value !== 'string') return
	const normalized = value.trim().toLowerCase()
	if (!RETIRED_ALIASES.has(normalized)) return
	throw new Error(
		`Hard-coded Cal.com account alias "${value}" (${fieldName}) is not supported. Pass secretName for an API key or integrationName for OAuth. Save an API key at ${SECRET_URL} or connect OAuth at ${OAUTH_URL}.`,
	)
}

export function authSetupMessage(prefix = 'Cal.com credentials are missing or were rejected.') {
	return `${prefix} Save an API key at ${SECRET_URL} (approve host api.cal.com) or connect OAuth at ${OAUTH_URL}.`
}