mirror of
https://github.com/tauri-apps/tauri-plugin-http.git
synced 2026-01-31 00:45:17 +01:00
feat(plugins): inject API on window.__TAURI__ (#383)
Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/5060238742 Co-authored-by: lucasfernog <lucasfernog@users.noreply.github.com>
This commit is contained in:
committed by
tauri-bot
parent
ba844ddf5b
commit
d2a66b4a50
26
dist-js/index.d.ts
vendored
26
dist-js/index.d.ts
vendored
@@ -1,3 +1,29 @@
|
||||
/**
|
||||
* Access the HTTP client written in Rust.
|
||||
*
|
||||
* ## Security
|
||||
*
|
||||
* This API has a scope configuration that forces you to restrict the URLs and paths that can be accessed using glob patterns.
|
||||
*
|
||||
* For instance, this scope configuration only allows making HTTP requests to the GitHub API for the `tauri-apps` organization:
|
||||
* ```json
|
||||
* {
|
||||
* "plugins": {
|
||||
* "http": {
|
||||
* "scope": ["https://api.github.com/repos/tauri-apps/*"]
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
declare global {
|
||||
interface Window {
|
||||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
20
dist-js/index.min.js
vendored
20
dist-js/index.min.js
vendored
@@ -1,8 +1,6 @@
|
||||
var f$1=Object.defineProperty;var g=(a,b)=>{for(var c in b)f$1(a,c,{get:b[c],enumerable:!0});};var e=(a,b,c)=>{if(!b.has(a))throw TypeError("Cannot "+c)};var h$1=(a,b,c)=>(e(a,b,"read from private field"),c?c.call(a):b.get(a)),i=(a,b,c)=>{if(b.has(a))throw TypeError("Cannot add the same private member more than once");b instanceof WeakSet?b.add(a):b.set(a,c);},j=(a,b,c,d)=>(e(a,b,"write to private field"),d?d.call(a,c):b.set(a,c),c);
|
||||
|
||||
var f={};g(f,{Channel:()=>a,PluginListener:()=>c,addPluginListener:()=>v,convertFileSrc:()=>m,invoke:()=>u,transformCallback:()=>o});function h(){return window.crypto.getRandomValues(new Uint32Array(1))[0]}function o(t,e=!1){let n=h(),r=`_${n}`;return Object.defineProperty(window,r,{value:i=>(e&&Reflect.deleteProperty(window,r),t?.(i)),writable:!1,configurable:!0}),n}var s,a=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0;i(this,s,()=>{});this.id=o(e=>{h$1(this,s).call(this,e);});}set onmessage(e){j(this,s,e);}get onmessage(){return h$1(this,s)}toJSON(){return `__CHANNEL__:${this.id}`}};s=new WeakMap;var c=class{constructor(e,n,r){this.plugin=e,this.event=n,this.channelId=r;}async unregister(){return u(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function v(t,e,n){let r=new a;return r.onmessage=n,u(`plugin:${t}|register_listener`,{event:e,handler:r}).then(()=>new c(t,e,r.id))}async function u(t,e={}){return new Promise((n,r)=>{let i=o(d=>{n(d),Reflect.deleteProperty(window,`_${g}`);},!0),g=o(d=>{r(d),Reflect.deleteProperty(window,`_${i}`);},!0);window.__TAURI_IPC__({cmd:t,callback:i,error:g,...e});})}function m(t,e="asset"){let n=encodeURIComponent(t);return navigator.userAgent.includes("Windows")?`https://${e}.localhost/${n}`:`${e}://localhost/${n}`}
|
||||
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@@ -173,7 +171,7 @@ class Client {
|
||||
* ```
|
||||
*/
|
||||
async drop() {
|
||||
return u("plugin:http|drop_client", {
|
||||
return window.__TAURI_INVOKE__("plugin:http|drop_client", {
|
||||
client: this.id,
|
||||
});
|
||||
}
|
||||
@@ -194,10 +192,12 @@ class Client {
|
||||
if (jsonResponse) {
|
||||
options.responseType = ResponseType.Text;
|
||||
}
|
||||
return u("plugin:http|request", {
|
||||
return window
|
||||
.__TAURI_INVOKE__("plugin:http|request", {
|
||||
clientId: this.id,
|
||||
options,
|
||||
}).then((res) => {
|
||||
})
|
||||
.then((res) => {
|
||||
const response = new Response(res);
|
||||
if (jsonResponse) {
|
||||
/* eslint-disable */
|
||||
@@ -338,9 +338,11 @@ class Client {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getClient(options) {
|
||||
return u("plugin:http|create_client", {
|
||||
return window
|
||||
.__TAURI_INVOKE__("plugin:http|create_client", {
|
||||
options,
|
||||
}).then((id) => new Client(id));
|
||||
})
|
||||
.then((id) => new Client(id));
|
||||
}
|
||||
/** @internal */
|
||||
let defaultClient = null;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,6 +1,6 @@
|
||||
import { invoke } from '@tauri-apps/api/tauri';
|
||||
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@@ -171,7 +171,7 @@ class Client {
|
||||
* ```
|
||||
*/
|
||||
async drop() {
|
||||
return invoke("plugin:http|drop_client", {
|
||||
return window.__TAURI_INVOKE__("plugin:http|drop_client", {
|
||||
client: this.id,
|
||||
});
|
||||
}
|
||||
@@ -192,10 +192,12 @@ class Client {
|
||||
if (jsonResponse) {
|
||||
options.responseType = ResponseType.Text;
|
||||
}
|
||||
return invoke("plugin:http|request", {
|
||||
return window
|
||||
.__TAURI_INVOKE__("plugin:http|request", {
|
||||
clientId: this.id,
|
||||
options,
|
||||
}).then((res) => {
|
||||
})
|
||||
.then((res) => {
|
||||
const response = new Response(res);
|
||||
if (jsonResponse) {
|
||||
/* eslint-disable */
|
||||
@@ -336,9 +338,11 @@ class Client {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getClient(options) {
|
||||
return invoke("plugin:http|create_client", {
|
||||
return window
|
||||
.__TAURI_INVOKE__("plugin:http|create_client", {
|
||||
options,
|
||||
}).then((id) => new Client(id));
|
||||
})
|
||||
.then((id) => new Client(id));
|
||||
}
|
||||
/** @internal */
|
||||
let defaultClient = null;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -24,7 +24,11 @@
|
||||
* @module
|
||||
*/
|
||||
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
declare global {
|
||||
interface Window {
|
||||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.0.0
|
||||
@@ -297,7 +301,7 @@ class Client {
|
||||
* ```
|
||||
*/
|
||||
async drop(): Promise<void> {
|
||||
return invoke("plugin:http|drop_client", {
|
||||
return window.__TAURI_INVOKE__("plugin:http|drop_client", {
|
||||
client: this.id,
|
||||
});
|
||||
}
|
||||
@@ -320,30 +324,32 @@ class Client {
|
||||
if (jsonResponse) {
|
||||
options.responseType = ResponseType.Text;
|
||||
}
|
||||
return invoke<IResponse<T>>("plugin:http|request", {
|
||||
clientId: this.id,
|
||||
options,
|
||||
}).then((res) => {
|
||||
const response = new Response(res);
|
||||
if (jsonResponse) {
|
||||
/* eslint-disable */
|
||||
try {
|
||||
response.data = JSON.parse(response.data as string);
|
||||
} catch (e) {
|
||||
if (response.ok && (response.data as unknown as string) === "") {
|
||||
response.data = {} as T;
|
||||
} else if (response.ok) {
|
||||
throw Error(
|
||||
`Failed to parse response \`${response.data}\` as JSON: ${e};
|
||||
return window
|
||||
.__TAURI_INVOKE__<IResponse<T>>("plugin:http|request", {
|
||||
clientId: this.id,
|
||||
options,
|
||||
})
|
||||
.then((res) => {
|
||||
const response = new Response(res);
|
||||
if (jsonResponse) {
|
||||
/* eslint-disable */
|
||||
try {
|
||||
response.data = JSON.parse(response.data as string);
|
||||
} catch (e) {
|
||||
if (response.ok && (response.data as unknown as string) === "") {
|
||||
response.data = {} as T;
|
||||
} else if (response.ok) {
|
||||
throw Error(
|
||||
`Failed to parse response \`${response.data}\` as JSON: ${e};
|
||||
try setting the \`responseType\` option to \`ResponseType.Text\` or \`ResponseType.Binary\` if the API does not return a JSON response.`
|
||||
);
|
||||
);
|
||||
}
|
||||
}
|
||||
/* eslint-enable */
|
||||
return response;
|
||||
}
|
||||
/* eslint-enable */
|
||||
return response;
|
||||
}
|
||||
return response;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -478,9 +484,11 @@ class Client {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getClient(options?: ClientOptions): Promise<Client> {
|
||||
return invoke<number>("plugin:http|create_client", {
|
||||
options,
|
||||
}).then((id) => new Client(id));
|
||||
return window
|
||||
.__TAURI_INVOKE__<number>("plugin:http|create_client", {
|
||||
options,
|
||||
})
|
||||
.then((id) => new Client(id));
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
|
||||
1
src/api-iife.js
Normal file
1
src/api-iife.js
Normal file
@@ -0,0 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_HTTP__=function(e){"use strict";var t;e.ResponseType=void 0,(t=e.ResponseType||(e.ResponseType={}))[t.JSON=1]="JSON",t[t.Text=2]="Text",t[t.Binary=3]="Binary";class r{constructor(e,t){this.type=e,this.payload=t}static form(e){const t={},s=(e,r)=>{if(null!==r){let s;s="string"==typeof r?r:r instanceof Uint8Array||Array.isArray(r)?Array.from(r):r instanceof File?{file:r.name,mime:r.type,fileName:r.name}:"string"==typeof r.file?{file:r.file,mime:r.mime,fileName:r.fileName}:{file:Array.from(r.file),mime:r.mime,fileName:r.fileName},t[String(e)]=s}};if(e instanceof FormData)for(const[t,r]of e)s(t,r);else for(const[t,r]of Object.entries(e))s(t,r);return new r("Form",t)}static json(e){return new r("Json",e)}static text(e){return new r("Text",e)}static bytes(e){return new r("Bytes",Array.from(e instanceof ArrayBuffer?new Uint8Array(e):e))}}class s{constructor(e){this.url=e.url,this.status=e.status,this.ok=this.status>=200&&this.status<300,this.headers=e.headers,this.rawHeaders=e.rawHeaders,this.data=e.data}}class n{constructor(e){this.id=e}async drop(){return window.__TAURI_INVOKE__("plugin:http|drop_client",{client:this.id})}async request(t){const r=!t.responseType||t.responseType===e.ResponseType.JSON;return r&&(t.responseType=e.ResponseType.Text),window.__TAURI_INVOKE__("plugin:http|request",{clientId:this.id,options:t}).then((e=>{const t=new s(e);if(r){try{t.data=JSON.parse(t.data)}catch(e){if(t.ok&&""===t.data)t.data={};else if(t.ok)throw Error(`Failed to parse response \`${t.data}\` as JSON: ${e};\n try setting the \`responseType\` option to \`ResponseType.Text\` or \`ResponseType.Binary\` if the API does not return a JSON response.`)}return t}return t}))}async get(e,t){return this.request({method:"GET",url:e,...t})}async post(e,t,r){return this.request({method:"POST",url:e,body:t,...r})}async put(e,t,r){return this.request({method:"PUT",url:e,body:t,...r})}async patch(e,t){return this.request({method:"PATCH",url:e,...t})}async delete(e,t){return this.request({method:"DELETE",url:e,...t})}}async function i(e){return window.__TAURI_INVOKE__("plugin:http|create_client",{options:e}).then((e=>new n(e)))}let o=null;return e.Body=r,e.Client=n,e.Response=s,e.fetch=async function(e,t){var r;return null===o&&(o=await i()),o.request({url:e,method:null!==(r=null==t?void 0:t.method)&&void 0!==r?r:"GET",...t})},e.getClient=i,e}({});Object.defineProperty(window.__TAURI__,"http",{value:__TAURI_HTTP__})}
|
||||
@@ -41,6 +41,7 @@ impl<R: Runtime, T: Manager<R>> HttpExt<R> for T {
|
||||
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R, Option<Config>> {
|
||||
Builder::<R, Option<Config>>::new("http")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::create_client,
|
||||
commands::drop_client,
|
||||
|
||||
Reference in New Issue
Block a user