Skip to content

Commit

Permalink
feat: 升级fetch组件
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 5, 2023
1 parent f7f283f commit 48d808c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 69 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@
"vue-i18n": "^9.2.2",
"vue-router": "^4.2.4",
"xmlbuilder2": "^3.1.1",
"zhi-blog-api": "^1.20.9",
"zhi-common": "^1.11.7",
"zhi-blog-api": "^1.20.10",
"zhi-common": "^1.11.8",
"zhi-device": "^2.3.0",
"zhi-fetch-middleware": "^0.2.7",
"zhi-github-middleware": "^0.1.4",
"zhi-lib-base": "^0.4.3",
"zhi-siyuan-api": "^1.29.10",
"zhi-xmlrpc-middleware": "^0.4.2"
"zhi-fetch-middleware": "^0.2.8",
"zhi-github-middleware": "^0.1.5",
"zhi-lib-base": "^0.4.4",
"zhi-siyuan-api": "^1.29.11",
"zhi-xmlrpc-middleware": "^0.4.3"
}
}
78 changes: 39 additions & 39 deletions pnpm-lock.yaml

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

52 changes: 36 additions & 16 deletions src/adaptors/api/base/baseBlogApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import { AppInstance } from "~/src/appInstance.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
import { useSiyuanDevice } from "~/src/composables/useSiyuanDevice.ts"
import { JsonUtil } from "zhi-common"
import { JsonUtil, ObjectUtil, StrUtil } from "zhi-common"
import { isDev } from "~/src/utils/constants.ts"

