Skip to content

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

Package listing

@kody/raycast

src/helpers.ts

56 lines · 1.8 KB · TypeScript
export const OFFICIAL_PACKAGE_ID = 'ea5db329-3cc0-4daa-b23a-3954cccfe560'
export const KODY_ID = 'raycast'
export const PACKAGE_NAME = '@kody/raycast'
export const PUBLIC_URL = 'https://kody.codes/@kody/raycast'
export const ORIGIN = 'https://kody.codes'

export const DISCOVERY_EXPORTS = [
	'list-packages',
	'get-package',
	'invoke',
	'guide',
] as const

export function tokenSetupUrl(packageId: string = OFFICIAL_PACKAGE_ID) {
	const id = encodeURIComponent(packageId)
	return `${ORIGIN}/account/packages/${id}?newToken=1&name=Raycast&exportNames=*`
}

export function invocationUrl(username: string, exportName = 'list-packages') {
	const owner = username.replace(/^@/, '').trim()
	if (!owner) {
		throw new Error('username is required to build an invocation URL.')
	}
	const path = routeExportName(exportName)
	return `${ORIGIN}/@${encodeURIComponent(owner)}/api/package-invocations/${KODY_ID}/${path}`
}

export function routeExportName(exportName: string) {
	const trimmed = exportName.trim()
	if (!trimmed || trimmed === '.' || trimmed === './') return '__root__'
	return trimmed.replace(/^\.\//, '')
}

export function getExportName(subpath: string) {
	if (subpath === '.') return ''
	if (subpath.startsWith('./')) return subpath.slice(2)
	return subpath
}

export function isDryRun(input: { dryRun?: unknown } | null | undefined) {
	return input?.dryRun === true
}

export function asRecord(value: unknown): Record<string, unknown> {
	if (value && typeof value === 'object' && !Array.isArray(value)) {
		return value as Record<string, unknown>
	}
	return {}
}

export function optionalString(input: Record<string, unknown>, key: string) {
	const value = input[key]
	if (typeof value !== 'string') return undefined
	const trimmed = value.trim()
	return trimmed ? trimmed : undefined
}