Skip to content

Commit fbb030e

Browse files
committed
Add comprehensive README.md with documentation and examples
- Complete API reference for all HTTP Fetch modules - Quick start guide for immediate usage - Code examples for common patterns - Installation and configuration instructions - Error handling documentation
1 parent 99395f9 commit fbb030e

File tree

1 file changed

+101
-2
lines changed

1 file changed

+101
-2
lines changed

README.md

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,105 @@
1-
# HTTP.fetch
1+
# HTTP Fetch
22

3-
Use `:httpc` to implement a `http.fetch` in `Elixir`
3+
A modern HTTP client library for Elixir that provides a fetch API similar to web browsers, built on Erlang's built-in `:httpc` module.
44

5+
## Features
56

7+
- **Browser-like API**: Familiar fetch interface with promises and async/await patterns
8+
- **Full HTTP support**: GET, POST, PUT, DELETE, PATCH, HEAD methods
9+
- **Promise-based**: Async operations with chaining support
10+
- **Request cancellation**: AbortController support for cancelling requests
11+
- **Automatic JSON parsing**: Built-in JSON response handling
12+
- **Zero dependencies**: Uses only Erlang/OTP built-in modules
613

14+
## Quick Start
15+
16+
```elixir
17+
# Simple GET request
18+
{:ok, response} =
19+
HTTP.fetch("https://jsonplaceholder.typicode.com/posts/1")
20+
|> HTTP.Promise.await()
21+
22+
# Get response data
23+
IO.puts("Status: #{response.status}")
24+
text = HTTP.Response.text(response)
25+
{:ok, json} = HTTP.Response.json(response)
26+
27+
# POST request with JSON
28+
{:ok, response} =
29+
HTTP.fetch("https://jsonplaceholder.typicode.com/posts", [
30+
method: "POST",
31+
headers: %{"Content-Type" => "application/json"},
32+
body: JSON.encode\!(%{title: "Hello", body: "World"})
33+
])
34+
|> HTTP.Promise.await()
35+
```
36+
37+
## API Reference
38+
39+
### HTTP.fetch/2
40+
Performs an HTTP request and returns a Promise.
41+
42+
```elixir
43+
promise = HTTP.fetch(url, [
44+
method: "GET",
45+
headers: %{"Accept" => "application/json"},
46+
body: "request body",
47+
content_type: "application/json",
48+
options: [timeout: 10_000],
49+
signal: abort_controller
50+
])
51+
```
52+
53+
### HTTP.Promise
54+
Asynchronous promise wrapper for HTTP requests.
55+
56+
```elixir
57+
{:ok, response} = HTTP.Promise.await(promise)
58+
59+
# Promise chaining
60+
HTTP.fetch("https://api.example.com/data")
61+
|> HTTP.Promise.then(fn response -> HTTP.Response.json(response) end)
62+
|> HTTP.Promise.await()
63+
```
64+
65+
### HTTP.Response
66+
Represents an HTTP response.
67+
68+
```elixir
69+
text = HTTP.Response.text(response)
70+
{:ok, json} = HTTP.Response.json(response)
71+
```
72+
73+
### HTTP.Request
74+
Request configuration struct.
75+
76+
```elixir
77+
request = %HTTP.Request{
78+
method: :post,
79+
url: "https://api.example.com/data",
80+
headers: [{"Authorization", "Bearer token"}],
81+
body: "data"
82+
}
83+
```
84+
85+
### HTTP.AbortController
86+
Request cancellation.
87+
88+
```elixir
89+
controller = HTTP.AbortController.new()
90+
HTTP.AbortController.abort(controller)
91+
```
92+
93+
## Error Handling
94+
95+
The library handles:
96+
- Network errors and timeouts
97+
- HTTP error status codes
98+
- JSON parsing errors
99+
- Invalid URLs
100+
- Cancelled requests
101+
102+
## License
103+
104+
MIT License
105+
EOF < /dev/null

0 commit comments

Comments
 (0)