Skip to content

Commit

Permalink
feat: Add api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 31, 2023
1 parent 47e9fe1 commit 9c1a5e5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/zhi-blog-api/src/lib/blogConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export enum PageType {
/**
* MDX
*
* @see https://mdxjs.com/
* @see {@link https://mdxjs.com/ mdx}
*/
MDX,
}
Expand Down
2 changes: 2 additions & 0 deletions packages/zhi-siyuan-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import SiyuanApi from "./lib/zhi-siyuan-api"
import { SiyuanData } from "./lib/siyuanKernelApi"

export default SiyuanApi
export { SiyuanData }
44 changes: 35 additions & 9 deletions packages/zhi-siyuan-api/src/lib/siyuanKernelApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,47 @@
* questions.
*/

import Env, { EnvConstants } from "zhi-env"
import Env from "zhi-env"
import SiyuanConfig from "./SiyuanConfig"
import LogFactory, { DefaultLogger, EnvHelper, LogConstants, LogLevelEnum } from "zhi-log"
import LogFactory, { DefaultLogger, EnvHelper, LogLevelEnum } from "zhi-log"
import { name, version } from "../../package.json"
import ZhiCommon from "zhi-common"
import SiyuanConstants from "./SiyuanConstants"

/**
* 思源笔记服务端API v2.0.27
* https://github.com/siyuan-note/siyuan/blob/master/API_zh_CN.md#%E9%89%B4%E6%9D%83
* https://github.com/leolee9086/noob-core/blob/master/frontEnd/noobApi/util/kernelApi.js
* 思源 API 返回类型
*/
export interface SiyuanData {
/**
* 非 0 为异常情况
*/
code: number

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

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

/**
* 思源笔记服务端API v2.8.2
*
* 1. 均是 POST 方法
*
* 2. 需要带参的接口,参数为 JSON 字符串,放置到 body 里,标头 Content-Type 为 application/json
*
* 3. 鉴权:在 `设置` - `关于` 里查看 API token,请求标头:Authorization: Token xxx
*
* @public
* @author terwer
* @since 1.0.0
* @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}
*/
class SiyuanKernelApi {
/**
Expand Down Expand Up @@ -83,7 +109,7 @@ class SiyuanKernelApi {
WHERE 1 = 1
AND (b1.root_id ='${keyword}' OR (b1.content LIKE '%${keyword}%') OR (b1.tag LIKE '%${keyword}%')
)`
const data = await this.sql(stmt)
const data = (await this.sql(stmt)) as any[]
this.logger.debug("getRootBlocksCount data=>", data[0].count)
return data[0].count
}
Expand All @@ -92,8 +118,8 @@ class SiyuanKernelApi {
* 以sql发送请求
* @param sql sql
*/
public async sql(sql: string): Promise<any> {
const sqldata = {
public async sql(sql: string): Promise<SiyuanData["data"]> {
const sqldata: { stmt: string } = {
stmt: sql,
}
const url = "/api/query/sql"
Expand All @@ -107,7 +133,7 @@ class SiyuanKernelApi {
* @param url - url
* @param data - 数据
*/
public async siyuanRequest(url: string, data: any): Promise<any> {
public async siyuanRequest(url: string, data: object): Promise<SiyuanData> {
const reqUrl = `${this.siyuanConfig.apiUrl}${url}`

const fetchOps = {
Expand Down

0 comments on commit 9c1a5e5

Please sign in to comment.