General: use node-fetch to post notes

This commit is contained in:
2024-07-28 01:07:35 +01:00
parent 0addaa0d7f
commit 426f05dfbd
2 changed files with 37 additions and 22 deletions

View File

@@ -82,7 +82,8 @@ export async function uploadCommandGeneral(
const upload = new Upload2NotionGeneral(plugin, dbDetails); const upload = new Upload2NotionGeneral(plugin, dbDetails);
const res = await upload.syncMarkdownToNotionGeneral(basename, cover, tags, markDownData, nowFile, this.app); const res = await upload.syncMarkdownToNotionGeneral(basename, cover, tags, markDownData, nowFile, this.app);
if (res.status === 200) { const { response } = res;
if (response.status === 200) {
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green"; new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
} else { } else {
new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000); new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000);

View File

@@ -1,4 +1,4 @@
import { App, Notice, requestUrl, TFile } from "obsidian"; import { App, Notice, TFile } from "obsidian";
import { Client } from "@notionhq/client"; import { Client } from "@notionhq/client";
import { markdownToBlocks } from "@tryfabric/martian"; import { markdownToBlocks } from "@tryfabric/martian";
import * as yamlFrontMatter from "yaml-front-matter"; import * as yamlFrontMatter from "yaml-front-matter";
@@ -7,6 +7,7 @@ import MyPlugin from "src/main";
import { DatabaseDetails, PluginSettings } from "../../ui/settingTabs"; import { DatabaseDetails, PluginSettings } from "../../ui/settingTabs";
import { UploadBaseGeneral } from "./BaseUpload2NotionGeneral"; import { UploadBaseGeneral } from "./BaseUpload2NotionGeneral";
import { updateYamlInfo } from "../updateYaml"; import { updateYamlInfo } from "../updateYaml";
import fetch from 'node-fetch';
export class Upload2NotionGeneral extends UploadBaseGeneral { export class Upload2NotionGeneral extends UploadBaseGeneral {
settings: PluginSettings; settings: PluginSettings;
@@ -101,21 +102,30 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
}; };
} }
try { console.log(bodyString)
return await requestUrl({
url: `https://api.notion.com/v1/pages`, const response = await fetch("https://api.notion.com/v1/pages", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
// 'User-Agent': 'obsidian.md', "Authorization": "Bearer " + notionAPI,
Authorization:
"Bearer " + notionAPI,
"Notion-Version": "2022-06-28", "Notion-Version": "2022-06-28",
}, },
body: JSON.stringify(bodyString), body: JSON.stringify(bodyString),
}); });
} catch (error) {
new Notice(`network error ${error}`); const data: any = await response.json();
if (!response.ok) {
new Notice(`Error ${data.status}: ${data.code} \n Check the console for more information \n opt+cmd+i/ctrl+shift+i`, 5000);
console.log(`Error message: \n ${data.message}`);
} else {
console.log(`Page created: ${data.url}`);
console.log(`Page ID: ${data.id}`);
}
return {
response, // for status code
data // for id and url
} }
} }
@@ -154,11 +164,15 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
} else { } else {
res = await this.createPage(title, cover, tags, file2Block); res = await this.createPage(title, cover, tags, file2Block);
} }
if (res.status === 200) {
await updateYamlInfo(markdown, nowFile, res, app, this.plugin, this.dbDetails); let {response, data} = res;
} else {
new Notice(`${res.text}`); // console.log(response)
if (response && response.status === 200) {
await updateYamlInfo(markdown, nowFile, data, app, this.plugin, this.dbDetails);
} }
return res; return res;
} }
} }