
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@myfave/react-native-rest-client
Advanced tools
Simplify the RESTful calls of your React Native app.
npm install --save react-native-rest-client
Create your own api client by extending the RestClient class
import RestClient from 'react-native-rest-client';
export default class YourRestApi extends RestClient {
constructor () {
// Initialize with your base URL
super('https://api.myawesomeservice.com');
}
// Now you can write your own methods easily
login (username, password) {
// Returns a Promise with the response.
return this.POST('/auth', { username, password });
}
getCurrentUser () {
// If the request is successful, you can return the expected object
// instead of the whole response.
return this.GET('/auth')
.then(response => response.user);
}
};
Then you can use your custom client like this
const api = new YourRestApi();
api.login('johndoe', 'p4$$w0rd')
.then(response => response.token) // Successfully logged in
.then(token => saveToken(token)) // Remember your credentials
.catch(err => alert(err.message)); // Catch any error
import RestClient from 'react-native-rest-client';
export default class YourRestApi extends RestClient {
constructor (authToken) {
super('https://api.myawesomeservice.com', {
headers: {
// Include as many custom headers as you need
Authorization: `JWT ${authToken}`
// Content-Type: application/json
// and
// Accept: application/json
// are added by default
},
// Simulate a slow connection on development by adding
// a 2 second delay before each request.
devMode: __DEV_,
simulatedDelay: 2000
});
}
getWeather (date) {
// Send the url query as an object
return this.GET('/weather', { date })
.then(response => response.data);
}
checkIn (lat, lon) {
return this.POST('/checkin', { lat, lon });
}
};
You must call the parent constructor as shown in the example above.
| Parameter | Type | Required | Default |
|---|---|---|---|
| baseUrl | String | Yes | undefined |
| options | Object | No | {} |
Supports the following values
| Key | Type | Required | Default | Comments |
|---|---|---|---|---|
| headers | String | No | {} | Headers to be appended to the request. RestApi will always include Content-Type: application/json and Accept: application/json. |
| devMode | Boolean | No | false | When true, it enables the simulatedDelay. |
| simulatedDelay | Number | No | 0 | Useful for simulating a slow connection. Number of milliseconds to wait before making the request. NOTE: It will only take effect if devMode is true. |
Each one of these methods returns a Promise with the response as the parameter.
| Parameter | Type | Required | Default | Comments |
|---|---|---|---|---|
| route | String | Yes | '' | Partial route to be appended to the baseUrl |
| query | Object | No | {} | Object to be encoded and appended as the query part to the URL |
| body | Object | No | {} | Data to be sent as the JSON body of the message |
fetch) comes out of the box with React Native, and
adding support for more platforms would require to add pre-compilers, polyfills and other
tricks, which are completely out of the scope of this library. If you know what you're
doing though, feel free to tweak your stack and use this library.MIT
FAQs
Your REST client for React Native made easy
We found that @myfave/react-native-rest-client demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.