Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kentcdodds/notion

src/get-block-children.ts

33 lines · 1.0 KB · TypeScript
import { nextCursor, notionRequest } from './request.ts'
import { boundedPageSize, inputRecord, optionalString, requiredString } from './validation.ts'

type BlockChildrenResponse = {
  results?: unknown[]
  has_more?: boolean
  next_cursor?: string | null
  [key: string]: unknown
}

/** Read the child blocks of a Notion page or block (page content). */
export default async function getBlockChildren(params: Record<string, unknown>) {
  const input = inputRecord(params)
  const blockId = requiredString(input, 'blockId')

  const result = await notionRequest<BlockChildrenResponse>(
    '/blocks/' + encodeURIComponent(blockId) + '/children',
    {
      query: {
        page_size: boundedPageSize(input.pageSize),
        start_cursor: optionalString(input, 'startCursor'),
      },
      account: optionalString(input, 'account'),
      integration: optionalString(input, 'integration'),
    },
  )

  return {
    blocks: result.results ?? [],
    hasMore: result.has_more === true,
    nextCursor: nextCursor(result),
  }
}