1.32.0 • Published 17 days ago

@resourge/http-service v1.32.0

Weekly downloads
-
License
MIT
Repository
github
Last release
17 days ago

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:

1.32.0

17 days ago

1.31.0

23 days ago

1.30.3

24 days ago

1.29.3

1 month ago

1.30.2

1 month ago

1.30.0

1 month ago

1.30.1

1 month ago

1.29.2

1 month ago

1.29.0

1 month ago

1.29.1

1 month ago

1.27.0

2 months ago

1.25.6

2 months ago

1.26.0

2 months ago

1.28.1

2 months ago

1.28.0

2 months ago

1.25.4

2 months ago

1.25.5

2 months ago

1.25.3

3 months ago

1.25.2

4 months ago

1.25.0

4 months ago

1.25.1

4 months ago

1.14.1

10 months ago

1.14.0

10 months ago

1.18.0

8 months ago

1.16.0

8 months ago

1.14.2

10 months ago

1.21.0

7 months ago

1.21.1

7 months ago

1.23.0

7 months ago

1.21.2

7 months ago

1.23.1

7 months ago

1.21.3

7 months ago

1.15.0

8 months ago

1.13.2

11 months ago

1.19.0

8 months ago

1.17.1

8 months ago

1.17.0

8 months ago

1.15.2

8 months ago

1.15.1

8 months ago

1.20.1

7 months ago

1.22.0

7 months ago

1.20.0

7 months ago

1.24.0

7 months ago

1.12.6

1 year ago

1.13.1

1 year ago

1.13.0

1 year ago

1.12.3

1 year ago

1.12.2

1 year ago

1.12.1

1 year ago

1.12.0

1 year ago

1.12.5

1 year ago

1.12.4

1 year ago

1.11.0

1 year ago

1.11.1

1 year ago

1.10.1

1 year ago

1.10.0

1 year ago

1.9.0

1 year ago

1.8.15

1 year ago

1.8.14

1 year ago

1.8.13

1 year ago

1.8.12

1 year ago

1.8.11

1 year ago

1.8.10

1 year ago

1.8.9

1 year ago

1.8.8

1 year ago

1.8.7

1 year ago

1.8.6

1 year ago

1.8.5

1 year ago

1.8.4

1 year ago

1.8.3

1 year ago

1.8.2

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.7.2

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.8

2 years ago

1.5.7

2 years ago

1.5.6

2 years ago

1.5.5

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago