Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Notion Node): Fetch child blocks recursively #7304

Merged
12 changes: 12 additions & 0 deletions packages/nodes-base/nodes/Notion/BlockDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,16 @@ export const blockFields: INodeProperties[] = [
default: 50,
description: 'Max number of results to return',
},
{
displayName: 'Also Fetch Nested Blocks',
name: 'fetchNestedBlocks',
type: 'boolean',
displayOptions: {
show: {
resource: ['block'],
operation: ['getAll'],
},
},
default: false,
},
];
43 changes: 42 additions & 1 deletion packages/nodes-base/nodes/Notion/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export async function notionApiRequestAllItems(
propertyName: string,
method: string,
endpoint: string,

body: any = {},
query: IDataObject = {},
): Promise<any> {
Expand Down Expand Up @@ -109,6 +108,48 @@ export async function notionApiRequestAllItems(
return returnData;
}

export async function notionApiRequestGetBlockChildrens(
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
blocks: IDataObject[],
responseData: IDataObject[] = [],
limit?: number,
) {
if (blocks.length === 0) return responseData;

for (const block of blocks) {
responseData.push(block);

if (block.type === 'child_page') continue;

if (block.has_children) {
let childrens = await notionApiRequestAllItems.call(
this,
'results',
'GET',
`/blocks/${block.id}/children`,
);

childrens = (childrens || []).map((entry: IDataObject) => ({
object: entry.object,
parent_id: block.id,
...entry,
}));

await notionApiRequestGetBlockChildrens.call(this, childrens, responseData);
}

if (limit && responseData.length === limit) {
return responseData;
}

if (limit && responseData.length > limit) {
return responseData.slice(0, limit);
}
}

return responseData;
}

export function getBlockTypes() {
return [
{
Expand Down
17 changes: 15 additions & 2 deletions packages/nodes-base/nodes/Notion/v2/NotionV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
mapSorting,
notionApiRequest,
notionApiRequestAllItems,
notionApiRequestGetBlockChildrens,
simplifyObjects,
validateJSON,
} from '../GenericFunctions';
Expand Down Expand Up @@ -273,6 +274,7 @@ export class NotionV2 implements INodeType {
this.getNodeParameter('blockId', i, '', { extractValue: true }) as string,
);
const returnAll = this.getNodeParameter('returnAll', i);
const fetchNestedBlocks = this.getNodeParameter('fetchNestedBlocks', i) as boolean;

if (returnAll) {
responseData = await notionApiRequestAllItems.call(
Expand All @@ -282,16 +284,27 @@ export class NotionV2 implements INodeType {
`/blocks/${blockId}/children`,
{},
);

if (fetchNestedBlocks) {
responseData = await notionApiRequestGetBlockChildrens.call(this, responseData);
}
} else {
qs.page_size = this.getNodeParameter('limit', i);
const limit = this.getNodeParameter('limit', i);
qs.page_size = limit;
responseData = await notionApiRequest.call(
this,
'GET',
`/blocks/${blockId}/children`,
{},
qs,
);
responseData = responseData.results;
const results = responseData.results;

if (fetchNestedBlocks) {
responseData = await notionApiRequestGetBlockChildrens.call(this, results, [], limit);
} else {
responseData = results;
}
}

responseData = responseData.map((_data: IDataObject) => ({
Expand Down
Loading