Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/amoutonbrady/tiny-http


https://github.com/amoutonbrady/tiny-http

Last synced: about 21 hours ago
JSON representation

Awesome Lists containing this project

README

        

# tiny-http








A tiny client side HTTP client, light and extensible

- ✔ Extensible, everything is pretty much a middleware
- ✔ Light, no dependencies
- ✔ Tailored, tree shakeable, use only what you need
- ✔ TS ready, written 100% in Typescript

## Installation

`npm install @amoutonbrady/tiny-http`

### Options

```ts
type Pipe = (options: Options) => Options;
type ResponsePiper = (res: T1) => T2;
type ErrorPiper = (err: T1) => T2;

interface Options {
url: string;
middlewares: Pipe[];
responseType: 'json' | 'blob' | 'text' | 'arrayBuffer' | 'formData' | 'clone';
headers: Record;
params: URLSearchParams;
preResolvers: ((res: Response, value: T) => void)[];
resolvers: ResponsePiper[];
catchers: ErrorPiper[];
fetchOptions: RequestInit;
}
```

You can feed these options to a brand new `http()` call:

```ts
const client = http({
url: "http://localhost",
headers: {
"Content-Type": "application/json",
},
...
})

const [error, response] = await client.get("/test");
```

or/and pipe the client to modify them as you please

```ts
import {
http,
url,
headers,
params,
middleware,
resolve,
json,
error,
} from '@amoutonbrady/tiny-http';

const client = http().pipe(
url('http://localhost', true), // Replace the URL, the second parameter is to replace or append
headers({ 'Content-Type': 'application/json' }),
params({ test: 'trololol' }),
middleware((opts) => opts), // Pretty much useless as you can just do (opts) => opts
json(), // sets responseType to `json`
resolve((res) => res), // Ran after the responseType is resolved fetch(url).then(r => r.json()).r(myResolver)
error((err) => err),
);

const [error, response] = await client.get('/test');
```