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

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

Awesome Lists containing this project

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 install

pnpm build

```

## unit test

```
pnpm vitest run

pnpm coerage
```