-
Notifications
You must be signed in to change notification settings - Fork 2
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
6 changed files
with
232 additions
and
29 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
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,93 @@ | ||
/* | ||
* 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, [email protected] | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { createLogger } from "../utils/simple-logger" | ||
import { isDev, siyuanApiToken, siyuanApiUrl } from "../Constants" | ||
|
||
/** | ||
* 思源 API 返回类型 | ||
*/ | ||
export interface SiyuanData { | ||
/** | ||
* 非 0 为异常情况 | ||
*/ | ||
code: number | ||
|
||
/** | ||
* 正常情况下是空字符串,异常情况下会返回错误文案 | ||
*/ | ||
msg: string | ||
|
||
/** | ||
* 可能为 \{\}、[] 或者 NULL,根据不同接口而不同 | ||
*/ | ||
data: any[] | object | null | undefined | ||
} | ||
|
||
export class BaseApi { | ||
private logger | ||
|
||
constructor() { | ||
this.logger = createLogger("base-api") | ||
} | ||
|
||
/** | ||
* 向思源请求数据 | ||
* | ||
* @param url - url | ||
* @param data - 数据 | ||
*/ | ||
public async siyuanRequest(url: string, data: object): Promise<SiyuanData> { | ||
const reqUrl = `${siyuanApiUrl}${url}` | ||
|
||
const fetchOps = { | ||
body: JSON.stringify(data), | ||
method: "POST", | ||
} | ||
if (siyuanApiToken !== "") { | ||
Object.assign(fetchOps, { | ||
headers: { | ||
Authorization: `Token ${siyuanApiToken}`, | ||
}, | ||
}) | ||
} | ||
|
||
if (isDev) { | ||
this.logger.info("开始向思源请求数据,reqUrl=>", reqUrl) | ||
this.logger.info("开始向思源请求数据,fetchOps=>", fetchOps) | ||
} | ||
|
||
const response = await fetch(reqUrl, fetchOps) | ||
const resJson = await response.json() | ||
if (isDev) { | ||
this.logger.info("思源请求数据返回,resJson=>", resJson) | ||
} | ||
|
||
if (resJson.code === -1) { | ||
throw new Error(resJson.msg) | ||
} | ||
return resJson.code === 0 ? resJson.data : null | ||
} | ||
} |
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,113 @@ | ||
/* | ||
* 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, [email protected] | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { BaseApi, SiyuanData } from "./base-api" | ||
|
||
/** | ||
* 思源笔记服务端API v2.8.8 | ||
* | ||
* @author terwer | ||
* @version 1.0.0 | ||
* @since 1.0.0 | ||
*/ | ||
class KernelApi extends BaseApi { | ||
/** | ||
* 推送消息 | ||
* | ||
* 参数 | ||
* | ||
* ```json | ||
* { | ||
* "msg": "test", | ||
* "timeout": 7000 | ||
* } | ||
* ``` | ||
* | ||
* timeout:消息持续显示时间,单位为毫秒。可以不传入该字段,默认为 7000 毫秒 | ||
* | ||
* 返回值 | ||
* | ||
* ``` | ||
* { | ||
* "code": 0, | ||
* "msg": "", | ||
* "data": { | ||
* "id": "62jtmqi" | ||
* } | ||
* } | ||
* | ||
* id:消息 ID | ||
* ``` | ||
* | ||
* @param msg 消息 | ||
*/ | ||
public async pushMsg(msg: string): Promise<SiyuanData> { | ||
const msgObj = { | ||
msg: msg, | ||
timeout: 3000, | ||
} | ||
return await this.siyuanRequest("/api/notification/pushMsg", msgObj) | ||
} | ||
|
||
/** | ||
* 推送报错消息 | ||
* | ||
* 参数 | ||
* | ||
* ``` | ||
* { | ||
* "msg": "test", | ||
* "timeout": 7000 | ||
* } | ||
* ``` | ||
* | ||
* timeout:消息持续显示时间,单位为毫秒。可以不传入该字段,默认为 7000 毫秒 | ||
* | ||
* 返回值 | ||
* | ||
* ``` | ||
* { | ||
* "code": 0, | ||
* "msg": "", | ||
* "data": { | ||
* "id": "qc9znut" | ||
* } | ||
* } | ||
* | ||
* id:消息 ID | ||
* ``` | ||
* | ||
* @param msg - 错误消息 | ||
*/ | ||
public async pushErrMsg(msg: string): Promise<SiyuanData> { | ||
const msgObj = { | ||
msg: msg, | ||
timeout: 3000, | ||
} | ||
return await this.siyuanRequest("/api/notification/pushErrMsg", msgObj) | ||
} | ||
} | ||
|
||
export default KernelApi |
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
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
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