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

ngonis-http-service

v0.0.4

Published

Angular module designed to simplify HTTP requests and API interactions within Angular applications. It provides a standardized service (NgonisService) for performing common HTTP operations such as GET, POST, PUT, DELETE, and handling errors

Downloads

8

Readme

Ngonis Service Library for Angular

The Ngonis Service Library is an Angular module designed to simplify HTTP requests and API interactions within Angular applications. It provides a standardized service (NgonisService) for performing common HTTP operations such as GET, POST, PUT, DELETE, and handling errors.

Features

  • Easy integration into Angular applications.
  • Encapsulates HTTP request logic and error handling.
  • Supports dynamic configuration of API base URLs.
  • Promotes code reusability and maintainability.

Installation

Install the library via npm:

npm install ngonis-http-service

Usage

1. Import Module

Import the NgonisService into your Angular application's root module (e.g., AppModule).

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { NgonisService,, BASE_URL_TOKEN } from 'ngonis-http-service';
import { environment } from '../environments/environment'; // Adjust path as needed

@NgModule({
  declarations: [
    // Your components
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
  ],
  providers: [
    NgonisService,
    { provide: BASE_URL_TOKEN, useValue: environment.baseUrl }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

2. Inject and Use NgonisService

Inject NgonisService into your components or services and use its methods to interact with your API.

import { Component, OnInit } from '@angular/core';
import { NgonisService } from 'ngonis-http-service';

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css']
})
export class MyComponent implements OnInit {

  constructor(private nService: NgonisService<any>) {}

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

  fetchUsers(): void {
    this.nService.get<any[]>('your-url').subscribe(
      (data) => {
        console.log('Users:', data);
        // Handle data
      },
      (error) => {
        console.error('Error fetching users:', error);
        // Handle error
      }
    );
  }
}

3. Configuration

  • Base URL: Define the base URL of your API in your environment configuration (environment.ts or environment.prod.ts).
// environment.ts
export const environment = {
  production: false,
  baseUrl: 'https://api.example.com'
};

4. Methods Available

  • get(url: string): Perform a GET request to the specified URL.
  • post(url: string, body: any): Perform a POST request to the specified URL with the given body.
  • put(url: string, body: any): Perform a PUT request to the specified URL with the given body.
  • delete(url: string): Perform a DELETE request to the specified URL.
  • search(parameter: string): Perform a GET request with search parameters.

Error Handling

Errors are handled internally in NgonisService. You can customize error handling by extending the service.

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { NgonisService } from 'ngonis-http-service';

@Injectable({
  providedIn: 'root'
})
export class CustomService<T> extends NgonisService<T> {

  constructor(httpClient: HttpClient) {
    super(httpClient, 'https://custom-api.example.com');
  }

  // Override or extend methods as needed
}

Notes:

  • Replace https://api.example.com with your actual API base URL.
  • Customize the error handling and methods in NgonisService as per your application's requirements.