Skip to content

Commit

Permalink
feat(zhi-lib-siyuan-api): add siyuan api
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 5, 2023
1 parent fdc0d82 commit c748669
Show file tree
Hide file tree
Showing 29 changed files with 1,591 additions and 10 deletions.
10 changes: 10 additions & 0 deletions packages/zhi-core/src/lib/util/ZhiCoreUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import { Env } from "@siyuan-community/zhi-env"
import { ZhiUtil } from "@siyuan-community/zhi-common"
import { SiyuanKernelApi } from "@siyuan-community/zhi-siyuan-api"

/**
* 工具类统一入口,每个应用自己实现
Expand All @@ -34,12 +35,21 @@ import { ZhiUtil } from "@siyuan-community/zhi-common"
* @since 1.0.0
*/
class ZhiCoreUtil extends ZhiUtil {
private static kApi: SiyuanKernelApi | undefined

public static override zhiEnv(): Env {
if (!this.env) {
this.env = new Env(import.meta.env)
}
return this.env
}

public static kernelApi() {
if (!this.kApi) {
this.kApi = new SiyuanKernelApi(this.zhiEnv())
}
return this.kApi
}
}

export default ZhiCoreUtil
14 changes: 8 additions & 6 deletions packages/zhi-core/src/lib/zhi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Bootstrap from "./core/Bootstrap"
class Zhi {
private readonly logger
private readonly common
private readonly kernelApi

private readonly runAs

Expand All @@ -54,6 +55,7 @@ class Zhi {
constructor(runAs: DeviceTypeEnum) {
this.logger = ZhiCoreUtil.zhiLog("zhi-core")
this.common = ZhiCoreUtil.zhiCommon()
this.kernelApi = ZhiCoreUtil.kernelApi()

this.runAs = runAs ?? DeviceTypeEnum.DeviceType_Node
}
Expand Down Expand Up @@ -90,9 +92,9 @@ class Zhi {
this.SUPPORTED_THEME_VERSION
)
this.logger.error(errMsg)
// this.kernelApi.pushErrMsg({
// msg: errMsg,
// })
this.kernelApi.pushErrMsg({
msg: errMsg,
})
return
}

Expand All @@ -103,9 +105,9 @@ class Zhi {
this.SUPPORTED_KERNEL_VERSION
)
this.logger.warn(warnMsg)
// this.kernelApi.pushMsg({
// msg: warnMsg,
// })
this.kernelApi.pushMsg({
msg: warnMsg,
})
return
}

