Sfoglia il codice sorgente

fix(core): API example, http API export (#1224)

Lucas Fernandes Nogueira 4 anni fa
parent
commit
c10d40311c

+ 1 - 1
api/src/bundle.ts

@@ -4,7 +4,7 @@ import * as dialog from './dialog'
 import * as event from './event'
 import * as fs from './fs'
 import * as path from './path'
-import http from './http'
+import * as http from './http'
 import * as shell from './shell'
 import * as tauri from './tauri'
 import * as window from './window'

+ 2 - 4
api/src/http.ts

@@ -155,13 +155,11 @@ async function deleteRequest<T>(
   })
 }
 
-export default {
+export {
   request,
   get,
   post,
   put,
   patch,
-  delete: deleteRequest,
-  ResponseType,
-  BodyType
+  deleteRequest as httpDelete,
 }

+ 1 - 1
tauri/examples/api/src/App.svelte

@@ -68,7 +68,7 @@
       {/each}
     </div>
   </div>
-  <div id="response">{response}</div>
+  <div id="response">{@html response}</div>
   <div class="bottom">
     <a class="dark-link" target="_blank" href="https://tauri.studio">
       Tauri Documentation

+ 23 - 20
tauri/examples/api/src/components/FileSystem.svelte

@@ -7,18 +7,20 @@
 
   export let onMessage
 
+  let pathToRead = ''
+
   function getDir() {
-    var dirSelect = document.getElementById('dir')
+    const dirSelect = document.getElementById('dir')
     return dirSelect.value ? parseInt(dir.value) : null
   }
 
   function arrayBufferToBase64(buffer, callback) {
-    var blob = new Blob([buffer], {
+    const blob = new Blob([buffer], {
       type: 'application/octet-binary'
     })
-    var reader = new FileReader()
+    const reader = new FileReader()
     reader.onload = function (evt) {
-      var dataurl = evt.target.result
+      const dataurl = evt.target.result
       callback(dataurl.substr(dataurl.indexOf(',') + 1))
     }
     reader.readAsDataURL(blob)
@@ -29,31 +31,32 @@
     .map(dir => [dir, Dir[dir]]);
 
   function read() {
-    var pathToRead = pathInput.value
-    var isFile = pathToRead.match(/\S+\.\S+$/g)
-    var opts = {
+    const isFile = pathToRead.match(/\S+\.\S+$/g)
+    const opts = {
       dir: getDir()
     }
-    var promise = isFile ? readBinaryFile(pathToRead, opts) : readDir(pathToRead, opts)
+    const promise = isFile ? readBinaryFile(pathToRead, opts) : readDir(pathToRead, opts)
     promise.then(function (response) {
       if (isFile) {
         if (pathToRead.includes('.png') || pathToRead.includes('.jpg')) {
           arrayBufferToBase64(new Uint8Array(response), function (base64) {
-            var src = 'data:image/png;base64,' + base64
+            const src = 'data:image/png;base64,' + base64
             onMessage('<img src="' + src + '"></img>')
           })
         } else {
-          var value = String.fromCharCode.apply(null, response)
+          const value = String.fromCharCode.apply(null, response)
           onMessage('<textarea id="file-response" style="height: 400px"></textarea><button id="file-save">Save</button>')
-          var fileInput = document.getElementById('file-response')
-          fileInput.value = value
-          document.getElementById('file-save').addEventListener('click', function () {
-            writeFile({
-              file: pathToRead,
-              contents: fileInput.value
-            }, {
-              dir: getDir()
-            }).catch(onMessage)
+          setTimeout(() => {
+            const fileInput = document.getElementById('file-response')
+            fileInput.value = value
+            document.getElementById('file-save').addEventListener('click', function () {
+              writeFile({
+                file: pathToRead,
+                contents: fileInput.value
+              }, {
+                dir: getDir()
+              }).catch(onMessage)
+            })
           })
         }
       } else {
@@ -70,6 +73,6 @@
     <option value={dir[1]}>{dir[0]}</option>
     {/each}
   </select>
-  <input id="path-to-read" placeholder="Type the path to read..." />
+  <input id="path-to-read" placeholder="Type the path to read..." bind:value={pathToRead} />
   <button class="button" id="read">Read</button>
 </form>

+ 2 - 1
tauri/examples/api/src/components/Window.svelte

@@ -1,5 +1,6 @@
 <script>
-  import { setTitle, open } from "@tauri-apps/api/window";
+  import { setTitle } from "@tauri-apps/api/window";
+  import { open } from "@tauri-apps/api/shell";
 
   let urlValue = "https://tauri.studio";
   let windowTitle = 'Awesome Tauri Example!';