浏览代码

feat(plugin): WIP request api

charlie 3 年之前
父节点
当前提交
2aa50f6199
共有 3 个文件被更改,包括 41 次插入1 次删除
  1. 19 0
      libs/src/LSPlugin.ts
  2. 14 1
      libs/src/modules/LSPlugin.Experiments.ts
  3. 8 0
      src/main/logseq/api.cljs

+ 19 - 0
libs/src/LSPlugin.ts

@@ -5,6 +5,8 @@ import { LSPluginCaller } from './LSPlugin.caller'
 import { LSPluginExperiments } from './modules/LSPlugin.Experiments'
 import { LSPluginExperiments } from './modules/LSPlugin.Experiments'
 import { LSPluginFileStorage } from './modules/LSPlugin.Storage'
 import { LSPluginFileStorage } from './modules/LSPlugin.Storage'
 
 
+export type WithOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
+
 export type PluginLocalIdentity = string
 export type PluginLocalIdentity = string
 
 
 export type ThemeMode = 'light' | 'dark'
 export type ThemeMode = 'light' | 'dark'
@@ -114,6 +116,23 @@ export type IDatom = [e: number, a: string, v: any, t: number, added: boolean]
 
 
 export type IGitResult = { stdout: string; stderr: string; exitCode: number }
 export type IGitResult = { stdout: string; stderr: string; exitCode: number }
 
 
+export type IRequestOptions<R = any> = {
+  url: string
+  headers: Record<string, string>
+  method: 'GET' | 'POST' | 'PUT' | 'DELETE'
+  data: any
+  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 {
 export interface AppUserInfo {
   [key: string]: any
   [key: string]: any
 }
 }

+ 14 - 1
libs/src/modules/LSPlugin.Experiments.ts

@@ -1,4 +1,4 @@
-import { LSPluginUser } from '../LSPlugin.user'
+import { IRequestOptions, IRequestTask, LSPluginUser, WithOptional } from '../LSPlugin.user'
 import { PluginLocal } from '../LSPlugin.core'
 import { PluginLocal } from '../LSPlugin.core'
 import { safeSnakeCase } from '../helpers'
 import { safeSnakeCase } from '../helpers'
 
 
@@ -80,6 +80,19 @@ 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 {
   ensureHostScope(): any {
     if (window === top) {
     if (window === top) {
       throw new Error('Can not access host scope!')
       throw new Error('Can not access host scope!')

+ 8 - 0
src/main/logseq/api.cljs

@@ -808,6 +808,14 @@
     (plugin-handler/register-extensions-enhancer
     (plugin-handler/register-extensions-enhancer
       (keyword pid) type {:enhancer enhancer})))
       (keyword pid) type {:enhancer enhancer})))
 
 
+(defn ^:export exper_request
+  [pid options]
+  nil)
+
+(defn ^:export exper_abort_request
+  [req-id]
+  nil)
+
 ;; helpers
 ;; helpers
 (defn ^:export query_element_by_id
 (defn ^:export query_element_by_id
   [id]
   [id]