You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+85-15Lines changed: 85 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ You might have written code that uses redux and redux-saga to implement data fet
6
6
7
7
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.
8
8
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.
10
10
11
11
This package has only one dependency - [redux-saga](https://github.com/redux-saga/redux-sag) which needs to be installed in your project.
12
12
@@ -32,9 +32,9 @@ $ pnpm add redux-saga-query
32
32
33
33
## Usage Example
34
34
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).
36
36
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.
38
38
39
39
```
40
40
import { initSagaQuery } from "redux-saga-query";
@@ -66,7 +66,7 @@ export var rootReducer = {
66
66
};
67
67
```
68
68
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.
70
70
71
71
```
72
72
function* fetchBooks() {
@@ -83,15 +83,15 @@ function* fetchBooks() {
83
83
}
84
84
```
85
85
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).
87
87
88
88
# Documentation
89
89
90
90
## API
91
91
92
92
### initSagaQuery
93
93
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.
95
95
96
96
**Arguments**
97
97
@@ -118,7 +118,7 @@ Use this function to initialize redux-saga-query. You should call it once per do
118
118
119
119
-**Required**: No
120
120
-**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
122
122
123
123
`query`
124
124
@@ -189,7 +189,7 @@ Use this function to initialize redux-saga-query. You should call it once per do
189
189
### Key
190
190
191
191
-**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
193
193
194
194
### QueryRecord
195
195
@@ -209,7 +209,7 @@ Use this function to initialize redux-saga-query. You should call it once per do
209
209
`isLoaded`
210
210
211
211
-**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
213
213
214
214
`isError`
215
215
@@ -219,17 +219,17 @@ Use this function to initialize redux-saga-query. You should call it once per do
219
219
`isValid`
220
220
221
221
-**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
223
223
224
224
`isReset`
225
225
226
226
-**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
228
228
229
229
`timestamp`
230
230
231
231
-**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
233
233
234
234
`data`
235
235
@@ -239,7 +239,7 @@ Use this function to initialize redux-saga-query. You should call it once per do
239
239
`error`
240
240
241
241
-**Type**: Unknown | Null
242
-
-**Description**: Value returned by `extractError` function (default or custom)
242
+
-**Description**: Value returned by `extractError` function
243
243
244
244
### MutationRecord
245
245
@@ -254,7 +254,7 @@ Use this function to initialize redux-saga-query. You should call it once per do
254
254
`isLoaded`
255
255
256
256
-**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
258
258
259
259
`isError`
260
260
@@ -269,4 +269,74 @@ Use this function to initialize redux-saga-query. You should call it once per do
269
269
`error`
270
270
271
271
-**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
0 commit comments