Skip to content

Commit

Permalink
feat: support check already exist tiddler
Browse files Browse the repository at this point in the history
  • Loading branch information
oeyoews committed Apr 2, 2024
1 parent bd546c9 commit cb7094f
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions utils/save2TiddlyWiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const save2TiddlyWiki = async (
tag: string[],
status: Ref<IStatus>
) => {
const baseURL = `http://localhost:${port}/recipes/default/tiddlers`;

const tags = tag
.map(function (tag) {
if (tag.includes(' ')) {
Expand All @@ -30,10 +32,14 @@ const save2TiddlyWiki = async (

const currentTime = formattime(new Date());

const baseURL = `http://localhost:${port}`;
const savetwFetch = ofetch.create({
baseURL,
method: 'PUT',
retry: 0,
headers: {
'Content-Type': 'application/json',
'x-requested-with': 'TiddlyWiki',
},
async onResponse({ request, response, options }) {
if (response.ok) {
notify({
Expand All @@ -43,30 +49,49 @@ const save2TiddlyWiki = async (
}
},
async onResponseError({ request, response, options }) {
console.log('[fetch error]', response.status);
notify({
message: '保存失败' + response._data,
type: 'error',
});
},
});

await savetwFetch(`/recipes/default/tiddlers/${title}`, {
method: 'PUT',
const getTwFetch = ofetch.create({
baseURL,
method: 'GET',
retry: 0,
headers: {
'Content-Type': 'application/json',
'x-requested-with': 'TiddlyWiki',
},
body: {
text,
creator: status.value.username,
type: 'text/markdown',
url,
created: currentTime,
modified: currentTime,
tags,
async onResponse({ request, response, options }) {
switch (response.status) {
case 200:
notify({
message: `${title} 已存在`,
type: 'error',
});
break;
case 404:
await savetwFetch(`/${title}`, {
body: {
text,
creator: status.value.username,
type: 'text/markdown',
url,
created: currentTime,
modified: currentTime,
tags,
},
});
break;
default:
break;
}
},
});

await getTwFetch(`/${title}`);
};

export default save2TiddlyWiki;

0 comments on commit cb7094f

Please sign in to comment.