tauri.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <%
  2. if (typeof(confName) === 'undefined') {
  3. confName = 'quasar.conf.js'
  4. }
  5. %>
  6. /**
  7. * THIS FILE IS GENERATED AUTOMATICALLY.
  8. * DO NOT EDIT.
  9. *
  10. * Please whitelist these API functions in <% confName %>
  11. *
  12. **/
  13. /**
  14. * @module tauri
  15. * @description This API interface makes powerful interactions available
  16. * to be run on client side applications. They are opt-in features, and
  17. * must be enabled in <% confName %>
  18. *
  19. * Each binding MUST provide these interfaces in order to be compliant,
  20. * and also whitelist them based upon the developer's settings.
  21. */
  22. function s4() {
  23. return Math.floor((1 + Math.random()) * 0x10000)
  24. .toString(16)
  25. .substring(1)
  26. }
  27. const uid = function () {
  28. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  29. s4() + '-' + s4() + s4() + s4()
  30. }
  31. <% if (ctx.dev) { %>
  32. /**
  33. * @name __whitelistWarning
  34. * @description Present a stylish warning to the developer that their API
  35. * call has not been whitelisted in <% confName %>
  36. * @param {String} func - function name to warn
  37. * @private
  38. */
  39. const __whitelistWarning = function (func) {
  40. console.warn('%c[Tauri] Danger \ntauri.' + func + ' not whitelisted 💣\n%c\nAdd to <% confName %>: \n\ntauri: \n whitelist: { \n ' + func + ': true \n\nReference: https://quasar.dev/quasar-cli/creating-tauri-apps/api#' + func , 'background: red; color: white; font-weight: 800; padding: 2px; font-size:1.5em', ' ')
  41. }
  42. <% } %>
  43. /**
  44. * @name __reject
  45. * @description is a private promise used to deflect un-whitelisted tauri API calls
  46. * Its only purpose is to maintain thenable structure in client code without
  47. * breaking the application
  48. * * @type {Promise<any>}
  49. * @private
  50. */
  51. const __reject = new Promise((reject) => { reject })
  52. export default class Tauri {
  53. <% if (ctx.dev) { %>
  54. /**
  55. * @name invoke
  56. * @description Calls a Quasar Core feature, such as setTitle
  57. * @param {Object} args
  58. */
  59. <% } %>
  60. static invoke (args) {
  61. Object.freeze(args)
  62. external.invoke(JSON.stringify(args))
  63. }
  64. <% if (ctx.dev) { %>
  65. /**
  66. * @name addEventListener
  67. * @description Add an event listener to Tauri back end
  68. * @param {String} event
  69. * @param {Function} handler
  70. * @param {Boolean} once
  71. */
  72. <% } %>
  73. static addEventListener(event, handler, once = false) {
  74. this.invoke({
  75. cmd: 'addEventListener',
  76. event,
  77. handler: this.transformCallback(handler, once),
  78. once
  79. })
  80. }
  81. <% if (ctx.dev) { %>
  82. /**
  83. * @name emit
  84. * @description Emits an event to the Tauri back end
  85. * @param {String} event
  86. * @param {Object} payload
  87. */
  88. <% } %>
  89. static emit(event, payload) {
  90. this.invoke({
  91. cmd: 'emit',
  92. event,
  93. payload
  94. })
  95. }
  96. <% if (ctx.dev) { %>
  97. /**
  98. * @name transformCallback
  99. * @description Registers a callback with a uid
  100. * @param {Function} callback
  101. * @param {Boolean} once
  102. * @returns {*}
  103. */
  104. <% } %>
  105. static transformCallback (callback, once = true) {
  106. const identifier = Object.freeze(uid())
  107. window[identifier] = (result) => {
  108. if (once) {
  109. delete window[identifier]
  110. }
  111. return callback && callback(result)
  112. }
  113. return identifier
  114. }
  115. <% if (ctx.dev) { %>
  116. /**
  117. * @name promisified
  118. * @description Turns a request into a chainable promise
  119. * @param {Object} args
  120. * @returns {Promise<any>}
  121. */
  122. <% } %>
  123. static promisified (args) {
  124. return new Promise((resolve, reject) => {
  125. this.invoke({
  126. callback: this.transformCallback(resolve),
  127. error: this.transformCallback(reject),
  128. ...args
  129. })
  130. })
  131. }
  132. <% if (ctx.dev) { %>
  133. /**
  134. * @name readTextFile
  135. * @description Accesses a non-binary file on the user's filesystem
  136. * and returns the content. Permissions based on the app's PID owner
  137. * @param {String} path
  138. * @returns {*|Promise<any>|Promise}
  139. */
  140. <% } %>
  141. static readTextFile (path) {
  142. <% if (tauri.whitelist.readTextFile === true || tauri.whitelist.all === true) { %>
  143. Object.freeze(path)
  144. return this.promisified({ cmd: 'readTextFile', path })
  145. <% } else { %>
  146. <% if (ctx.dev) { %>
  147. __whitelistWarning('readTextFile')
  148. <% } %>
  149. return __reject
  150. <% } %>
  151. }
  152. <% if (ctx.dev) { %>
  153. /**
  154. * @name readBinaryFile
  155. * @description Accesses a binary file on the user's filesystem
  156. * and returns the content. Permissions based on the app's PID owner
  157. * @param {String} path
  158. * @returns {*|Promise<any>|Promise}
  159. */
  160. <% } %>
  161. static readBinaryFile (path) {
  162. <% if (tauri.whitelist.readBinaryFile === true || tauri.whitelist.all === true) { %>
  163. Object.freeze(path)
  164. return this.promisified({ cmd: 'readBinaryFile', path })
  165. <% } else { %>
  166. <% if (ctx.dev) { %>
  167. __whitelistWarning('readBinaryFile')
  168. <% } %>
  169. return __reject
  170. <% } %>
  171. }
  172. <% if (ctx.dev) { %>
  173. /**
  174. * @name writeFile
  175. * @description Write a file to the Local Filesystem.
  176. * Permissions based on the app's PID owner
  177. * @param {Object} cfg
  178. * @param {String} cfg.file
  179. * @param {String|Binary} cfg.contents
  180. */
  181. <% } %>
  182. static writeFile (cfg) {
  183. Object.freeze(cfg)
  184. <% if (tauri.whitelist.writeFile === true || tauri.whitelist.all === true) { %>
  185. this.invoke({ cmd: 'writeFile', file: cfg.file, contents: cfg.contents })
  186. <% } else { %>
  187. <% if (ctx.dev) { %>
  188. __whitelistWarning('writeFile')
  189. <% } %>
  190. return __reject
  191. <% } %> }
  192. <% if (ctx.dev) { %>
  193. /**
  194. * @name listFiles
  195. * @description Get the files in a path.
  196. * Permissions based on the app's PID owner
  197. * @param {String} path
  198. * @returns {*|Promise<any>|Promise}
  199. */
  200. <% } %>
  201. static listFiles (path) {
  202. <% if (tauri.whitelist.listFiles === true || tauri.whitelist.all === true) { %>
  203. Object.freeze(path)
  204. return this.promisified({ cmd: 'listFiles', path })
  205. <% } else { %>
  206. <% if (ctx.dev) { %>
  207. __whitelistWarning('listFiles')
  208. <% } %>
  209. return __reject
  210. <% } %>
  211. }
  212. <% if (ctx.dev) { %>
  213. /**
  214. * @name listDirs
  215. * @description Get the directories in a path.
  216. * Permissions based on the app's PID owner
  217. * @param {String} path
  218. * @returns {*|Promise<any>|Promise}
  219. */
  220. <% } %>
  221. static listDirs (path) {
  222. <% if (tauri.whitelist.listDirs === true || tauri.whitelist.all === true) { %>
  223. Object.freeze(path)
  224. return this.promisified({ cmd: 'listDirs', path })
  225. <% } else { %>
  226. <% if (ctx.dev) { %>
  227. __whitelistWarning('listDirs')
  228. <% } %>
  229. return __reject
  230. <% } %>
  231. }
  232. <% if (ctx.dev) { %>
  233. /**
  234. * @name setTitle
  235. * @description Set the application's title
  236. * @param {String} title
  237. */
  238. <% } %>
  239. static setTitle (title) {
  240. <% if (tauri.whitelist.setTitle === true || tauri.whitelist.all === true) { %>
  241. Object.freeze(title)
  242. this.invoke({ cmd: 'setTitle', title })
  243. <% } else { %>
  244. <% if (ctx.dev) { %>
  245. __whitelistWarning('setTitle')
  246. <% } %>
  247. return __reject
  248. <% } %>
  249. }
  250. <% if (ctx.dev) { %>
  251. /**
  252. * @name open
  253. * @description Open an URI
  254. * @param {String} uri
  255. */
  256. <% } %>
  257. static open (uri) {
  258. <% if (tauri.whitelist.open === true || tauri.whitelist.all === true) { %>
  259. Object.freeze(uri)
  260. this.invoke({ cmd: 'open', uri })
  261. <% } else { %>
  262. <% if (ctx.dev) { %>
  263. __whitelistWarning('open')
  264. <% } %>
  265. return __reject
  266. <% } %>
  267. }
  268. <% if (ctx.dev) { %>
  269. /**
  270. * @name execute
  271. * @description Execute a program with arguments.
  272. * Permissions based on the app's PID owner
  273. * @param {String} command
  274. * @param {String|Array} args
  275. * @returns {*|Promise<any>|Promise}
  276. */
  277. <% } %>
  278. static execute (command, args) {
  279. <% if (tauri.whitelist.execute === true || tauri.whitelist.all === true) { %>
  280. Object.freeze(command)
  281. if (typeof args === 'string' || typeof args === 'object') {
  282. Object.freeze(args)
  283. }
  284. return this.promisified({ cmd: 'execute', command, args: typeof (args) === 'string' ? [args] : args })
  285. <% } else { %>
  286. <% if (ctx.dev) { %>
  287. __whitelistWarning('execute')
  288. <% } %>
  289. return __reject
  290. <% } %>
  291. }
  292. <% if (ctx.dev) { %>
  293. /**
  294. * @name bridge
  295. * @description Securely pass a message to the backend.
  296. * @example
  297. * this.$q.tauri.bridge('QBP/1/ping/client-1', 'pingback')
  298. * @param {String} command - a compressed, slash-delimited and
  299. * versioned API call to the backend.
  300. * @param {String|Object}payload
  301. * @returns {*|Promise<any>|Promise}
  302. */
  303. <% } %>
  304. static bridge (command, payload) {
  305. <% if (tauri.whitelist.bridge === true || tauri.whitelist.all === true) { %>
  306. Object.freeze(command)
  307. if (typeof payload === 'string' || typeof payload === 'object') {
  308. Object.freeze(payload)
  309. }
  310. return this.promisified({ cmd: 'bridge', command, payload: typeof (payload) === 'object' ? [payload] : payload })
  311. <% } else { %>
  312. <% if (ctx.dev) { %>
  313. __whitelistWarning('bridge')
  314. <% } %>
  315. return __reject
  316. <% } %>
  317. }
  318. }