makenotion / notion-sdk-js

Official Notion JavaScript Client
https://developers.notion.com/docs
MIT License
4.87k stars 576 forks source link

Type checking issue when querying a database: properties field #527

Open RichEwin opened 5 days ago

RichEwin commented 5 days ago

Report bugs here only for the Node JavaScript library.

If you're having problems using Notion's API, or have any other feedback about the API including feature requests for the JavaScript library, please email support at developers@makenotion.com.

Describe the bug When querying a database the response types are suggesting that 'properties' does not exist on 'PartialPageObjectResponse'

Property 'properties' does not exist on type 'PageObjectResponse | PartialPageObjectResponse | PartialDatabaseObjectResponse | DatabaseObjectResponse'.
  Property 'properties' does not exist on type 'PartialPageObjectResponse'
 const response = await notionClient.databases.query({
      database_id: databaseId,
      filter: {
        property: marketCode,
        select: {
          equals: market?.toLowerCase(),
        },
      },
    });

    const results = response.results[0]?.properties;

Below is the response when querying the database; you can see properties exist.

{
  object: 'page',
  id: '10...',
  created_time: '2024...',
  last_edited_time: '2024...',
  created_by: { object: 'user', id: '48e6f...' },
  last_edited_by: { object: 'user', id: '48e6...' },
  cover: null,
  icon: null,
  parent: {
    type: 'database_id',
    database_id: '10d...'
  },
  archived: false,
  in_trash: false,
  properties: {
    'Market...': { id: 'BMOO', type: 'select', select: [Object] },
    'Market...': { id: 'G%3DDn', type: 'select', select: [Object] },
    ...
  },
  url: 'https://www.notion.so/...',
  public_url: null
}

Is there a way to handle this, instead of me manually defining the response which seems unnecessary.

To Reproduce Node version: Notion JS library version:

Steps to reproduce the behavior:

Expected behavior A clear and concise description of what you expected to happen.

Screenshots Please include any screenshots that help explain your problem.

Additional context Add any other context about the problem here.

feliceforgione commented 1 day ago

When querying database using a filter, I receive a "body failed validation" error

APIResponseError: body failed validation. Fix one:
body.filter.or should be defined, instead was `undefined`.
body.filter.and should be defined, instead was `undefined`.
body.filter.title should be defined, instead was `undefined`.
body.filter.rich_text should be defined, instead was `undefined`.
body.filter.number should be defined, instead was `undefined`.
body.filter.checkbox should be defined, instead was `undefined`.
body.filter.select should be defined, instead was `undefined`.
body.filter.multi_select should be defined, instead was `undefined`.
body.filter.status should be defined, instead was `undefined`.
body.filter.date should be defined, instead was `undefined`.
body.filter.people should be defined, instead was `undefined`.
body.filter.files should be defined, instead was `undefined`.
body.filter.url should be defined, instead was `undefined`.
body.filter.email should be defined, instead was `undefined`.
body.filter.phone_number should be defined, instead was `undefined`.
body.filter.relation should be defined, instead was `undefined`.
body.filter.created_by should be defined, instead was `undefined`.
body.filter.created_time should be defined, instead was `undefined`.
body.filter.last_edited_by should be defined, instead was `undefined`.
body.filter.last_edited_time should be defined, instead was `undefined`.
body.filter.formula should be defined, instead was `undefined`.
body.filter.unique_id should be defined, instead was `undefined`.
body.filter.rollup should be defined, instead was `undefined`.
    at async ProjectsList (./src/app/components/ProjectsList.tsx:23:35)
digest: "716658379"
const { results } = await notion.databases.query({
    database_id: databaseId!,
    filter: {
      property: "Tags",
      contains: "AI",
    }
  });
  console.log(results);

Below is the response when querying the database; you can see properties exist.

  [
  {
    object: 'page',
    id: '039c4ca9-e237-481a-9581-',
    created_time: '2024-10-01T20:21:00.000Z',
    last_edited_time: '2024-10-05T00:35:00.000Z',
    created_by: { object: 'user', id: 'f65771fb-5ea1-4e48-b963-' },
    last_edited_by: { object: 'user', id: 'f65771fb-5ea1-4e48-b963-' },
    cover: null,
    icon: null,
    parent: {
      type: 'database_id',
      database_id: '112794ea-af04-8071-8225-'
    },
    archived: false,
    in_trash: false,
    properties: {
      'Current Versions': [Object],
      'Github Repos': [Object],
      Directory: [Object],
      'Main Run Command': [Object],
      'Short Description': [Object],
      Tags: [Object],
      Name: [Object]
    },
    url: 'https://www.notion.so/....',
    public_url: null
  }
]

Node version: 20.17.0 Notion JS library version: v2.2.15

I also have typescript issues when trying to access the custom properties. It seems like the typescript definitions are not accurate when it comes to properties.