https://github.com/coreh/restore
RESTful Data Store
https://github.com/coreh/restore
datastore fetch javascript rest restful state unidirectional-data-flow
Last synced: 9 months ago
JSON representation
RESTful Data Store
- Host: GitHub
- URL: https://github.com/coreh/restore
- Owner: coreh
- License: mit
- Created: 2018-04-07T06:51:49.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-30T00:33:20.000Z (over 7 years ago)
- Last Synced: 2025-03-17T21:45:50.390Z (9 months ago)
- Topics: datastore, fetch, javascript, rest, restful, state, unidirectional-data-flow
- Language: TypeScript
- Homepage:
- Size: 327 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE
Awesome Lists containing this project
README
# RESTore
## Introduction
**RESTore** combines the unidirectional data flow model found in libraries like [Redux](https://redux.js.org) with familiar HTTP semantics.
Its design is informed by field experience with mantaining large Redux applications, with the key realization that despite the fact that almost any useful Redux application will end up interfacing with a REST API, it is currently not very ergonomic to do so.
RESTore's API is fully asynchronous, and meant to go hand-in-hand with the new suspending strategy found in React.
### Development Status
This library is still on its early days, and is certainly not suitable for production. Expect significant changes as the API gets polished.
### Key inspirations
RESTore is inspired by the following APIs/concepts:
- Redux
- React Suspense
- REST
- Fetch API
- Express/Connect/Koa
- Service Workers
## Conceptual Comparison (Redux)
| Redux | RESTore |
|----------------|----------------------------------------------------------|
| Store | Store |
| Action | Request |
| Reducer | Handler |
| `.dispatch()` | `.fetch()` |
| Action Type | HTTP Verb (`GET`, `POST`, `PUT`, ...) |
| Action Creator | Convenience Methods (`.get()`, `.post()`, `.put()`, ...) |
| Selector | Path |
| `.subscribe()` | `.subscribe()` |
## Examples
### Store Definition
```js
import RESTore from '@coreh/restore';
const store = new RESTore();
store.use('/users/:id', async function ({ method, body, path }, next) {
switch (method) {
case 'PATCH':
const stored = this.stored(path);
if (stored === undefined) {
throw new Error('User does not exist');
}
return {
...stored,
...body,
};
default:
return next();
}
});
store.use('/users', async function ({ method, body, path }, next) {
switch (method) {
case 'POST':
return {
[RESTore.Path]: ['users', body.username], // Same as `/users/${body.username}`
...body,
};
default:
return next();
}
});
```
### Store Usage
```js
await store.post('/users', {
username: 'coreh',
likes: ['Chocolate', 'Coffee'],
});
await store.patch('/users/coreh', { singing: true });
console.log(await store.get('/users/coreh'));
// { username: 'coreh',
// likes: ['Chocolate', 'Coffee'],
// singing: true }
```
### Mounting existing REST API endpoints
```js
import endpoint from '@coreh/restore/endpoint';
// Declaration
store.use(endpoint('https://api.example.com/'));
// Usage
store.get(['flights', airport])
```
### Integrating with React "Suspense"
```jsx
const Weather = (props) => {
// .take() -> .get(), but will throw promise if not fetched
const weather = store.take(['weather', props.location]);
return (
Temperature: {weather.temperature}
Humidity: {weather.humidity}
)
}
```
### Yield multiple resources (Async Generators)
TODO
### Caching
TODO
### Optimistic Loading / Progress Reporting
TODO
## License
MIT, see [LICENSE](LICENSE).
