mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +02:00
feat(plugins): inject API on window.__TAURI__ (#383)
This commit is contained in:
committed by
GitHub
parent
3c8577bc9a
commit
b131bc8f7c
@@ -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 */
|
||||
|
||||
@@ -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