Skip to content

Commit

Permalink
docs: update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jun 11, 2023
1 parent a7bb299 commit 415c2e3
Show file tree
Hide file tree
Showing 25 changed files with 1,872 additions and 11 deletions.
1 change: 1 addition & 0 deletions libs/zhi-siyuan-api/.env.development.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_DEBUG_MODE=true
4 changes: 4 additions & 0 deletions libs/zhi-siyuan-api/.eslintrc.cjs
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"],
}
3 changes: 3 additions & 0 deletions libs/zhi-siyuan-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.DS_Store
testdata
7 changes: 7 additions & 0 deletions libs/zhi-siyuan-api/CHANGELOG.md
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
89 changes: 89 additions & 0 deletions libs/zhi-siyuan-api/README.md
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
```
12 changes: 12 additions & 0 deletions libs/zhi-siyuan-api/index.html
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>
45 changes: 45 additions & 0 deletions libs/zhi-siyuan-api/package.json
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"
}
}
32 changes: 32 additions & 0 deletions libs/zhi-siyuan-api/src/index.spec.ts
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")
})
})
10 changes: 10 additions & 0 deletions libs/zhi-siyuan-api/src/index.ts
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 libs/zhi-siyuan-api/src/lib/adaptor/siYuanApiAdaptor.spec.ts
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)
})
})
Loading

0 comments on commit 415c2e3

Please sign in to comment.