https://github.com/wflixu/obfetch
a httpClient base on rxjs and fetch api inspired by angular httpClient
https://github.com/wflixu/obfetch
axios fetch fetch-api http http-client rxjs
Last synced: about 2 months ago
JSON representation
a httpClient base on rxjs and fetch api inspired by angular httpClient
- Host: GitHub
- URL: https://github.com/wflixu/obfetch
- Owner: wflixu
- License: mit
- Created: 2023-02-20T09:13:16.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-06T05:08:34.000Z (about 1 year ago)
- Last Synced: 2025-03-06T12:05:14.426Z (about 2 months ago)
- Topics: axios, fetch, fetch-api, http, http-client, rxjs
- Language: TypeScript
- Homepage:
- Size: 332 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- fucking-awesome-angular - obfetch - A HttpClient base on RxJS and fetch API inspired by Angular `httpClient`. (Table of contents / Third Party Components)
- awesome-angular - obfetch - A HttpClient base on RxJS and fetch API inspired by Angular `httpClient`. (Table of contents / Third Party Components)
README
# obfetch
An http client based on rxjs and fetch API inspired by Angular HttpClient.# use
## install
```shell
#npm
npm install obfetch#pnpm
npm add obfetch```
## example### basic
```
// http.ts/js
import { HttpClient } from 'obfetch';
epxort const http = new HttpClient({
baseURL:'http://somehost:8888/some/path'
});// business.ts
import {http} from 'http'http.get('http://127.0.0.1:8443/ping').subscribe(data => {
console.log(data);
})
http.post('http://127.0.0.1:8443/ping', {username:'youname'}).subscribe(data => {
console.log(data);
})```
### Interceptors
obfetch supports a form of middleware known as interceptors, you can use interceptor config request or format response data。
```
// Defining an interceptor modifying request
export function tokenInterceptor(req: HttpRequest, next: HttpHandlerFn): Observable> {
let token = getTokenSomeWhere();
if(token) {
const reqWithHeader = req.clone({
headers: req.headers.set('Authorizen', token),
});
return next(reqWithHeader);
}
return next(req);
}// Defining an interceptor formatting response data
export function responseDataFormatInterceptor(req: HttpRequest, next: HttpHandlerFn): Observable> {
return next(req).pipe(
map((event: HttpEvent) => {
if (event instanceof HttpResponse) {
return event.clone({ body: event.body?.data });
}
return event;
}),
);
}// Use interceptor
http.use([tokenInterceptor,responseDataFormatInterceptor])//
```
# Developing
## local build
```
pnpm installpnpm build
```
## unit test
```
pnpm vitest runpnpm coerage
```