enhance: use /api instead of /api-invoker

This commit is contained in:
Tienson Qin
2022-12-29 23:16:12 +08:00
parent 7fd33b5971
commit c2a4e55f02
3 changed files with 26 additions and 24 deletions

View File

@@ -1,4 +1,3 @@
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><style>body {
max-width: 980px;
outline: 1300px solid #fff;
@@ -1068,28 +1067,28 @@ body .markdown-body
.highlight .il{color:#009999}
.highlight .gc{color:#999;background-color:#EAF2F5}
</style><title>http_apis</title></head><body><article class="markdown-body"><h1 id="server-apis-for-logseq">Server APIs for Logseq<a class="headerlink" href="#server-apis-for-logseq" title="Permanent link"></a></h1>
<p>Server is running at <code>http://${HOST}:${PORT}</code></p>
<h2 id="available-apis">Available APIs<a class="headerlink" href="#available-apis" title="Permanent link"></a></h2>
<p>Logseq HTTP server is running at <code>http://${HOST}:${PORT}</code>.</p>
<h2 id="available-apis">Available API<a class="headerlink" href="#available-apis" title="Permanent link"></a></h2>
<blockquote>
<p>🔐 For data security purposes, all API requests must provide a valid token by providing the <code>Authorization</code> field in the header. And, these tokens data can be configured on the client side.</p>
<p>🔐 All API requests must provide a valid token for data security purposes by providing the <code>Authorization</code> field in the header. Tokens can be configured in Logseq.</p>
</blockquote>
<h3 id="post-api-invoker">[POST] <code>/api-invoker</code><a class="headerlink" href="#post-api-invoker" title="Permanent link"></a></h3>
<p>Invoke all APIs that provided by Logseq plugin SDK.</p>
<h3 id="post-api">[POST] <code>/api</code><a class="headerlink" href="#post-api" title="Permanent link"></a></h3>
<p>Invoke any APs that's provided by Logseq plugin SDK.</p>
<ul>
<li>
<h4 id="request-body-content-type-applicationjson">Request Body (<code>Content-Type: application/json</code>)<a class="headerlink" href="#request-body-content-type-applicationjson" title="Permanent link"></a></h4>
<ul>
<li><code>method</code> {string} [required] pattern string as <code>logseq.Editor.getCurrentBlock</code>
You can get more valid method from <a href="https://plugins-doc.logseq.com/logseq/Editor/getCurrentBlock">https://plugins-doc.logseq.com/logseq/Editor/getCurrentBlock</a></li>
You can find more APIs from <a href="https://plugins-doc.logseq.com">https://plugins-doc.logseq.com</a></li>
<li><code>args</code> {array} [optional] arguments list for method</li>
</ul>
</li>
<li>
<h4 id="examples">Examples<a class="headerlink" href="#examples" title="Permanent link"></a></h4>
<div class="highlight"><pre><span class="c1">### Send POST request with json body</span>
<span class="l l-Scalar l-Scalar-Plain">POST http://127.0.0.1:12315/api-invoker</span>
<span class="l l-Scalar l-Scalar-Plain">POST http://127.0.0.1:12315/api</span>
<span class="l l-Scalar l-Scalar-Plain">Content-Type</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">application/json</span>
<span class="l l-Scalar l-Scalar-Plain">Authorization</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">your-valid-token-xxx</span>
<span class="l l-Scalar l-Scalar-Plain">Authorization</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">Bearer your-valid-token-xxx</span>
<span class="p p-Indicator">{</span>
<span class="s">&quot;method&quot;</span><span class="p p-Indicator">:</span> <span class="s">&quot;logseq.Editor.getBlock&quot;</span><span class="p p-Indicator">,</span>
@@ -1099,9 +1098,9 @@ body .markdown-body
<span class="p p-Indicator">}</span>
<span class="c1">### show a message tip</span>
<span class="l l-Scalar l-Scalar-Plain">POST http://127.0.0.1:12315/api-invoker</span>
<span class="l l-Scalar l-Scalar-Plain">POST http://127.0.0.1:12315/api</span>
<span class="l l-Scalar l-Scalar-Plain">Content-Type</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">application/json</span>
<span class="l l-Scalar l-Scalar-Plain">Authorization</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">your-valid-token-xxx</span>
<span class="l l-Scalar l-Scalar-Plain">Authorization</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">Bearer your-valid-token-xxx</span>
<span class="p p-Indicator">{</span>
<span class="s">&quot;method&quot;</span><span class="p p-Indicator">:</span> <span class="s">&quot;logseq.UI.showMsg&quot;</span><span class="p p-Indicator">,</span>
@@ -1119,4 +1118,4 @@ body .markdown-body
<li>Logseq forum <a href="https://discuss.logseq.com/c/questions-and-help/8">https://discuss.logseq.com/c/questions-and-help/8</a></li>
<li>Plugin docs <a href="https://plugins-doc.logseq.com">https://plugins-doc.logseq.com</a></li>
<li>Plugin SDK <a href="https://www.npmjs.com/package/@logseq/libs">https://www.npmjs.com/package/@logseq/libs</a></li>
</ul></article></body></html>
</ul></article></body></html>

View File

@@ -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))
(reset-state!) t))

View File

@@ -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."