tauri.d.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Invoke your custom commands.
  3. *
  4. * This package is also accessible with `window.__TAURI__.tauri` when [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
  5. * @module
  6. */
  7. /** @ignore */
  8. declare global {
  9. interface Window {
  10. __TAURI_IPC__: (message: any) => void;
  11. ipc: {
  12. postMessage: (args: string) => void;
  13. };
  14. __TAURI__: {
  15. convertFileSrc: (src: string, protocol: string) => string;
  16. };
  17. }
  18. }
  19. /**
  20. * Transforms a callback function to a string identifier that can be passed to the backend.
  21. * The backend uses the identifier to `eval()` the callback.
  22. *
  23. * @return A unique identifier associated with the callback function.
  24. *
  25. * @since 1.0.0
  26. */
  27. declare function transformCallback(callback?: (response: any) => void, once?: boolean): number;
  28. /**
  29. * Command arguments.
  30. *
  31. * @since 1.0.0
  32. */
  33. type InvokeArgs = Record<string, unknown>;
  34. /**
  35. * Sends a message to the backend.
  36. * @example
  37. * ```typescript
  38. * import { invoke } from '@tauri-apps/api/tauri';
  39. * await invoke('login', { user: 'tauri', password: 'poiwe3h4r5ip3yrhtew9ty' });
  40. * ```
  41. *
  42. * @param cmd The command name.
  43. * @param args The optional arguments to pass to the command.
  44. * @return A promise resolving or rejecting to the backend response.
  45. *
  46. * @since 1.0.0
  47. */
  48. declare function invoke<T>(cmd: string, args?: InvokeArgs): Promise<T>;
  49. /**
  50. * Convert a device file path to an URL that can be loaded by the webview.
  51. * Note that `asset:` and `https://asset.localhost` must be added to [`tauri.security.csp`](https://tauri.app/v1/api/config/#securityconfig.csp) in `tauri.conf.json`.
  52. * Example CSP value: `"csp": "default-src 'self'; img-src 'self' asset: https://asset.localhost"` to use the asset protocol on image sources.
  53. *
  54. * Additionally, `asset` must be added to [`tauri.allowlist.protocol`](https://tauri.app/v1/api/config/#allowlistconfig.protocol)
  55. * in `tauri.conf.json` and its access scope must be defined on the `assetScope` array on the same `protocol` object.
  56. * For example:
  57. * ```json
  58. * {
  59. * "tauri": {
  60. * "allowlist": {
  61. * "protocol": {
  62. * "asset": true,
  63. * "assetScope": ["$APPDATA/assets/*"]
  64. * }
  65. * }
  66. * }
  67. * }
  68. * ```
  69. *
  70. * @param filePath The file path.
  71. * @param protocol The protocol to use. Defaults to `asset`. You only need to set this when using a custom protocol.
  72. * @example
  73. * ```typescript
  74. * import { appDataDir, join } from '@tauri-apps/api/path';
  75. * import { convertFileSrc } from '@tauri-apps/api/tauri';
  76. * const appDataDirPath = await appDataDir();
  77. * const filePath = await join(appDataDirPath, 'assets/video.mp4');
  78. * const assetUrl = convertFileSrc(filePath);
  79. *
  80. * const video = document.getElementById('my-video');
  81. * const source = document.createElement('source');
  82. * source.type = 'video/mp4';
  83. * source.src = assetUrl;
  84. * video.appendChild(source);
  85. * video.load();
  86. * ```
  87. *
  88. * @return the URL that can be used as source on the webview.
  89. *
  90. * @since 1.0.0
  91. */
  92. declare function convertFileSrc(filePath: string, protocol?: string): string;
  93. export type { InvokeArgs };
  94. export { transformCallback, invoke, convertFileSrc };