Skip to content

Commit

Permalink
improve(plugin): types of 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 98ea014 commit 7b2ce59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
29 changes: 23 additions & 6 deletions libs/src/modules/LSPlugin.Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type IRequestOptions<R = any> = {
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'
data: Object | ArrayBuffer
timeout: number
dataType: 'json' | 'text' | 'base64' | 'arraybuffer'
returnType: 'json' | 'text' | 'base64' | 'arraybuffer'
success: (result: R) => void
fail: (err: any) => void
final: () => void
Expand All @@ -27,7 +27,8 @@ export class LSPluginRequestTask<R = any> {

constructor(
private _client: LSPluginRequest,
private _requestId: RequestTaskID
private _requestId: RequestTaskID,
private _requestOptions: Partial<IRequestOptions> = {}
) {

this._promise = new Promise<any>((resolve, reject) => {
Expand All @@ -39,10 +40,24 @@ export class LSPluginRequestTask<R = any> {
this._client.once(
genTaskCallbackType(this._requestId),
(e) => {
// handle error
resolve(e)
}
)
})

const { success, fail, final } = this._requestOptions

this._promise
.then((res) => {
success?.(res)
})
.catch((e) => {
fail?.(e)
})
.finally(() => {
final?.()
})
}

abort() {
Expand Down Expand Up @@ -85,21 +100,23 @@ export class LSPluginRequest extends EventEmitter {

static createRequestTask(
client: LSPluginRequest,
requestID: RequestTaskID
requestID: RequestTaskID,
requestOptions: Partial<IRequestOptions>
) {
return new LSPluginRequestTask(
client, requestID
client, requestID, requestOptions
)
}

_request<R = any>(options: WithOptional<IRequestOptions<R>, keyof Omit<IRequestOptions, 'url'>>): LSPluginRequestTask<R> {
const pid = this.ctx.baseInfo.id
const reqID = this.ctx.Experiments.invokeExperMethod('request', pid, options)
const { success, fail, final, ...requestOptions } = options
const reqID = this.ctx.Experiments.invokeExperMethod('request', pid, requestOptions)

// TODO: impl
const task = LSPluginRequest.createRequestTask(
this.ctx.Request,
reqID
reqID, { success, fail, final }
)

return task
Expand Down
4 changes: 2 additions & 2 deletions src/electron/electron/handler.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@
(plugin/uninstall! id))

(defmethod handle :httpRequest [_ [_ _req-id opts]]
(let [{:keys [url method dataType data headers]} opts]
(let [{:keys [url method data returnType 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")))])]
(keyword (string/lower-case (or returnType "json")))])]
(-> (utils/fetch url
(-> {:method method
:headers (and headers (bean/->js headers))}
Expand Down

0 comments on commit 7b2ce59

Please sign in to comment.