-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
98 lines (82 loc) · 2.4 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { Plugin, Notice, Editor, requestUrl, PluginSettingTab, Setting } from 'obsidian';
import AmpliFlowSettingTab from 'settings';
interface AuthResponse {
status: number;
errors: Record<string, string>;
data: {
id: string;
username: string;
email: string;
token: string;
};
}
interface PagePayload {
title: string;
content: string;
}
interface AmpliFlowSettings {
tenant: string;
email: string;
password: string;
}
const DEFAULT_SETTINGS: AmpliFlowSettings = {
tenant: '',
email: '',
password: ''
};
export default class AmpliFlowPublisher extends Plugin {
settings: AmpliFlowSettings;
async onload() {
console.log('Loading AmpliFlow Publisher plugin');
await this.loadSettings();
this.addSettingTab(new AmpliFlowSettingTab(this.app, this));
this.addCommand({
id: 'publish-note',
name: 'Publish note to AmpliFlow',
editorCallback: (editor: Editor) => this.publishNoteToAmpliFlow(editor)
});
}
async publishNoteToAmpliFlow(editor: Editor) {
const noteContent = editor.getValue();
const noteTitle = this.app.workspace.getActiveFile()?.basename || 'Untitled';
try {
const token = await this.getAuthToken();
await this.createPage({ title: noteTitle, content: noteContent }, token);
new Notice('Page published successfully!');
} catch (error) {
console.error('Failed to publish page:', error);
new Notice('Failed to publish page.');
}
}
async getAuthToken(): Promise<string> {
const response = await requestUrl({
url: `https://${this.settings.tenant}.ampliflow.com/api/Auth/login`,
method: 'POST',
contentType: 'application/json',
body: JSON.stringify({
email: this.settings.email,
password: this.settings.password,
localeId: 'en'
})
});
const data: AuthResponse = response.json;
return data.data.token;
}
async createPage(payload: PagePayload, token: string): Promise<void> {
await requestUrl({
url: `https://${this.settings.tenant}.ampliflow.com/api/page`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(payload)
});
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}