trilium-api 1.0.1 → 1.0.2

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.
@@ -1,166 +0,0 @@
1
- /**
2
- * Demo script for the Note Mapper
3
- *
4
- * This demonstrates how to use TriliumMapper to map Trilium notes
5
- * to strongly-typed objects.
6
- *
7
- * Usage:
8
- * pnpm tsx src/demo-mapper.ts
9
- *
10
- * With a real Trilium instance:
11
- * TRILIUM_API_KEY=your-token pnpm tsx src/demo-mapper.ts
12
- */
13
-
14
- import { TriliumMapper, transforms, type TriliumNote } from './client.js';
15
-
16
- console.log('Note Mapper Demo');
17
- console.log('='.repeat(50));
18
- console.log();
19
-
20
- // Define a sample blog post type
21
- interface BlogPost {
22
- id: string;
23
- title: string;
24
- slug: string;
25
- status: 'draft' | 'published' | 'archived';
26
- wordCount: number;
27
- tags: string[];
28
- publishedAt: Date | null;
29
- readTimeMinutes: number;
30
- }
31
-
32
- // Create a mapper configuration
33
- const blogMapper = new TriliumMapper<BlogPost>({
34
- // Simple property mapping
35
- id: 'note.noteId',
36
- title: 'note.title',
37
-
38
- // Required label
39
- slug: { from: '#slug', required: true },
40
-
41
- // Label with default value
42
- status: { from: '#status', default: 'draft' },
43
-
44
- // Label with transform
45
- wordCount: { from: '#wordCount', transform: transforms.number, default: 0 },
46
-
47
- // Label with array transform
48
- tags: { from: '#tags', transform: transforms.commaSeparated, default: [] },
49
-
50
- // Label with date transform
51
- publishedAt: { from: '#publishedAt', transform: transforms.date, default: null },
52
-
53
- // Computed value based on other fields
54
- readTimeMinutes: {
55
- computed: (partial) => Math.ceil((partial.wordCount || 0) / 200),
56
- },
57
- });
58
-
59
- // Create sample Trilium notes (simulating API response)
60
- const sampleNotes: TriliumNote[] = [
61
- {
62
- noteId: 'note1',
63
- title: 'Getting Started with TypeScript',
64
- type: 'text',
65
- mime: 'text/html',
66
- isProtected: false,
67
- blobId: 'blob1',
68
- dateCreated: '2024-01-15T10:00:00.000Z',
69
- dateModified: '2024-01-20T15:30:00.000Z',
70
- utcDateCreated: '2024-01-15T10:00:00.000Z',
71
- utcDateModified: '2024-01-20T15:30:00.000Z',
72
- attributes: [
73
- { attributeId: 'attr1', noteId: 'note1', type: 'label', name: 'slug', value: 'getting-started-typescript', isInheritable: false },
74
- { attributeId: 'attr2', noteId: 'note1', type: 'label', name: 'status', value: 'published', isInheritable: false },
75
- { attributeId: 'attr3', noteId: 'note1', type: 'label', name: 'wordCount', value: '1500', isInheritable: false },
76
- { attributeId: 'attr4', noteId: 'note1', type: 'label', name: 'tags', value: 'typescript,programming,tutorial', isInheritable: false },
77
- { attributeId: 'attr5', noteId: 'note1', type: 'label', name: 'publishedAt', value: '2024-01-20', isInheritable: false },
78
- ],
79
- },
80
- {
81
- noteId: 'note2',
82
- title: 'Advanced Patterns in Node.js',
83
- type: 'text',
84
- mime: 'text/html',
85
- isProtected: false,
86
- blobId: 'blob2',
87
- dateCreated: '2024-02-01T09:00:00.000Z',
88
- dateModified: '2024-02-05T14:00:00.000Z',
89
- utcDateCreated: '2024-02-01T09:00:00.000Z',
90
- utcDateModified: '2024-02-05T14:00:00.000Z',
91
- attributes: [
92
- { attributeId: 'attr6', noteId: 'note2', type: 'label', name: 'slug', value: 'advanced-nodejs-patterns', isInheritable: false },
93
- { attributeId: 'attr7', noteId: 'note2', type: 'label', name: 'status', value: 'draft', isInheritable: false },
94
- { attributeId: 'attr8', noteId: 'note2', type: 'label', name: 'wordCount', value: '2400', isInheritable: false },
95
- { attributeId: 'attr9', noteId: 'note2', type: 'label', name: 'tags', value: 'nodejs,javascript,backend', isInheritable: false },
96
- ],
97
- },
98
- {
99
- noteId: 'note3',
100
- title: 'Quick Tips for VS Code',
101
- type: 'text',
102
- mime: 'text/html',
103
- isProtected: false,
104
- blobId: 'blob3',
105
- dateCreated: '2024-03-01T11:00:00.000Z',
106
- dateModified: '2024-03-01T11:30:00.000Z',
107
- utcDateCreated: '2024-03-01T11:00:00.000Z',
108
- utcDateModified: '2024-03-01T11:30:00.000Z',
109
- attributes: [
110
- { attributeId: 'attr10', noteId: 'note3', type: 'label', name: 'slug', value: 'vscode-tips', isInheritable: false },
111
- // No status - will use default 'draft'
112
- // No wordCount - will use default 0
113
- // No tags - will use default []
114
- ],
115
- },
116
- ];
117
-
118
- console.log('Mapper Configuration:');
119
- console.log('-'.repeat(50));
120
- console.log(`
121
- const blogMapper = new TriliumMapper<BlogPost>({
122
- id: 'note.noteId',
123
- title: 'note.title',
124
- slug: { from: '#slug', required: true },
125
- status: { from: '#status', default: 'draft' },
126
- wordCount: { from: '#wordCount', transform: transforms.number, default: 0 },
127
- tags: { from: '#tags', transform: transforms.commaSeparated, default: [] },
128
- publishedAt: { from: '#publishedAt', transform: transforms.date, default: null },
129
- readTimeMinutes: {
130
- computed: (partial) => Math.ceil((partial.wordCount || 0) / 200),
131
- },
132
- });
133
- `);
134
-
135
- console.log('Mapping Results:');
136
- console.log('-'.repeat(50));
137
-
138
- // Map all notes
139
- const blogPosts = blogMapper.map(sampleNotes);
140
-
141
- for (const post of blogPosts) {
142
- console.log();
143
- console.log(`Title: ${post.title}`);
144
- console.log(` ID: ${post.id}`);
145
- console.log(` Slug: ${post.slug}`);
146
- console.log(` Status: ${post.status}`);
147
- console.log(` Word Count: ${post.wordCount}`);
148
- console.log(` Tags: [${post.tags.join(', ')}]`);
149
- console.log(` Published: ${post.publishedAt?.toISOString() || 'Not published'}`);
150
- console.log(` Read Time: ${post.readTimeMinutes} min`);
151
- }
152
-
153
- console.log();
154
- console.log('='.repeat(50));
155
- console.log();
156
-
157
- // Demonstrate single note mapping
158
- console.log('Single Note Mapping:');
159
- console.log('-'.repeat(50));
160
- console.log('const singlePost = blogMapper.map(notes[0]);');
161
- const singlePost = blogMapper.map(sampleNotes[0]!);
162
- console.log('Result:', JSON.stringify(singlePost, null, 2));
163
- console.log();
164
-
165
- console.log('='.repeat(50));
166
- console.log('Demo completed!');
@@ -1,108 +0,0 @@
1
- /**
2
- * Demo script for the Search Query Builder
3
- *
4
- * This demonstrates how to use buildSearchQuery to construct
5
- * type-safe Trilium search queries.
6
- *
7
- * Usage:
8
- * pnpm tsx src/demo-search.ts
9
- */
10
-
11
- import { buildSearchQuery } from './mapper.js';
12
-
13
- console.log('Search Query Builder Demo');
14
- console.log('='.repeat(50));
15
- console.log();
16
-
17
- // Simple label search
18
- console.log('1. Simple label search:');
19
- console.log(' Code: buildSearchQuery({ "#blog": true })');
20
- console.log(' Result:', buildSearchQuery({ '#blog': true }));
21
- console.log();
22
-
23
- // Label with value
24
- console.log('2. Label with value:');
25
- console.log(' Code: buildSearchQuery({ "#status": "published" })');
26
- console.log(' Result:', buildSearchQuery({ '#status': 'published' }));
27
- console.log();
28
-
29
- // Label absence check
30
- console.log('3. Label absence check:');
31
- console.log(' Code: buildSearchQuery({ "#draft": false })');
32
- console.log(' Result:', buildSearchQuery({ '#draft': false }));
33
- console.log();
34
-
35
- // Comparison operators
36
- console.log('4. Comparison operators:');
37
- console.log(' Code: buildSearchQuery({ "#wordCount": { value: 1000, operator: ">=" } })');
38
- console.log(' Result:', buildSearchQuery({ '#wordCount': { value: 1000, operator: '>=' } }));
39
- console.log();
40
-
41
- // Note properties
42
- console.log('5. Note properties:');
43
- console.log(' Code: buildSearchQuery({ "note.type": "text", title: { value: "Blog", operator: "*=" } })');
44
- console.log(
45
- ' Result:',
46
- buildSearchQuery({ 'note.type': 'text', title: { value: 'Blog', operator: '*=' } })
47
- );
48
- console.log();
49
-
50
- // Relations
51
- console.log('6. Relations:');
52
- console.log(' Code: buildSearchQuery({ "~author": "John" })');
53
- console.log(' Result:', buildSearchQuery({ '~author': 'John' }));
54
- console.log();
55
-
56
- // AND conditions
57
- console.log('7. AND conditions:');
58
- console.log(' Code: buildSearchQuery({ AND: [{ "#blog": true }, { "#published": true }] })');
59
- console.log(' Result:', buildSearchQuery({ AND: [{ '#blog': true }, { '#published': true }] }));
60
- console.log();
61
-
62
- // OR conditions
63
- console.log('8. OR conditions:');
64
- console.log(' Code: buildSearchQuery({ OR: [{ "#status": "published" }, { "#status": "featured" }] })');
65
- console.log(
66
- ' Result:',
67
- buildSearchQuery({ OR: [{ '#status': 'published' }, { '#status': 'featured' }] })
68
- );
69
- console.log();
70
-
71
- // Complex nested conditions
72
- console.log('9. Complex nested conditions:');
73
- const complexQuery = {
74
- AND: [
75
- { '#blog': true },
76
- {
77
- OR: [{ '#status': 'published' }, { '#status': 'featured' }],
78
- },
79
- { '#wordCount': { value: 500, operator: '>=' as const } },
80
- ],
81
- };
82
- console.log(' Code: buildSearchQuery({');
83
- console.log(' AND: [');
84
- console.log(' { "#blog": true },');
85
- console.log(' { OR: [{ "#status": "published" }, { "#status": "featured" }] },');
86
- console.log(' { "#wordCount": { value: 500, operator: ">=" } }');
87
- console.log(' ]');
88
- console.log(' })');
89
- console.log(' Result:', buildSearchQuery(complexQuery));
90
- console.log();
91
-
92
- // NOT conditions
93
- console.log('10. NOT conditions:');
94
- console.log(' Code: buildSearchQuery({ NOT: { "#archived": true } })');
95
- console.log(' Result:', buildSearchQuery({ NOT: { '#archived': true } }));
96
- console.log();
97
-
98
- // Multiple conditions (implicit AND)
99
- console.log('11. Multiple conditions (implicit AND):');
100
- console.log(' Code: buildSearchQuery({ "#blog": true, "#published": true, "note.type": "text" })');
101
- console.log(
102
- ' Result:',
103
- buildSearchQuery({ '#blog': true, '#published': true, 'note.type': 'text' })
104
- );
105
- console.log();
106
-
107
- console.log('='.repeat(50));
108
- console.log('Demo completed!');
package/src/demo.ts DELETED
@@ -1,126 +0,0 @@
1
- /**
2
- * Demo script to test connecting to a local Trilium instance
3
- *
4
- * Usage:
5
- * pnpm tsx src/demo.ts
6
- *
7
- * Make sure to set your ETAPI token below or via environment variable:
8
- * TRILIUM_API_KEY=your-token pnpm tsx src/demo.ts
9
- */
10
-
11
- import { createTriliumClient, type TriliumNote } from './client.js';
12
-
13
- const TRILIUM_URL = process.env.TRILIUM_URL || 'http://localhost:8080';
14
- const TRILIUM_API_KEY = process.env.TRILIUM_API_KEY || 'YOUR_ETAPI_TOKEN_HERE';
15
- const MAX_DEPTH = parseInt(process.env.MAX_DEPTH || '3', 10);
16
-
17
- type Client = ReturnType<typeof createTriliumClient>;
18
-
19
- async function printNoteTree(
20
- client: Client,
21
- noteId: string,
22
- depth: number = 0,
23
- prefix: string = '',
24
- isLast: boolean = true
25
- ): Promise<void> {
26
- if (depth > MAX_DEPTH) {
27
- console.log(`${prefix}${isLast ? '└── ' : '├── '}...`);
28
- return;
29
- }
30
-
31
- const { data: note, error } = await client.GET('/notes/{noteId}', {
32
- params: { path: { noteId } },
33
- });
34
-
35
- if (error || !note) {
36
- console.log(`${prefix}${isLast ? '└── ' : '├── '}[Error fetching note]`);
37
- return;
38
- }
39
-
40
- const icon = getTypeIcon(note.type);
41
- const connector = depth === 0 ? '' : isLast ? '└── ' : '├── ';
42
- console.log(`${prefix}${connector}${icon} ${note.title || '(untitled)'}`);
43
-
44
- const childIds = note.childNoteIds || [];
45
- if (childIds.length === 0) return;
46
-
47
- const newPrefix = depth === 0 ? '' : prefix + (isLast ? ' ' : '│ ');
48
-
49
- for (let i = 0; i < childIds.length; i++) {
50
- const childId = childIds[i];
51
- if (!childId) continue;
52
- const isLastChild = i === childIds.length - 1;
53
- await printNoteTree(client, childId, depth + 1, newPrefix, isLastChild);
54
- }
55
- }
56
-
57
- function getTypeIcon(type: TriliumNote['type']): string {
58
- switch (type) {
59
- case 'text':
60
- return '📄';
61
- case 'code':
62
- return '💻';
63
- case 'image':
64
- return '🖼️';
65
- case 'search':
66
- return '🔍';
67
- case 'book':
68
- return '📚';
69
- case 'noteMap':
70
- return '🗺️';
71
- case 'mermaid':
72
- return '📊';
73
- case 'webView':
74
- return '🌐';
75
- case 'launcher':
76
- return '🚀';
77
- case 'doc':
78
- return '📝';
79
- case 'contentWidget':
80
- return '🧩';
81
- case 'relationMap':
82
- return '🔗';
83
- case 'render':
84
- return '⚡';
85
- case 'file':
86
- return '📎';
87
- default:
88
- return '📁';
89
- }
90
- }
91
-
92
- async function main() {
93
- console.log('🔌 Connecting to Trilium at:', TRILIUM_URL);
94
- console.log();
95
-
96
- const client = createTriliumClient({
97
- baseUrl: TRILIUM_URL,
98
- apiKey: TRILIUM_API_KEY,
99
- });
100
-
101
- // Get app info
102
- const { data: appInfo, error: appError } = await client.GET('/app-info');
103
-
104
- if (appError) {
105
- console.error('❌ Failed to connect:', appError);
106
- console.error('\nMake sure:');
107
- console.error(' 1. Trilium is running at', TRILIUM_URL);
108
- console.error(' 2. Your ETAPI token is correct');
109
- console.error(' 3. ETAPI is enabled in Trilium settings');
110
- process.exit(1);
111
- }
112
-
113
- console.log('✅ Connected to Trilium', appInfo?.appVersion);
114
- console.log();
115
-
116
- // Print tree view
117
- console.log(`📂 Note Tree (max depth: ${MAX_DEPTH}):`);
118
- console.log('─'.repeat(40));
119
- await printNoteTree(client, 'root');
120
- console.log('─'.repeat(40));
121
-
122
- console.log();
123
- console.log('🎉 Done! Set MAX_DEPTH env var to see more levels.');
124
- }
125
-
126
- main().catch(console.error);
package/src/index.ts DELETED
@@ -1,35 +0,0 @@
1
- // Main exports for the Trilium API client
2
- export {
3
- createTriliumClient,
4
- default as createClient,
5
- // Mapper utilities
6
- TriliumMapper,
7
- buildSearchQuery,
8
- transforms,
9
- } from './client.js';
10
-
11
- // Re-export types
12
- export type {
13
- TriliumNote,
14
- TriliumBranch,
15
- TriliumAttribute,
16
- TriliumAttachment,
17
- TriliumAppInfo,
18
- TriliumClientConfig,
19
- paths,
20
- components,
21
- // Mapper types
22
- MappingConfig,
23
- FieldMapping,
24
- TransformFunction,
25
- ComputedFunction,
26
- TriliumSearchHelpers,
27
- TriliumSearchConditions,
28
- TriliumSearchLogical,
29
- ComparisonOperator,
30
- ConditionValue,
31
- SearchValue,
32
- } from './client.js';
33
-
34
- // Re-export generated types for advanced usage
35
- export type { operations } from './generated/trilium.js';