Skip to content

Commit

Permalink
feat(plugin): WIP request api
Browse files Browse the repository at this point in the history
  • Loading branch information
xyhp915 authored and tiensonqin committed Jun 15, 2022
1 parent 0f62bc2 commit 2aa50f6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
19 changes: 19 additions & 0 deletions libs/src/LSPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { LSPluginCaller } from './LSPlugin.caller'
import { LSPluginExperiments } from './modules/LSPlugin.Experiments'
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 ThemeMode = 'light' | 'dark'
Expand Down Expand Up @@ -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 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 {
[key: string]: any
}
Expand Down
15 changes: 14 additions & 1 deletion libs/src/modules/LSPlugin.Experiments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LSPluginUser } from '../LSPlugin.user'
import { IRequestOptions, IRequestTask, LSPluginUser, WithOptional } from '../LSPlugin.user'
import { PluginLocal } from '../LSPlugin.core'
import { safeSnakeCase } from '../helpers'

Expand Down Expand Up @@ -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 {
if (window === top) {
throw new Error('Can not access host scope!')
Expand Down
8 changes: 8 additions & 0 deletions src/main/logseq/api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,14 @@
(plugin-handler/register-extensions-enhancer
(keyword pid) type {:enhancer enhancer})))

(defn ^:export exper_request
[pid options]
nil)

(defn ^:export exper_abort_request
[req-id]
nil)

;; helpers
(defn ^:export query_element_by_id
[id]
Expand Down

0 comments on commit 2aa50f6

Please sign in to comment.