Skip to content
← Public packages

@cameronpak/raycast-kodys-pouch

Your Kody Skills and Tools, one command away.

src/get-package.ts

70 lines · 1.7 KB · TypeScript
import { kody } from 'kody:runtime'

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
		typesPath: string | null
		description: string
		typeDefinition: string | null
	}>
}

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

/**
 * 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.
 */
export default async function getPackage(
	input: GetPackageInput = {},
): Promise<GetPackageResult> {
	let packageId = input.packageId

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

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

	const pkg = await kody.packageGet({ 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,
		})),
	}
}