THIS IS A WORK IN PROGRESS
react-native-redux-rest-resource
A helper to handle REST Apis with Redux in react-native
How to set it up
// resources.js; const PostsResource = ;
// store.js;;;;;; const enhancer = ; enhancer;
How to trigger RESTful Requests
// TestClass.js;;;; ; const styles = StyleSheet; { thispropsindex; // Get all the posts thisprops; // Create a post ; // Get a post ; // Create a post ; // Delete the post of ID 1 } { return <View style=stylescontainer> !thispropsloading && !thispropserror ? thispropsposts : null thispropsloading ? <ActivityIndicator /> : null thispropserror ? <Text>thispropserrormessage</Text> : null </View> ; } const mapStateToProps = posts: Object error: statePostserror loading: statePostsloading;const mapDispatchToProps = ; mapStateToProps mapDispatchToPropsTestClass;
How to handle the result of the requests
// postsReducer.js; = state = List: {} loading: false error: null action ;
Options
KEY | VALUE | DEFAULT VALUE | WHAT DOES IT DO |
---|---|---|---|
API_URL | URL | 'localhost/' |
The Base url of your API |
CAMELIZE_DECAMELIZE | true/false | true |
All the keys of objects coming in from the server will be camelized (ex: customer_id will be transformed to customerId ) and everything that goes out of the client will be decamelized (ex: customerId will be transformed to customer_id ) |
DEBUG | true/false | true |
console.log s stuff about what is happening network-wise |
HTTP_ERROR_CODE_MESSAGES | An object with keys being the status code and values being the messages | { 401: 'FORBIDDEN', 403: 'FORBIDDEN', 404: 'NOT_FOUND', } |
Customize the messages of the errors caught while doing the requests |
HTTP_HEADERS | Headers put on the requests | {} |
Headers to add to the requests |
JWT_TOKEN | YOUR_JWT_TOKEN | null |
This will add an Authorization header with value Bearer YOUR_JWT_TOKEN |
REDUX_ACTIONS_PREFIX | Prefix to add to actions type | 'RNRRR' | Namespaces actions to avoid conflicts with your actions |
TODO
- postfix actionCreators with appropriate
endpoint
camelized and pluralized term - Create Basic Reducer automatically?