Skip to content

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

Package listing

@kody/linear

src/accounts.ts

100 lines · 2.9 KB · TypeScript
import {
	apiKeySetupUrl,
	oauthConnectUrl,
	DEFAULT_API_KEY_SECRET,
	DEFAULT_INTEGRATION_NAME,
	DEFAULT_OAUTH_SCOPES,
	LINEAR_API_KEY_URL,
	LINEAR_CALLBACK_URL,
	LINEAR_OAUTH_APP_URL,
	LINEAR_REQUIRED_HOST,
	LINEAR_SCOPES,
} from './setup.ts'
import {
	listLinearIntegrationNames,
	listUserSecretNames,
	requireAuthMode,
	resolveIntegrationName,
	resolveSecretName,
	type ResolvedLinearAuth,
} from './auth.ts'
import type { LinearAuthInput } from './types.ts'
import { requireRecord } from './types.ts'

export type LinearAccountInfo = {
	integrationName: string
	secretName: string
	oauthConnected: boolean
	apiKeySaved: boolean
	preferredAuth: ResolvedLinearAuth['mode'] | null
	connectUrl: string
	apiKeyUrl: string
	scopes: typeof LINEAR_SCOPES
	defaultScopes: Array<string>
	requiredHost: string
	callbackUrl: string
	oauthAppUrl: string
	apiKeyDashboardUrl: string
}

/**
 * Resolve which Linear OAuth integration or API-key secret to use.
 *
 * - `integration` / `integrationName` wins when set (exact name, e.g. `linear-work`)
 * - `account: 'work'` → `linear-work`
 * - `account: 'default'` / `'linear'` / omitted → `linear`
 * - API key secret defaults to `linearApiKey`, or `linearApiKey-<purpose>` for
 *   `linear-<purpose>` integrations
 *
 * @example
 * import listLinearAccounts from 'kody:@kody/linear/accounts'
 * const accounts = await listLinearAccounts()
 */
export async function accounts(
	params: LinearAuthInput = {},
): Promise<LinearAccountInfo> {
	const integrationName = resolveIntegrationName(params)
	const secretName = resolveSecretName(params)
	const [secrets, integrations] = await Promise.all([
		listUserSecretNames(),
		listLinearIntegrationNames(),
	])
	const oauthConnected = integrations.includes(integrationName)
	const apiKeySaved = secrets.has(secretName)
	const forced = requireAuthMode(params.auth)
	let preferredAuth: LinearAccountInfo['preferredAuth'] = null
	if (forced === 'oauth' && oauthConnected) preferredAuth = 'oauth'
	else if (forced === 'api-key' && apiKeySaved) preferredAuth = 'api-key'
	else if (oauthConnected) preferredAuth = 'oauth'
	else if (apiKeySaved) preferredAuth = 'api-key'

	return {
		integrationName,
		secretName,
		oauthConnected,
		apiKeySaved,
		preferredAuth,
		connectUrl: oauthConnectUrl(integrationName),
		apiKeyUrl: apiKeySetupUrl(secretName),
		scopes: LINEAR_SCOPES,
		defaultScopes: [...DEFAULT_OAUTH_SCOPES],
		requiredHost: LINEAR_REQUIRED_HOST,
		callbackUrl: LINEAR_CALLBACK_URL,
		oauthAppUrl: LINEAR_OAUTH_APP_URL,
		apiKeyDashboardUrl: LINEAR_API_KEY_URL,
	}
}

export { DEFAULT_API_KEY_SECRET, DEFAULT_INTEGRATION_NAME }

/**
 * Resolve which Linear OAuth integration or API-key secret to use.
 * @example
 * import listLinearAccounts from 'kody:@kody/linear/accounts'
 * const info = await listLinearAccounts({ account: 'work' })
 */
export default async function accountsEntrypoint(
	params: LinearAuthInput & Record<string, unknown> = {},
) {
	return accounts(requireRecord(params, 'accounts') as LinearAuthInput)
}