import { pickAuthInput } from './auth.ts' import { airtableRequest } from './client.ts' import { mapComment } from './models.ts' import { recordsPath } from './setup.ts' import type { AirtableAuthInput, AirtableCommentSummary, DryRunResult, JsonRecord, } from './types.ts' import { optionalBoolean, requireConfirmOrDryRun, requireRecord, requireString, } from './types.ts' export type CreateCommentInput = AirtableAuthInput & { baseId: string tableIdOrName: string recordId: string text: string confirm?: boolean dryRun?: boolean } function commentPayload(input: CreateCommentInput): { path: string body: JsonRecord } { return { path: recordsPath( requireString(input.baseId, 'baseId'), requireString(input.tableIdOrName, 'tableIdOrName'), requireString(input.recordId, 'recordId'), 'comments', ), body: { text: requireString(input.text, 'text') }, } } /** * Create an Airtable record comment. Requires `confirm: true`, or use * `dryRun: true`. Needs `data.recordComments:write`. * @example * import createComment from 'kody:@kody/airtable/create-comment' * const preview = await createComment({ * baseId: 'appXXXXXXXXXXXXXX', * tableIdOrName: 'tblXXXXXXXXXXXXXX', * recordId: 'recXXXXXXXXXXXXXX', * text: 'Shipped in #482.', * dryRun: true, * }) */ export async function createComment( input: CreateCommentInput, ): Promise< AirtableCommentSummary | DryRunResult<{ method: string; path: string; body: JsonRecord }> > { const payload = commentPayload(input) if (requireConfirmOrDryRun(input, 'Creating an Airtable comment') === 'dryRun') { return { dryRun: true, wouldCall: { method: 'POST', ...payload } } } const result = await airtableRequest({ ...input, operation: 'comments.write', method: 'POST', path: payload.path, body: payload.body, }) if (!result.data) throw new Error('Airtable create comment did not return a comment.') return mapComment(result.data) } /** * Create an Airtable record comment. Requires `confirm: true`, or use * `dryRun: true`. * @example * import createComment from 'kody:@kody/airtable/create-comment' * const preview = await createComment({ * baseId: 'appXXXXXXXXXXXXXX', * tableIdOrName: 'Tasks', * recordId: 'recXXXXXXXXXXXXXX', * text: 'Noted', * dryRun: true, * }) */ export default async function createCommentEntrypoint( params: Partial & Record = {}, ) { const input = requireRecord(params, 'create-comment') return createComment({ baseId: requireString(input.baseId, 'baseId'), tableIdOrName: requireString(input.tableIdOrName, 'tableIdOrName'), recordId: requireString(input.recordId, 'recordId'), text: requireString(input.text, 'text'), ...pickAuthInput(input), confirm: optionalBoolean(input.confirm, 'confirm'), dryRun: optionalBoolean(input.dryRun, 'dryRun'), }) }