Skip to content

Commit 1fafa9b

Browse files
committed
update readme
1 parent 272e83e commit 1fafa9b

File tree

3 files changed

+87
-17
lines changed

3 files changed

+87
-17
lines changed

README.md

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You might have written code that uses redux and redux-saga to implement data fet
66

77
All you need to do now, is to instantiate redux-saga-query and use functions that it provides: _reducer_ - that you just need to connect to your store and it will manage data, _query_ - for requesting data that needs to be cached, _mutation_ - for requesting data without caching, and more.
88

9-
The main idea is that you can request your data from anywhere in your sagas and not to worry about managing it's state.
9+
The main idea is that you can request your data from anywhere in your sagas and not worry about managing its state.
1010

1111
This package has only one dependency - [redux-saga](https://github.com/redux-saga/redux-sag) which needs to be installed in your project.
1212

@@ -32,9 +32,9 @@ $ pnpm add redux-saga-query
3232

3333
## Usage Example
3434

35-
Suppose you have a redux in your project. It can be vanilla redux or [redux-toolkit](https://redux-toolkit.js.org/), doesn't matter. It can be even custom I/O environment like redux, since redux-saga [allows you to do that](https://redux-saga.js.org/docs/advanced/UsingRunSaga).
35+
Suppose you have a redux in your project. It can be vanilla redux or [redux-toolkit](https://redux-toolkit.js.org/), doesn't matter. It can even be a custom I/O environment like redux, since redux-saga [allows you to do that](https://redux-saga.js.org/docs/advanced/UsingRunSaga).
3636

37-
Since you have storage, you have some data that needs to be derived from some place, for example, from some API. This package gives you an `initSagaQuery` function which needs to be called ones per domain. Domain is like a record in store that will be responsible for managing its data.
37+
Since you have storage, you have some data that needs to be derived from some place, for example, from some API. This package gives you an `initSagaQuery` function which needs to be called once per domain. Domain is like a record in store that will be responsible for managing its data.
3838

3939
```
4040
import { initSagaQuery } from "redux-saga-query";
@@ -66,7 +66,7 @@ export var rootReducer = {
6666
};
6767
```
6868

69-
Now you ready to use other functions provided by `initSagaQuery` in your application.
69+
Now you are ready to use other functions provided by `initSagaQuery` in your application.
7070

7171
```
7272
function* fetchBooks() {
@@ -83,15 +83,15 @@ function* fetchBooks() {
8383
}
8484
```
8585

86-
Here we used `query` function to retrieve a list of books from external api. Under the hood this function dispatches `REQUEST` action if data is stale, makes a new call using your function, caches the result to storage and returns it back where you called it. If data is not stale, it doesn't make new request and returns back cached data from storage. More information in [Documentation](https://github.com/zavvdev/redux-saga-query/tree/main?tab=readme-ov-file#documentation) and [Example](https://github.com/zavvdev/redux-saga-query/tree/main/example).
86+
Here we used a `query` function to retrieve a list of books from an external api. Under the hood this function dispatches `REQUEST` action if data is stale, makes a new call using your function, caches the result to storage and returns it back where you called it. If data is not stale, it doesn't make new requests and returns back cached data from storage. More information in [Documentation](https://github.com/zavvdev/redux-saga-query/tree/main?tab=readme-ov-file#documentation) and [Example](https://github.com/zavvdev/redux-saga-query/tree/main/example).
8787

8888
# Documentation
8989

9090
## API
9191

9292
### initSagaQuery
9393

94-
Use this function to initialize redux-saga-query. You should call it once per domain. Domain is responsible for storing cached data. It's just an object with keys that store you data. You can have one or multiple domains. For example, one common `api` domain.
94+
Use this function to initialize redux-saga-query. You should call it once per domain. Domain is responsible for storing cached data. It's just an object with keys that store your data. You can have one or multiple domains. For example, one common `api` domain.
9595

9696
**Arguments**
9797

@@ -118,7 +118,7 @@ Use this function to initialize redux-saga-query. You should call it once per do
118118

119119
- **Required**: No
120120
- **Type**: Number
121-
- **Description**: Amount of time in milliseconds that should pass until next retry will be executed it case `fn` function of query or mutation throws an error
121+
- **Description**: Amount of time in milliseconds that should pass until next retry will be executed in case `fn` function of query or mutation throws an error
122122

123123
`query`
124124

@@ -189,7 +189,7 @@ Use this function to initialize redux-saga-query. You should call it once per do
189189
### Key
190190

191191
- **Type**: [Array<string | number | bigint | boolean>](https://github.com/zavvdev/redux-saga-query/blob/3cc865dafe4194b1caccb39574e4a87c2ca18962/src/index.d.ts#L14)
192-
- **Description**: Unique identifier for each query or mutation which used as part of dispatched action type by redux-saga-query
192+
- **Description**: Unique identifier for each query and mutation
193193

194194
### QueryRecord
195195

@@ -209,7 +209,7 @@ Use this function to initialize redux-saga-query. You should call it once per do
209209
`isLoaded`
210210

211211
- **Type**: Boolean
212-
- **Description**: Has value `true` if `fn` function of query has been successfully derived its data
212+
- **Description**: Has value `true` if `fn` function of query has successfully derived its data
213213

214214
`isError`
215215

@@ -219,17 +219,17 @@ Use this function to initialize redux-saga-query. You should call it once per do
219219
`isValid`
220220

221221
- **Type**: Boolean
222-
- **Description**: Has value `true` if `fn` query data doesn't need to be requested again. In other words, it's not stale because `staleTime` milliseconds has not been passed yet. But keep in mind that this value won't be updated automatically if nothing is being requested yet using its query Key. For example, if you have data that is valid and you don't request it again after it has become invalid, this field will still show you that it's valid event if it's staleTime has already passed. The only way to update state and get fresh status of this field is to trigger request again somewhere
222+
- **Description**: Has value `true` if query data doesn't need to be requested again. In other words, it's not stale because `staleTime` milliseconds has not passed yet. But keep in mind that this value won't be updated automatically if nothing is being requested yet using the respective query Key. For example, if you have data that is valid and you didn't request it again after it had become invalid, this field will still show you that it's valid even if its staleTime had already passed. The only way to update state and get fresh status of this field is to trigger request again somewhere
223223

224224
`isReset`
225225

226226
- **Type**: Boolean
227-
- **Description**: Has value `true` if `fn` query data is in reset state. Technically, reset state is when you have no data but query record is present in domain state. Redux-saga-query does not make reset of your data. It can only be reset by you manually by calling `reset` function
227+
- **Description**: Has value `true` if fn query data is in reset state. Technically, a reset state is when you have no data but a query record is present in the domain state. Redux-saga-query does not reset your data. It can only be reset by you manually by calling [reset](https://github.com/zavvdev/redux-saga-query/tree/main?tab=readme-ov-file#initsagaquery) function
228228

229229
`timestamp`
230230

231231
- **Type**: Number | Undefined
232-
- **Description**: Has the last timestamp when `fn` function of query has been successfully derived its data
232+
- **Description**: Has the last timestamp when `fn` function of query has successfully derived its data
233233

234234
`data`
235235

@@ -239,7 +239,7 @@ Use this function to initialize redux-saga-query. You should call it once per do
239239
`error`
240240

241241
- **Type**: Unknown | Null
242-
- **Description**: Value returned by `extractError` function (default or custom)
242+
- **Description**: Value returned by `extractError` function
243243

244244
### MutationRecord
245245

@@ -254,7 +254,7 @@ Use this function to initialize redux-saga-query. You should call it once per do
254254
`isLoaded`
255255

256256
- **Type**: Boolean
257-
- **Description**: Has value `true` if `fn` function of mutation has been successfully derived its data
257+
- **Description**: Has value `true` if `fn` function of mutation has successfully derived its data
258258

259259
`isError`
260260

@@ -269,4 +269,74 @@ Use this function to initialize redux-saga-query. You should call it once per do
269269
`error`
270270

271271
- **Type**: Unknown | Null
272-
- **Description**: Value returned by `extractError` function (default or custom)
272+
- **Description**: Value returned by `extractError` function
273+
274+
### reducer
275+
276+
- **Type**: Function
277+
- **Description**: Used for managing your query and mutation data. You only need to connect this function to your store
278+
- **Arguments**:
279+
280+
`state` - previous state
281+
282+
`action` - dispatched action
283+
284+
- **Returns**: Next state
285+
286+
### selector
287+
288+
- **Type**: Function
289+
- **Description**: Selects QueryRecord or Mutation Record from the storage
290+
- **Arguments**:
291+
292+
`key` - [Key](https://github.com/zavvdev/redux-saga-query/tree/main?tab=readme-ov-file#key) that represents specific query. Should not be partial.
293+
294+
- **Returns**: [QueryRecord](https://github.com/zavvdev/redux-saga-query/tree/main?tab=readme-ov-file#queryrecord), [MutationRecord](https://github.com/zavvdev/redux-saga-query/tree/main?tab=readme-ov-file#mutationrecord) or null
295+
296+
### query
297+
298+
- **Type**: Function
299+
- **Arguments**: Object with next fields:
300+
301+
`key` - [Key](https://github.com/zavvdev/redux-saga-query/tree/main?tab=readme-ov-file#key) that represents this query. Should be unique for each query
302+
303+
`fn` - Any function that returns some serializable data
304+
305+
`options` - Fully partial [options](https://github.com/zavvdev/redux-saga-query/blob/272e83e5e8038f11e0231e6f4700f56e8d096b94/src/index.d.ts#L1) that will be applied only for this specific query. Represented as an object the same as in `initSagaQuery` _query_ argument but fully partial
306+
307+
- **Returns**: Generator function which yields redux-saga effects. Returns the result of `fn` if it has successfully derived data or throws an error with value returned from `extractError` function
308+
- **Description**: Caches the data returned from `fn` function
309+
310+
### mutation
311+
312+
- **Type**: Function
313+
- **Arguments**: Object with next fields:
314+
315+
`key` - [Key](https://github.com/zavvdev/redux-saga-query/tree/main?tab=readme-ov-file#key) that represents this mutation. Should be unique for each mutation
316+
317+
`fn` - Any function that returns some serializable data
318+
319+
`options` - [Options](https://github.com/zavvdev/redux-saga-query/blob/272e83e5e8038f11e0231e6f4700f56e8d096b94/src/index.d.ts#L8) that will be applied only for this specific mutation. Represented as an object the same as in `initSagaQuery` _mutation_ argument
320+
321+
- **Returns**: Generator function which yields redux-saga effects. Returns the result of `fn` if it has successfully derived data or throws an error with value returned from `extractError` function
322+
- **Description**: Does not cache the data returned from `fn` function
323+
324+
### invalidate
325+
326+
- **Type**: Function
327+
- **Arguments**:
328+
329+
`key` - [Key](https://github.com/zavvdev/redux-saga-query/tree/main?tab=readme-ov-file#key) that represents any query with exact or partial match
330+
331+
- **Returns**: Void
332+
- **Description**: Searches in domain for any partially matched keys. If found any - invalidates their data if its valid and not currently in progress (isLoading or isFetching)
333+
334+
### reset
335+
336+
- **Type**: Function
337+
- **Arguments**:
338+
339+
`key` - [Key](https://github.com/zavvdev/redux-saga-query/tree/main?tab=readme-ov-file#key) that represents any query with exact or partial match
340+
341+
- **Returns**: Void
342+
- **Description**: Searches in domain for any partially matched keys. If found any - resets their data if it hasn't been reset yet. Reset means that domain still has a record for this specific query but without any data

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ interface Modules {
4646

4747
selector: (
4848
key: Key,
49-
) => (state: State) => QueryRecord | MutationRecord;
49+
) => (state: State) => QueryRecord | MutationRecord | null;
5050

5151
query: <T extends unknown>(args: {
5252
key: Key;

src/modules/selector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Key } from "../entities/Key";
33
var getSelector = function (domain) {
44
return (key) => (state) => {
55
var createdKey = Key.from(key);
6-
return state?.[domain]?.[createdKey];
6+
return state?.[domain]?.[createdKey] || null;
77
};
88
};
99

0 commit comments

Comments
 (0)