Skip to content

src/auth-fetch.ts

35 lines · 1.1 KB · TypeScript
Raw
import { createAuthenticatedFetch } from 'kody:runtime'
import {
	type GoogleAccountInfo,
	resolveGoogleAccount,
} from './accounts.ts'

/**
 * Literal-name authenticated fetch for a resolved Google account alias.
 * Publish stamps each `createAuthenticatedFetch("…")` call site.
 */
export async function createGoogleAuthenticatedFetch(
	account: GoogleAccountInfo | string | { account?: string },
): Promise<typeof fetch> {
	const resolved =
		typeof account === 'object' &&
		account !== null &&
		'integrationName' in account &&
		'account' in account
			? (account as GoogleAccountInfo)
			: resolveGoogleAccount(account)
	switch (resolved.account) {
		case 'personal':
			return await createAuthenticatedFetch('google')
		case 'business':
			return await createAuthenticatedFetch('google-business')
		case 'youtube-brand':
			return await createAuthenticatedFetch('google-youtube-brand')
		case 'youtube-plus':
			return await createAuthenticatedFetch('google-youtube-plus')
		default: {
			const exhaustive: never = resolved.account
			throw new Error(`Unhandled Google account: ${String(exhaustive)}`)
		}
	}
}