@hashiprobr/expo-use-rest v1.0.4
expo-use-rest
A React Hook for simplifying requests to REST servers and the file upload features of expo-file-system
This hook receives an url and returns an object with seven properties:
a boolean state
running, that indicates whether it is waiting for a request or upload;an asynchronous method
get(uri), that sends a GET tourl+uriand returns its response;an asynchronous method
post(uri, body), that sends a POST tourl+uriand returns its response (see below for details);an asynchronous method
put(uri, body), that sends a PUT tourl+uriand returns its response (see below for details);an asynchronous method
delete(uri), that sends a DELETE tourl+uriand returns its response;an asynchronous method
uploadPost(restUri, fileUri), that sends a POST tourl+restUriand returns its response (see below for details);an asynchronous method
uploadPut(restUri, fileUri), that sends a PUT tourl+restUriand returns its response (see below for details).
In post and put, if the request body is a string, it is sent unchanged as
text/plain. Otherwise, it is serialized and sent as application/json.
In uploadPost and uploadPut, the bytes of the file located at fileUri are
sent with
FileSystem.uploadAsync.
If the response body is application/json, it is unserialized and received as
whatever it represents. Otherwise, it is received unchanged as a string.
If the response status is not 200, throws an error with properties status and
message. The status is 0 if the request could not even be made.
Peer dependencies
{
"expo": "^43.0.5",
"expo-file-system": "^13.0.3",
"react": "^17.0.1",
"react-native": ">=0.64.3"
}Install
With npm:
npm install @hashiprobr/expo-use-restWith yarn:
yarn add @hashiprobr/expo-use-restWith expo:
expo install @hashiprobr/expo-use-restExample
import React from 'react';
import { View, Text, Button } from 'react-native';
import useRest from '@hashiprobr/expo-use-rest';
export default function MyComponent() {
const client = useRest('http://address.of.a.host:8080');
async function onPress() {
let body;
try {
body = await client.get('/uri/to/an/endpoint');
} catch (error) {
console.error(error);
}
if (body) {
console.log(body);
}
}
return (
<View
style={{
flexGrow: 1,
justifyContent: 'center',
alignItems: 'center',
}}
>
{client.running ? (
<Text>running...</Text>
) : (
<Button title="run" onPress={onPress} />
)}
</View>
);
}