n8n-nodes-notion-advanced 1.1.26-beta → 1.1.28-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,100 @@
1
+ import { IDataObject, IExecuteFunctions, IHttpRequestMethods, INodeExecutionData } from 'n8n-workflow';
2
+ import { Block, BlockInput, BlockWithId, RichTextObject, RichTextAnnotations, NotionColor, PageInput } from '../../types/NotionTypes';
3
+ /**
4
+ * Makes an authenticated request to the Notion API
5
+ */
6
+ export declare function notionApiRequest(this: IExecuteFunctions, method: IHttpRequestMethods, endpoint: string, body?: IDataObject, qs?: IDataObject): Promise<any>;
7
+ /**
8
+ * Validates Notion API credentials by making a test request
9
+ */
10
+ export declare function validateCredentials(this: IExecuteFunctions): Promise<boolean>;
11
+ /**
12
+ * Creates a rich text object from a string with optional formatting
13
+ */
14
+ export declare function createRichText(text: string, annotations?: Partial<RichTextAnnotations>, link?: string): RichTextObject;
15
+ /**
16
+ * Parses rich text input from various formats
17
+ */
18
+ export declare function parseRichTextInput(input: any): RichTextObject[];
19
+ /**
20
+ * Creates a paragraph block
21
+ */
22
+ export declare function createParagraphBlock(text: string | RichTextObject[], color?: NotionColor, children?: Block[]): Block;
23
+ /**
24
+ * Creates a heading block
25
+ */
26
+ export declare function createHeadingBlock(level: 1 | 2 | 3, text: string | RichTextObject[], color?: NotionColor, isToggleable?: boolean): Block;
27
+ /**
28
+ * Creates a list item block
29
+ */
30
+ export declare function createListItemBlock(type: 'bulleted_list_item' | 'numbered_list_item', text: string | RichTextObject[], color?: NotionColor, children?: Block[]): Block;
31
+ /**
32
+ * Creates a to-do block
33
+ */
34
+ export declare function createToDoBlock(text: string | RichTextObject[], checked?: boolean, color?: NotionColor, children?: Block[]): Block;
35
+ /**
36
+ * Creates a code block
37
+ */
38
+ export declare function createCodeBlock(code: string, language?: string, caption?: RichTextObject[]): Block;
39
+ /**
40
+ * Creates a quote block
41
+ */
42
+ export declare function createQuoteBlock(text: string | RichTextObject[], color?: NotionColor, children?: Block[]): Block;
43
+ /**
44
+ * Creates a callout block
45
+ */
46
+ export declare function createCalloutBlock(text: string | RichTextObject[], icon?: string, color?: NotionColor, children?: Block[]): Block;
47
+ /**
48
+ * Creates a divider block
49
+ */
50
+ export declare function createDividerBlock(): Block;
51
+ /**
52
+ * Creates an equation block
53
+ */
54
+ export declare function createEquationBlock(expression: string): Block;
55
+ /**
56
+ * Creates an image block
57
+ */
58
+ export declare function createImageBlock(url: string, caption?: RichTextObject[]): Block;
59
+ /**
60
+ * Creates a bookmark block
61
+ */
62
+ export declare function createBookmarkBlock(url: string, caption?: RichTextObject[]): Block;
63
+ /**
64
+ * Creates an embed block
65
+ */
66
+ export declare function createEmbedBlock(url: string, caption?: RichTextObject[]): Block;
67
+ /**
68
+ * Creates a table block with rows
69
+ */
70
+ export declare function createTableBlock(tableWidth: number, rows: RichTextObject[][][], hasColumnHeader?: boolean, hasRowHeader?: boolean): Block;
71
+ /**
72
+ * Converts BlockInput to actual Block objects
73
+ */
74
+ export declare function convertBlockInput(blockInput: BlockInput): Block;
75
+ /**
76
+ * Resolves a page ID from various input formats (URL, ID, title search)
77
+ */
78
+ export declare function resolvePageId(this: IExecuteFunctions, pageInput: string): Promise<string>;
79
+ /**
80
+ * Validates a block structure before sending to Notion API
81
+ */
82
+ export declare function validateBlock(block: Block): void;
83
+ /**
84
+ * Paginated request helper for Notion API
85
+ */
86
+ export declare function paginatedRequest(this: IExecuteFunctions, method: IHttpRequestMethods, endpoint: string, body?: IDataObject): Promise<any[]>;
87
+ /**
88
+ * Creates page input from parameters for validation
89
+ */
90
+ export declare function createPageInput(title: string, parent: string, properties?: {
91
+ [key: string]: any;
92
+ }, children?: BlockInput[], icon?: string, cover?: string): PageInput;
93
+ /**
94
+ * Gets blocks with full metadata including IDs
95
+ */
96
+ export declare function getBlocksWithIds(this: IExecuteFunctions, blockId: string): Promise<BlockWithId[]>;
97
+ /**
98
+ * Creates execution data from results
99
+ */
100
+ export declare function createExecutionData(data: IDataObject[]): INodeExecutionData[];
@@ -0,0 +1,451 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.notionApiRequest = notionApiRequest;
4
+ exports.validateCredentials = validateCredentials;
5
+ exports.createRichText = createRichText;
6
+ exports.parseRichTextInput = parseRichTextInput;
7
+ exports.createParagraphBlock = createParagraphBlock;
8
+ exports.createHeadingBlock = createHeadingBlock;
9
+ exports.createListItemBlock = createListItemBlock;
10
+ exports.createToDoBlock = createToDoBlock;
11
+ exports.createCodeBlock = createCodeBlock;
12
+ exports.createQuoteBlock = createQuoteBlock;
13
+ exports.createCalloutBlock = createCalloutBlock;
14
+ exports.createDividerBlock = createDividerBlock;
15
+ exports.createEquationBlock = createEquationBlock;
16
+ exports.createImageBlock = createImageBlock;
17
+ exports.createBookmarkBlock = createBookmarkBlock;
18
+ exports.createEmbedBlock = createEmbedBlock;
19
+ exports.createTableBlock = createTableBlock;
20
+ exports.convertBlockInput = convertBlockInput;
21
+ exports.resolvePageId = resolvePageId;
22
+ exports.validateBlock = validateBlock;
23
+ exports.paginatedRequest = paginatedRequest;
24
+ exports.createPageInput = createPageInput;
25
+ exports.getBlocksWithIds = getBlocksWithIds;
26
+ exports.createExecutionData = createExecutionData;
27
+ const n8n_workflow_1 = require("n8n-workflow");
28
+ /**
29
+ * Makes an authenticated request to the Notion API
30
+ */
31
+ async function notionApiRequest(method, endpoint, body = {}, qs = {}) {
32
+ const credentials = (await this.getCredentials('notionApi'));
33
+ const options = {
34
+ method,
35
+ headers: {
36
+ 'Authorization': `Bearer ${credentials.apiKey}`,
37
+ 'Notion-Version': '2022-06-28',
38
+ 'Content-Type': 'application/json',
39
+ },
40
+ url: `https://api.notion.com/v1${endpoint}`,
41
+ body,
42
+ qs,
43
+ json: true,
44
+ };
45
+ try {
46
+ return await this.helpers.httpRequest(options);
47
+ }
48
+ catch (error) {
49
+ throw new n8n_workflow_1.NodeApiError(this.getNode(), {
50
+ message: error.message,
51
+ description: 'Failed to make Notion API request',
52
+ httpCode: error.status || error.statusCode || 500,
53
+ });
54
+ }
55
+ }
56
+ /**
57
+ * Validates Notion API credentials by making a test request
58
+ */
59
+ async function validateCredentials() {
60
+ try {
61
+ await notionApiRequest.call(this, 'GET', '/users/me');
62
+ return true;
63
+ }
64
+ catch (error) {
65
+ return false;
66
+ }
67
+ }
68
+ /**
69
+ * Creates a rich text object from a string with optional formatting
70
+ */
71
+ function createRichText(text, annotations = {}, link) {
72
+ return {
73
+ type: 'text',
74
+ text: {
75
+ content: text,
76
+ link: link ? { url: link } : null,
77
+ },
78
+ annotations: {
79
+ bold: annotations.bold || false,
80
+ italic: annotations.italic || false,
81
+ strikethrough: annotations.strikethrough || false,
82
+ underline: annotations.underline || false,
83
+ code: annotations.code || false,
84
+ color: annotations.color || 'default',
85
+ },
86
+ plain_text: text,
87
+ href: link || null,
88
+ };
89
+ }
90
+ /**
91
+ * Parses rich text input from various formats
92
+ */
93
+ function parseRichTextInput(input) {
94
+ if (typeof input === 'string') {
95
+ return [createRichText(input)];
96
+ }
97
+ if (Array.isArray(input)) {
98
+ return input.map((item) => {
99
+ if (typeof item === 'string') {
100
+ return createRichText(item);
101
+ }
102
+ if (typeof item === 'object' && item !== null) {
103
+ return createRichText(item.text || item.content || '', item.annotations || {}, item.link || item.url);
104
+ }
105
+ return createRichText('');
106
+ });
107
+ }
108
+ if (typeof input === 'object' && input !== null) {
109
+ if (input.type === 'text' || input.type === 'mention' || input.type === 'equation') {
110
+ return [input];
111
+ }
112
+ return [createRichText(input.text || input.content || '', input.annotations || {}, input.link || input.url)];
113
+ }
114
+ return [createRichText('')];
115
+ }
116
+ /**
117
+ * Creates a paragraph block
118
+ */
119
+ function createParagraphBlock(text, color, children) {
120
+ return {
121
+ type: 'paragraph',
122
+ paragraph: {
123
+ rich_text: typeof text === 'string' ? [createRichText(text)] : text,
124
+ color,
125
+ children,
126
+ },
127
+ };
128
+ }
129
+ /**
130
+ * Creates a heading block
131
+ */
132
+ function createHeadingBlock(level, text, color, isToggleable) {
133
+ const richText = typeof text === 'string' ? [createRichText(text)] : text;
134
+ const headingData = {
135
+ rich_text: richText,
136
+ color,
137
+ is_toggleable: isToggleable,
138
+ };
139
+ switch (level) {
140
+ case 1:
141
+ return {
142
+ type: 'heading_1',
143
+ heading_1: headingData,
144
+ };
145
+ case 2:
146
+ return {
147
+ type: 'heading_2',
148
+ heading_2: headingData,
149
+ };
150
+ case 3:
151
+ return {
152
+ type: 'heading_3',
153
+ heading_3: headingData,
154
+ };
155
+ default:
156
+ throw new Error('Invalid heading level. Must be 1, 2, or 3.');
157
+ }
158
+ }
159
+ /**
160
+ * Creates a list item block
161
+ */
162
+ function createListItemBlock(type, text, color, children) {
163
+ const richText = typeof text === 'string' ? [createRichText(text)] : text;
164
+ if (type === 'bulleted_list_item') {
165
+ return {
166
+ type: 'bulleted_list_item',
167
+ bulleted_list_item: {
168
+ rich_text: richText,
169
+ color,
170
+ children,
171
+ },
172
+ };
173
+ }
174
+ else {
175
+ return {
176
+ type: 'numbered_list_item',
177
+ numbered_list_item: {
178
+ rich_text: richText,
179
+ color,
180
+ children,
181
+ },
182
+ };
183
+ }
184
+ }
185
+ /**
186
+ * Creates a to-do block
187
+ */
188
+ function createToDoBlock(text, checked = false, color, children) {
189
+ return {
190
+ type: 'to_do',
191
+ to_do: {
192
+ rich_text: typeof text === 'string' ? [createRichText(text)] : text,
193
+ checked,
194
+ color,
195
+ children,
196
+ },
197
+ };
198
+ }
199
+ /**
200
+ * Creates a code block
201
+ */
202
+ function createCodeBlock(code, language, caption) {
203
+ return {
204
+ type: 'code',
205
+ code: {
206
+ rich_text: [createRichText(code)],
207
+ language,
208
+ caption,
209
+ },
210
+ };
211
+ }
212
+ /**
213
+ * Creates a quote block
214
+ */
215
+ function createQuoteBlock(text, color, children) {
216
+ return {
217
+ type: 'quote',
218
+ quote: {
219
+ rich_text: typeof text === 'string' ? [createRichText(text)] : text,
220
+ color,
221
+ children,
222
+ },
223
+ };
224
+ }
225
+ /**
226
+ * Creates a callout block
227
+ */
228
+ function createCalloutBlock(text, icon, color, children) {
229
+ const iconObject = icon ? { type: 'emoji', emoji: icon } : undefined;
230
+ return {
231
+ type: 'callout',
232
+ callout: {
233
+ rich_text: typeof text === 'string' ? [createRichText(text)] : text,
234
+ icon: iconObject,
235
+ color,
236
+ children,
237
+ },
238
+ };
239
+ }
240
+ /**
241
+ * Creates a divider block
242
+ */
243
+ function createDividerBlock() {
244
+ return {
245
+ type: 'divider',
246
+ divider: {},
247
+ };
248
+ }
249
+ /**
250
+ * Creates an equation block
251
+ */
252
+ function createEquationBlock(expression) {
253
+ return {
254
+ type: 'equation',
255
+ equation: {
256
+ expression,
257
+ },
258
+ };
259
+ }
260
+ /**
261
+ * Creates an image block
262
+ */
263
+ function createImageBlock(url, caption) {
264
+ return {
265
+ type: 'image',
266
+ image: {
267
+ type: 'external',
268
+ external: { url },
269
+ caption,
270
+ },
271
+ };
272
+ }
273
+ /**
274
+ * Creates a bookmark block
275
+ */
276
+ function createBookmarkBlock(url, caption) {
277
+ return {
278
+ type: 'bookmark',
279
+ bookmark: {
280
+ url,
281
+ caption,
282
+ },
283
+ };
284
+ }
285
+ /**
286
+ * Creates an embed block
287
+ */
288
+ function createEmbedBlock(url, caption) {
289
+ return {
290
+ type: 'embed',
291
+ embed: {
292
+ url,
293
+ caption,
294
+ },
295
+ };
296
+ }
297
+ /**
298
+ * Creates a table block with rows
299
+ */
300
+ function createTableBlock(tableWidth, rows, hasColumnHeader = false, hasRowHeader = false) {
301
+ const tableRows = rows.map(cells => ({
302
+ type: 'table_row',
303
+ table_row: { cells },
304
+ }));
305
+ return {
306
+ type: 'table',
307
+ table: {
308
+ table_width: tableWidth,
309
+ has_column_header: hasColumnHeader,
310
+ has_row_header: hasRowHeader,
311
+ children: tableRows,
312
+ },
313
+ };
314
+ }
315
+ /**
316
+ * Converts BlockInput to actual Block objects
317
+ */
318
+ function convertBlockInput(blockInput) {
319
+ const { type, content = '', properties = {}, children } = blockInput;
320
+ const richText = parseRichTextInput(blockInput.richText || content);
321
+ const childBlocks = children ? children.map(convertBlockInput) : undefined;
322
+ switch (type) {
323
+ case 'paragraph':
324
+ return createParagraphBlock(richText, properties.color, childBlocks);
325
+ case 'heading_1':
326
+ case 'heading_2':
327
+ case 'heading_3':
328
+ const level = parseInt(type.split('_')[1]);
329
+ return createHeadingBlock(level, richText, properties.color, properties.isToggleable);
330
+ case 'bulleted_list_item':
331
+ case 'numbered_list_item':
332
+ return createListItemBlock(type, richText, properties.color, childBlocks);
333
+ case 'to_do':
334
+ return createToDoBlock(richText, properties.checked, properties.color, childBlocks);
335
+ case 'code':
336
+ return createCodeBlock(content, properties.language, properties.caption);
337
+ case 'quote':
338
+ return createQuoteBlock(richText, properties.color, childBlocks);
339
+ case 'callout':
340
+ return createCalloutBlock(richText, properties.icon, properties.color, childBlocks);
341
+ case 'divider':
342
+ return createDividerBlock();
343
+ case 'equation':
344
+ return createEquationBlock(properties.expression || content);
345
+ case 'image':
346
+ return createImageBlock(properties.url || content, properties.caption);
347
+ case 'bookmark':
348
+ return createBookmarkBlock(properties.url || content, properties.caption);
349
+ case 'embed':
350
+ return createEmbedBlock(properties.url || content, properties.caption);
351
+ default:
352
+ throw new Error(`Unsupported block type: ${type}`);
353
+ }
354
+ }
355
+ /**
356
+ * Resolves a page ID from various input formats (URL, ID, title search)
357
+ */
358
+ async function resolvePageId(pageInput) {
359
+ // If it looks like a UUID, return it directly
360
+ const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
361
+ if (uuidRegex.test(pageInput)) {
362
+ return pageInput;
363
+ }
364
+ // If it looks like a Notion URL, extract the ID
365
+ const urlRegex = /https:\/\/www\.notion\.so\/[^\/]*-([a-f0-9]{32})/;
366
+ const urlMatch = pageInput.match(urlRegex);
367
+ if (urlMatch) {
368
+ const rawId = urlMatch[1];
369
+ // Convert 32-character ID to UUID format
370
+ return `${rawId.slice(0, 8)}-${rawId.slice(8, 12)}-${rawId.slice(12, 16)}-${rawId.slice(16, 20)}-${rawId.slice(20)}`;
371
+ }
372
+ // Otherwise, search for page by title
373
+ const searchResponse = await notionApiRequest.call(this, 'POST', '/search', {
374
+ query: pageInput,
375
+ filter: { property: 'object', value: 'page' },
376
+ });
377
+ if (searchResponse.results && searchResponse.results.length > 0) {
378
+ const page = searchResponse.results[0];
379
+ return page.id;
380
+ }
381
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Could not find page with identifier: ${pageInput}`);
382
+ }
383
+ /**
384
+ * Validates a block structure before sending to Notion API
385
+ */
386
+ function validateBlock(block) {
387
+ if (!block.type) {
388
+ throw new Error('Block must have a type property');
389
+ }
390
+ // Add specific validation for each block type as needed
391
+ switch (block.type) {
392
+ case 'paragraph':
393
+ if (!('paragraph' in block) || !block.paragraph || !Array.isArray(block.paragraph.rich_text)) {
394
+ throw new Error('Paragraph block must have rich_text array');
395
+ }
396
+ break;
397
+ case 'code':
398
+ if (!('code' in block) || !block.code || !Array.isArray(block.code.rich_text)) {
399
+ throw new Error('Code block must have rich_text array');
400
+ }
401
+ break;
402
+ // Add more validation cases as needed
403
+ }
404
+ }
405
+ /**
406
+ * Paginated request helper for Notion API
407
+ */
408
+ async function paginatedRequest(method, endpoint, body = {}) {
409
+ const results = [];
410
+ let hasMore = true;
411
+ let nextCursor;
412
+ while (hasMore) {
413
+ const requestBody = { ...body };
414
+ if (nextCursor) {
415
+ requestBody.start_cursor = nextCursor;
416
+ }
417
+ const response = await notionApiRequest.call(this, method, endpoint, requestBody);
418
+ if (response.results) {
419
+ results.push(...response.results);
420
+ }
421
+ hasMore = response.has_more || false;
422
+ nextCursor = response.next_cursor || undefined;
423
+ }
424
+ return results;
425
+ }
426
+ /**
427
+ * Creates page input from parameters for validation
428
+ */
429
+ function createPageInput(title, parent, properties, children, icon, cover) {
430
+ return {
431
+ title,
432
+ parent,
433
+ properties,
434
+ children,
435
+ icon,
436
+ cover,
437
+ };
438
+ }
439
+ /**
440
+ * Gets blocks with full metadata including IDs
441
+ */
442
+ async function getBlocksWithIds(blockId) {
443
+ const response = await notionApiRequest.call(this, 'GET', `/blocks/${blockId}/children`);
444
+ return response.results;
445
+ }
446
+ /**
447
+ * Creates execution data from results
448
+ */
449
+ function createExecutionData(data) {
450
+ return data.map(item => ({ json: item }));
451
+ }