Skip to content

HTTP Service Examples

Explore how to use the @resourge/http-service package in common scenarios.


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',
},
});

HttpService.get('/posts/1')
.then(response => console.log(response))
.catch(error => console.error(error));
const postData = {
title: 'New Post',
body: 'Lorem ipsum...',
userId: 1,
};
HttpService.post('/posts', postData)
.then(response => console.log(response))
.catch(error => console.error(error));
const updateData = {
title: 'Updated Post',
body: 'Updated content',
};
HttpService.put('/posts/1', updateData)
.then(response => console.log(response))
.catch(error => console.error(error));
HttpService.delete('/posts/1')
.then(response => console.log(response))
.catch(error => console.error(error));
const patchData = {
body: 'Patched content',
};
HttpService.patch('/posts/1', patchData)
.then(response => console.log(response))
.catch(error => console.error(error));

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));

import { LoadingService } from '@resourge/http-service';
LoadingService.show(); // Show default loader
LoadingService.hide(); // Hide default loader
LoadingService.show('customLoaderId'); // Show loader with custom ID
LoadingService.hide('customLoaderId'); // Hide loader with custom ID
const removeListener = LoadingService.addListener(() => {
console.log('Loading state changed!');
});
// Remove listener when no longer needed
removeListener();
const removeCustomListener = LoadingService.addListener('myLoaderId', () => {
console.log('Custom loading state changed!');
});
// Remove listener later
removeCustomListener();

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();