Skip to content
← Public packages

@kody/notion

Search, read, query, and safely write Notion pages and databases through the saved notion OAuth integration.

src/get-block-children.ts

31 lines · 927 B · 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'),
      },
    },
  )

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