mirror of
https://github.com/logseq/logseq.git
synced 2026-04-24 22:25:01 +00:00
improve(plugin): WIP api of http request
This commit is contained in:
@@ -281,7 +281,7 @@ class LSPluginCaller extends EventEmitter {
|
||||
})
|
||||
|
||||
this._call = async (...args: any) => {
|
||||
// parent all will get message before handshaked
|
||||
// parent all will get message before handshake
|
||||
await refChild.call(LSPMSGFn(pl.id), {
|
||||
type: args[0],
|
||||
payload: Object.assign(args[1] || {}, {
|
||||
|
||||
@@ -4,6 +4,7 @@ import EventEmitter from 'eventemitter3'
|
||||
import { LSPluginCaller } from './LSPlugin.caller'
|
||||
import { LSPluginExperiments } from './modules/LSPlugin.Experiments'
|
||||
import { LSPluginFileStorage } from './modules/LSPlugin.Storage'
|
||||
import { LSPluginRequest } from './modules/LSPlugin.Request'
|
||||
|
||||
export type WithOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
||||
|
||||
@@ -116,23 +117,6 @@ export type IDatom = [e: number, a: string, v: any, t: number, added: boolean]
|
||||
|
||||
export type IGitResult = { stdout: string; stderr: string; exitCode: number }
|
||||
|
||||
export type IRequestOptions<R = any> = {
|
||||
url: string
|
||||
headers: Record<string, string>
|
||||
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'
|
||||
data: Object | ArrayBuffer
|
||||
timeout: number
|
||||
dataType: 'json' | 'text' | 'base64' | 'arraybuffer'
|
||||
success: (result: R) => void
|
||||
fail: (err: any) => void
|
||||
final: () => void
|
||||
}
|
||||
|
||||
export type IRequestTask<R> = {
|
||||
abort: () => void
|
||||
promise: Promise<R>
|
||||
}
|
||||
|
||||
export interface AppUserInfo {
|
||||
[key: string]: any
|
||||
}
|
||||
@@ -967,6 +951,7 @@ export interface ILSPluginUser extends EventEmitter<LSPluginUserEvents> {
|
||||
Git: IGitProxy
|
||||
UI: IUIProxy
|
||||
|
||||
Request: LSPluginRequest
|
||||
FileStorage: LSPluginFileStorage
|
||||
Experiments: LSPluginExperiments
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import * as CSS from 'csstype'
|
||||
import EventEmitter from 'eventemitter3'
|
||||
import { LSPluginFileStorage } from './modules/LSPlugin.Storage'
|
||||
import { LSPluginExperiments } from './modules/LSPlugin.Experiments'
|
||||
import { LSPluginRequest } from './modules/LSPlugin.Request'
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
@@ -318,8 +319,7 @@ const KEY_MAIN_UI = 0
|
||||
*/
|
||||
export class LSPluginUser
|
||||
extends EventEmitter<LSPluginUserEvents>
|
||||
implements ILSPluginUser
|
||||
{
|
||||
implements ILSPluginUser {
|
||||
// @ts-ignore
|
||||
private _version: string = LIB_VERSION
|
||||
private _debugTag: string = ''
|
||||
@@ -333,6 +333,7 @@ export class LSPluginUser
|
||||
private _ui = new Map<number, uiState>()
|
||||
|
||||
private _mFileStorage: LSPluginFileStorage
|
||||
private _mRequest: LSPluginRequest
|
||||
private _mExperiments: LSPluginExperiments
|
||||
|
||||
/**
|
||||
@@ -673,6 +674,12 @@ export class LSPluginUser
|
||||
return m
|
||||
}
|
||||
|
||||
get Request(): LSPluginRequest {
|
||||
let m = this._mRequest
|
||||
if (!m) m = this._mRequest = new LSPluginRequest(this)
|
||||
return m
|
||||
}
|
||||
|
||||
get Experiments(): LSPluginExperiments {
|
||||
let m = this._mExperiments
|
||||
if (!m) m = this._mExperiments = new LSPluginExperiments(this)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IRequestOptions, IRequestTask, LSPluginUser, WithOptional } from '../LSPlugin.user'
|
||||
import { LSPluginUser } from '../LSPlugin.user'
|
||||
import { PluginLocal } from '../LSPlugin.core'
|
||||
import { safeSnakeCase } from '../helpers'
|
||||
|
||||
@@ -24,7 +24,7 @@ export class LSPluginExperiments {
|
||||
)
|
||||
}
|
||||
|
||||
private invokeExperMethod(type: string, ...args: Array<any>) {
|
||||
public invokeExperMethod(type: string, ...args: Array<any>) {
|
||||
const host = this.ensureHostScope()
|
||||
type = safeSnakeCase(type)?.toLowerCase()
|
||||
return host.logseq.api['exper_' + type]?.apply(host, args)
|
||||
@@ -80,19 +80,6 @@ export class LSPluginExperiments {
|
||||
)
|
||||
}
|
||||
|
||||
request<R = any>(options: WithOptional<IRequestOptions<R>, keyof Omit<IRequestOptions, 'url'>>): IRequestTask<R> {
|
||||
const pid = this.ctx.baseInfo.id
|
||||
const reqID = this.invokeExperMethod('request', pid, options)
|
||||
|
||||
// TODO: impl
|
||||
const task = {
|
||||
abort: (() => reqID),
|
||||
promise: Promise.resolve(null)
|
||||
}
|
||||
|
||||
return task
|
||||
}
|
||||
|
||||
ensureHostScope(): any {
|
||||
if (window === top) {
|
||||
throw new Error('Can not access host scope!')
|
||||
|
||||
@@ -368,6 +368,33 @@
|
||||
(defmethod handle :uninstallMarketPlugin [_ [_ id]]
|
||||
(plugin/uninstall! id))
|
||||
|
||||
(defmethod handle :httpRequest [_ [_ _req-id opts]]
|
||||
(let [{:keys [url method dataType data headers]} opts]
|
||||
(when-let [[method type] (and (not (string/blank? url))
|
||||
[(keyword (string/upper-case (or method "GET")))
|
||||
(keyword (string/lower-case (or dataType "json")))])]
|
||||
(-> (utils/fetch url
|
||||
(-> {:method method
|
||||
:headers (and headers (bean/->js headers))}
|
||||
(merge (when (and (not (contains? #{:GET :HEAD} method)) data)
|
||||
;; TODO: support type of arrayBuffer
|
||||
{:body (js/JSON.stringify (bean/->js data))}))))
|
||||
(p/then (fn [^js res]
|
||||
(case type
|
||||
:json
|
||||
(.json res)
|
||||
|
||||
:arraybuffer
|
||||
(.arrayBuffer res)
|
||||
|
||||
:base64
|
||||
(-> (.buffer res)
|
||||
(p/then #(.toString % "base64")))
|
||||
|
||||
:text
|
||||
(.text res))))
|
||||
(p/catch identity)))))
|
||||
|
||||
(defmethod handle :quitAndInstall []
|
||||
(.quitAndInstall autoUpdater))
|
||||
|
||||
|
||||
@@ -531,6 +531,15 @@
|
||||
(into {} (map (fn [v] [(keyword (:id v)) v]) plugins)))
|
||||
(state/pub-event! [:plugin/consume-updates]))))
|
||||
|
||||
(defn call-plugin
|
||||
[^js pl type payload]
|
||||
(when pl
|
||||
(.call (.-caller pl) (name type) (bean/->js payload))))
|
||||
|
||||
(defn request-callback
|
||||
[^js pl req-id payload]
|
||||
(call-plugin pl :#lsp#request#callback {:requestId req-id :payload payload}))
|
||||
|
||||
;; components
|
||||
(rum/defc lsp-indicator < rum/reactive
|
||||
[]
|
||||
|
||||
@@ -808,9 +808,17 @@
|
||||
(plugin-handler/register-extensions-enhancer
|
||||
(keyword pid) type {:enhancer enhancer})))
|
||||
|
||||
(defonce *request-k (volatile! 0))
|
||||
|
||||
(defn ^:export exper_request
|
||||
[pid options]
|
||||
nil)
|
||||
[pid ^js options]
|
||||
(when-let [^js _pl (plugin-handler/get-plugin-inst pid)]
|
||||
(let [req-id (vreset! *request-k (inc @*request-k))
|
||||
req-cb #(plugin-handler/request-callback _pl req-id %)]
|
||||
(-> (ipc/ipc :httpRequest req-id options)
|
||||
(p/then #(req-cb %))
|
||||
(p/catch #(req-cb %)))
|
||||
req-id)))
|
||||
|
||||
(defn ^:export exper_abort_request
|
||||
[req-id]
|
||||
|
||||
Reference in New Issue
Block a user