diff --git a/package.json b/package.json index e1cbdef7..a34cb996 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/zhi-siyuan-api/setup.ts b/packages/zhi-siyuan-api/setup.ts new file mode 100644 index 00000000..86604548 --- /dev/null +++ b/packages/zhi-siyuan-api/setup.ts @@ -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.========") +}) diff --git a/packages/zhi-siyuan-api/src/index.ts b/packages/zhi-siyuan-api/src/index.ts index bd97c85a..54f75b4e 100644 --- a/packages/zhi-siyuan-api/src/index.ts +++ b/packages/zhi-siyuan-api/src/index.ts @@ -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" diff --git a/packages/zhi-siyuan-api/src/lib/ISiyuanKernelApi.ts b/packages/zhi-siyuan-api/src/lib/ISiyuanKernelApi.ts index b1d3e4ce..95204e9a 100644 --- a/packages/zhi-siyuan-api/src/lib/ISiyuanKernelApi.ts +++ b/packages/zhi-siyuan-api/src/lib/ISiyuanKernelApi.ts @@ -66,6 +66,11 @@ interface ISiyuanKernelApi { getNotebookConf(notebookId: string): Promise // /api/notebook/setNotebookConf setNotebookConf(notebookConf: object): Promise + + // /api/notification/pushMsg + pushMsg(msgObj: object): Promise + // /api/notification/pushErrMsg + pushErrMsg(msgObj: object): Promise } export default ISiyuanKernelApi diff --git a/packages/zhi-siyuan-api/src/lib/siyuanKernelApi.spec.ts b/packages/zhi-siyuan-api/src/lib/siyuanKernelApi.spec.ts index 36f6aad5..eedd5ce5 100644 --- a/packages/zhi-siyuan-api/src/lib/siyuanKernelApi.spec.ts +++ b/packages/zhi-siyuan-api/src/lib/siyuanKernelApi.spec.ts @@ -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) + }) }) diff --git a/packages/zhi-siyuan-api/src/lib/siyuanKernelApi.ts b/packages/zhi-siyuan-api/src/lib/siyuanKernelApi.ts index c8a0d58e..bbf5502a 100644 --- a/packages/zhi-siyuan-api/src/lib/siyuanKernelApi.ts +++ b/packages/zhi-siyuan-api/src/lib/siyuanKernelApi.ts @@ -239,6 +239,74 @@ class SiyuanKernelApi implements ISiyuanKernelApi { public async setNotebookConf(notebookConf: object): Promise { 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 { + 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 { + return await this.siyuanRequest("/api/notification/pushErrMsg", msgObj) + } } export default SiyuanKernelApi diff --git a/packages/zhi-siyuan-api/src/lib/zhi-siyuan-api.spec.ts b/packages/zhi-siyuan-api/src/lib/zhi-siyuan-api.spec.ts index 25973f1b..a6353efb 100644 --- a/packages/zhi-siyuan-api/src/lib/zhi-siyuan-api.spec.ts +++ b/packages/zhi-siyuan-api/src/lib/zhi-siyuan-api.spec.ts @@ -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" diff --git a/packages/zhi-siyuan-api/src/lib/zhi-siyuan-api.ts b/packages/zhi-siyuan-api/src/lib/zhi-siyuan-api.ts index 674e462f..8de277f1 100644 --- a/packages/zhi-siyuan-api/src/lib/zhi-siyuan-api.ts +++ b/packages/zhi-siyuan-api/src/lib/zhi-siyuan-api.ts @@ -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 diff --git a/packages/zhi-siyuan-api/vite.config.ts b/packages/zhi-siyuan-api/vite.config.ts index 01b16808..b4a6d446 100644 --- a/packages/zhi-siyuan-api/vite.config.ts +++ b/packages/zhi-siyuan-api/vite.config.ts @@ -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}"], }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1da273a3..d85d20a1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -22,6 +22,9 @@ dependencies: compare-versions: specifier: 6.0.0-rc.1 version: 6.0.0-rc.1 + cross-fetch: + specifier: ^3.1.5 + version: 3.1.5 enquirer: specifier: ^2.3.6 version: 2.3.6