n8n-nodes-notion-advanced 1.0.0-beta.1

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