Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amoutonbrady/tiny-http
https://github.com/amoutonbrady/tiny-http
Last synced: about 21 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/amoutonbrady/tiny-http
- Owner: amoutonbrady
- Created: 2020-07-23T21:47:30.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-08-03T06:05:31.000Z (over 1 year ago)
- Last Synced: 2024-09-26T13:18:15.065Z (about 1 month ago)
- Language: TypeScript
- Size: 80.2 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
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');
```