@discovery-solutions/json-connection
v2.0.6
Published
A React Library to connect with JSON Server APIs
Downloads
1
Keywords
Readme
@discovery-solutions/json-connection
This is a library designed to integrate with APIs built on top of JSON Server Library.
It works with localStorage to cache data, and allow access all the API endpoints in a facilitated way.
Install
npm i --save @discovery-solutions/json-connection
Setup
import { ConnectionProvider } from "@discovery-solutions/json-connection";
export function App() {
return (
<ConnectionProvider url="http://localhost:3501">
<ListUsers/>
<ConnectionProvider>
)
}
Requests (Hook Method)
import { useEntity } from "@discovery-solutions/json-connection";
export function ListUsers() {
const [ users, manager, loading ] = useEntity("user");
// manager.insert();
// manager.update();
// manager.get();
// manager.delete();
// manager.search();
// manager.list()
// manager.oldest()
// manager.latest()
if (loading)
return "Loading...";
return users.map(user => (
<div>
<h1>{ user.name }</h1>
<button onClick={ () => manager.delete(user.id) }>
delete
</button>
</div>
));
}
Requests (Alternative Method)
import { EntityManager, Entity } from "@discovery-solutions/json-connection";
export function ListUsers() {
const [ users, setUsers ] = useState();
const manager = new EntityManager(Entity.USER);
useEffect(() => {
manager.list().then(setUsers);
}, []);
return ...;
}