Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

obfetch

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

obfetch

A http client based on rxjs and fetch API inspired by Angular HttpClient.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

obfetch

An http client based on rxjs and fetch API inspired by Angular HttpClient.

use

install

#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<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> {
  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<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> {
  return next(req).pipe(
      map((event: HttpEvent<any>) => {
        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

Keywords

FAQs

Package last updated on 04 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc