@tauri-apps/plugin-http 2.0.0-beta.7 → 2.0.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist-js/index.cjs CHANGED
@@ -28,6 +28,7 @@ var core = require('@tauri-apps/api/core');
28
28
  *
29
29
  * @module
30
30
  */
31
+ const ERROR_REQUEST_CANCELLED = "Request canceled";
31
32
  /**
32
33
  * Fetch a resource from the network. It returns a `Promise` that resolves to the
33
34
  * `Response` to that `Request`, whether it is successful or not.
@@ -43,6 +44,11 @@ var core = require('@tauri-apps/api/core');
43
44
  * @since 2.0.0
44
45
  */
45
46
  async function fetch(input, init) {
47
+ // abort early here if needed
48
+ const signal = init?.signal;
49
+ if (signal?.aborted) {
50
+ throw new Error(ERROR_REQUEST_CANCELLED);
51
+ }
46
52
  const maxRedirections = init?.maxRedirections;
47
53
  const connectTimeout = init?.connectTimeout;
48
54
  const proxy = init?.proxy;
@@ -52,7 +58,6 @@ async function fetch(input, init) {
52
58
  delete init.connectTimeout;
53
59
  delete init.proxy;
54
60
  }
55
- const signal = init?.signal;
56
61
  const headers = init?.headers
57
62
  ? init.headers instanceof Headers
58
63
  ? init.headers
@@ -80,6 +85,10 @@ async function fetch(input, init) {
80
85
  // eslint-disable-next-line
81
86
  typeof val === "string" ? val : val.toString(),
82
87
  ]);
88
+ // abort early here if needed
89
+ if (signal?.aborted) {
90
+ throw new Error(ERROR_REQUEST_CANCELLED);
91
+ }
83
92
  const rid = await core.invoke("plugin:http|fetch", {
84
93
  clientConfig: {
85
94
  method: req.method,
@@ -91,11 +100,15 @@ async function fetch(input, init) {
91
100
  proxy,
92
101
  },
93
102
  });
94
- signal?.addEventListener("abort", () => {
95
- void core.invoke("plugin:http|fetch_cancel", {
96
- rid,
97
- });
98
- });
103
+ const abort = () => core.invoke("plugin:http|fetch_cancel", { rid });
104
+ // abort early here if needed
105
+ if (signal?.aborted) {
106
+ // we don't care about the result of this proimse
107
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
108
+ abort();
109
+ throw new Error(ERROR_REQUEST_CANCELLED);
110
+ }
111
+ signal?.addEventListener("abort", () => abort);
99
112
  const { status, statusText, url, headers: responseHeaders, rid: responseRid, } = await core.invoke("plugin:http|fetch_send", {
100
113
  rid,
101
114
  });
@@ -107,12 +120,19 @@ async function fetch(input, init) {
107
120
  : body instanceof Array && body.length > 0
108
121
  ? new Uint8Array(body)
109
122
  : null, {
110
- headers: responseHeaders,
111
123
  status,
112
124
  statusText,
113
125
  });
114
- // url is read only but seems like we can do this
126
+ // url and headers are read only properties
127
+ // but seems like we can set them like this
128
+ //
129
+ // we define theme like this, because using `Response`
130
+ // constructor, it removes url and some headers
131
+ // like `set-cookie` headers
115
132
  Object.defineProperty(res, "url", { value: url });
133
+ Object.defineProperty(res, "headers", {
134
+ value: new Headers(responseHeaders),
135
+ });
116
136
  return res;
117
137
  }
118
138
 
package/dist-js/index.js CHANGED
@@ -26,6 +26,7 @@ import { invoke } from '@tauri-apps/api/core';
26
26
  *
27
27
  * @module
28
28
  */
29
+ const ERROR_REQUEST_CANCELLED = "Request canceled";
29
30
  /**
30
31
  * Fetch a resource from the network. It returns a `Promise` that resolves to the
31
32
  * `Response` to that `Request`, whether it is successful or not.
@@ -41,6 +42,11 @@ import { invoke } from '@tauri-apps/api/core';
41
42
  * @since 2.0.0
42
43
  */
