native-api-service
v1.0.2
Published
A service to help you with your API request using client fetch
Downloads
30
Maintainers
Readme
native-api-service
A simple way to make API request from the client end
Installation
Use the package manager npm to install native-api-service.
npm install native-api-service
Explanation
ApiService.request(method, path, callback, errorCallback, payload, exectuteWhileLoading);
- method: this is any of the HTTP request method - get, delete, head, options, posst, put, patch,
- path: this is the relative path to the needed resource - '/user'
- callback: this is a function that gets executed when the request is successful - (response) => console.log('response from API -> ', response)
- errorCallback: this is a error function that gets executed when the request fails - (error) => console.log('error from API -> ', error)
- payload (optional): this is an object that needs you to send data to the API - { key: value }
- exectuteWhileLoading (optional): this is a function that gets executed while the request is loading - NB: you can show a toast or upload progress
Usage
import { ApiService } from 'native-api-service';
const userApiService = new ApiService('https://jsonplaceholder.typicode.com');
// or
// const userApiService = new ApiService('https://jsonplaceholder.typicode.com', 'Bearer **token**');
// or
// const userApiService = new ApiService('https://jsonplaceholder.typicode.com', 'Bearer **token**', 'application/json');
userApiService.request(
'get',
'/users',
response => {
console.log(response);
return response;
},
error => {
console.log(error);
return error;
},
null,
console.log('Loading all users...'),
);
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.