Skip to content

Commit

Permalink
feat: Add msg api for kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Apr 2, 2023
1 parent f3c82c9 commit 546b94f
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"clsx": "^1.2.1",
"commander": "^10.0.0",
"compare-versions": "6.0.0-rc.1",
"cross-fetch": "^3.1.5",
"enquirer": "^2.3.6",
"git-clone": "^0.2.0",
"handlebars": "^4.7.7",
Expand Down
39 changes: 39 additions & 0 deletions packages/zhi-siyuan-api/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 } from "vitest"
import fetch from "cross-fetch"

// Add `fetch` polyfill.
// https://markus.oberlehner.net/blog/using-mock-service-worker-with-vitest-and-fetch/
global.fetch = fetch

beforeEach(() => {
console.log("======test is starting...======")
})

afterEach(() => {
console.log("======test is finished.========")
})
25 changes: 25 additions & 0 deletions packages/zhi-siyuan-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +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.
*/

import SiyuanApi from "./lib/zhi-siyuan-api"
import SiyuanKernelApi from "./lib/siyuanKernelApi"
import type { SiyuanData } from "./lib/ISiyuanKernelApi"
Expand Down
5 changes: 5 additions & 0 deletions packages/zhi-siyuan-api/src/lib/ISiyuanKernelApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ interface ISiyuanKernelApi {
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
Expand Down
18 changes: 18 additions & 0 deletions packages/zhi-siyuan-api/src/lib/siyuanKernelApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,22 @@ describe("SiyuanKernelApi", () => {
})
console.log("result=>", result)
})

it("pushMsg", async () => {
const env = new Env(import.meta.env)
const kernelApi = new SiyuanKernelApi(env)
const result = await kernelApi.pushMsg({
msg: "测试消息",
})
console.log("result=>", result)
})

it("pushErrMsg", async () => {
const env = new Env(import.meta.env)
const kernelApi = new SiyuanKernelApi(env)
const result = await kernelApi.pushErrMsg({
msg: "测试错误消息",
})
console.log("result=>", result)
})
})
68 changes: 68 additions & 0 deletions packages/zhi-siyuan-api/src/lib/siyuanKernelApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,74 @@ class SiyuanKernelApi implements ISiyuanKernelApi {
public async setNotebookConf(notebookConf: object): Promise<SiyuanData> {
return await this.siyuanRequest("/api/notebook/setNotebookConf", notebookConf)
}

/**
* 推送消息
*
* 参数
*
* ```json
* {
* "msg": "test",
* "timeout": 7000
* }
* ```
*
* timeout:消息持续显示时间,单位为毫秒。可以不传入该字段,默认为 7000 毫秒
*
* 返回值
*
* ```
* {
* "code": 0,
* "msg": "",
* "data": {
* "id": "62jtmqi"
* }
* }
*
* id:消息 ID
* ```
*
* @param msgObj 消息体
*/
public async pushMsg(msgObj: object): Promise<SiyuanData> {
return await this.siyuanRequest("/api/notification/pushMsg", msgObj)
}

/**
* 推送报错消息
*
* 参数
*
* ```
* {
* "msg": "test",
* "timeout": 7000
* }
* ```
*
* timeout:消息持续显示时间,单位为毫秒。可以不传入该字段,默认为 7000 毫秒
*
* 返回值
*
* ```
* {
* "code": 0,
* "msg": "",
* "data": {
* "id": "qc9znut"
* }
* }
*
* id:消息 ID
* ```
*
* @param msgObj
*/
public async pushErrMsg(msgObj: object): Promise<SiyuanData> {
return await this.siyuanRequest("/api/notification/pushErrMsg", msgObj)
}
}

export default SiyuanKernelApi
25 changes: 25 additions & 0 deletions packages/zhi-siyuan-api/src/lib/zhi-siyuan-api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +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.
*/

import SiyuanApi from "./zhi-siyuan-api"
import { expect } from "vitest"

Expand Down
25 changes: 25 additions & 0 deletions packages/zhi-siyuan-api/src/lib/zhi-siyuan-api.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
/*
* 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 SiyuanApi from "./siyuanApi"
export default SiyuanApi
1 change: 1 addition & 0 deletions packages/zhi-siyuan-api/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default defineConfig({
cache: {
dir: "../../node_modules/.vitest",
},
setupFiles: ["./setup.ts"],
environment: "jsdom",
include: ["src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
},
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 546b94f

Please sign in to comment.