makenotion / notion-sdk-js

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

Inconsistent type: Cursor for parameters is string|undefined but for responses is string|null #498

Open jcbages opened 6 months ago

jcbages commented 6 months 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 calling something like database.query that receives a cursor as a parameter, its type is string | undefined as it's start_cursor:?string. However, the returned cursor from the response is of type string | null. This causes issues when looping to obtain multiple pages while reassigning the cursor variable as the initial typing of the cursor variable will conflict with one of these.

I checked the type definitions and this is the case for all calls that receive and return a cursor. This is easy to workaround but it degrades the typing experience a little.

To Reproduce Node version: Notion JS library version: 2.2.14

Steps to reproduce the behavior:

let databaseId = '<some_id>';
let entries = [];
let cursor: string | null = null;
let entries = [];

while (true) {
    const { results, has_more, next_cursor } = await this.notion.databases.query({
        database_id: databaseId,
        start_cursor: cursor
    });

    cursor = next_cursor;
    entries.push(...results);

    if (!has_more) {
        break;
    }
}

return entries;

Expected behavior Expected: No typing issues Got: Type 'string | null' is not assignable to type 'string | undefined'.

If we change the type of the cursor to be string | undefined then the error happens at cursor = next_cursor saying: Type 'string | undefined' is not assignable to type 'string | null'.

Additional context Happy to make a PR for this :)

Sho-ki commented 4 months ago

I made a PR for this issue. https://github.com/makenotion/notion-sdk-js/pull/510