npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@monabbous/ng-api-wrapper

v0.2.5

Published

This library is used to simplify the connections to your backend RESTful api, and supports multiple endpoint/servers. Also provides a Base Resource service which can be used to make the crud operations consistent and simple.

Downloads

1

Readme

Angular API Wrapper

This library is used to simplify the connections to your backend RESTful api, and supports multiple endpoint/servers. Also provides a Base Resource service which can be used to make the crud operations consistent and simple.

Basically instead of writing this piece of code repeatedly and change only the last bit

    http.get('https://app-backend.com/api/v2/items');
    http.get('https://app-backend.com/api/v2/stores');

now you just write this and change the path and optionally the server and api version that you pre-configure

    api.get({path: 'items'});
    api.get({path: 'stores'});

Features

This wrapper has few key features under the hood including:

  • Multiple Endpoints where you can set multiple baseUrls with it's versions so you can switch using them.
  • @monabbous/unflatter where it unflattens the body and params of the http requests
  • HTTP Method Overriding where in the config you can set the methodOverride to true and it will send all the requests as POST request and injects the _method attribute with on of the corresponding values (GET, PUT, PATCH, DELETE)
  • Automatic Form Data Body conversion where it detects if there is a Blob value in the body and then automatically converts the json to formData.
  • JWT Bearer Token where you can set the value of the static variable NgApiWrapperService.token and will send the request with the Authorization header with the value of 'Bearer ' + NgApiWrapperService.token, or you can set the token in localStorage.setItem('token', token), also you can give the token per request using the token parameter of the request.

Installation

Install with npm

npm i @monabbous/ng-api-wrapper

Usage

first import the module and write your configurations in your AppModule

imports: [
    .
    .
    NgApiWrapperModule.forRoot({
          servers: {
            primary: {
              baseUrl: 'https://app-backend.com/',
              versions: {
                1: 'api/v1/',
                2: 'api/v2/',
              },
              defaultVersion: 2,
            }
          },
          defaultServer: 'primary',
          onError: (response, parameters) => {
            console.log(response, parameters);
            return of({response});
          },
          onSuccess: (response, parameters) => {
            console.log(response, parameters);
            return of({response});
          }
    }),
    .
    .
],

Now you can use the service in your app as so

  constructor(
    public api: NgApiWrapperService,
  ) {
    api.get({path: 'items'})
      .subscribe(response => {
        console.log(response);
      });
  }

Api Resource

To use it, first create a new Service and an interface that extends the ResourceModel (which has id property) and use as so:

export interface Item extends ResourceModel {
  name: string;
  category: {
    id: number;
    name: string;
  };
  base_price: number;
  discount: number;
}

@Injectable({
  providedIn: 'any'
})
export class ItemService extends NgApiResourceService<Item> {
  // this is the path of the resource that will be used to be called from the backend
  resourceName = 'items';
}

and now you can use this service as following

  constructor(
    public items: ItemService,
  ) {
    items.get()
        .subscribe(response => {
        console.log(response);
        });
      
    // these filters is sent to the backend as queryParams
    const filters = {name: 'test' };
    items.get(filters)
        .subscribe(response => {
          console.log(response);
        });
        
    // the where function adds a global filter to the service
    items.where('page', 1).where('category_id', 1).get(filters)
        .subscribe(response => {
          console.log(response);
        });
        
    // this basically request the path _reourceName/:id_ from the backend
    items.find(1)
        .subscribe(response => {
          console.log(response);
        });
        
  }
  

you can simplify your usage of this service like so:

constructor(
    public items: ItemService,
    public activatedRoute: ActivatedRoute,
) {
    items.init({
        route: activatedRoute,
        idParameter: 'id'
    });
    
    // now you have the page$ variable from get() with the browsers queryParams from the ActivatedRoute and used as filters
    items.page$
        .subscribe(response => {
          console.log(response);
        });

    // now you have the model$ variable from find() and the id is from the Params of the ActivatedRoute rerieved from the `idParameter` in the init()
    items.model$
        .subscribe(response => {
          console.log(response);
        });
}

that's it just install with npm i @monabbous/ng-api-wrapper and start APIing :D