HTTP Service Examples
Explore how to use the @resourge/http-service
package in common scenarios.
Creating the HttpService Instance
Section titled “Creating the HttpService Instance”Create a configured instance of BaseHttpService
:
import { BaseHttpService } from '@resourge/http-service';
const HttpService = new BaseHttpService({ baseUrl: 'https://api.example.com', headers: { Authorization: 'Bearer token123', },});
Making HTTP Requests
Section titled “Making HTTP Requests”GET Request
Section titled “GET Request”HttpService.get('/posts/1') .then(response => console.log(response)) .catch(error => console.error(error));
POST Request
Section titled “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
Section titled “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
Section titled “DELETE Request”HttpService.delete('/posts/1') .then(response => console.log(response)) .catch(error => console.error(error));
PATCH Request
Section titled “PATCH Request”const patchData = { body: 'Patched content',};
HttpService.patch('/posts/1', patchData) .then(response => console.log(response)) .catch(error => console.error(error));
File Upload
Section titled “File Upload”Upload files with additional form data:
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));
Using LoadingService
Section titled “Using LoadingService”Show and Hide Loading Indicators
Section titled “Show and Hide Loading Indicators”import { LoadingService } from '@resourge/http-service';
LoadingService.show(); // Show default loaderLoadingService.hide(); // Hide default loader
LoadingService.show('customLoaderId'); // Show loader with custom IDLoadingService.hide('customLoaderId'); // Hide loader with custom ID
Listening to Loading State Changes
Section titled “Listening to Loading State Changes”const removeListener = LoadingService.addListener(() => { console.log('Loading state changed!');});
// Remove listener when no longer neededremoveListener();
Listening to Custom Loader
Section titled “Listening to Custom Loader”const removeCustomListener = LoadingService.addListener('myLoaderId', () => { console.log('Custom loading state changed!');});
// Remove listener laterremoveCustomListener();
Extending BaseHttpService
Section titled “Extending BaseHttpService”Create a custom service by extending BaseHttpService
:
class CustomHttpService extends BaseHttpService { constructor() { super({ baseUrl: 'https://api.example.com', headers: { Authorization: 'Bearer token123' }, }); }
public customMethod() { // Your custom logic here }}
const customService = new CustomHttpService();customService.customMethod();