mirror of
https://github.com/tauri-apps/tauri-plugin-http.git
synced 2026-01-31 00:45:17 +01:00
chore: adjust prettier config, .gitignore and use taplo to format toml files (#1728)
* chore: adjust prettier config, .gitignore and use taplo to format toml files This brings the plugins-workspace repository to the same code style of the main tauri repo * format toml * ignore examples gen dir * add .vscode/extensions.json * remove packageManager field * fmt * fix audit * taplo ignore permissions autogenerated files * remove create dummy dist * fix prettier workflow * install fmt in prettier workflow --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app> Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/10701104849 Co-authored-by: lucasfernog <lucasfernog@users.noreply.github.com>
This commit is contained in:
@@ -10,11 +10,11 @@ repository = { workspace = true }
|
||||
links = "tauri-plugin-http"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
rustc-args = [ "--cfg", "docsrs" ]
|
||||
rustdoc-args = [ "--cfg", "docsrs" ]
|
||||
rustc-args = ["--cfg", "docsrs"]
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[build-dependencies]
|
||||
tauri-plugin = { workspace = true, features = [ "build" ] }
|
||||
tauri-plugin = { workspace = true, features = ["build"] }
|
||||
schemars = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
url = { workspace = true }
|
||||
|
||||
10
README.md
10
README.md
@@ -60,11 +60,11 @@ fn main() {
|
||||
Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
|
||||
|
||||
```javascript
|
||||
import { fetch } from "@tauri-apps/plugin-http";
|
||||
const response = await fetch("http://localhost:3003/users/2", {
|
||||
method: "GET",
|
||||
timeout: 30,
|
||||
});
|
||||
import { fetch } from '@tauri-apps/plugin-http'
|
||||
const response = await fetch('http://localhost:3003/users/2', {
|
||||
method: 'GET',
|
||||
timeout: 30
|
||||
})
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
@@ -28,7 +28,7 @@ var core = require('@tauri-apps/api/core');
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
const ERROR_REQUEST_CANCELLED = "Request canceled";
|
||||
const ERROR_REQUEST_CANCELLED = 'Request canceled';
|
||||
/**
|
||||
* Fetch a resource from the network. It returns a `Promise` that resolves to the
|
||||
* `Response` to that `Request`, whether it is successful or not.
|
||||
@@ -83,13 +83,13 @@ async function fetch(input, init) {
|
||||
name,
|
||||
// we need to ensure we have all header values as strings
|
||||
// eslint-disable-next-line
|
||||
typeof val === "string" ? val : val.toString(),
|
||||
typeof val === 'string' ? val : val.toString()
|
||||
]);
|
||||
// abort early here if needed
|
||||
if (signal?.aborted) {
|
||||
throw new Error(ERROR_REQUEST_CANCELLED);
|
||||
}
|
||||
const rid = await core.invoke("plugin:http|fetch", {
|
||||
const rid = await core.invoke('plugin:http|fetch', {
|
||||
clientConfig: {
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
@@ -97,10 +97,10 @@ async function fetch(input, init) {
|
||||
data,
|
||||
maxRedirections,
|
||||
connectTimeout,
|
||||
proxy,
|
||||
},
|
||||
proxy
|
||||
}
|
||||
});
|
||||
const abort = () => core.invoke("plugin:http|fetch_cancel", { rid });
|
||||
const abort = () => core.invoke('plugin:http|fetch_cancel', { rid });
|
||||
// abort early here if needed
|
||||
if (signal?.aborted) {
|
||||
// we don't care about the result of this proimse
|
||||
@@ -108,12 +108,12 @@ async function fetch(input, init) {
|
||||
abort();
|
||||
throw new Error(ERROR_REQUEST_CANCELLED);
|
||||
}
|
||||
signal?.addEventListener("abort", () => void abort());
|
||||
const { status, statusText, url, headers: responseHeaders, rid: responseRid, } = await core.invoke("plugin:http|fetch_send", {
|
||||
rid,
|
||||
signal?.addEventListener('abort', () => void abort());
|
||||
const { status, statusText, url, headers: responseHeaders, rid: responseRid } = await core.invoke('plugin:http|fetch_send', {
|
||||
rid
|
||||
});
|
||||
const body = await core.invoke("plugin:http|fetch_read_body", {
|
||||
rid: responseRid,
|
||||
const body = await core.invoke('plugin:http|fetch_read_body', {
|
||||
rid: responseRid
|
||||
});
|
||||
const res = new Response(body instanceof ArrayBuffer && body.byteLength !== 0
|
||||
? body
|
||||
@@ -121,7 +121,7 @@ async function fetch(input, init) {
|
||||
? new Uint8Array(body)
|
||||
: null, {
|
||||
status,
|
||||
statusText,
|
||||
statusText
|
||||
});
|
||||
// url and headers are read only properties
|
||||
// but seems like we can set them like this
|
||||
@@ -129,9 +129,9 @@ async function fetch(input, init) {
|
||||
// we define theme like this, because using `Response`
|
||||
// constructor, it removes url and some headers
|
||||
// like `set-cookie` headers
|
||||
Object.defineProperty(res, "url", { value: url });
|
||||
Object.defineProperty(res, "headers", {
|
||||
value: new Headers(responseHeaders),
|
||||
Object.defineProperty(res, 'url', { value: url });
|
||||
Object.defineProperty(res, 'headers', {
|
||||
value: new Headers(responseHeaders)
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import { invoke } from '@tauri-apps/api/core';
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
const ERROR_REQUEST_CANCELLED = "Request canceled";
|
||||
const ERROR_REQUEST_CANCELLED = 'Request canceled';
|
||||
/**
|
||||
* Fetch a resource from the network. It returns a `Promise` that resolves to the
|
||||
* `Response` to that `Request`, whether it is successful or not.
|
||||
@@ -81,13 +81,13 @@ async function fetch(input, init) {
|
||||
name,
|
||||
// we need to ensure we have all header values as strings
|
||||
// eslint-disable-next-line
|
||||
typeof val === "string" ? val : val.toString(),
|
||||
typeof val === 'string' ? val : val.toString()
|
||||
]);
|
||||
// abort early here if needed
|
||||
if (signal?.aborted) {
|
||||
throw new Error(ERROR_REQUEST_CANCELLED);
|
||||
}
|
||||
const rid = await invoke("plugin:http|fetch", {
|
||||
const rid = await invoke('plugin:http|fetch', {
|
||||
clientConfig: {
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
@@ -95,10 +95,10 @@ async function fetch(input, init) {
|
||||
data,
|
||||
maxRedirections,
|
||||
connectTimeout,
|
||||
proxy,
|
||||
},
|
||||
proxy
|
||||
}
|
||||
});
|
||||
const abort = () => invoke("plugin:http|fetch_cancel", { rid });
|
||||
const abort = () => invoke('plugin:http|fetch_cancel', { rid });
|
||||
// abort early here if needed
|
||||
if (signal?.aborted) {
|
||||
// we don't care about the result of this proimse
|
||||
@@ -106,12 +106,12 @@ async function fetch(input, init) {
|
||||
abort();
|
||||
throw new Error(ERROR_REQUEST_CANCELLED);
|
||||
}
|
||||
signal?.addEventListener("abort", () => void abort());
|
||||
const { status, statusText, url, headers: responseHeaders, rid: responseRid, } = await invoke("plugin:http|fetch_send", {
|
||||
rid,
|
||||
signal?.addEventListener('abort', () => void abort());
|
||||
const { status, statusText, url, headers: responseHeaders, rid: responseRid } = await invoke('plugin:http|fetch_send', {
|
||||
rid
|
||||
});
|
||||
const body = await invoke("plugin:http|fetch_read_body", {
|
||||
rid: responseRid,
|
||||
const body = await invoke('plugin:http|fetch_read_body', {
|
||||
rid: responseRid
|
||||
});
|
||||
const res = new Response(body instanceof ArrayBuffer && body.byteLength !== 0
|
||||
? body
|
||||
@@ -119,7 +119,7 @@ async function fetch(input, init) {
|
||||
? new Uint8Array(body)
|
||||
: null, {
|
||||
status,
|
||||
statusText,
|
||||
statusText
|
||||
});
|
||||
// url and headers are read only properties
|
||||
// but seems like we can set them like this
|
||||
@@ -127,9 +127,9 @@ async function fetch(input, init) {
|
||||
// we define theme like this, because using `Response`
|
||||
// constructor, it removes url and some headers
|
||||
// like `set-cookie` headers
|
||||
Object.defineProperty(res, "url", { value: url });
|
||||
Object.defineProperty(res, "headers", {
|
||||
value: new Headers(responseHeaders),
|
||||
Object.defineProperty(res, 'url', { value: url });
|
||||
Object.defineProperty(res, 'headers', {
|
||||
value: new Headers(responseHeaders)
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
* @module
|
||||
*/
|
||||
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
|
||||
/**
|
||||
* Configuration of a proxy that a Client should pass requests to.
|
||||
@@ -37,34 +37,34 @@ export interface Proxy {
|
||||
/**
|
||||
* Proxy all traffic to the passed URL.
|
||||
*/
|
||||
all?: string | ProxyConfig;
|
||||
all?: string | ProxyConfig
|
||||
/**
|
||||
* Proxy all HTTP traffic to the passed URL.
|
||||
*/
|
||||
http?: string | ProxyConfig;
|
||||
http?: string | ProxyConfig
|
||||
/**
|
||||
* Proxy all HTTPS traffic to the passed URL.
|
||||
*/
|
||||
https?: string | ProxyConfig;
|
||||
https?: string | ProxyConfig
|
||||
}
|
||||
|
||||
export interface ProxyConfig {
|
||||
/**
|
||||
* The URL of the proxy server.
|
||||
*/
|
||||
url: string;
|
||||
url: string
|
||||
/**
|
||||
* Set the `Proxy-Authorization` header using Basic auth.
|
||||
*/
|
||||
basicAuth?: {
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
/**
|
||||
* A configuration for filtering out requests that shouldn't be proxied.
|
||||
* Entries are expected to be comma-separated (whitespace between entries is ignored)
|
||||
*/
|
||||
noProxy?: string;
|
||||
noProxy?: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,16 +77,16 @@ export interface ClientOptions {
|
||||
* Defines the maximum number of redirects the client should follow.
|
||||
* If set to 0, no redirects will be followed.
|
||||
*/
|
||||
maxRedirections?: number;
|
||||
maxRedirections?: number
|
||||
/** Timeout in milliseconds */
|
||||
connectTimeout?: number;
|
||||
connectTimeout?: number
|
||||
/**
|
||||
* Configuration of a proxy that a Client should pass requests to.
|
||||
*/
|
||||
proxy?: Proxy;
|
||||
proxy?: Proxy
|
||||
}
|
||||
|
||||
const ERROR_REQUEST_CANCELLED = "Request canceled";
|
||||
const ERROR_REQUEST_CANCELLED = 'Request canceled'
|
||||
|
||||
/**
|
||||
* Fetch a resource from the network. It returns a `Promise` that resolves to the
|
||||
@@ -104,41 +104,41 @@ const ERROR_REQUEST_CANCELLED = "Request canceled";
|
||||
*/
|
||||
export async function fetch(
|
||||
input: URL | Request | string,
|
||||
init?: RequestInit & ClientOptions,
|
||||
init?: RequestInit & ClientOptions
|
||||
): Promise<Response> {
|
||||
// abort early here if needed
|
||||
const signal = init?.signal;
|
||||
const signal = init?.signal
|
||||
if (signal?.aborted) {
|
||||
throw new Error(ERROR_REQUEST_CANCELLED);
|
||||
throw new Error(ERROR_REQUEST_CANCELLED)
|
||||
}
|
||||
|
||||
const maxRedirections = init?.maxRedirections;
|
||||
const connectTimeout = init?.connectTimeout;
|
||||
const proxy = init?.proxy;
|
||||
const maxRedirections = init?.maxRedirections
|
||||
const connectTimeout = init?.connectTimeout
|
||||
const proxy = init?.proxy
|
||||
|
||||
// Remove these fields before creating the request
|
||||
if (init) {
|
||||
delete init.maxRedirections;
|
||||
delete init.connectTimeout;
|
||||
delete init.proxy;
|
||||
delete init.maxRedirections
|
||||
delete init.connectTimeout
|
||||
delete init.proxy
|
||||
}
|
||||
|
||||
const headers = init?.headers
|
||||
? init.headers instanceof Headers
|
||||
? init.headers
|
||||
: new Headers(init.headers)
|
||||
: new Headers();
|
||||
: new Headers()
|
||||
|
||||
const req = new Request(input, init);
|
||||
const buffer = await req.arrayBuffer();
|
||||
const req = new Request(input, init)
|
||||
const buffer = await req.arrayBuffer()
|
||||
const data =
|
||||
buffer.byteLength !== 0 ? Array.from(new Uint8Array(buffer)) : null;
|
||||
buffer.byteLength !== 0 ? Array.from(new Uint8Array(buffer)) : null
|
||||
|
||||
// append new headers created by the browser `Request` implementation,
|
||||
// if not already declared by the caller of this function
|
||||
for (const [key, value] of req.headers) {
|
||||
if (!headers.get(key)) {
|
||||
headers.set(key, value);
|
||||
headers.set(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ export async function fetch(
|
||||
? Array.from(headers.entries())
|
||||
: Array.isArray(headers)
|
||||
? headers
|
||||
: Object.entries(headers);
|
||||
: Object.entries(headers)
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const mappedHeaders: Array<[string, string]> = headersArray.map(
|
||||
@@ -155,16 +155,16 @@ export async function fetch(
|
||||
name,
|
||||
// we need to ensure we have all header values as strings
|
||||
// eslint-disable-next-line
|
||||
typeof val === "string" ? val : (val as any).toString(),
|
||||
],
|
||||
);
|
||||
typeof val === 'string' ? val : (val as any).toString()
|
||||
]
|
||||
)
|
||||
|
||||
// abort early here if needed
|
||||
if (signal?.aborted) {
|
||||
throw new Error(ERROR_REQUEST_CANCELLED);
|
||||
throw new Error(ERROR_REQUEST_CANCELLED)
|
||||
}
|
||||
|
||||
const rid = await invoke<number>("plugin:http|fetch", {
|
||||
const rid = await invoke<number>('plugin:http|fetch', {
|
||||
clientConfig: {
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
@@ -172,28 +172,28 @@ export async function fetch(
|
||||
data,
|
||||
maxRedirections,
|
||||
connectTimeout,
|
||||
proxy,
|
||||
},
|
||||
});
|
||||
proxy
|
||||
}
|
||||
})
|
||||
|
||||
const abort = () => invoke("plugin:http|fetch_cancel", { rid });
|
||||
const abort = () => invoke('plugin:http|fetch_cancel', { rid })
|
||||
|
||||
// abort early here if needed
|
||||
if (signal?.aborted) {
|
||||
// we don't care about the result of this proimse
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
abort();
|
||||
throw new Error(ERROR_REQUEST_CANCELLED);
|
||||
abort()
|
||||
throw new Error(ERROR_REQUEST_CANCELLED)
|
||||
}
|
||||
|
||||
signal?.addEventListener("abort", () => void abort());
|
||||
signal?.addEventListener('abort', () => void abort())
|
||||
|
||||
interface FetchSendResponse {
|
||||
status: number;
|
||||
statusText: string;
|
||||
headers: [[string, string]];
|
||||
url: string;
|
||||
rid: number;
|
||||
status: number
|
||||
statusText: string
|
||||
headers: [[string, string]]
|
||||
url: string
|
||||
rid: number
|
||||
}
|
||||
|
||||
const {
|
||||
@@ -201,17 +201,17 @@ export async function fetch(
|
||||
statusText,
|
||||
url,
|
||||
headers: responseHeaders,
|
||||
rid: responseRid,
|
||||
} = await invoke<FetchSendResponse>("plugin:http|fetch_send", {
|
||||
rid,
|
||||
});
|
||||
rid: responseRid
|
||||
} = await invoke<FetchSendResponse>('plugin:http|fetch_send', {
|
||||
rid
|
||||
})
|
||||
|
||||
const body = await invoke<ArrayBuffer | number[]>(
|
||||
"plugin:http|fetch_read_body",
|
||||
'plugin:http|fetch_read_body',
|
||||
{
|
||||
rid: responseRid,
|
||||
},
|
||||
);
|
||||
rid: responseRid
|
||||
}
|
||||
)
|
||||
|
||||
const res = new Response(
|
||||
body instanceof ArrayBuffer && body.byteLength !== 0
|
||||
@@ -221,9 +221,9 @@ export async function fetch(
|
||||
: null,
|
||||
{
|
||||
status,
|
||||
statusText,
|
||||
},
|
||||
);
|
||||
statusText
|
||||
}
|
||||
)
|
||||
|
||||
// url and headers are read only properties
|
||||
// but seems like we can set them like this
|
||||
@@ -231,10 +231,10 @@ export async function fetch(
|
||||
// we define theme like this, because using `Response`
|
||||
// constructor, it removes url and some headers
|
||||
// like `set-cookie` headers
|
||||
Object.defineProperty(res, "url", { value: url });
|
||||
Object.defineProperty(res, "headers", {
|
||||
value: new Headers(responseHeaders),
|
||||
});
|
||||
Object.defineProperty(res, 'url', { value: url })
|
||||
Object.defineProperty(res, 'headers', {
|
||||
value: new Headers(responseHeaders)
|
||||
})
|
||||
|
||||
return res;
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { createConfig } from "../../shared/rollup.config.js";
|
||||
import { createConfig } from '../../shared/rollup.config.js'
|
||||
|
||||
export default createConfig();
|
||||
export default createConfig()
|
||||
|
||||
Reference in New Issue
Block a user