/**
* API授权统一封装基类
Expand Down Expand Up @@ -61,7 +62,7 @@ export class BaseBlogApi extends BlogApi {
const { kernelApi, isStorageViaSiyuanApi } = useSiyuanApi()
const { isInChromeExtension, isInSiyuanWidget } = useSiyuanDevice()
this.kernelApi = kernelApi
this.commonFetchClient = new CommonFetchClient(appInstance)
this.commonFetchClient = new CommonFetchClient(appInstance, cfg.apiUrl, cfg.middlewareUrl, isDev)
this.isInChromeExtension = isInChromeExtension()
this.isInSiyuanWidget = isInSiyuanWidget()
this.isStorageViaSiyuanApi = isStorageViaSiyuanApi()
Expand All @@ -87,12 +88,24 @@ export class BaseBlogApi extends BlogApi {
method: "GET" | "POST" | "PUT" | "DELETE" = "GET",
contentType: string = "application/json"
): Promise<any> {
let body
// 兼容字符类型的body
if (typeof params === "string" && !StrUtil.isEmptyString(params)) {
body = JSON.stringify(params)
}
// 兼容object类型的body
if (typeof params === "object" && !ObjectUtil.isEmptyObject(params)) {
body = params
}

if (this.isInChromeExtension) {
this.logger.info("using chrome background")
const fetchOptions = {
const fetchOptions: any = {
method: method,
headers: headers,
body: params,
}
if (body) {
fetchOptions.body = body
}
this.logger.info("commonFetchClient from proxyFetch url =>", url)
this.logger.info("commonFetchClient from proxyFetch fetchOptions =>", fetchOptions)
Expand All @@ -102,15 +115,20 @@ export class BaseBlogApi extends BlogApi {
} else if (this.isInSiyuanWidget) {
this.logger.info("using siyuan electron api")
const apiUrl = `${this.cfg.apiUrl}${url}`
let header = {}
if (headers.length > 0) {
header = headers[0]
const header = headers.length > 0 ? headers[0] : {}
let fetchOptions: any = {
method,
headers: {
...header,
...{
"Content-Type": contentType,
},
},
}
const fetchOptions = {
method: method,
headers: header,
body: params,
if (body) {
fetchOptions.body = body
}

this.logger.info("commonFetchClient from electron url =>", apiUrl)
this.logger.info("commonFetchClient from electron fetchOptions =>", fetchOptions)
const res = await this.commonFetchClient.fetchCall(apiUrl, fetchOptions)
Expand All @@ -122,25 +140,27 @@ export class BaseBlogApi extends BlogApi {
this.logger.info("commonFetchClient from forwardProxy url =>", apiUrl)
this.logger.info("commonFetchClient from forwardProxy fetchOptions =>", {
headers,
params,
body,
method,
contentType,
})
const fetchResult = await this.kernelApi.forwardProxy(apiUrl, headers, params, method, contentType, 7000)
const fetchResult = await this.kernelApi.forwardProxy(apiUrl, headers, body, method, contentType, 7000)
this.logger.debug("proxyFetch result=>", fetchResult)
const resText = fetchResult?.body
const res = JsonUtil.safeParse<any>(resText, {} as any)
return res
} else {
this.logger.info("using middleware proxy")
const fetchOptions = {
const fetchOptions: any = {
method: method,
headers: headers,
body: params,
}
if (body) {
fetchOptions.body = body
}
this.logger.info("commonFetchClient from proxyFetch url =>", url)
this.logger.info("commonFetchClient from proxyFetch fetchOptions =>", fetchOptions)
const res = await this.commonFetchClient.fetchCall(url, fetchOptions, this.cfg.middlewareUrl)
const res = await this.commonFetchClient.fetchCall(url, fetchOptions)
this.logger.debug("commonFetchClient res from proxyFetch =>", res)
return res
}
Expand Down
7 changes: 4 additions & 3 deletions src/adaptors/api/yuque/adaptor/yuqueApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,17 @@ class YuqueApiAdaptor extends BaseBlogApi {
body: content,
}
const result = await this.proxyFetch(url, [headers], params, "POST", "application/json")
this.logger.debug("yuqueRequest addDoc=>", result)
const resultData = result.data
this.logger.debug("yuqueRequest addDoc=>", resultData)
if (!result) {
throw new Error("请求语雀API异常")
}

// 包含了笔记本需要返回标识笔记本的ID,否则更新可能报错
if (repo) {
return `${result.id}_${repo}`
return `${resultData.id}_${repo}`
} else {
return `${result.id}`
return `${resultData.id}`
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/adaptors/web/base/baseWebApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { createAppLogger } from "~/src/utils/appLogger.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
import { useSiyuanDevice } from "~/src/composables/useSiyuanDevice.ts"
import { JsonUtil } from "zhi-common"
import {isDev} from "~/src/utils/constants.ts";

/**
* 网页授权统一封装基类
Expand Down Expand Up @@ -60,7 +61,7 @@ class BaseWebApi extends WebApi {
const { kernelApi } = useSiyuanApi()
const { isInSiyuanWidget, isInChromeExtension } = useSiyuanDevice()
this.kernelApi = kernelApi
this.commonFetchClient = new CommonFetchClient(appInstance)
this.commonFetchClient = new CommonFetchClient(appInstance, cfg.apiUrl, cfg.middlewareUrl, isDev)
this.isInSiyuanWidget = isInSiyuanWidget()
this.isInChromeExtension = isInChromeExtension()
}
Expand Down Expand Up @@ -126,7 +127,7 @@ class BaseWebApi extends WebApi {
}
this.logger.info("commonFetchClient from proxyFetch url =>", url)
this.logger.info("commonFetchClient from proxyFetch fetchOptions =>", fetchOptions)
const res = await this.commonFetchClient.fetchCall(url, fetchOptions, this.cfg.middlewareUrl)
const res = await this.commonFetchClient.fetchCall(url, fetchOptions)
this.logger.debug("commonFetchClient res from proxyFetch =>", res)
return res
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/test/kmsTest.vue
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@ const kmsHandleApi = async () => {
method: "POST",
}
const middlewareUrl = "http://127.0.0.1:3000/api/middleware"
const commonFetchClient = new CommonFetchClient(appInstance, requestUrl)
const result = await commonFetchClient.fetchCall(endpointUrl, fetchOptions, middlewareUrl)
const result = await commonFetchClient.fetchCall(endpointUrl, fetchOptions)
// const kmsCfg = {}
// const kmsApiAdaptor = {}
// const kmsApi = Utils.blogApi(appInstance, kmsApiAdaptor)
Expand Down

0 comments on commit 48d808c

Please sign in to comment.