43
44
  async function fetch(input, init) {
45
+ // abort early here if needed
46
+ const signal = init?.signal;
47
+ if (signal?.aborted) {
48
+ throw new Error(ERROR_REQUEST_CANCELLED);
49
+ }
44
50
  const maxRedirections = init?.maxRedirections;
45
51
  const connectTimeout = init?.connectTimeout;
46
52
  const proxy = init?.proxy;
@@ -50,7 +56,6 @@ async function fetch(input, init) {
50
56
  delete init.connectTimeout;
51
57
  delete init.proxy;
52
58
  }
53
- const signal = init?.signal;
54
59
  const headers = init?.headers
55
60
  ? init.headers instanceof Headers
56
61
  ? init.headers
@@ -78,6 +83,10 @@ async function fetch(input, init) {
78
83
  // eslint-disable-next-line
79
84
  typeof val === "string" ? val : val.toString(),
80
85
  ]);
86
+ // abort early here if needed
87
+ if (signal?.aborted) {
88
+ throw new Error(ERROR_REQUEST_CANCELLED);
89
+ }
81
90
  const rid = await invoke("plugin:http|fetch", {
82
91
  clientConfig: {
83
92
  method: req.method,
@@ -89,11 +98,15 @@ async function fetch(input, init) {
89
98
  proxy,
90
99
  },
91
100
  });
92
- signal?.addEventListener("abort", () => {
93
- void invoke("plugin:http|fetch_cancel", {
94
- rid,
95
- });
96
- });
101
+ const abort = () => invoke("plugin:http|fetch_cancel", { rid });
102
+ // abort early here if needed
103
+ if (signal?.aborted) {
104
+ // we don't care about the result of this proimse
105
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
106
+ abort();
107
+ throw new Error(ERROR_REQUEST_CANCELLED);
108
+ }
109
+ signal?.addEventListener("abort", () => abort);
97
110
  const { status, statusText, url, headers: responseHeaders, rid: responseRid, } = await invoke("plugin:http|fetch_send", {
98
111
  rid,
99
112
  });
@@ -105,12 +118,19 @@ async function fetch(input, init) {
105
118
  : body instanceof Array && body.length > 0
106
119
  ? new Uint8Array(body)
107
120
  : null, {
108
- headers: responseHeaders,
109
121
  status,
110
122
  statusText,
111
123
  });
112
- // url is read only but seems like we can do this
124
+ // url and headers are read only properties
125
+ // but seems like we can set them like this
126
+ //
127
+ // we define theme like this, because using `Response`
128
+ // constructor, it removes url and some headers
129
+ // like `set-cookie` headers
113
130
  Object.defineProperty(res, "url", { value: url });
131
+ Object.defineProperty(res, "headers", {
132
+ value: new Headers(responseHeaders),
133
+ });
114
134
  return res;
115
135
  }
116
136
 
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-http",
3
- "version": "2.0.0-beta.7",
3
+ "version": "2.0.0-beta.9",
4
4
  "license": "MIT or APACHE-2.0",
5
5
  "authors": [
6
6
  "Tauri Programme within The Commons Conservancy"
7
7
  ],
8
+ "repository": "https://github.com/tauri-apps/plugins-workspace",
8
9
  "type": "module",
9
10
  "types": "./dist-js/index.d.ts",
10
11
  "main": "./dist-js/index.cjs",
@@ -14,15 +15,15 @@
14
15
  "import": "./dist-js/index.js",
15
16
  "require": "./dist-js/index.cjs"
16
17
  },
18
+ "scripts": {
19
+ "build": "rollup -c"
20
+ },
17
21
  "files": [
18
22
  "dist-js",
19
23
  "README.md",
20
24
  "LICENSE"
21
25
  ],
22
26
  "dependencies": {
23
- "@tauri-apps/api": "2.0.0-beta.14"
24
- },
25
- "scripts": {
26
- "build": "rollup -c"
27
+ "@tauri-apps/api": "^2.0.0-beta.16"
27
28
  }
28
- }
29
+ }