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

@resourge/http-service

v1.38.1

Published

[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

Downloads

946

Readme

http-service

License

http-service is a comprehensive package that provides essential services for web applications. This package includes BaseHttpService for making HTTP requests to servers and LoadingService for managing loading indicators. Together, these services offer a robust solution for handling asynchronous operations and displaying loading feedback.

Table of Contents

Installation

Install using Yarn:

yarn add @resourge/http-service

or NPM:

npm install @resourge/http-service

BaseHttpService

BaseHttpService is a main service for making requests to a server. It serves as a simple wrapper around the Fetch API, with added features such as request throttling for GET requests and support for file uploads.

Usage

import { BaseHttpService } from '@resourge/http-service'

const HttpService = new BaseHttpService({
  baseUrl: 'https://api.example.com',
  headers: {
    'Authorization': 'Bearer token123'
  }
});

Making Requests

GET Request

HttpService.get('/posts/1')
  .then(response => console.log(response))
  .catch(error => console.error(error));

POST Request

const postData = {
  title: 'New Post',
  body: 'Lorem ipsum...',
  userId: 1
};

HttpService.post('/posts', postData)
  .then(response => console.log(response))
  .catch(error => console.error(error));

PUT Request

const updateData = {
  title: 'Updated Post',
  body: 'Updated content'
};

HttpService.put('/posts/1', updateData)
  .then(response => console.log(response))
  .catch(error => console.error(error));

DELETE Request

HttpService.delete('/posts/1')
  .then(response => console.log(response))
  .catch(error => console.error(error));

PATCH Request

const patchData = {
  body: 'Patched content'
};

HttpService.patch('/posts/1', patchData)
  .then(response => console.log(response))
  .catch(error => console.error(error));

File Upload

const files = [file1, file2];
const formData = {
  description: 'File description'
};

HttpService.upload('POST', '/files', files, formData)
  .then(response => console.log(response))
  .catch(error => console.error(error));

Custom Interceptors

Interceptors can be used to modify request or response configurations:

HttpService.setToken(config => {
  // Modify request headers
  config.headers['Authorization'] = 'New Token';
  return config;
});

// Add a request interceptor
HttpService.interceptors.request.use(
  response => {
    // Modify response data or handle it
    return response;
  },
  error => {
    // Handle errors or modify error responses
    return Promise.reject(error);
  }
);

// Add a response interceptor
HttpService.interceptors.response.use(
  response => {
    // Modify response data or handle it
    return response;
  },
  error => {
    // Handle errors or modify error responses
    return Promise.reject(error);
  }
);

Extending BaseHttpService

You can extend BaseHttpService to create a specialized service with additional methods or custom configurations:

class CustomHttpService extends BaseHttpService {
  constructor() {
    super({
      baseUrl: 'https://api.example.com',
      headers: {
        'Authorization': 'Bearer token123'
      }
    });
  }

  // Add custom methods
  public customMethod() {
    // Custom logic here
  }
}

const CustomService = new CustomHttpService();
CustomService.customMethod();

LoadingService

LoadingService is a simple service designed to manage the state of loading indicators in your application. It provides methods to show or hide loading indicators and allows components to listen for changes in loading state.

Usage

import { LoadingService } from '@resourge/http-service';

// Show loading indicator
LoadingService.show();

// Show loading indicator with custom loaderId
LoadingService.show('myLoaderId');

// Hide loading indicator
LoadingService.hide();

// Hide loading indicator with custom loaderId
LoadingService.hide('myLoaderId');


// Add an event listener for the default loader
const removeListener = LoadingService.addListener(() => {
  // Handle loading state change
  console.log('Loading state changed!');
});

// Add an event listener for a specific loaderId
const removeCustomListener = LoadingService.addListener('myLoaderId', () => {
  // Handle loading state change for custom loaderId
  console.log('Custom Loading state changed!');
});

// To remove the listener later
removeListener(); // or removeCustomListener();

API

getLoading(loaderId?: string): boolean

  • Returns the current loading state for the specified loaderId. If no loaderId is provided, returns the default loading state.

addListener(loaderId?: string, onEmit: () => void): () => void

  • Adds an event listener for the specified loaderId. When the loading state changes, the onEmit function will be called.
  • Returns a function to remove the listener when no longer needed.

show(loaderId?: string): void

  • Shows the loading indicator for the specified loaderId. If no loaderId is provided, shows the default loading indicator.

hide(loaderId?: string): void

  • Hides the loading indicator for the specified loaderId. If no loaderId is provided, hides the default loading indicator.

Documentation

For comprehensive documentation and usage examples, visit the Http Service documentation.

Contributing

Contributions to @resourge/http-service are welcome! To contribute, please follow the contributing guidelines.

License

Fetch is licensed under the MIT License.

Contact

For questions or support, please contact the maintainers: