nexus-http v2.4.1
Description 📄
nexus-http
is a simple http client that allows you to make requests easily with fetch API.
Install 📦️
npm i --save nexus-http
How to use ✏️
In a js
file remove the extra typing from the nexusHttp.get
part and .then
.
import { nexusHttp, HttpError, HttpResponse } from 'nexus-http';
nexusHttp.get<User[]>('http://localhost:3000/api/users', {
query: {
page: 1,
limit: 12,
deleted: false
}
})
.then((response: HttpResponse<User[]>) => {
// Add some logic when status is >= 200 and <= 299
})
.catch((response: HttpResponse<HttpError>) => {
// Add some logic on error
})
.finally(() => {
// Add some logic after success or error
});
With baseUrl:
import { nexusHttp, HttpError, HttpResponse } from 'nexus-http';
nexusHttp.setBaseUrl('http://localhost:3000/api/');
nexusHttp.get<User[]>('/users') // url before adding query params: http://localhost:3000/api/users
.then((response: HttpResponse<User[]>) => {
// Add some logic when status is >= 200 and <= 299
})
.catch((response: HttpResponse<HttpError>) => {
// Add some logic on error
})
.finally(() => {
// Add some logic after success or error
});
Create your own instance:
import { nexusHttp, HttpError, HttpResponse } from 'nexus-http';
const nexusHttp = new NexusHttp();
nexusHttp.get<User[]>('http://localhost:3000/api/users')
.then((response: HttpResponse<User[]>) => {
// Add some logic when status is >= 200 and <= 299
})
.catch((response: HttpResponse<HttpError>) => {
// Add some logic on error
})
.finally(() => {
// Add some logic after success or error
});
Client
if fetch is not supported, the client use XmlClient by default.
import { nexusHttp, HttpError, HttpResponse, XmlClient } from 'nexus-http';
const nexusHttp = new NexusHttp(); nexusHttp.useClient(XmlClient); // Now this instance of nexusHttp use the XmlClient (XMLHttpRequest)
nexusHttp.get<User[]>('http://localhost:3000/api/users') .then((response: HttpResponse<User[]>) => { // Add some logic when status is >= 200 and <= 299 }) .catch((response: HttpResponse) => { // Add some logic on error }) .finally(() => { // Add some logic after success or error });
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago