Skip to content

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

Package listing

@kody/raycast

src/get-package.ts

76 lines · 2.1 KB · TypeScript
import { kody } from 'kody:runtime'
import { getExportName, optionalString, asRecord } from './helpers.ts'

export type GetPackageInput = {
	packageId?: string
	kodyId?: string
}

export type GetPackageResult = {
	packageId: string
	kodyId: string
	name: string
	description: string
	tags: string[]
	hasApp: boolean
	exports: Array<{
		subpath: string
		exportName: string
		importSpecifier: string
		runtimeTarget: string | null
		typesPath: string | null
		description: string | null
		typeDefinition: string | null
		tokenSetupUrl: string | null
		invocationUrl: string | null
	}>
}

/**
 * Load one saved package and expose its callable export metadata.
 *
 * @param params.packageId - Saved package id (preferred).
 * @param params.kodyId - Lookup package id by kody id when packageId is omitted.
 * @returns Package metadata with export subpaths and import specifiers.
 *
 * @example
 * import getPackage from 'kody:@kody/raycast/get-package'
 * const pkg = await getPackage({ kodyId: 'notify' })
 */
export default async function getPackage(
	input: GetPackageInput = {},
): Promise<GetPackageResult> {
	const record = asRecord(input)
	let packageId = optionalString(record, 'packageId')
	const kodyId = optionalString(record, 'kodyId')

	if (!packageId && kodyId) {
		const list = await kody.package_list({})
		packageId = list.packages.find(pkg => pkg.kody_id === kodyId)?.package_id
	}

	if (!packageId) {
		throw new Error('Pass packageId or kodyId.')
	}

	const pkg = await kody.package_get({ package_id: packageId })
	return {
		packageId: pkg.package_id,
		kodyId: pkg.kody_id,
		name: pkg.name,
		description: pkg.description,
		tags: pkg.tags,
		hasApp: pkg.has_app,
		exports: pkg.exports.map(entry => ({
			subpath: entry.subpath,
			exportName: getExportName(entry.subpath),
			importSpecifier: entry.import_specifier,
			runtimeTarget: entry.runtime_target,
			typesPath: entry.types_path,
			description: entry.description,
			typeDefinition: entry.type_definition,
			tokenSetupUrl: entry.external_invocation?.token_setup_url ?? null,
			invocationUrl: entry.external_invocation?.url ?? null,
		})),
	}
}