@amoutonbrady/tiny-http
v0.7.1
Published
A tiny client side HTTP client, light and extensible
Downloads
54
Maintainers
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
type Pipe = (options: Options) => Options;
type ResponsePiper<T1 = any, T2 = any> = (res: T1) => T2;
type ErrorPiper<T1 = Error, T1 = Error> = (err: T1) => T2;
interface Options {
url: string;
middlewares: Pipe[];
responseType: 'json' | 'blob' | 'text' | 'arrayBuffer' | 'formData' | 'clone';
headers: Record<string, string>;
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:
const client = http({
url: "http://localhost",
headers: {
"Content-Type": "application/json",
},
...
})
const [error, response] = await client.get<string>("/test");
or/and pipe the client to modify them as you please
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<string>('/test');