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

@maspav/angular-http-handler

v0.0.6

Published

An Angular service designed to simplify the process of handling HTTP requests and responses in Angular applications

Downloads

328

Readme

@maspav/angular-http-handler

Version

Table of Contents

Introduction

@maspav/angular-http-handler is an Angular service designed to simplify the process of handling HTTP requests and responses in Angular applications. This service includes support for error handling, response transformations, pagination management, and more.

Features

  • Centralized HTTP request handling.
  • Easy error management.
  • Simplified response transformation.
  • Pagination management.
  • Configurable base URL for API requests.

Installation

To install the library, use npm:

npm i @maspav/angular-http-handler

Usage

Importing the Module First, import the module in your AppModule:

import { AngularHttpHandlerModule } from '@maspav/angular-http-handler';

@NgModule({
  imports: [
    AngularHttpHandlerModule;
  ],
})
export class AppModule { }

Using the Service Inject the HttpHandlerService into your component or service:

import { Component } from '@angular/core';
import { AngularHttpHandlerService } from '@maspav/angular-http-handler';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html'
})
export class ExampleComponent {

  constructor(private httpHandler: AngularHttpHandlerService) { }

}

Configuration

The AngularHttpHandlerService can be customized by passing an optional configuration object when setting up the module, that is, the base url.

import { AngularHttpHandlerModule } from '@maspav/angular-http-handler';

@NgModule({
  imports: [
    AngularHttpHandlerModule.forRoot({ baseUrl: 'https://api.yourdomain.com/' });
  ],
})
export class AppModule { }

Examples (With Configuration)

Making a POST Request

 postRequestFunction(payload: object) {
  return this.httpHandler.post('endpoint', payload).subscribe(
    response => console.log('Item created:', response),
    error => console.error('Error:', error)
  );
}

Making a GET Request

getRequestFunction(params: any) {
  return this.httpHandler.get('endpoint', params).subscribe(
    response => console.log('Data received:', response),
    error => console.error('Error:', error)
  );
}

Making a DELETE Request

deleteRequestFunction(params: any) {
  return this.httpHandler.delete('endpoint', params).subscribe(
    response => console.log('Item deleted:', response),
    error => console.error('Error:', error)
  );
}

Making a PUT Request

putRequestFunction(payload: object) {
  return this.httpHandler.put('endpoint', payload).subscribe(
    response => console.log('Item updated:', response),
    error => console.error('Error:', error)
  );
}

Examples (Without Configuration)

Making a POST Request

 postRequestFunction(payload: object) {
  return this.httpHandler.post('https://api.yourdomain.com/endpoint', payload).subscribe(
    response => console.log('Item created:', response),
    error => console.error('Error:', error)
  );
}

Making a GET Request

getRequestFunction(params: any) {
  return this.httpHandler.get('https://api.yourdomain.com/endpoint', params).subscribe(
    response => console.log('Data received:', response),
    error => console.error('Error:', error)
  );
}

Making a DELETE Request

deleteRequestFunction(params: any) {
  return this.httpHandler.delete('https://api.yourdomain.com/endpoint', params).subscribe(
    response => console.log('Item deleted:', response),
    error => console.error('Error:', error)
  );
}

Making a PUT Request

putRequestFunction(payload: object) {
  return this.httpHandler.put('https://api.yourdomain.com/endpoint', payload).subscribe(
    response => console.log('Item updated:', response),
    error => console.error('Error:', error)
  );
}

Interfaces Paginator Manages pagination details for API responses:

 export interface Paginator {
  collectionSize?: number | any;
  page?: number | any;
  total?: number;
  from?: number;
  to?: number;
  perPage?: number;
  disabled?: boolean;
  pages?: number;
}

Loader Handles loading state and error messaging:

 export interface Loader {
  loading: boolean;
  error?: string;
}

Metadata Contains pagination and link information for API responses:

export interface Metadata {
  pageLinks: {
    previousPage?: any;
    firstPage: string;
    currentPage: string;
    nextPage?: any;
    lastPage: string;
  };
  pageInfo: {
    pages: number;
    total: number;
    currentPage: number;
    pageSize: number;
    perPage: number;
    from: number;
    to: number;
  };
}

ErrorConfig Configures error handling behavior:

export interface ErrorConfig {
  bypassAlertStatuses?: number[] | '*';
  retryCount?: number;
  message?: string;
}

Using Interfaces in a Component Example Component:

import { Component, OnInit } from '@angular/core';
import { AngularHttpHandlerService, Paginator, Loader, Metadata } from '@maspav/angular-http-handler';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html'
})
export class ExampleComponent implements OnInit {

  // Define properties using the provided interfaces
  paginator: Paginator = {
    page: 1,
    perPage: 10,
    total: 0,
    from: 0,
    to: 0,
    pages: 0,
    collectionSize: 0,
    disabled: false,
  };

  loader: Loader = {
    loading: false,
    error: ''
  };

  metadata: Metadata;

  constructor(private httpHandler: AngularHttpHandlerService) { }

  ngOnInit(): void {
    this.fetchData();
  }

  // Example function to fetch data and use the interfaces
  fetchData() {
    this.setLoader(true);
    this.httpHandler.get('https://api.yourdomain.com/endpoint', { page: this.paginator.page })
      .subscribe(
        (response: any) => {
          // Assuming response.metadata contains the metadata information
          this.metadata = response.metadata;
          // Use the metadata to update paginator and other UI elements
          this.setPaginator(this.metadata);
          this.setLoader(false);
        },
        (error: any) => {
          this.setLoader(false, error.message);
        }
      );
  }

  // Set the loader state
  setLoader(loading: boolean, error?: string) {
    this.loader.loading = loading;
    this.loader.error = error || '';
  }

  // Set the paginator based on API response metadata
  setPaginator(metadata: Metadata) {
    this.paginator.collectionSize = metadata.pageInfo.total;
    this.paginator.page = metadata.pageInfo.currentPage;
    this.paginator.perPage = metadata.pageInfo.perPage;
    this.paginator.pages = metadata.pageInfo.pages;
    this.paginator.from = metadata.pageInfo.from;
    this.paginator.to = metadata.pageInfo.to;
  }
}