notion-sdk-js icon indicating copy to clipboard operation
notion-sdk-js copied to clipboard

Easily get page prop value?

Open silveltman opened this issue 1 year ago • 1 comments

I find myself having a difficult time getting the values of a page property. Especially with rollups. Are there ways to make this easier?

To give a idea of what I mean:

function getValue(prop) {
  const value = prop[prop.type]

  switch (prop.type) {
    case 'title':
      return text(value)
    case 'rich_text':
      return text(value)
    case 'rollup':
      return rollup(value)
    case 'files':
      return files(value)
    case 'array':
      return array(value)
    case 'multiselect':
      return multiselect(value)
    case 'relation':
      return relation(value)
    default:
      return value
  }
}

// Title and Rich Text
const text = (value) => value[0].plain_text

// Rollup
const rollup = (value) => getValue(value)

// Files
const files = (value) => value.map((item) => getValue(item).url)

// Array
const array = (value) => value.map((item) => getValue(item))

// Multiselect
const multiselect = (value) => value.map((item) => item.name)

// Relation
const relation = (value) => value.map((item) => item.id)

export { getValue }

silveltman avatar Mar 06 '23 17:03 silveltman

Please see my comment here, it might be useful. https://github.com/makenotion/notion-sdk-js/issues/391#issuecomment-1474119103

janwirth avatar Mar 17 '23 16:03 janwirth