native-service
v1.0.1
Published
[React-native] Now, you can make an api request easiest. Cache, Paginate, Checking error, Transformation, Urlencoded It use apisauce for requesting Next: upload, download (Comming soon)
Downloads
4
Readme
native-service
[React-native] Now, you can make an api request easiest. Cache, Paginate, Checking error, Transformation, Urlencoded It use apisauce for requesting Next: upload, download (Comming soon)
Getting started
$ npm install native-service --save
ServiceManager.js:
import Service from 'native-service';
import UserService from "./UserService";
import SampleService from './SampleService'
export class ServiceManager {
constructor() {
Service.config({
baseURL: 'https://api.something.com',
timeout: 5000
});
this.sampleService = new SampleService();
}
getSampleService = () => {
return this.sampleService;
}
}
export const serviceManager = new ServiceManager();
SampleService.js
import { GET, POST } from "native-service";
export default class SampleService {
login = POST('/auth/login').cache("login");
getInfo = POST('/info/{userId}');
//Paginated API
getPaginatedList = GET('/list').paginate((currentPage) => {
return {
orderType: 'asc',
size: 20,
page: currentPage
};
});
//Override url only for this api
apiToOtherServer = GET('another/server').overrideConfig({
baseURL: 'https://api.anotherserver.com',
timeout: 10000
});
}
FUNCTIONS | Functions | What for | Both ios/android | | ------------- |:-------------:| -----:| | cache("key") | After requesting, save the result to Cache, with name "key" | ✓ | | header({}) | Header | ✓ | |transform((response) => { return response; })| Transform response data| ✓ | |paginate((currentPage) => {return {orderType: 'asc',size: 20,page: currentPage};});| This api is paginated| ✓ | |overrideConfig({ baseURL: '', timeout: 1000 }) | Override config only for current api | ✓ | |raw,urlencoded| default is raw |✓ |
Call Request
//Login API
var api = serviceManager.getSampleService().login;
var connect = await api.data({ username: "abc", password: "123"}).connect(); //Connect is your result
if(api.isError() || api.isClientError() || api.isServerError() || api.isTimeoutError() || api.isConnectionError() || api.isNetworkError() || api.isCancelError()) {
//There is error in connection
}
//
var api2 = serviceManager.getSampleService().getPaginatedList;
var connect = await api2.data({ }).connect();//Connect is your result
var nextPage = await api2.data({ }).nextPage();//nextPage is your result
var pageAtPosition3 = await api2.data({ }).page(3);//pageAtPosition3 is your result
FUNCTIONS | Functions | What for | Both ios/android | | ------------- |:-------------:| -----:| | data({}) | Data to request | ✓ | | connect() | Send request now | ✓ | | nextPage() | Request next page | ✓ | | page(3) | Request the page at position 3 | ✓ |
#ERROR | ERROR | Check if | Both ios/android | | ------------- |:-------------:| -----:| | isError() | There is any error | ✓ | | isClientError() | Any non-specific 400 series error. | ✓ | | isServerError() | Any 500 series error. | ✓ | | isTimeoutError() | Server didn't respond in time. | ✓ | | isConnectionError() | Server not available, bad dns. | ✓ | | isNetworkError() | Network not available. | ✓ | | isCancelError() | Request has been cancelled. | ✓ |