import { planetscaleRequest } from './client.ts' import { mapBranch } from './models.ts' import type { PlanetscaleAuthInput, PlanetscaleBranchSummary, PlanetscalePageInfo } from './types.ts' import { clampInt, compactRecord, optionalString, requireRecord, requireString } from './types.ts' export type ListBranchesInput = PlanetscaleAuthInput & { organization: string database: string page?: number perPage?: number } export type ListBranchesResult = { items: Array pageInfo: PlanetscalePageInfo } /** * List branches for a PlanetScale database. * @example * import listBranches from 'kody:@kody/planetscale/list-branches' * const { items } = await listBranches({ organization: 'acme', database: 'app' }) */ export async function listBranches(input: ListBranchesInput): Promise { const organization = requireString(input.organization, 'organization') const database = requireString(input.database, 'database') const result = await planetscaleRequest>({ ...input, operation: 'branches.read', path: `/organizations/${encodeURIComponent(organization)}/databases/${encodeURIComponent(database)}/branches`, query: compactRecord({ page: clampInt(input.page, 1, 100, 1, 'page'), per_page: clampInt(input.perPage, 1, 100, 25), }), }) return { items: (Array.isArray(result.data) ? result.data : []).map(mapBranch), pageInfo: result.pageInfo, } } export default async function listBranchesEntrypoint( params: Partial & Record = {}, ) { const input = requireRecord(params, 'list-branches') return listBranches({ organization: requireString(input.organization, 'organization'), database: requireString(input.database, 'database'), page: input.page as number | undefined, perPage: input.perPage as number | undefined, account: optionalString(input.account, 'account'), secretName: optionalString(input.secretName, 'secretName'), }) }