n8n-nodes-notion-advanced 1.2.3-beta → 1.2.5-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,905 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotionAdvanced = void 0;
4
+ const n8n_workflow_1 = require("n8n-workflow");
5
+ const NotionUtils_1 = require("./NotionUtils");
6
+ // Ensure all block creation functions are available for dynamic usage
7
+ const blockCreators = {
8
+ createParagraphBlock: NotionUtils_1.createParagraphBlock,
9
+ createHeadingBlock: NotionUtils_1.createHeadingBlock,
10
+ createListItemBlock: NotionUtils_1.createListItemBlock,
11
+ createToDoBlock: NotionUtils_1.createToDoBlock,
12
+ createCodeBlock: NotionUtils_1.createCodeBlock,
13
+ createQuoteBlock: NotionUtils_1.createQuoteBlock,
14
+ createCalloutBlock: NotionUtils_1.createCalloutBlock,
15
+ createDividerBlock: NotionUtils_1.createDividerBlock,
16
+ createEquationBlock: NotionUtils_1.createEquationBlock,
17
+ createImageBlock: NotionUtils_1.createImageBlock,
18
+ createBookmarkBlock: NotionUtils_1.createBookmarkBlock,
19
+ createEmbedBlock: NotionUtils_1.createEmbedBlock,
20
+ createTableBlock: NotionUtils_1.createTableBlock,
21
+ };
22
+ // Prevent unused variable warning
23
+ void blockCreators;
24
+ class NotionAdvanced {
25
+ constructor() {
26
+ this.description = {
27
+ displayName: 'Notion Advanced',
28
+ name: 'notionAdvanced',
29
+ icon: 'file:notion.svg',
30
+ group: ['productivity'],
31
+ version: 1,
32
+ subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
33
+ description: 'Full-featured Notion node with exhaustive block and formatting support using existing credentials',
34
+ defaults: {
35
+ name: 'Notion Advanced',
36
+ },
37
+ inputs: ['main'],
38
+ outputs: ['main'],
39
+ credentials: [
40
+ {
41
+ name: 'notionApi',
42
+ required: true,
43
+ },
44
+ ],
45
+ properties: [
46
+ {
47
+ displayName: 'Resource',
48
+ name: 'resource',
49
+ type: 'options',
50
+ noDataExpression: true,
51
+ options: [
52
+ {
53
+ name: 'Page',
54
+ value: 'page',
55
+ },
56
+ {
57
+ name: 'Block',
58
+ value: 'block',
59
+ },
60
+ {
61
+ name: 'Database',
62
+ value: 'database',
63
+ },
64
+ {
65
+ name: 'User',
66
+ value: 'user',
67
+ },
68
+ ],
69
+ default: 'page',
70
+ },
71
+ // PAGE OPERATIONS
72
+ {
73
+ displayName: 'Operation',
74
+ name: 'operation',
75
+ type: 'options',
76
+ noDataExpression: true,
77
+ displayOptions: {
78
+ show: {
79
+ resource: ['page'],
80
+ },
81
+ },
82
+ options: [
83
+ {
84
+ name: 'Create',
85
+ value: 'create',
86
+ description: 'Create a new page',
87
+ action: 'Create a page',
88
+ },
89
+ {
90
+ name: 'Get',
91
+ value: 'get',
92
+ description: 'Get a page',
93
+ action: 'Get a page',
94
+ },
95
+ {
96
+ name: 'Update',
97
+ value: 'update',
98
+ description: 'Update page properties',
99
+ action: 'Update a page',
100
+ },
101
+ {
102
+ name: 'Archive',
103
+ value: 'archive',
104
+ description: 'Archive a page',
105
+ action: 'Archive a page',
106
+ },
107
+ {
108
+ name: 'Search',
109
+ value: 'search',
110
+ description: 'Search pages',
111
+ action: 'Search pages',
112
+ },
113
+ ],
114
+ default: 'create',
115
+ },
116
+ // BLOCK OPERATIONS
117
+ {
118
+ displayName: 'Operation',
119
+ name: 'operation',
120
+ type: 'options',
121
+ noDataExpression: true,
122
+ displayOptions: {
123
+ show: {
124
+ resource: ['block'],
125
+ },
126
+ },
127
+ options: [
128
+ {
129
+ name: 'Create',
130
+ value: 'create',
131
+ description: 'Create new blocks',
132
+ action: 'Create blocks',
133
+ },
134
+ {
135
+ name: 'Get',
136
+ value: 'get',
137
+ description: 'Get a block',
138
+ action: 'Get a block',
139
+ },
140
+ {
141
+ name: 'Update',
142
+ value: 'update',
143
+ description: 'Update a block',
144
+ action: 'Update a block',
145
+ },
146
+ {
147
+ name: 'Delete',
148
+ value: 'delete',
149
+ description: 'Delete a block',
150
+ action: 'Delete a block',
151
+ },
152
+ {
153
+ name: 'Get Children',
154
+ value: 'getChildren',
155
+ description: 'Get child blocks',
156
+ action: 'Get child blocks',
157
+ },
158
+ {
159
+ name: 'Append Children',
160
+ value: 'appendChildren',
161
+ description: 'Append children to a block',
162
+ action: 'Append children to a block',
163
+ },
164
+ ],
165
+ default: 'create',
166
+ },
167
+ // DATABASE OPERATIONS
168
+ {
169
+ displayName: 'Operation',
170
+ name: 'operation',
171
+ type: 'options',
172
+ noDataExpression: true,
173
+ displayOptions: {
174
+ show: {
175
+ resource: ['database'],
176
+ },
177
+ },
178
+ options: [
179
+ {
180
+ name: 'Get',
181
+ value: 'get',
182
+ description: 'Get a database',
183
+ action: 'Get a database',
184
+ },
185
+ {
186
+ name: 'Query',
187
+ value: 'query',
188
+ description: 'Query a database',
189
+ action: 'Query a database',
190
+ },
191
+ {
192
+ name: 'Create',
193
+ value: 'create',
194
+ description: 'Create a database',
195
+ action: 'Create a database',
196
+ },
197
+ ],
198
+ default: 'get',
199
+ },
200
+ // USER OPERATIONS
201
+ {
202
+ displayName: 'Operation',
203
+ name: 'operation',
204
+ type: 'options',
205
+ noDataExpression: true,
206
+ displayOptions: {
207
+ show: {
208
+ resource: ['user'],
209
+ },
210
+ },
211
+ options: [
212
+ {
213
+ name: 'Get',
214
+ value: 'get',
215
+ description: 'Get a user',
216
+ action: 'Get a user',
217
+ },
218
+ {
219
+ name: 'List',
220
+ value: 'list',
221
+ description: 'List all users',
222
+ action: 'List users',
223
+ },
224
+ ],
225
+ default: 'get',
226
+ },
227
+ // PAGE FIELDS
228
+ {
229
+ displayName: 'Parent Page/Database',
230
+ name: 'parent',
231
+ type: 'string',
232
+ required: true,
233
+ displayOptions: {
234
+ show: {
235
+ resource: ['page'],
236
+ operation: ['create'],
237
+ },
238
+ },
239
+ default: '',
240
+ description: 'Page ID, Database ID, or search term for the parent',
241
+ },
242
+ {
243
+ displayName: 'Page ID',
244
+ name: 'pageId',
245
+ type: 'string',
246
+ required: true,
247
+ displayOptions: {
248
+ show: {
249
+ resource: ['page'],
250
+ operation: ['get', 'update', 'archive'],
251
+ },
252
+ },
253
+ default: '',
254
+ description: 'Page ID or URL',
255
+ },
256
+ {
257
+ displayName: 'Title',
258
+ name: 'title',
259
+ type: 'string',
260
+ required: true,
261
+ displayOptions: {
262
+ show: {
263
+ resource: ['page'],
264
+ operation: ['create'],
265
+ },
266
+ },
267
+ default: '',
268
+ description: 'Page title',
269
+ },
270
+ {
271
+ displayName: 'Properties',
272
+ name: 'properties',
273
+ type: 'fixedCollection',
274
+ typeOptions: {
275
+ multipleValues: true,
276
+ },
277
+ displayOptions: {
278
+ show: {
279
+ resource: ['page'],
280
+ operation: ['create', 'update'],
281
+ },
282
+ },
283
+ default: {},
284
+ options: [
285
+ {
286
+ name: 'property',
287
+ displayName: 'Property',
288
+ values: [
289
+ {
290
+ displayName: 'Name',
291
+ name: 'name',
292
+ type: 'string',
293
+ default: '',
294
+ description: 'Property name',
295
+ },
296
+ {
297
+ displayName: 'Type',
298
+ name: 'type',
299
+ type: 'options',
300
+ options: [
301
+ { name: 'Title', value: 'title' },
302
+ { name: 'Rich Text', value: 'rich_text' },
303
+ { name: 'Number', value: 'number' },
304
+ { name: 'Select', value: 'select' },
305
+ { name: 'Multi-select', value: 'multi_select' },
306
+ { name: 'Date', value: 'date' },
307
+ { name: 'People', value: 'people' },
308
+ { name: 'Files', value: 'files' },
309
+ { name: 'Checkbox', value: 'checkbox' },
310
+ { name: 'URL', value: 'url' },
311
+ { name: 'Email', value: 'email' },
312
+ { name: 'Phone', value: 'phone_number' },
313
+ { name: 'Formula', value: 'formula' },
314
+ { name: 'Relation', value: 'relation' },
315
+ { name: 'Rollup', value: 'rollup' },
316
+ { name: 'Created Time', value: 'created_time' },
317
+ { name: 'Created By', value: 'created_by' },
318
+ { name: 'Last Edited Time', value: 'last_edited_time' },
319
+ { name: 'Last Edited By', value: 'last_edited_by' },
320
+ ],
321
+ default: 'rich_text',
322
+ },
323
+ {
324
+ displayName: 'Value',
325
+ name: 'value',
326
+ type: 'string',
327
+ default: '',
328
+ description: 'Property value (JSON for complex types)',
329
+ },
330
+ ],
331
+ },
332
+ ],
333
+ },
334
+ // BLOCK FIELDS
335
+ {
336
+ displayName: 'Block ID',
337
+ name: 'blockId',
338
+ type: 'string',
339
+ required: true,
340
+ displayOptions: {
341
+ show: {
342
+ resource: ['block'],
343
+ operation: ['get', 'update', 'delete', 'getChildren', 'appendChildren'],
344
+ },
345
+ },
346
+ default: '',
347
+ description: 'Block ID',
348
+ },
349
+ {
350
+ displayName: 'Parent ID',
351
+ name: 'parentId',
352
+ type: 'string',
353
+ required: true,
354
+ displayOptions: {
355
+ show: {
356
+ resource: ['block'],
357
+ operation: ['create'],
358
+ },
359
+ },
360
+ default: '',
361
+ description: 'Parent page or block ID',
362
+ },
363
+ {
364
+ displayName: 'Blocks',
365
+ name: 'blocks',
366
+ type: 'fixedCollection',
367
+ typeOptions: {
368
+ multipleValues: true,
369
+ },
370
+ displayOptions: {
371
+ show: {
372
+ resource: ['block'],
373
+ operation: ['create', 'appendChildren'],
374
+ },
375
+ },
376
+ default: {},
377
+ options: [
378
+ {
379
+ name: 'block',
380
+ displayName: 'Block',
381
+ values: [
382
+ {
383
+ displayName: 'Type',
384
+ name: 'type',
385
+ type: 'options',
386
+ options: [
387
+ { name: 'Paragraph', value: 'paragraph' },
388
+ { name: 'Heading 1', value: 'heading_1' },
389
+ { name: 'Heading 2', value: 'heading_2' },
390
+ { name: 'Heading 3', value: 'heading_3' },
391
+ { name: 'Bulleted List Item', value: 'bulleted_list_item' },
392
+ { name: 'Numbered List Item', value: 'numbered_list_item' },
393
+ { name: 'To Do', value: 'to_do' },
394
+ { name: 'Toggle', value: 'toggle' },
395
+ { name: 'Quote', value: 'quote' },
396
+ { name: 'Callout', value: 'callout' },
397
+ { name: 'Code', value: 'code' },
398
+ { name: 'Divider', value: 'divider' },
399
+ { name: 'Image', value: 'image' },
400
+ { name: 'Video', value: 'video' },
401
+ { name: 'Audio', value: 'audio' },
402
+ { name: 'File', value: 'file' },
403
+ { name: 'PDF', value: 'pdf' },
404
+ { name: 'Bookmark', value: 'bookmark' },
405
+ { name: 'Embed', value: 'embed' },
406
+ { name: 'Link Preview', value: 'link_preview' },
407
+ { name: 'Equation', value: 'equation' },
408
+ { name: 'Table', value: 'table' },
409
+ { name: 'Column List', value: 'column_list' },
410
+ { name: 'Synced Block', value: 'synced_block' },
411
+ { name: 'Template', value: 'template' },
412
+ { name: 'Table of Contents', value: 'table_of_contents' },
413
+ ],
414
+ default: 'paragraph',
415
+ },
416
+ {
417
+ displayName: 'Content',
418
+ name: 'content',
419
+ type: 'string',
420
+ default: '',
421
+ description: 'Block content (text, code, URL, etc.)',
422
+ },
423
+ {
424
+ displayName: 'Rich Text (JSON)',
425
+ name: 'richText',
426
+ type: 'string',
427
+ default: '',
428
+ description: 'Rich text as JSON array with formatting',
429
+ },
430
+ {
431
+ displayName: 'Properties (JSON)',
432
+ name: 'properties',
433
+ type: 'string',
434
+ default: '{}',
435
+ description: 'Block properties as JSON (color, checked, language, etc.)',
436
+ },
437
+ {
438
+ displayName: 'Children (JSON)',
439
+ name: 'children',
440
+ type: 'string',
441
+ default: '',
442
+ description: 'Child blocks as JSON array',
443
+ },
444
+ ],
445
+ },
446
+ ],
447
+ },
448
+ // SEARCH FIELDS
449
+ {
450
+ displayName: 'Search Query',
451
+ name: 'query',
452
+ type: 'string',
453
+ displayOptions: {
454
+ show: {
455
+ resource: ['page'],
456
+ operation: ['search'],
457
+ },
458
+ },
459
+ default: '',
460
+ description: 'Search query',
461
+ },
462
+ {
463
+ displayName: 'Filter',
464
+ name: 'filter',
465
+ type: 'string',
466
+ displayOptions: {
467
+ show: {
468
+ resource: ['page'],
469
+ operation: ['search'],
470
+ },
471
+ },
472
+ default: '',
473
+ description: 'Search filter as JSON',
474
+ },
475
+ // DATABASE FIELDS
476
+ {
477
+ displayName: 'Database ID',
478
+ name: 'databaseId',
479
+ type: 'string',
480
+ required: true,
481
+ displayOptions: {
482
+ show: {
483
+ resource: ['database'],
484
+ operation: ['get', 'query'],
485
+ },
486
+ },
487
+ default: '',
488
+ description: 'Database ID',
489
+ },
490
+ // USER FIELDS
491
+ {
492
+ displayName: 'User ID',
493
+ name: 'userId',
494
+ type: 'string',
495
+ required: true,
496
+ displayOptions: {
497
+ show: {
498
+ resource: ['user'],
499
+ operation: ['get'],
500
+ },
501
+ },
502
+ default: '',
503
+ description: 'User ID',
504
+ },
505
+ // COMMON OPTIONS
506
+ {
507
+ displayName: 'Additional Fields',
508
+ name: 'additionalFields',
509
+ type: 'collection',
510
+ placeholder: 'Add Field',
511
+ default: {},
512
+ options: [
513
+ {
514
+ displayName: 'Icon',
515
+ name: 'icon',
516
+ type: 'string',
517
+ default: '',
518
+ description: 'Page icon (emoji or image URL)',
519
+ },
520
+ {
521
+ displayName: 'Cover',
522
+ name: 'cover',
523
+ type: 'string',
524
+ default: '',
525
+ description: 'Page cover image URL',
526
+ },
527
+ {
528
+ displayName: 'Archive',
529
+ name: 'archived',
530
+ type: 'boolean',
531
+ default: false,
532
+ description: 'Whether to archive the page',
533
+ },
534
+ ],
535
+ },
536
+ ],
537
+ };
538
+ }
539
+ async execute() {
540
+ const items = this.getInputData();
541
+ let responseData = [];
542
+ // Validate credentials first
543
+ const isValid = await NotionUtils_1.validateCredentials.call(this);
544
+ if (!isValid) {
545
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid Notion API credentials');
546
+ }
547
+ for (let i = 0; i < items.length; i++) {
548
+ try {
549
+ const resource = this.getNodeParameter('resource', i);
550
+ const operation = this.getNodeParameter('operation', i);
551
+ let item;
552
+ const nodeInstance = this.constructor.prototype;
553
+ if (resource === 'page') {
554
+ item = await nodeInstance.executePage.call(nodeInstance, this, operation, i);
555
+ }
556
+ else if (resource === 'block') {
557
+ item = await nodeInstance.executeBlock.call(nodeInstance, this, operation, i);
558
+ }
559
+ else if (resource === 'database') {
560
+ item = await nodeInstance.executeDatabase.call(nodeInstance, this, operation, i);
561
+ }
562
+ else if (resource === 'user') {
563
+ item = await nodeInstance.executeUser.call(nodeInstance, this, operation, i);
564
+ }
565
+ else {
566
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown resource: ${resource}`);
567
+ }
568
+ responseData.push(item);
569
+ }
570
+ catch (error) {
571
+ if (this.continueOnFail()) {
572
+ responseData.push({ error: error.message });
573
+ }
574
+ else {
575
+ throw error;
576
+ }
577
+ }
578
+ }
579
+ // Convert to proper execution data format
580
+ const executionData = (0, NotionUtils_1.createExecutionData)(responseData);
581
+ return [executionData];
582
+ }
583
+ async executePage(executeFunctions, operation, itemIndex) {
584
+ switch (operation) {
585
+ case 'create':
586
+ return this.createPage(executeFunctions, itemIndex);
587
+ case 'get':
588
+ return this.getPage(executeFunctions, itemIndex);
589
+ case 'update':
590
+ return this.updatePage(executeFunctions, itemIndex);
591
+ case 'archive':
592
+ return this.archivePage(executeFunctions, itemIndex);
593
+ case 'search':
594
+ return this.searchPages(executeFunctions, itemIndex);
595
+ default:
596
+ throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Unknown page operation: ${operation}`);
597
+ }
598
+ }
599
+ async executeBlock(executeFunctions, operation, itemIndex) {
600
+ switch (operation) {
601
+ case 'create':
602
+ return this.createBlocks(executeFunctions, itemIndex);
603
+ case 'get':
604
+ return this.getBlock(executeFunctions, itemIndex);
605
+ case 'update':
606
+ return this.updateBlock(executeFunctions, itemIndex);
607
+ case 'delete':
608
+ return this.deleteBlock(executeFunctions, itemIndex);
609
+ case 'getChildren':
610
+ return this.getBlockChildren(executeFunctions, itemIndex);
611
+ case 'appendChildren':
612
+ return this.appendBlockChildren(executeFunctions, itemIndex);
613
+ default:
614
+ throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Unknown block operation: ${operation}`);
615
+ }
616
+ }
617
+ async executeDatabase(executeFunctions, operation, itemIndex) {
618
+ switch (operation) {
619
+ case 'get':
620
+ return this.getDatabase(executeFunctions, itemIndex);
621
+ case 'query':
622
+ return this.queryDatabase(executeFunctions, itemIndex);
623
+ case 'create':
624
+ return this.createDatabase(executeFunctions, itemIndex);
625
+ default:
626
+ throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Unknown database operation: ${operation}`);
627
+ }
628
+ }
629
+ async executeUser(executeFunctions, operation, itemIndex) {
630
+ switch (operation) {
631
+ case 'get':
632
+ return this.getUser(executeFunctions, itemIndex);
633
+ case 'list':
634
+ return this.listUsers(executeFunctions, itemIndex);
635
+ default:
636
+ throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Unknown user operation: ${operation}`);
637
+ }
638
+ }
639
+ // PAGE OPERATIONS
640
+ async createPage(executeFunctions, itemIndex) {
641
+ const parent = executeFunctions.getNodeParameter('parent', itemIndex);
642
+ const title = executeFunctions.getNodeParameter('title', itemIndex);
643
+ const properties = executeFunctions.getNodeParameter('properties', itemIndex, {});
644
+ const additionalFields = executeFunctions.getNodeParameter('additionalFields', itemIndex, {});
645
+ // Create structured page input for validation
646
+ const pageInput = (0, NotionUtils_1.createPageInput)(title, parent, properties, undefined, additionalFields.icon, additionalFields.cover);
647
+ const parentId = await NotionUtils_1.resolvePageId.call(executeFunctions, pageInput.parent);
648
+ const body = {
649
+ parent: { page_id: parentId },
650
+ properties: {
651
+ title: {
652
+ title: [(0, NotionUtils_1.createRichText)(pageInput.title)],
653
+ },
654
+ },
655
+ };
656
+ // Add additional properties
657
+ if (properties.property && Array.isArray(properties.property)) {
658
+ for (const prop of properties.property) {
659
+ const { name, type, value } = prop;
660
+ try {
661
+ const parsedValue = JSON.parse(value);
662
+ body.properties[name] = { [type]: parsedValue };
663
+ }
664
+ catch {
665
+ if (type === 'rich_text') {
666
+ body.properties[name] = { rich_text: [(0, NotionUtils_1.createRichText)(value)] };
667
+ }
668
+ else {
669
+ body.properties[name] = { [type]: value };
670
+ }
671
+ }
672
+ }
673
+ }
674
+ // Add icon and cover if provided
675
+ if (additionalFields.icon) {
676
+ body.icon = { type: 'emoji', emoji: additionalFields.icon };
677
+ }
678
+ if (additionalFields.cover) {
679
+ body.cover = { type: 'external', external: { url: additionalFields.cover } };
680
+ }
681
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'POST', '/pages', body);
682
+ }
683
+ async getPage(executeFunctions, itemIndex) {
684
+ const pageId = executeFunctions.getNodeParameter('pageId', itemIndex);
685
+ const resolvedId = await NotionUtils_1.resolvePageId.call(executeFunctions, pageId);
686
+ const page = await NotionUtils_1.notionApiRequest.call(executeFunctions, 'GET', `/pages/${resolvedId}`);
687
+ return page;
688
+ }
689
+ async updatePage(executeFunctions, itemIndex) {
690
+ const pageId = executeFunctions.getNodeParameter('pageId', itemIndex);
691
+ const properties = executeFunctions.getNodeParameter('properties', itemIndex, {});
692
+ const additionalFields = executeFunctions.getNodeParameter('additionalFields', itemIndex, {});
693
+ const resolvedId = await NotionUtils_1.resolvePageId.call(executeFunctions, pageId);
694
+ const body = { properties: {} };
695
+ // Add properties
696
+ if (properties.property && Array.isArray(properties.property)) {
697
+ for (const prop of properties.property) {
698
+ const { name, type, value } = prop;
699
+ try {
700
+ const parsedValue = JSON.parse(value);
701
+ body.properties[name] = { [type]: parsedValue };
702
+ }
703
+ catch {
704
+ if (type === 'rich_text') {
705
+ body.properties[name] = { rich_text: [(0, NotionUtils_1.createRichText)(value)] };
706
+ }
707
+ else {
708
+ body.properties[name] = { [type]: value };
709
+ }
710
+ }
711
+ }
712
+ }
713
+ // Add archived flag if provided
714
+ if (additionalFields.archived !== undefined) {
715
+ body.archived = additionalFields.archived;
716
+ }
717
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'PATCH', `/pages/${resolvedId}`, body);
718
+ }
719
+ async archivePage(executeFunctions, itemIndex) {
720
+ const pageId = executeFunctions.getNodeParameter('pageId', itemIndex);
721
+ const resolvedId = await NotionUtils_1.resolvePageId.call(executeFunctions, pageId);
722
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'PATCH', `/pages/${resolvedId}`, {
723
+ archived: true,
724
+ });
725
+ }
726
+ async searchPages(executeFunctions, itemIndex) {
727
+ const query = executeFunctions.getNodeParameter('query', itemIndex, '');
728
+ const filter = executeFunctions.getNodeParameter('filter', itemIndex, '');
729
+ const body = {};
730
+ if (query) {
731
+ body.query = query;
732
+ }
733
+ if (filter) {
734
+ try {
735
+ body.filter = JSON.parse(filter);
736
+ }
737
+ catch {
738
+ body.filter = { property: 'object', value: 'page' };
739
+ }
740
+ }
741
+ else {
742
+ body.filter = { property: 'object', value: 'page' };
743
+ }
744
+ const results = await NotionUtils_1.paginatedRequest.call(executeFunctions, 'POST', '/search', body);
745
+ const searchResponse = {
746
+ object: 'list',
747
+ results,
748
+ count: results.length
749
+ };
750
+ return searchResponse;
751
+ }
752
+ // BLOCK OPERATIONS
753
+ async createBlocks(executeFunctions, itemIndex) {
754
+ const parentId = executeFunctions.getNodeParameter('parentId', itemIndex);
755
+ const blocks = executeFunctions.getNodeParameter('blocks', itemIndex, {});
756
+ const resolvedParentId = await NotionUtils_1.resolvePageId.call(executeFunctions, parentId);
757
+ if (!blocks.block || !Array.isArray(blocks.block)) {
758
+ throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), 'No blocks provided');
759
+ }
760
+ const blockData = [];
761
+ for (const blockInputRaw of blocks.block) {
762
+ try {
763
+ // Parse properties and children if they are JSON strings
764
+ const properties = blockInputRaw.properties ? JSON.parse(blockInputRaw.properties) : {};
765
+ const children = blockInputRaw.children ? JSON.parse(blockInputRaw.children) : undefined;
766
+ const richText = blockInputRaw.richText ? (0, NotionUtils_1.parseRichTextInput)(blockInputRaw.richText) : undefined;
767
+ // Create proper BlockInput structure
768
+ const blockInput = {
769
+ type: blockInputRaw.type,
770
+ content: blockInputRaw.content,
771
+ richText,
772
+ properties,
773
+ children,
774
+ };
775
+ const block = (0, NotionUtils_1.convertBlockInput)(blockInput);
776
+ (0, NotionUtils_1.validateBlock)(block);
777
+ blockData.push(block);
778
+ }
779
+ catch (error) {
780
+ throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Error processing block: ${error.message}`);
781
+ }
782
+ }
783
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'PATCH', `/blocks/${resolvedParentId}/children`, {
784
+ children: blockData,
785
+ });
786
+ }
787
+ async getBlock(executeFunctions, itemIndex) {
788
+ const blockId = executeFunctions.getNodeParameter('blockId', itemIndex);
789
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'GET', `/blocks/${blockId}`);
790
+ }
791
+ async updateBlock(executeFunctions, itemIndex) {
792
+ const blockId = executeFunctions.getNodeParameter('blockId', itemIndex);
793
+ const blocks = executeFunctions.getNodeParameter('blocks', itemIndex, {});
794
+ if (!blocks.block || !Array.isArray(blocks.block) || blocks.block.length === 0) {
795
+ throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), 'No block data provided for update');
796
+ }
797
+ const blockInput = blocks.block[0];
798
+ const properties = blockInput.properties ? JSON.parse(blockInput.properties) : {};
799
+ const richText = blockInput.richText ? (0, NotionUtils_1.parseRichTextInput)(blockInput.richText) : undefined;
800
+ const block = (0, NotionUtils_1.convertBlockInput)({
801
+ type: blockInput.type,
802
+ content: blockInput.content,
803
+ richText,
804
+ properties,
805
+ });
806
+ (0, NotionUtils_1.validateBlock)(block);
807
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'PATCH', `/blocks/${blockId}`, block);
808
+ }
809
+ async deleteBlock(executeFunctions, itemIndex) {
810
+ const blockId = executeFunctions.getNodeParameter('blockId', itemIndex);
811
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'DELETE', `/blocks/${blockId}`);
812
+ }
813
+ async getBlockChildren(executeFunctions, itemIndex) {
814
+ const blockId = executeFunctions.getNodeParameter('blockId', itemIndex);
815
+ const results = await NotionUtils_1.getBlocksWithIds.call(executeFunctions, blockId);
816
+ return { results, count: results.length };
817
+ }
818
+ async appendBlockChildren(executeFunctions, itemIndex) {
819
+ const blockId = executeFunctions.getNodeParameter('blockId', itemIndex);
820
+ const blocks = executeFunctions.getNodeParameter('blocks', itemIndex, {});
821
+ if (!blocks.block || !Array.isArray(blocks.block)) {
822
+ throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), 'No blocks provided');
823
+ }
824
+ const blockData = [];
825
+ for (const blockInput of blocks.block) {
826
+ const properties = blockInput.properties ? JSON.parse(blockInput.properties) : {};
827
+ const children = blockInput.children ? JSON.parse(blockInput.children) : undefined;
828
+ const richText = blockInput.richText ? (0, NotionUtils_1.parseRichTextInput)(blockInput.richText) : undefined;
829
+ const block = (0, NotionUtils_1.convertBlockInput)({
830
+ type: blockInput.type,
831
+ content: blockInput.content,
832
+ richText,
833
+ properties,
834
+ children,
835
+ });
836
+ (0, NotionUtils_1.validateBlock)(block);
837
+ blockData.push(block);
838
+ }
839
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'PATCH', `/blocks/${blockId}/children`, {
840
+ children: blockData,
841
+ });
842
+ }
843
+ // DATABASE OPERATIONS
844
+ async getDatabase(executeFunctions, itemIndex) {
845
+ const databaseId = executeFunctions.getNodeParameter('databaseId', itemIndex);
846
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'GET', `/databases/${databaseId}`);
847
+ }
848
+ async queryDatabase(executeFunctions, itemIndex) {
849
+ const databaseId = executeFunctions.getNodeParameter('databaseId', itemIndex);
850
+ const filter = executeFunctions.getNodeParameter('filter', itemIndex, '');
851
+ const body = {};
852
+ if (filter) {
853
+ try {
854
+ body.filter = JSON.parse(filter);
855
+ }
856
+ catch (error) {
857
+ throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Invalid filter JSON: ${error.message}`);
858
+ }
859
+ }
860
+ const results = await NotionUtils_1.paginatedRequest.call(executeFunctions, 'POST', `/databases/${databaseId}/query`, body);
861
+ const response = {
862
+ object: 'list',
863
+ results,
864
+ count: results.length,
865
+ has_more: false,
866
+ next_cursor: null
867
+ };
868
+ return response;
869
+ }
870
+ async createDatabase(executeFunctions, itemIndex) {
871
+ const parent = executeFunctions.getNodeParameter('parent', itemIndex);
872
+ const title = executeFunctions.getNodeParameter('title', itemIndex);
873
+ const properties = executeFunctions.getNodeParameter('properties', itemIndex, {});
874
+ const parentId = await NotionUtils_1.resolvePageId.call(executeFunctions, parent);
875
+ const body = {
876
+ parent: { page_id: parentId },
877
+ title: [(0, NotionUtils_1.createRichText)(title)],
878
+ properties: {},
879
+ };
880
+ // Add properties schema
881
+ if (properties.property && Array.isArray(properties.property)) {
882
+ for (const prop of properties.property) {
883
+ const { name, type, value } = prop;
884
+ try {
885
+ const parsedValue = JSON.parse(value);
886
+ body.properties[name] = { [type]: parsedValue };
887
+ }
888
+ catch {
889
+ body.properties[name] = { [type]: {} };
890
+ }
891
+ }
892
+ }
893
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'POST', '/databases', body);
894
+ }
895
+ // USER OPERATIONS
896
+ async getUser(executeFunctions, itemIndex) {
897
+ const userId = executeFunctions.getNodeParameter('userId', itemIndex);
898
+ return await NotionUtils_1.notionApiRequest.call(executeFunctions, 'GET', `/users/${userId}`);
899
+ }
900
+ async listUsers(executeFunctions, _itemIndex) {
901
+ const results = await NotionUtils_1.paginatedRequest.call(executeFunctions, 'GET', '/users');
902
+ return { results, count: results.length };
903
+ }
904
+ }
905
+ exports.NotionAdvanced = NotionAdvanced;