2.4.1 • Published 2 years ago

nexus-http v2.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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.4.1

2 years ago

2.4.0

2 years ago

2.3.0

2 years ago

2.2.1

2 years ago

2.1.2

2 years ago

2.0.3

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.0.2

2 years ago

2.2.2

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.0-rc2

3 years ago

1.1.0-pre1

3 years ago

1.1.0-pre3

3 years ago

1.0.0

3 years ago

1.1.0-pre2

3 years ago

1.1.0-rc1

3 years ago

1.1.0-pre4

3 years ago

1.0.0-pre3

3 years ago

1.0.0-pre2

3 years ago

1.0.0-pre1

3 years ago