From c2a4e55f02de79ee935e9fbf54966fac25aa3d54 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Thu, 29 Dec 2022 23:16:12 +0800 Subject: [PATCH] enhance: use /api instead of /api-invoker --- resources/docs/api_server.html | 23 +++++++++++------------ src/electron/electron/server.cljs | 17 +++++++++-------- src/main/frontend/fs.cljs | 10 ++++++---- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/resources/docs/api_server.html b/resources/docs/api_server.html index 1f91def9e1..663954597f 100644 --- a/resources/docs/api_server.html +++ b/resources/docs/api_server.html @@ -1,4 +1,3 @@ - http_apis

Server APIs for Logseq

-

Server is running at http://${HOST}:${PORT}

-

Available APIs

+

Logseq HTTP server is running at http://${HOST}:${PORT}.

+

Available API

-

🔐 For data security purposes, all API requests must provide a valid token by providing the Authorization field in the header. And, these tokens data can be configured on the client side.

+

🔐 All API requests must provide a valid token for data security purposes by providing the Authorization field in the header. Tokens can be configured in Logseq.

-

[POST] /api-invoker

-

Invoke all APIs that provided by Logseq plugin SDK.

+

[POST] /api

+

Invoke any APs that's provided by Logseq plugin SDK.

\ No newline at end of file + diff --git a/src/electron/electron/server.cljs b/src/electron/electron/server.cljs index fa461bcb8b..dce2854d91 100644 --- a/src/electron/electron/server.cljs +++ b/src/electron/electron/server.cljs @@ -69,11 +69,12 @@ (defn- validate-auth-token [token] - (when-let [valid-tokens (cfgs/get-item :server/tokens)] - (when (or (string/blank? token) - (not (some #(or (= % token) - (= (:value %) token)) valid-tokens))) - (throw (js/Error. "Access Deny!"))))) + (let [token (string/replace token "Bearer " "")] + (when-let [valid-tokens (cfgs/get-item :server/tokens)] + (when (or (string/blank? token) + (not (some #(or (= % token) + (= (:value %) token)) valid-tokens))) + (throw (js/Error. "Access Deny!")))))) (defn- api-pre-handler! [^js req ^js rep callback] @@ -98,7 +99,7 @@ (utils/send-to-renderer @*win :invokeLogseqAPI {:syncId sid :method method :args args}) (.handleOnce ipcMain (str ::sync! sid) ret-handle))))) -(defn- api-invoker-fn! +(defn- api-handler! [^js req ^js rep] (if-let [^js body (.-body req)] (if-let [method (resolve-real-api-method (.-method body))] @@ -132,7 +133,7 @@ ;; hooks & routes _ (doto s (.addHook "preHandler" api-pre-handler!) - (.post "/api-invoker" api-invoker-fn!) + (.post "/api" api-handler!) (.get "/" (fn [_ ^js rep] (let [html (fs-extra/readFileSync (.join path js/__dirname "./docs/api_server.html")) HOST (get-host) @@ -164,4 +165,4 @@ [^js win] (reset! *win win) (let [t (setup-state-watch!)] - (reset-state!) t)) \ No newline at end of file + (reset-state!) t)) diff --git a/src/main/frontend/fs.cljs b/src/main/frontend/fs.cljs index 1190c7f68a..40ed913c60 100644 --- a/src/main/frontend/fs.cljs +++ b/src/main/frontend/fs.cljs @@ -57,10 +57,12 @@ [dir & {:keys [path-only?]}] (p/let [result (protocol/readdir (get-fs dir) dir) result (bean/->clj result)] - (if (and path-only? (map? (first result))) - (->> (map :uri result) - (map gp-util/path-normalize)) - (map #(update % :uri gp-util/path-normalize) result)))) + (let [result (if (and path-only? (map? (first result))) + (map :uri result) + result)] + (if (and (map? (first result)) (:uri (first result))) + (map #(update % :uri gp-util/path-normalize) result) + (map gp-util/path-normalize result))))) (defn unlink! "Should move the path to logseq/recycle instead of deleting it."