rust-fetch 0.7.2 → 1.0.0
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/README.md +192 -0
- package/package.json +28 -27
- package/rust_fetch.d.ts +106 -0
- package/{wasm-module/pkg/wasm_module.js → rust_fetch.js} +247 -39
- package/rust_fetch_bg.wasm +0 -0
- package/client.ts +0 -14
- package/rust-fetch-package/Cargo.toml +0 -13
- package/rust-fetch-package/src/lib.rs +0 -14
- package/server.ts +0 -13
- package/tsconfig.json +0 -111
- package/wasm-module/Cargo.lock +0 -1387
- package/wasm-module/Cargo.toml +0 -15
- package/wasm-module/client.d.ts +0 -4
- package/wasm-module/client.js +0 -37
- package/wasm-module/pkg/package.json +0 -15
- package/wasm-module/pkg/wasm_module.d.ts +0 -53
- package/wasm-module/pkg/wasm_module_bg.wasm +0 -0
- package/wasm-module/pkg/wasm_module_bg.wasm.d.ts +0 -20
- package/wasm-module/server.d.ts +0 -4
- package/wasm-module/server.js +0 -80
- package/wasm-module/src/greetings/mod.rs +0 -7
- package/wasm-module/src/http/mod.rs +0 -86
- package/wasm-module/src/lib.rs +0 -10
- package/wasm-module/src/math/mod.rs +0 -7
package/README.md
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# Rust Fetch - World-Class HTTP Client for WebAssembly
|
|
2
|
+
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](https://www.rust-lang.org/)
|
|
5
|
+
[](https://webassembly.org/)
|
|
6
|
+
|
|
7
|
+
A production-ready, world-class HTTP client library for WebAssembly with excellent error handling, retry logic, and a clean API design.
|
|
8
|
+
|
|
9
|
+
## 🚀 Features
|
|
10
|
+
|
|
11
|
+
- **Type-safe HTTP Client**: Builder pattern for constructing requests with compile-time safety
|
|
12
|
+
- **Comprehensive Error Handling**: Detailed error types with context and proper error chaining
|
|
13
|
+
- **Automatic Retries**: Built-in retry logic with exponential backoff for transient failures
|
|
14
|
+
- **Multiple Response Formats**: Support for JSON, text, and binary responses
|
|
15
|
+
- **Platform Optimized**: Specific optimizations for both WASM and native targets
|
|
16
|
+
- **Developer Friendly**: Clean API with both simple functions and advanced client builders
|
|
17
|
+
- **Backward Compatible**: Maintains compatibility with legacy API while providing modern alternatives
|
|
18
|
+
|
|
19
|
+
## 📦 Installation
|
|
20
|
+
|
|
21
|
+
Add this to your `Cargo.toml`:
|
|
22
|
+
|
|
23
|
+
```toml
|
|
24
|
+
[dependencies]
|
|
25
|
+
rust-fetch = "1.0.0"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
For WebAssembly projects, make sure to include:
|
|
29
|
+
|
|
30
|
+
```toml
|
|
31
|
+
[dependencies]
|
|
32
|
+
wasm-bindgen = "0.2"
|
|
33
|
+
wasm-bindgen-futures = "0.4"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## 🔧 Usage
|
|
37
|
+
|
|
38
|
+
### Simple API
|
|
39
|
+
|
|
40
|
+
For quick and easy HTTP requests:
|
|
41
|
+
|
|
42
|
+
```rust
|
|
43
|
+
use rust_fetch::prelude::*;
|
|
44
|
+
|
|
45
|
+
// Fetch JSON data
|
|
46
|
+
let data = fetch_json("https://api.example.com/data").await?;
|
|
47
|
+
|
|
48
|
+
// Fetch text/HTML
|
|
49
|
+
let html = fetch_text("https://example.com").await?;
|
|
50
|
+
|
|
51
|
+
// Advanced request with options
|
|
52
|
+
let response = fetch_with_options(
|
|
53
|
+
"https://api.example.com/users",
|
|
54
|
+
"POST",
|
|
55
|
+
headers_obj, // JavaScript headers object
|
|
56
|
+
Some(r#"{"name": "John Doe"}"#.to_string()),
|
|
57
|
+
).await?;
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Advanced Client API
|
|
61
|
+
|
|
62
|
+
For more control and configuration:
|
|
63
|
+
|
|
64
|
+
```rust
|
|
65
|
+
use rust_fetch::client::Client;
|
|
66
|
+
use rust_fetch::types::RetryConfig;
|
|
67
|
+
use std::time::Duration;
|
|
68
|
+
|
|
69
|
+
// Create a configured client
|
|
70
|
+
let client = Client::builder()
|
|
71
|
+
.base_url("https://api.example.com")
|
|
72
|
+
.default_header("Authorization", "Bearer token")
|
|
73
|
+
.timeout(Duration::from_secs(10))
|
|
74
|
+
.retry_config(RetryConfig {
|
|
75
|
+
max_retries: 3,
|
|
76
|
+
initial_delay: Duration::from_millis(100),
|
|
77
|
+
..Default::default()
|
|
78
|
+
})
|
|
79
|
+
.build()?;
|
|
80
|
+
|
|
81
|
+
// Make requests
|
|
82
|
+
let response = client
|
|
83
|
+
.post("/users")
|
|
84
|
+
.json(&serde_json::json!({
|
|
85
|
+
"name": "Jane Doe",
|
|
86
|
+
"email": "jane@example.com"
|
|
87
|
+
}))?
|
|
88
|
+
.send()
|
|
89
|
+
.await?;
|
|
90
|
+
|
|
91
|
+
// Access response data
|
|
92
|
+
println!("Status: {}", response.status);
|
|
93
|
+
if let Some(json) = response.json() {
|
|
94
|
+
println!("Response: {:?}", json);
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### WebAssembly JavaScript API
|
|
99
|
+
|
|
100
|
+
When compiled to WebAssembly, the library exposes a JavaScript-friendly API:
|
|
101
|
+
|
|
102
|
+
```javascript
|
|
103
|
+
import init, { WasmClient, fetch_json, fetch_text } from './rust_fetch.js';
|
|
104
|
+
|
|
105
|
+
await init();
|
|
106
|
+
|
|
107
|
+
// Simple API
|
|
108
|
+
const data = await fetch_json("https://api.example.com/data");
|
|
109
|
+
const html = await fetch_text("https://example.com");
|
|
110
|
+
|
|
111
|
+
// Client API
|
|
112
|
+
const client = new WasmClient();
|
|
113
|
+
const response = await client.get("https://api.example.com/users");
|
|
114
|
+
console.log(response);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## 🏗️ Architecture
|
|
118
|
+
|
|
119
|
+
The library is organized into several modules:
|
|
120
|
+
|
|
121
|
+
- **`error`**: Comprehensive error types with detailed context
|
|
122
|
+
- **`types`**: Core types like `Method`, `Headers`, `Response`, etc.
|
|
123
|
+
- **`client`**: The main HTTP client with builder pattern
|
|
124
|
+
- **`http`**: High-level convenience functions for common use cases
|
|
125
|
+
|
|
126
|
+
### Error Handling
|
|
127
|
+
|
|
128
|
+
The library provides rich error types that preserve context:
|
|
129
|
+
|
|
130
|
+
```rust
|
|
131
|
+
use rust_fetch::error::{Error, Result};
|
|
132
|
+
|
|
133
|
+
match fetch_json(url).await {
|
|
134
|
+
Ok(data) => println!("Success: {:?}", data),
|
|
135
|
+
Err(Error::Network { message, source }) => {
|
|
136
|
+
eprintln!("Network error: {}", message);
|
|
137
|
+
}
|
|
138
|
+
Err(Error::Http { status, status_text, body }) => {
|
|
139
|
+
eprintln!("HTTP {}: {}", status, status_text);
|
|
140
|
+
}
|
|
141
|
+
Err(e) => eprintln!("Other error: {}", e),
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Request Building
|
|
146
|
+
|
|
147
|
+
The client provides a fluent API for building requests:
|
|
148
|
+
|
|
149
|
+
```rust
|
|
150
|
+
let response = client
|
|
151
|
+
.request(Method::Put, "/users/123")
|
|
152
|
+
.header("Content-Type", "application/json")
|
|
153
|
+
.header("X-Custom-Header", "value")
|
|
154
|
+
.timeout(Duration::from_secs(5))
|
|
155
|
+
.json(&updated_user)?
|
|
156
|
+
.send()
|
|
157
|
+
.await?;
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## 🧪 Testing
|
|
161
|
+
|
|
162
|
+
Run tests for native target:
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
cargo test
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Run tests for WebAssembly target:
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
wasm-pack test --headless --chrome
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## 🤝 Contributing
|
|
175
|
+
|
|
176
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
177
|
+
|
|
178
|
+
## 📄 License
|
|
179
|
+
|
|
180
|
+
This project is licensed under either of
|
|
181
|
+
|
|
182
|
+
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
183
|
+
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
184
|
+
|
|
185
|
+
at your option.
|
|
186
|
+
|
|
187
|
+
## 🙏 Acknowledgments
|
|
188
|
+
|
|
189
|
+
Built with love using:
|
|
190
|
+
- [reqwest](https://github.com/seanmonstar/reqwest) - HTTP client
|
|
191
|
+
- [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen) - WebAssembly bindings
|
|
192
|
+
- [serde](https://github.com/serde-rs/serde) - Serialization framework
|
package/package.json
CHANGED
|
@@ -1,30 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rust-fetch",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"collaborators": [
|
|
5
|
+
"Asem Qaffaf <asem@qaffaf.com>"
|
|
6
|
+
],
|
|
7
|
+
"description": "A world-class WebAssembly HTTP client library with excellent error handling and clean API",
|
|
8
|
+
"version": "1.0.0",
|
|
9
|
+
"license": "MIT OR Apache-2.0",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/asemqaffaf/rust-fetch"
|
|
6
13
|
},
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
"keywords": [
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"@types/node": "^22.13.5",
|
|
26
|
-
"typescript": "^5.7.3"
|
|
27
|
-
},
|
|
28
|
-
"main": "index.js",
|
|
29
|
-
"description": ""
|
|
30
|
-
}
|
|
14
|
+
"files": [
|
|
15
|
+
"rust_fetch_bg.wasm",
|
|
16
|
+
"rust_fetch.js",
|
|
17
|
+
"rust_fetch.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"main": "rust_fetch.js",
|
|
20
|
+
"types": "rust_fetch.d.ts",
|
|
21
|
+
"sideEffects": [
|
|
22
|
+
"./snippets/*"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"wasm",
|
|
26
|
+
"http",
|
|
27
|
+
"client",
|
|
28
|
+
"fetch",
|
|
29
|
+
"webassembly"
|
|
30
|
+
]
|
|
31
|
+
}
|
package/rust_fetch.d.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* Simple fetch function for JSON data
|
|
5
|
+
*/
|
|
6
|
+
export function fetch_json(url: string): Promise<any>;
|
|
7
|
+
/**
|
|
8
|
+
* Fetch JSON data and return as a Promise
|
|
9
|
+
*/
|
|
10
|
+
export function fetch_json_promise(url: string): Promise<any>;
|
|
11
|
+
/**
|
|
12
|
+
* Simple fetch function for text/HTML data
|
|
13
|
+
*/
|
|
14
|
+
export function fetch_text(url: string): Promise<string>;
|
|
15
|
+
/**
|
|
16
|
+
* Advanced fetch function with full options
|
|
17
|
+
*/
|
|
18
|
+
export function fetch_with_options(url: string, method: string, headers: any, body?: string): Promise<any>;
|
|
19
|
+
/**
|
|
20
|
+
* Create a configured HTTP client (exported for advanced use)
|
|
21
|
+
*/
|
|
22
|
+
export function create_client(): WasmClient;
|
|
23
|
+
/**
|
|
24
|
+
* Fetch JSON data (deprecated, use fetch_json instead)
|
|
25
|
+
*/
|
|
26
|
+
export function fetch_wasm_json(url: string): Promise<any>;
|
|
27
|
+
/**
|
|
28
|
+
* Fetch HTML data (deprecated, use fetch_text instead)
|
|
29
|
+
*/
|
|
30
|
+
export function fetch_wasm_html(url: string): Promise<string>;
|
|
31
|
+
/**
|
|
32
|
+
* Fetch data and return as map (deprecated, use fetch_json instead)
|
|
33
|
+
*/
|
|
34
|
+
export function fetch_wasm_map(url: string): Promise<any>;
|
|
35
|
+
/**
|
|
36
|
+
* Fetch API data (deprecated, use fetch_json instead)
|
|
37
|
+
*/
|
|
38
|
+
export function fetch_wasm_api(url: string): Promise<any>;
|
|
39
|
+
/**
|
|
40
|
+
* WASM bindings for the client
|
|
41
|
+
*/
|
|
42
|
+
export class WasmClient {
|
|
43
|
+
free(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Create a new client
|
|
46
|
+
*/
|
|
47
|
+
constructor();
|
|
48
|
+
/**
|
|
49
|
+
* Make a GET request
|
|
50
|
+
*/
|
|
51
|
+
get(url: string): Promise<any>;
|
|
52
|
+
/**
|
|
53
|
+
* Make a POST request with JSON body
|
|
54
|
+
*/
|
|
55
|
+
post_json(url: string, body: any): Promise<any>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
59
|
+
|
|
60
|
+
export interface InitOutput {
|
|
61
|
+
readonly memory: WebAssembly.Memory;
|
|
62
|
+
readonly fetch_json: (a: number, b: number) => any;
|
|
63
|
+
readonly fetch_json_promise: (a: number, b: number) => any;
|
|
64
|
+
readonly fetch_text: (a: number, b: number) => any;
|
|
65
|
+
readonly fetch_with_options: (a: number, b: number, c: number, d: number, e: any, f: number, g: number) => any;
|
|
66
|
+
readonly create_client: () => [number, number, number];
|
|
67
|
+
readonly fetch_wasm_json: (a: number, b: number) => any;
|
|
68
|
+
readonly fetch_wasm_html: (a: number, b: number) => any;
|
|
69
|
+
readonly fetch_wasm_map: (a: number, b: number) => any;
|
|
70
|
+
readonly fetch_wasm_api: (a: number, b: number) => any;
|
|
71
|
+
readonly __wbg_wasmclient_free: (a: number, b: number) => void;
|
|
72
|
+
readonly wasmclient_new: () => [number, number, number];
|
|
73
|
+
readonly wasmclient_get: (a: number, b: number, c: number) => any;
|
|
74
|
+
readonly wasmclient_post_json: (a: number, b: number, c: number, d: any) => any;
|
|
75
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
76
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
77
|
+
readonly __wbindgen_exn_store: (a: number) => void;
|
|
78
|
+
readonly __externref_table_alloc: () => number;
|
|
79
|
+
readonly __wbindgen_export_4: WebAssembly.Table;
|
|
80
|
+
readonly __wbindgen_export_5: WebAssembly.Table;
|
|
81
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
82
|
+
readonly closure243_externref_shim: (a: number, b: number, c: any) => void;
|
|
83
|
+
readonly closure265_externref_shim: (a: number, b: number, c: any, d: any) => void;
|
|
84
|
+
readonly __wbindgen_start: () => void;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
88
|
+
/**
|
|
89
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
90
|
+
* a precompiled `WebAssembly.Module`.
|
|
91
|
+
*
|
|
92
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
93
|
+
*
|
|
94
|
+
* @returns {InitOutput}
|
|
95
|
+
*/
|
|
96
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
100
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
101
|
+
*
|
|
102
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
103
|
+
*
|
|
104
|
+
* @returns {Promise<InitOutput>}
|
|
105
|
+
*/
|
|
106
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|