Expand Down
6 changes: 3 additions & 3 deletions packages/zhi-lib-blog-api/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@siyuan-community/zhi-blog-api",
"version": "0.1.0",
"version": "1.1.1",
"type": "module",
"description": "a common blog interface",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"main": "./packages/zhi-lib-blog-api/src/index.js",
"typings": "./index.d.ts",
"repository": "terwer/zhi",
"homepage": "https://github.com/terwer/zhi/tree/main/packages/zhi-lib-blog-api",
"author": "terwer",
Expand Down
2 changes: 1 addition & 1 deletion packages/zhi-lib-blog-api/src/lib/blogApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class BlogApi implements IBlogApi {
*/
constructor(apiAdaptor: IBlogApi) {
this.logger = ZhiBlogApiUtil.zhiLog("zhi-blog-api")
this.VERSION = "1.0.0"
this.VERSION = "1.1.1"
this.apiAdaptor = apiAdaptor
}

Expand Down
11 changes: 11 additions & 0 deletions packages/zhi-lib-blog-api/src/lib/zhi-lib-blog-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@
*/

import BlogApi from "./blogApi"
import CategoryInfo from "./models/categoryInfo"
import type { IBlogApi } from "./IBlogApi"
import MediaObject from "./models/mediaObject"
import Post from "./models/post"
import PostStatusEnum from "./enums/postStatusEnum"
import UserBlog from "./models/userBlog"
import BlogConfig, { PasswordType } from "./blogConfig"
import BlogPlaceholder from "./blogPlaceholder"

export { BlogApi }
export { CategoryInfo, IBlogApi, MediaObject, Post, PostStatusEnum, UserBlog }
export { BlogConfig, PasswordType, BlogPlaceholder }
18 changes: 18 additions & 0 deletions packages/zhi-lib-siyuan-api/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
14 changes: 14 additions & 0 deletions packages/zhi-lib-siyuan-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# zhi-lib-siyuan-api
a siyuan-note api including both kernel and client

## Dev

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build zhi-lib-siyuan-api` to build the library.

## Running unit tests

Run `nx test zhi-lib-siyuan-api` to execute the unit tests via [Jest](https://jestjs.io).
18 changes: 18 additions & 0 deletions packages/zhi-lib-siyuan-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@siyuan-community/zhi-siyuan-api",
"version": "0.1.0",
"type": "module",
"description": "a siyuan-note api including both kernel and client",
"main": "./dist/index.js",
"typings": "./index.d.ts",
"repository": "terwer/zhi",
"homepage": "https://github.com/terwer/zhi/tree/main/packages/zhi-lib-siyuan-api",
"author": "terwer",
"license": "GPL",
"keywords": [
"zhi",
"siyuan",
"blog",
"api"
]
}
35 changes: 35 additions & 0 deletions packages/zhi-lib-siyuan-api/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "zhi-lib-siyuan-api",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/zhi-lib-siyuan-api/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/vite:build",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/zhi-lib-siyuan-api"
}
},
"publish": {
"command": "node tools/scripts/publish.mjs zhi-lib-siyuan-api {args.ver} {args.tag}",
"dependsOn": ["build"]
},
"test": {
"executor": "@nx/vite:test",
"outputs": ["coverage/packages/zhi-lib-siyuan-api"],
"options": {
"passWithNoTests": true,
"reportsDirectory": "../../coverage/packages/zhi-lib-siyuan-api"
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/zhi-lib-siyuan-api/**/*.ts"]
}
}
},
"tags": []
}
28 changes: 28 additions & 0 deletions packages/zhi-lib-siyuan-api/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.
*/

interface ImportMeta {
readonly env: ImportMetaEnv
}
26 changes: 26 additions & 0 deletions packages/zhi-lib-siyuan-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/

export * from "./lib/zhi-lib-siyuan-api"
77 changes: 77 additions & 0 deletions packages/zhi-lib-siyuan-api/src/lib/ISiyuanKernelApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.
*/

/**
* 思源 API 返回类型
*/
interface SiyuanData {
/**
* 非 0 为异常情况
*/
code: number

/**
* 正常情况下是空字符串,异常情况下会返回错误文案
*/
msg: string

/**
* 可能为 \{\}、[] 或者 NULL,根据不同接口而不同
*/
data: any[] | object | null | undefined
}

/**
* 思源笔记内核接口定义
*
* @see {@link https://github.com/siyuan-note/siyuan/blob/master/API_zh_CN.md#%E9%89%B4%E6%9D%83 siyuan-api}
* @see {@link https://github.com/leolee9086/noob-core/blob/master/frontEnd/noobApi/util/kernelApi.js kernelApi}
*/
interface ISiyuanKernelApi {
// /api/notebook/lsNotebooks
lsNotebooks(): Promise<SiyuanData>
// /api/notebook/openNotebook
openNotebook(notebookId: string): Promise<SiyuanData>
// /api/notebook/closeNotebook
closeNotebook(notebookId: string): Promise<SiyuanData>
// /api/notebook/renameNotebook
renameNotebook(notebookId: string, name: string): Promise<SiyuanData>
// /api/notebook/createNotebook
createNotebook(name: string): Promise<SiyuanData>
// /api/notebook/removeNotebook
removeNotebook(notebookId: string): Promise<SiyuanData>
// /api/notebook/getNotebookConf
getNotebookConf(notebookId: string): Promise<SiyuanData>
// /api/notebook/setNotebookConf
setNotebookConf(notebookConf: object): Promise<SiyuanData>

// /api/notification/pushMsg
pushMsg(msgObj: object): Promise<SiyuanData>
// /api/notification/pushErrMsg
pushErrMsg(msgObj: object): Promise<SiyuanData>
}

export default ISiyuanKernelApi
export { SiyuanData }
36 changes: 36 additions & 0 deletions packages/zhi-lib-siyuan-api/src/lib/siYuanApiAdaptor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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, expect } from "vitest"
import SiYuanApiAdaptor from "./siYuanApiAdaptor"
import SiyuanConfig from "./siyuanConfig"

describe("SiYuanApiAdaptor", () => {
it("should work", function () {
// const siyuanConfig = new SiyuanConfig("http://127.0.0.1:6806", "")
// const apiAdaptor = new SiYuanApiAdaptor(siyuanConfig)
// expect(apiAdaptor).toBeTruthy()
})
})
Loading

0 comments on commit c748669

Please sign in to comment.