-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,872 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
VITE_DEBUG_MODE=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ["./node_modules/@terwer/eslint-config-custom/typescript/index.cjs"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
.DS_Store | ||
testdata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# zhi-siyuan-api | ||
|
||
## 1.8.0 | ||
|
||
### Minor Changes | ||
|
||
- new kernel api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# zhi-siyuan-api | ||
|
||
a siyuan-note api including both kernel and client | ||
|
||
## Usage | ||
|
||
```ts | ||
import { SiyuanKernelApi } from "zhi-siyuan-api" | ||
import { ZhiUtil } from "zhi-common" | ||
|
||
// appInstance | ||
const appInstance: any = {} | ||
appInstance.zhiCommon = { | ||
ZhiUtil: ZhiUtil | ||
} | ||
console.log(appInstance) | ||
|
||
// kernelApi | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
const kernelApi = new SiyuanKernelApi(appInstance, siyuanConfig) | ||
const result = await kernelApi.lsNotebooks() | ||
console.log("result=>", result) | ||
``` | ||
|
||
dynamic invoke | ||
|
||
```ts | ||
import { SiyuanKernelApi } from "zhi-siyuan-api" | ||
|
||
// appInstance也可以动态加载,减小打包体积 | ||
const appInstance: any = {} | ||
|
||
// polyfills | ||
const moduleBase = "" | ||
console.log("moduleBase=>", moduleBase) | ||
// https://github.com/terwer/siyuan-plugin-publisher/blob/main/public/polyfills/fs.js | ||
appInstance.fs = (await import(`${moduleBase}/polyfills/fs.js`))["default"] | ||
// https://github.com/terwer/siyuan-plugin-publisher/blob/main/public/polyfills/path.js | ||
appInstance.path = (await import(`${moduleBase}/polyfills/path.js`))["default"] | ||
appInstance.importDep = async (moduleName) => { | ||
return await import(appInstance.path.join(moduleBase, moduleName)) | ||
} | ||
|
||
const zhiCommon = (await appInstance.importDep("./libs/zhi-common/index.js")) as any | ||
appInstance.zhiCommon = { | ||
ZhiUtil: zhiCommon["ZhiUtil"], | ||
} | ||
console.log(appInstance) | ||
|
||
// kernelApi | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
const kernelApi = new SiyuanKernelApi(appInstance, siyuanConfig) | ||
const result = await kernelApi.lsNotebooks() | ||
console.log("result=>", result) | ||
``` | ||
|
||
## Deps | ||
|
||
``` | ||
├── zhi-common - [dynaminc dependency] | ||
├── zhi-blog-api | ||
├── zhi-lib-base | ||
``` | ||
|
||
## Dev | ||
|
||
```bash | ||
pnpm dev -F zhi-siyuan-api | ||
``` | ||
|
||
## Build | ||
|
||
```bash | ||
pnpm build -F zhi-siyuan-api | ||
``` | ||
|
||
## Test | ||
|
||
Execute the unit tests via [vitest](https://vitest.dev) | ||
|
||
```bash | ||
pnpm test -F zhi-siyuan-api | ||
``` | ||
|
||
## Publish | ||
|
||
```bash | ||
pnpm publish -F zhi-siyuan-api --tag latest | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite + Vue + TS</title> | ||
</head> | ||
<body> | ||
This file is for lib hot-load test only, see <a href="/src/index.ts">/src/index.ts</a> | ||
<script type="module" src="/src/index.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "zhi-siyuan-api", | ||
"version": "1.8.0", | ||
"type": "module", | ||
"description": "a siyuan-note api including both kernel and client", | ||
"main": "./dist/index.js", | ||
"typings": "./dist/index.d.ts", | ||
"repository": "terwer/zhi", | ||
"homepage": "https://github.com/terwer/zhi/tree/main/libs/zhi-siyuan-api", | ||
"author": "terwer", | ||
"license": "GPL", | ||
"files": [ | ||
"dist", | ||
"README.md" | ||
], | ||
"keywords": [ | ||
"zhi", | ||
"lib", | ||
"siyuan", | ||
"note", | ||
"api" | ||
], | ||
"scripts": { | ||
"serve": "vite", | ||
"dev": "vite build --watch", | ||
"build": "vite build", | ||
"start": "vite preview", | ||
"test": "vitest --watch" | ||
}, | ||
"devDependencies": { | ||
"@terwer/eslint-config-custom": "workspace:*", | ||
"@terwer/tsconfig": "workspace:*", | ||
"@terwer/vite-config-custom": "workspace:*", | ||
"form-data": "^4.0.0" | ||
}, | ||
"dependencies": { | ||
"cross-fetch": "^3.1.6", | ||
"zhi-blog-api": "workspace:*", | ||
"zhi-common": "workspace:*", | ||
"zhi-lib-base": "workspace:*" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { describe, it } from "vitest" | ||
|
||
describe("zhi-siyuan-api", () => { | ||
it("index", () => { | ||
console.log("hello world") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import SiyuanApi from "./lib/zhi-siyuan-api" | ||
import SiyuanKernelApi from "./lib/kernel/siyuanKernelApi" | ||
import type { SiyuanData } from "./lib/kernel/ISiyuanKernelApi" | ||
import SiyuanConfig from "./lib/config/siyuanConfig" | ||
import SiYuanApiAdaptor from "./lib/adaptor/siYuanApiAdaptor" | ||
import SiyuanConstants from "./lib/siyuanConstants" | ||
|
||
export { SiyuanApi } | ||
export { SiyuanData, SiyuanKernelApi } | ||
export { SiyuanConstants, SiyuanConfig, SiYuanApiAdaptor } |
173 changes: 173 additions & 0 deletions
173
libs/zhi-siyuan-api/src/lib/adaptor/siYuanApiAdaptor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* Copyright (c) 2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { afterEach, beforeEach, describe, expect, it } from "vitest" | ||
import SiYuanApiAdaptor from "./siYuanApiAdaptor" | ||
import SiyuanConfig from "../config/siyuanConfig" | ||
import path from "path" | ||
import { MediaObject, Post } from "zhi-blog-api" | ||
import fs from "fs" | ||
import SiyuanKernelApi from "../kernel/siyuanKernelApi" | ||
|
||
describe("SiYuanApiAdaptor", async () => { | ||
// appInstance | ||
const appInstance: any = {} | ||
const projectBase = path.resolve(__dirname, "../../..") | ||
const moduleBase = path.resolve(__dirname, "../../../../..") | ||
const zhiCommon = (await import(path.join(moduleBase, "libs/zhi-common/dist/index.js"))) as any | ||
appInstance.zhiCommon = { | ||
ZhiUtil: zhiCommon["ZhiUtil"], | ||
} | ||
// lute | ||
require(path.join(moduleBase, "libs/zhi-common/public/libs/lute/lute-1.7.5-20230410.min.cjs")) | ||
|
||
beforeEach(async () => { | ||
console.log("======test is starting...======") | ||
}) | ||
|
||
afterEach(() => { | ||
console.log("======test is finished.========") | ||
}) | ||
|
||
it("test apiAdaptor", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
|
||
expect(apiAdaptor).toBeTruthy() | ||
}) | ||
|
||
it("test siyuan getUsersBlogs", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
|
||
const usersBlogs = await apiAdaptor.getUsersBlogs() | ||
console.log(usersBlogs) | ||
}) | ||
|
||
it("test siyuan getRecentPostsCount", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
|
||
const recentPostsCount = await apiAdaptor.getRecentPostsCount() | ||
console.log(recentPostsCount) | ||
}) | ||
|
||
it("test siyuan getRecentPosts", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
|
||
const recentPosts = await apiAdaptor.getRecentPosts(10) | ||
console.log(recentPosts) | ||
}) | ||
|
||
it("test siyuan newPost", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
siyuanConfig.notebook = "20230506132031-qbtyjdk" | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
|
||
const post = new Post() | ||
post.title = "自动发布的测试标题" | ||
post.description = "自动发布的测试内容" | ||
post.mt_keywords = "标签1,标签2" | ||
post.categories = ["分类1", "分类2"] | ||
// post.dateCreated = new Date() | ||
const postid = await apiAdaptor.newPost(post) | ||
console.log(postid) | ||
}) | ||
|
||
it("test siyuan getPost", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
|
||
const postid = "20230526221603-3mgotyw" | ||
const post = await apiAdaptor.getPost(postid) | ||
console.log(post) | ||
}) | ||
|
||
it("test siyuan editPost", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
|
||
const postid = "20230526221603-3mgotyw" | ||
const post = new Post() | ||
post.title = "自动发布的测试标题2" | ||
post.description = "# 自动发布的测试内容2" | ||
post.mt_keywords = "标签1,标签2" | ||
post.categories = ["分类1", "分类2"] | ||
// post.dateCreated = new Date() | ||
const data = await apiAdaptor.editPost(postid, post) | ||
console.log(data) | ||
}) | ||
|
||
it("test siyuan deletePost", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
siyuanConfig.notebook = "20230506132031-qbtyjdk" | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
|
||
const postid = "20230526224518-oea9ey7" | ||
const data = await apiAdaptor.deletePost(postid) | ||
console.log(data) | ||
}) | ||
|
||
it("test siyuan getCategories", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
|
||
const data = await apiAdaptor.getCategories() | ||
console.log(data) | ||
}) | ||
|
||
it("test siyuan getPreviewUrl", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
siyuanConfig.home = "http://127.0.0.1:6806" | ||
siyuanConfig.previewUrl = "siyuan://blocks/[postid]" | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
|
||
const postid = "20230526221603-3mgotyw" | ||
const data = await apiAdaptor.getPreviewUrl(postid) | ||
console.log(data) | ||
}) | ||
|
||
it("test siyuan newMediaObject", async () => { | ||
const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "") | ||
const apiAdaptor = new SiYuanApiAdaptor(appInstance, siyuanConfig) | ||
const siyuanKernelApi = new SiyuanKernelApi(appInstance, siyuanConfig) | ||
|
||
const url = path.join(projectBase, "./testdata/photo.jpg") | ||
const file = fs.readFileSync(url) | ||
const mediaObject = new MediaObject("20220616-132401-001.jpg", "image/jpeg", file) | ||
const data = await apiAdaptor.newMediaObject(mediaObject, async () => { | ||
const FormData = require("form-data") | ||
const formData = new FormData() | ||
formData.append("file[]", mediaObject.bits, mediaObject.name) | ||
formData.append("assetsDirPath", "/assets/") | ||
|
||
const data = await siyuanKernelApi.uploadAsset(formData) | ||
console.log("uploadAsset=>", data) | ||
return data | ||
}) | ||
console.log("test newMediaObject finished=>", data) | ||
}) | ||
}) |
Oops, something went wrong.