0.6.1 • Published 2 years ago
haxan v0.6.1
Haxan
Intuitive HTTP client for browsers and Node.js servers.
Installation
npm i haxan
Or use the .min.js bundle.
Examples
Using GET
to fetch a user from an API
import Haxan from "haxan";
interface User {
id: number;
name: string;
}
Haxan<User>("http://localhost:3000/api/user/1234")
.request()
.then((response) => {
if (response.ok) {
// Success!
console.log(response.data); // Response data
} else {
// Some error, but at least we got a response
}
})
.catch((error) => {
// Connection refused, no response
});
Setting query parameters
Haxan<string>("http://google.com/search")
.param("q", "Elephants") // -> http://google.com/search?q=Elephants
.request()
.then((response) => {
// Handle response
})
.catch((error) => {
// Handle error
});
Sending a JSON payload with POST
const payload = {
id: 4,
user_name: "@testname",
};
Haxan("http://localhost:3000/api/user")
.post(payload)
.request()
.then((response) => {
// Handle response
})
.catch((error) => {
// Handle error
});
Download a file in Node.js
Haxan<fs.ReadStream>("https://bit.ly/3k19d8D")
.type(Haxan.ResponseType.Stream)
.send()
.then((response) => {
response.data.pipe(fs.createWriteStream("punisher.jpeg"));
})
.catch((error) => {
// Handle error
});
Sending other kinds of content
// Use a different Content-Type instead
Haxan("http://localhost:3000/api/note")
.header("Content-Type", "text/yaml")
.post(
`
---
message: I hope this is valid YAML
`,
)
.send()
.then((response) => {
// Handle response
})
.catch((error) => {
// Handle error
});
0.7.0-next.1
2 years ago
0.7.0-next.2
2 years ago
0.6.1
2 years ago
0.6.0
2 years ago
0.5.0
3 years ago
0.4.1
3 years ago
0.4.0
3 years ago
0.3.0
3 years ago
0.2.1
3 years ago
0.2.0
3 years ago
0.1.1
3 years ago
0.1.0
3 years ago
0.0.1
4 years ago
0.0.1-nightly.4
4 years ago
0.0.1-nightly.3
4 years ago
0.0.1-nightly.2
4 years ago
0.0.1-nightly.1
4 years ago
0.0.1-nightly.0
4 years ago