From 946451eb510f1fcd9fec7bb9da05b6e45d3a5dbb Mon Sep 17 00:00:00 2001 From: Landon Gavin Date: Thu, 11 Apr 2024 21:10:36 -0400 Subject: [PATCH] fix: splitout example --- README.md | 103 ++++++++++-------- package-lock.json | 28 +++-- packages/examples/LICENSE | 7 ++ packages/examples/package.json | 37 +++++++ packages/examples/src/PokemonApi.ts | 32 ++++++ packages/examples/src/index.ts | 2 + .../src/examples => examples/src}/types.ts | 0 packages/examples/tsconfig.json | 22 ++++ packages/httpclient-axios/package.json | 5 +- .../src/examples/example-axios-options.ts | 4 +- packages/httpclient-axios/tsconfig.json | 3 + packages/httpclient/package.json | 6 +- packages/httpclient/src/HttpClient.test.ts | 1 - .../HttpRequestStrategy.ts | 2 +- .../TimeoutHttpRequestStrategy.test.ts | 2 +- .../TimeoutHttpRequestStrategy.ts | 5 +- .../src/HttpRequestStrategies/index.ts | 1 + .../httpclient/src/errors/TimeoutError.ts | 7 ++ .../httpclient/src/examples/PokemonApi.ts | 20 ---- .../httpclient/src/examples/example-basic.ts | 2 +- .../src/examples/example-cancelToken.ts | 2 +- packages/httpclient/tsconfig.json | 8 +- 22 files changed, 212 insertions(+), 87 deletions(-) create mode 100644 packages/examples/LICENSE create mode 100644 packages/examples/package.json create mode 100644 packages/examples/src/PokemonApi.ts create mode 100644 packages/examples/src/index.ts rename packages/{httpclient/src/examples => examples/src}/types.ts (100%) create mode 100644 packages/examples/tsconfig.json create mode 100644 packages/httpclient/src/errors/TimeoutError.ts delete mode 100644 packages/httpclient/src/examples/PokemonApi.ts diff --git a/README.md b/README.md index 0e6ccdd..e3ae439 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

- Typed wrapper around axios. + Typed wrapper around fetch or axios.

@@ -24,7 +24,8 @@

This package's API is still developing and will not follow SEMVER until release 1.0.0. - 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](#using-abortcontroller) in older environments. Exposes an interface to control how requests and responses are handled. See below about using [HttpClient's Request Strategies](#using-request-strategies). Some strategies are provided in this package, but you can also implement your own strategies. List of strategies are provided below. +HttpClient helps standardizes making HTTP calls regardless of the underlying client used, (fetch is used by default but other clients are available) 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](#using-abortcontroller) in older environments. Exposes an interface to control how requests and responses are handled. See below about using [HttpClient's Request Strategies](#using-request-strategies). Some strategies are provided in this package, but you can also implement your own strategies. List of strategies are provided below. +

Installation

@@ -38,6 +39,7 @@ npm install @seriouslag/httpclient

To see additional examples look in the `src/examples/` directory.

Basic example: + ```typescript import { HttpClient } from '@seriouslag/httpclient'; @@ -48,20 +50,20 @@ interface NamedLink { interface PokemonPage { count: number; - next: string|null; - previous: string|null; + next: string | null; + previous: string | null; results: NamedLink[]; } const httpClient = new HttpClient(); -async function fetchPokemonPage (offset: number = 0, pageSize: number = 20) { +async function fetchPokemonPage(offset: number = 0, pageSize: number = 20) { const pokemonApiUrl = 'https://pokeapi.co/api/v2'; return await this.httpClient.get(`${pokemonApiUrl}/pokemon`, { - params: { - offset: offset, - limit: pageSize, - }, + params: { + offset: offset, + limit: pageSize, + }, }); } @@ -72,37 +74,45 @@ async function fetchPokemonPage (offset: number = 0, pageSize: number = 20) { })(); ``` -

Configuring axios

+

Using axios

+ +We can use axios as the underlying client by installing the `@seriouslag/httpclient-axios` package. +A custom client adaptor can be provided to the HttpClient constructor, an interface is exposed to allow for custom client adaptors to be created. + +```bash +npm install @seriouslag/httpclient @seriouslag/httpclient-axios +``` +

Axios can be configured, axios options can be passed into the constructor of HttpClient.

```typescript import { HttpClient } from '@seriouslag/httpclient'; +import { AxiosClientAdaptor } from '@seriouslag/httpclient-axios'; import { Agent } from 'https'; const httpsAgent = new Agent({ rejectUnauthorized: false, }); -const httpClient = new HttpClient({ - axiosOptions: { - httpsAgent, - }, +const axiosClientAdaptor = new AxiosClientAdaptor({ + httpsAgent, }); + +const httpClient = new HttpClient(axiosClientAdaptor); ```

Using AbortController

Each of the HTTP methods of the HttpClient accept an instance of a AbortController. This allows HTTP requests to be cancelled if not already resolved. - ```typescript import { HttpClient } from '@seriouslag/httpclient'; interface PokemonPage { count: number; - next: string|null; - previous: string|null; + next: string | null; + previous: string | null; results: NamedLink[]; } @@ -110,29 +120,34 @@ const pokemonApiUrl = 'https://pokeapi.co/api/v2'; const httpClient = new HttpClient(); const cancelToken = new AbortController(); -const request = httpClient.get(`${pokemonApiUrl}/pokemon`, cancelToken); +const request = httpClient.get( + `${pokemonApiUrl}/pokemon`, + cancelToken, +); cancelToken.abort(); try { const result = await request; - console.log('Expect to not get here because request was aborted.', result) + console.log('Expect to not get here because request was aborted.', result); } catch (e) { - console.log('Expect to reach here because request was aborted.') + console.log('Expect to reach here because request was aborted.'); } ``` +

AbortController in older environments

Abort controller is native to node 15+ and modern browsers. If support is needed for older browsers/node versions then polyfills can be found. This polyfill is used in the Jest test environment for this repository: abortcontroller-polyfill - ```typescript - import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'; - import { HttpClient } from '@seriouslag/httpclient'; +```typescript +import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'; +import { HttpClient } from '@seriouslag/httpclient'; + +const httpClient = new HttpClient(); +``` - const httpClient = new HttpClient(); - ```

Using Request Strategies

@@ -140,6 +155,7 @@ try { A request strategy is middleware to handle how requests are made and how responses are handled. This is exposed to the consumer using the `HttpRequestStrategy` interface. A request strategy can be passed into the HttpClient (it will be defaulted if not) or it can be passed into each request (if not provided then the strategy provided by the HttpClient will be used). A custom strategy can be provided to the HttpClient's constructor. Provided strategies: +