import { pickAuthInput } from './auth.ts'
import { listBases } from './list-bases.ts'
import type { AirtableAuthInput, AirtableBaseSummary } from './types.ts'
import { requireRecord, requireString } from './types.ts'
export type GetBaseInput = AirtableAuthInput & {
/** Base id (`app…`). */
baseId: string
}
const MAX_BASE_PAGES = 20
/**
* Get one Airtable base the token can access by id. Needs `schema.bases:read`.
* @example
* import getBase from 'kody:@kody/airtable/get-base'
* const base = await getBase({ baseId: 'appXXXXXXXXXXXXXX' })
*/
export async function getBase(input: GetBaseInput): Promise<AirtableBaseSummary> {
const baseId = requireString(input.baseId, 'baseId')
let offset: string | undefined
for (let page = 0; page < MAX_BASE_PAGES; page += 1) {
const result = await listBases({ ...input, offset })
const match = result.items.find((item) => item.id === baseId)
if (match) return match
if (!result.pageInfo.nextOffset) break
offset = result.pageInfo.nextOffset
}
throw new Error(
`Airtable base not found for this token: ${baseId}. Confirm the base is granted to the PAT or OAuth connection and that schema.bases:read is included.`,
)
}
/**
* Get one Airtable base the token can access by id.
* @example
* import getBase from 'kody:@kody/airtable/get-base'
* const base = await getBase({ baseId: 'appXXXXXXXXXXXXXX' })
*/
export default async function getBaseEntrypoint(
params: Partial<GetBaseInput> & Record<string, unknown> = {},
) {
const input = requireRecord(params, 'get-base')
return getBase({
baseId: requireString(input.baseId, 'baseId'),
...pickAuthInput(input),
})
}