Skip to content
← Public packages

@kody/integrations-sh

Client for the public integrations.sh registry: search, detect, cached surface, and live discover.

src/surface.ts

54 lines · 1.6 KB · TypeScript
import { fetchRegistryJson } from './client.ts'
import { normalizeProviderDomain } from './domain.ts'
import { mapSurfaceDocument, type SurfaceDocument } from './types.ts'
import { buildSurfaceUrl } from './urls.ts'

const SURFACE_FETCH_TIMEOUT_MS = 15_000

export type SurfaceInput = {
	/** Bare provider hostname (for example github.com). */
	domain: string
}

export type SurfaceOutput = SurfaceDocument & {
	source: string
	provenance: 'cached'
}

/**
 * Load the cached integrations.sh surface document for a domain.
 * Use this when you only want stored/bundled data, not a live discover run.
 *
 * @param input - Bare provider hostname
 * @returns Untrusted credentials and surfaces from the registry cache
 *
 * @example
 * import surface from 'kody:@<username>/integrations-sh/surface'
 * const doc = await surface({ domain: 'github.com' })
 */
export default async function surface(
	input: SurfaceInput,
): Promise<SurfaceOutput> {
	const domain = normalizeProviderDomain(input.domain)
	const source = buildSurfaceUrl(domain)
	const fetched = await fetchRegistryJson(source, SURFACE_FETCH_TIMEOUT_MS)
	if (fetched.outcome === 'not-found') {
		throw new Error(
			`Domain "${domain}" has no stored integrations.sh surface. Try search, then discover.`,
		)
	}
	if (fetched.outcome !== 'success') {
		throw new Error(`integrations.sh surface failed: ${fetched.message}`)
	}
	const document = mapSurfaceDocument(fetched.json)
	if (document == null) {
		throw new Error(
			'integrations.sh surface failed: unexpected response shape',
		)
	}
	return {
		...document,
		source,
		provenance: 'cached',
	}
}