Skip to content

Commit

Permalink
feat: #1108 support wordpress.com
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 13, 2024
1 parent 8612fc5 commit 7ba8a57
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 81 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -86,9 +86,9 @@
"zhi-blog-api": "^1.56.2",
"zhi-common": "^1.31.0",
"zhi-device": "^2.11.0",
"zhi-fetch-middleware": "^0.8.0",
"zhi-fetch-middleware": "^0.8.2",
"zhi-github-middleware": "^0.4.15",
"zhi-gitlab-middleware": "^0.6.26",
"zhi-gitlab-middleware": "^0.6.28",
"zhi-lib-base": "^0.8.0",
"zhi-notion-markdown": "^0.1.4",
"zhi-siyuan-api": "^2.18.6",
Expand Down
116 changes: 58 additions & 58 deletions pnpm-lock.yaml

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

Expand Up @@ -40,7 +40,7 @@ import { useProxy } from "~/src/composables/useProxy.ts"
* @since 0.9.0
*/
class MetaweblogBlogApiAdaptor extends BaseBlogApi {
private readonly proxyXmlrpc: any
protected readonly proxyXmlrpc: any

/**
* 初始化 metaweblog API 适配器
Expand Down
19 changes: 19 additions & 0 deletions src/adaptors/api/wordpress/wordpressApiAdaptor.ts
Expand Up @@ -36,6 +36,8 @@ import { MetaweblogBlogApiAdaptor } from "~/src/adaptors/api/base/metaweblog/met
* @since 0.9.0
*/
class WordpressApiAdaptor extends MetaweblogBlogApiAdaptor {
private NEED_PROXY_BLOGS = ["wordpress.com"]

/**
* 初始化 WordPress API 适配器
*
Expand All @@ -47,5 +49,22 @@ class WordpressApiAdaptor extends MetaweblogBlogApiAdaptor {
this.logger = createAppLogger("wordpress-api-adaptor")
this.cfg.blogid = "wordpress"
}

/**
* WordPress API 调用
*
* @param method 方法名
* @param params 参数
* @returns 返回值
*/
protected override async metaweblogCall(method: string, params: any[]) {
const url = new URL(this.cfg.apiUrl)
const mainDomain = url.hostname.match(/[^.]+\.[^.]+$/)[0]
if (this.NEED_PROXY_BLOGS.includes(mainDomain)) {
return await this.proxyXmlrpc(this.cfg.apiUrl, method, params, true)
}
return await this.proxyXmlrpc(this.cfg.apiUrl, method, params)
}
}

export { WordpressApiAdaptor }
47 changes: 27 additions & 20 deletions src/composables/useProxy.ts
Expand Up @@ -74,8 +74,24 @@ const useProxy = (middlewareUrl?: string, corsProxyUrl?: string) => {
contentType: string = "application/json",
forceProxy: boolean = false
) => {
const siyuanSupported = ["application/json", "text/html", "text/xml", ""]
if (!forceProxy && isUseSiyuanProxy && siyuanSupported.includes(contentType)) {
const siyuanSupported = ["application/json", "text/html", "text/xml"]
if (forceProxy || !isUseSiyuanProxy || !siyuanSupported.includes(contentType)) {
logger.info("Using middleware proxy")
const header = headers.length > 0 ? headers[0] : {}
const fetchOptions = {
method: method,
headers: {
"Content-Type": contentType,
...header,
},
body: params,
}
logger.info("commonFetchClient url in proxyFetch =>", url)
logger.info("commonFetchClient fetchOptions in proxyFetch =>", fetchOptions)
const res = await commonFetchClient.fetchCall(url, fetchOptions, forceProxy)
logger.debug("Result of proxyFetch in commonFetchClient =>", res)
return res
} else {
logger.info("Using Siyuan forwardProxy, contentType=>", contentType)
let body: any
if (typeof params === "string" && !StrUtil.isEmptyString(params)) {
Expand Down Expand Up @@ -130,21 +146,6 @@ const useProxy = (middlewareUrl?: string, corsProxyUrl?: string) => {
logger.info("SiYuan proxy directly response fetchResult for content type:", contentType)
return fetchResult
}
} else {
logger.info("Using middleware proxy")
const header = headers.length > 0 ? headers[0] : {}
const fetchOptions = {
method: method,
headers: {
"Content-Type": contentType,
...header,
},
}
logger.info("commonFetchClient url in proxyFetch =>", url)
logger.info("commonFetchClient fetchOptions in proxyFetch =>", fetchOptions)
const res = await commonFetchClient.fetchCall(url, fetchOptions, forceProxy)
logger.debug("Result of proxyFetch in commonFetchClient =>", res)
return res
}
}

Expand All @@ -154,11 +155,17 @@ const useProxy = (middlewareUrl?: string, corsProxyUrl?: string) => {
* @param url - xmlrpc 端点地址
* @param reqMethod - 请求的方法名
* @param reqParams - 请求的参数
* @param forceProxy - 是否强制使用代理
*/
const proxyXmlrpc = async (url: string, reqMethod: string, reqParams: any[]) => {
const proxyXmlrpc = async (url: string, reqMethod: string, reqParams: any[], forceProxy: boolean = false) => {
const body = serializer.serializeMethodCall(reqMethod, reqParams)
const res = await proxyFetch(url, [], body, "POST", "text/xml")
let resText = res
let resText: string
if (forceProxy) {
const res = await proxyFetch(url, [], body, "POST", "text/xml", forceProxy)
resText = await res?.body["xml-body"]
} else {
resText = await proxyFetch(url, [], body, "POST", "text/xml", forceProxy)
}
resText = XmlrpcUtil.removeXmlHeader(resText)
const deserializer = new Deserializer()
const resJson = await deserializer.deserializeMethodResponse(resText)
Expand Down

0 comments on commit 7ba8a57

Please sign in to comment.