rust-fetch 1.0.0 โ 1.0.1
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 +233 -137
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,192 +1,288 @@
|
|
|
1
|
-
#
|
|
1
|
+
# rust-fetch
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://www.rust-lang.org/)
|
|
5
|
-
[](https://webassembly.org/)
|
|
3
|
+
A WebAssembly-powered HTTP client library for JavaScript/TypeScript applications, built with Rust. This library provides fast, memory-safe HTTP operations through WebAssembly bindings, allowing you to leverage Rust's performance and safety guarantees in your web applications.
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
## Features
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
- ๐ **High Performance**: Leverage Rust's performance for HTTP operations through WebAssembly
|
|
8
|
+
- ๐ **Memory Safe**: Built with Rust's memory safety guarantees
|
|
9
|
+
- ๐ **Cross-Platform**: Works in both browser and Node.js environments
|
|
10
|
+
- ๐ฆ **TypeScript Support**: Full TypeScript definitions included
|
|
11
|
+
- ๐ ๏ธ **Multiple Utilities**: HTTP fetching, JSON parsing, HTML retrieval, and utility functions
|
|
10
12
|
|
|
11
|
-
|
|
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
|
|
13
|
+
## Installation
|
|
18
14
|
|
|
19
|
-
|
|
15
|
+
```bash
|
|
16
|
+
npm install rust-fetch
|
|
17
|
+
```
|
|
20
18
|
|
|
21
|
-
|
|
19
|
+
or
|
|
22
20
|
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
rust-fetch = "1.0.0"
|
|
21
|
+
```bash
|
|
22
|
+
yarn add rust-fetch
|
|
26
23
|
```
|
|
27
24
|
|
|
28
|
-
|
|
25
|
+
## Project Structure
|
|
29
26
|
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
```
|
|
28
|
+
rust-fetch/
|
|
29
|
+
โโโ rust-fetch-package/ # Main Rust package (currently basic implementation)
|
|
30
|
+
โ โโโ Cargo.toml
|
|
31
|
+
โ โโโ src/
|
|
32
|
+
โ โโโ lib.rs
|
|
33
|
+
โโโ wasm-module/ # WebAssembly module with HTTP and utility functions
|
|
34
|
+
โ โโโ Cargo.toml
|
|
35
|
+
โ โโโ pkg/ # Generated WASM package
|
|
36
|
+
โ โโโ src/
|
|
37
|
+
โ โโโ greetings/ # Greeting utilities
|
|
38
|
+
โ โโโ http/ # HTTP client functions
|
|
39
|
+
โ โโโ math/ # Math utilities
|
|
40
|
+
โ โโโ lib.rs
|
|
41
|
+
โโโ client.ts # Client-side entry point
|
|
42
|
+
โโโ server.ts # Server-side entry point
|
|
43
|
+
โโโ package.json # NPM package configuration
|
|
44
|
+
โโโ tsconfig.json # TypeScript configuration
|
|
34
45
|
```
|
|
35
46
|
|
|
36
|
-
##
|
|
47
|
+
## Usage
|
|
37
48
|
|
|
38
|
-
###
|
|
49
|
+
### Client-Side (Browser)
|
|
39
50
|
|
|
40
|
-
|
|
51
|
+
```typescript
|
|
52
|
+
import initWasm, {
|
|
53
|
+
fetch_wasm_json,
|
|
54
|
+
fetch_wasm_html,
|
|
55
|
+
greet,
|
|
56
|
+
add
|
|
57
|
+
} from 'rust-fetch/client';
|
|
41
58
|
|
|
42
|
-
|
|
43
|
-
|
|
59
|
+
// Initialize WASM module
|
|
60
|
+
await initWasm();
|
|
44
61
|
|
|
45
62
|
// Fetch JSON data
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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);
|
|
63
|
+
try {
|
|
64
|
+
const data = await fetch_wasm_json('https://api.example.com/data');
|
|
65
|
+
console.log('JSON data:', data);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error('Error fetching JSON:', error);
|
|
95
68
|
}
|
|
69
|
+
|
|
70
|
+
// Fetch HTML content
|
|
71
|
+
try {
|
|
72
|
+
const html = await fetch_wasm_html('https://example.com');
|
|
73
|
+
console.log('HTML content:', html);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error('Error fetching HTML:', error);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Use utility functions
|
|
79
|
+
const greeting = greet('World');
|
|
80
|
+
console.log(greeting); // "Hello, World!"
|
|
81
|
+
|
|
82
|
+
const sum = add(5, 3);
|
|
83
|
+
console.log(sum); // 8
|
|
96
84
|
```
|
|
97
85
|
|
|
98
|
-
###
|
|
86
|
+
### Server-Side (Node.js)
|
|
99
87
|
|
|
100
|
-
|
|
88
|
+
```typescript
|
|
89
|
+
import initWasm, {
|
|
90
|
+
fetch_wasm_json,
|
|
91
|
+
fetch_wasm_html,
|
|
92
|
+
greet,
|
|
93
|
+
add
|
|
94
|
+
} from 'rust-fetch/server';
|
|
101
95
|
|
|
102
|
-
|
|
103
|
-
import init, { WasmClient, fetch_json, fetch_text } from './rust_fetch.js';
|
|
96
|
+
// WASM is automatically initialized on import
|
|
104
97
|
|
|
105
|
-
|
|
98
|
+
// Fetch JSON data
|
|
99
|
+
try {
|
|
100
|
+
const data = await fetch_wasm_json('https://api.example.com/data');
|
|
101
|
+
console.log('JSON data:', data);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.error('Error fetching JSON:', error);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Use other functions similarly
|
|
107
|
+
```
|
|
106
108
|
|
|
107
|
-
|
|
108
|
-
const data = await fetch_json("https://api.example.com/data");
|
|
109
|
-
const html = await fetch_text("https://example.com");
|
|
109
|
+
## API Reference
|
|
110
110
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
111
|
+
### HTTP Functions
|
|
112
|
+
|
|
113
|
+
#### `fetch_wasm_json(url: string): Promise<any>`
|
|
114
|
+
Fetches JSON data from the specified URL and returns it as a JavaScript object.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
const data = await fetch_wasm_json('https://jsonplaceholder.typicode.com/todos/1');
|
|
115
118
|
```
|
|
116
119
|
|
|
117
|
-
|
|
120
|
+
#### `fetch_wasm_json_new(url: string): Promise<any>`
|
|
121
|
+
Alternative implementation that returns a Promise directly (useful for different async patterns).
|
|
118
122
|
|
|
119
|
-
|
|
123
|
+
```typescript
|
|
124
|
+
const promise = fetch_wasm_json_new('https://api.example.com/data');
|
|
125
|
+
const data = await promise;
|
|
126
|
+
```
|
|
120
127
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
- **`client`**: The main HTTP client with builder pattern
|
|
124
|
-
- **`http`**: High-level convenience functions for common use cases
|
|
128
|
+
#### `fetch_wasm_map(url: string): Promise<any>`
|
|
129
|
+
Fetches JSON data and returns it as a JavaScript object using `serde_wasm_bindgen`.
|
|
125
130
|
|
|
126
|
-
|
|
131
|
+
```typescript
|
|
132
|
+
const data = await fetch_wasm_map('https://api.example.com/data');
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### `fetch_wasm_html(url: string): Promise<string>`
|
|
136
|
+
Fetches HTML content from the specified URL and returns it as a string.
|
|
127
137
|
|
|
128
|
-
|
|
138
|
+
```typescript
|
|
139
|
+
const html = await fetch_wasm_html('https://example.com');
|
|
140
|
+
```
|
|
129
141
|
|
|
130
|
-
|
|
131
|
-
|
|
142
|
+
#### `fetch_wasm_api(): Promise<any>`
|
|
143
|
+
Fetches data from a predefined API endpoint (JSONPlaceholder todo item).
|
|
132
144
|
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
}
|
|
145
|
+
```typescript
|
|
146
|
+
const todo = await fetch_wasm_api();
|
|
143
147
|
```
|
|
144
148
|
|
|
145
|
-
###
|
|
149
|
+
### Utility Functions
|
|
150
|
+
|
|
151
|
+
#### `greet(name: string): string`
|
|
152
|
+
Returns a greeting message.
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
const message = greet('Alice'); // "Hello, Alice!"
|
|
156
|
+
```
|
|
146
157
|
|
|
147
|
-
|
|
158
|
+
#### `add(a: number, b: number): number`
|
|
159
|
+
Adds two numbers together.
|
|
148
160
|
|
|
149
|
-
```
|
|
150
|
-
|
|
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?;
|
|
161
|
+
```typescript
|
|
162
|
+
const result = add(10, 20); // 30
|
|
158
163
|
```
|
|
159
164
|
|
|
160
|
-
##
|
|
165
|
+
## Building from Source
|
|
161
166
|
|
|
162
|
-
|
|
167
|
+
### Prerequisites
|
|
163
168
|
|
|
169
|
+
- Rust (latest stable version)
|
|
170
|
+
- Node.js (v14 or higher)
|
|
171
|
+
- wasm-pack (`cargo install wasm-pack`)
|
|
172
|
+
|
|
173
|
+
### Build Steps
|
|
174
|
+
|
|
175
|
+
1. Clone the repository:
|
|
164
176
|
```bash
|
|
165
|
-
|
|
177
|
+
git clone https://github.com/yourusername/rust-fetch.git
|
|
178
|
+
cd rust-fetch
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
2. Install dependencies:
|
|
182
|
+
```bash
|
|
183
|
+
npm install
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
3. Build the WASM module:
|
|
187
|
+
```bash
|
|
188
|
+
npm run build:wasm
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
4. Build TypeScript files:
|
|
192
|
+
```bash
|
|
193
|
+
npm run build
|
|
166
194
|
```
|
|
167
195
|
|
|
168
|
-
|
|
196
|
+
## Development
|
|
197
|
+
|
|
198
|
+
### Building the WASM Module
|
|
169
199
|
|
|
170
200
|
```bash
|
|
171
|
-
wasm-
|
|
201
|
+
cd wasm-module
|
|
202
|
+
wasm-pack build --target web
|
|
172
203
|
```
|
|
173
204
|
|
|
174
|
-
|
|
205
|
+
This generates the `pkg` directory with JavaScript bindings and TypeScript definitions.
|
|
206
|
+
|
|
207
|
+
### Running Tests
|
|
208
|
+
|
|
209
|
+
For Rust tests:
|
|
210
|
+
```bash
|
|
211
|
+
cd wasm-module
|
|
212
|
+
cargo test
|
|
213
|
+
|
|
214
|
+
cd ../rust-fetch-package
|
|
215
|
+
cargo test
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Performance Considerations
|
|
219
|
+
|
|
220
|
+
- The WASM module adds a small overhead for initialization (~100-200ms on first load)
|
|
221
|
+
- Once loaded, HTTP operations benefit from Rust's efficient memory management
|
|
222
|
+
- Best suited for applications that make multiple HTTP requests or need predictable performance
|
|
223
|
+
|
|
224
|
+
## Browser Support
|
|
225
|
+
|
|
226
|
+
- Chrome 57+
|
|
227
|
+
- Firefox 52+
|
|
228
|
+
- Safari 11+
|
|
229
|
+
- Edge 16+
|
|
230
|
+
- Node.js 14+
|
|
231
|
+
|
|
232
|
+
## Contributing
|
|
233
|
+
|
|
234
|
+
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
|
235
|
+
|
|
236
|
+
1. Fork the repository
|
|
237
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
238
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
239
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
240
|
+
5. Open a Pull Request
|
|
241
|
+
|
|
242
|
+
## License
|
|
243
|
+
|
|
244
|
+
This project is licensed under the ISC License - see the LICENSE file for details.
|
|
245
|
+
|
|
246
|
+
## Troubleshooting
|
|
247
|
+
|
|
248
|
+
### WASM Module Not Loading
|
|
249
|
+
|
|
250
|
+
If you encounter issues with the WASM module not loading:
|
|
251
|
+
|
|
252
|
+
1. Ensure your server supports the `application/wasm` MIME type
|
|
253
|
+
2. Check that the WASM file is being served correctly
|
|
254
|
+
3. Verify you're awaiting the `initWasm()` call before using any functions
|
|
255
|
+
|
|
256
|
+
### CORS Issues
|
|
257
|
+
|
|
258
|
+
When fetching from external APIs, ensure:
|
|
259
|
+
|
|
260
|
+
1. The target server allows CORS requests
|
|
261
|
+
2. You're using HTTPS URLs in production
|
|
262
|
+
3. Consider using a proxy server for APIs that don't support CORS
|
|
175
263
|
|
|
176
|
-
|
|
264
|
+
### Node.js Path Issues
|
|
177
265
|
|
|
178
|
-
|
|
266
|
+
If you encounter path resolution issues in Node.js:
|
|
179
267
|
|
|
180
|
-
|
|
268
|
+
1. Ensure the package is installed correctly
|
|
269
|
+
2. Check that the WASM file exists in `node_modules/rust-fetch/wasm-module/pkg/`
|
|
270
|
+
3. Try reinstalling the package
|
|
181
271
|
|
|
182
|
-
|
|
183
|
-
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
272
|
+
## Acknowledgments
|
|
184
273
|
|
|
185
|
-
|
|
274
|
+
- Built with [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen)
|
|
275
|
+
- Uses [reqwest](https://github.com/seanmonstar/reqwest) for HTTP operations
|
|
276
|
+
- Inspired by the Rust and WebAssembly community
|
|
186
277
|
|
|
187
|
-
##
|
|
278
|
+
## Roadmap
|
|
188
279
|
|
|
189
|
-
|
|
190
|
-
- [
|
|
191
|
-
- [
|
|
192
|
-
- [
|
|
280
|
+
- [ ] Add support for POST, PUT, DELETE methods
|
|
281
|
+
- [ ] Implement request headers customization
|
|
282
|
+
- [ ] Add streaming support for large responses
|
|
283
|
+
- [ ] Implement request cancellation
|
|
284
|
+
- [ ] Add comprehensive test suite
|
|
285
|
+
- [ ] Performance benchmarks against native fetch
|
|
286
|
+
- [ ] Support for form data and file uploads
|
|
287
|
+
- [ ] WebSocket support
|
|
288
|
+
- [ ] Request/response interceptors
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"Asem Qaffaf <asem@qaffaf.com>"
|
|
6
6
|
],
|
|
7
7
|
"description": "A world-class WebAssembly HTTP client library with excellent error handling and clean API",
|
|
8
|
-
"version": "1.0.
|
|
8
|
+
"version": "1.0.1",
|
|
9
9
|
"license": "MIT OR Apache-2.0",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
@@ -28,4 +28,4 @@
|
|
|
28
28
|
"fetch",
|
|
29
29
|
"webassembly"
|
|
30
30
|
]
|
|
31
|
-
}
|
|
31
|
+
}
|