1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-09-20 17:37:25 +03:00

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

Github issue / Community forum post (link here to close automatically):
This commit is contained in:
Michael Kret 2023-10-06 14:55:44 +03:00 committed by GitHub
parent 97bb703d0a
commit 193181a9c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 3 deletions

View File

@ -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,
},
];

View File

@ -81,7 +81,6 @@ export async function notionApiRequestAllItems(
propertyName: string,
method: string,
endpoint: string,
body: any = {},
query: IDataObject = {},
): Promise<any> {
@ -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 [
{

View File

@ -24,6 +24,7 @@ import {
mapSorting,
notionApiRequest,
notionApiRequestAllItems,
notionApiRequestGetBlockChildrens,
simplifyObjects,
validateJSON,
} from '../GenericFunctions';
@ -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(
@ -282,8 +284,13 @@ 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',
@ -291,7 +298,13 @@ export class NotionV2 implements INodeType {
{},
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) => ({