0.0.20 • Published 1 year ago

@seriouslag/httpclient v0.0.20

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

HttpClient helps standardarize making HTTP calls and handling when errors are thrown. HttpClient works both in the browser and node environments. Exposes an interface to abort HTTP calls using AbortController. See below about using AbortController in older environments. Exposes an interface to control how requests and responses are handled. See below about using HttpClient's Request Strategies. Some strategies are provided in this package, but you can also implement your own strategies. List of strategies are provided below.

npm install @seriouslag/httpclient

Basic example:

import { HttpClient } from '@seriouslag/httpclient';

interface NamedLink {
  name: string;
  url: string;
}

interface PokemonPage {
  count: number;
  next: string|null;
  previous: string|null;
  results: NamedLink[];
}

const httpClient = new HttpClient();

async function fetchPokemonPage (offset: number = 0, pageSize: number = 20) {
  const pokemonApiUrl = 'https://pokeapi.co/api/v2';
  return await this.httpClient.get<PokemonPage>(`${pokemonApiUrl}/pokemon`, {
      params: {
        offset: offset,
        limit: pageSize,
      },
  });
}

// IIFE
(async () => {
  const results = await fetchPokemonPage(0, 100);
  console.log(results);
})();
import { HttpClient } from '@seriouslag/httpclient';
import { Agent } from 'https';

const httpsAgent = new Agent({
  rejectUnauthorized: false,
});

const httpClient = new HttpClient({
  axiosOptions: {
    httpsAgent,
  },
});
import { HttpClient } from '@seriouslag/httpclient';

interface PokemonPage {
  count: number;
  next: string|null;
  previous: string|null;
  results: NamedLink[];
}

const pokemonApiUrl = 'https://pokeapi.co/api/v2';
const httpClient = new HttpClient();
const cancelToken = new AbortController();

const request = httpClient.get<PokemonPage>(`${pokemonApiUrl}/pokemon`, cancelToken);

cancelToken.abort();

try {
  const result = await request;
  console.log('Expect to not get here because request was aborted.', result)
} catch (e) {
  console.log('Expect to reach here because request was aborted.')
}
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only';
import { HttpClient } from '@seriouslag/httpclient';

const httpClient = new HttpClient();

Provided strategies:

class CreatedHttpRequestStrategy implements HttpRequestStrategy {

/* Passthrough request to axios and check response is created status / public async request<T = unknown> (client: AxiosInstance, axiosConfig: AxiosRequestConfig) { const response = await client.request(axiosConfig); this.checkResponseStatus(response); return response; }

/* Validates the HTTP response is successful created status or throws an error / private checkResponseStatus<T = unknown> (response: HttpResponse): HttpResponse { const isCreatedResponse = response.status === 201; if (isCreatedResponse) { return response; } throw response; } }

const httpRequestStrategy = new CreatedHttpRequestStrategy();

// all requests will now throw unless they return an HTTP response with a status of 201 const httpClient = new HttpClient({ httpRequestStrategy, });

<h3>Using Request Strategy in a request</h3>

<p>The following code creates an instance of the HttpClient with a provided HttpRequestStrategy (MaxRetryHttpRequestStrategy), then starts a request and passes a different strategy (DefaultHttpRequestStrategy) to the request. The request will now used the strategy provided instead of the HttpClients strategy.</p>

 ```typescript
import { HttpClient, DefaultHttpRequestStrategy, MaxRetryHttpRequestStrategy } from '@seriouslag/httpclient';

const httpClient = new HttpClient({
  httpRequestStrategy: new MaxRetryHttpRequestStrategy(10),
});

// IIFE
(async () => {
  const response = await httpClient.get('/endpoint', {
    httpRequestStrategy: new DefaultHttpRequestStrategy(),
  });
})();

Contributing

0.0.20

1 year ago

0.0.18

1 year ago

0.0.19

1 year ago

0.0.17

3 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.9

4 years ago

0.0.16

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago