Skip to content

Commit 3696b49

Browse files
committed
Merge branch 'enhancer-callback-2' of https://github.com/reduxjs/redux-toolkit into enhancer-callback-2
2 parents 7497f73 + 90a6b6d commit 3696b49

32 files changed

+283
-213
lines changed

examples/publish-ci/cra4/src/mocks/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const handlers = [
3434
}),
3535

3636
rest.get('/posts/:id', (req, res, ctx) => {
37-
const { id } = req.params as { id: string }
37+
const { id: idParam } = req.params as { id: string }
38+
const id = parseInt(idParam, 10)
3839
state = adapter.updateOne(state, {
3940
id,
4041
changes: { fetched_at: new Date().toUTCString() },

examples/publish-ci/cra5/src/mocks/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const handlers = [
3434
}),
3535

3636
rest.get('/posts/:id', (req, res, ctx) => {
37-
const { id } = req.params as { id: string }
37+
const { id: idParam } = req.params as { id: string }
38+
const id = parseInt(idParam, 10)
3839
state = adapter.updateOne(state, {
3940
id,
4041
changes: { fetched_at: new Date().toUTCString() },

examples/publish-ci/next/src/mocks/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const handlers = [
3434
}),
3535

3636
rest.get('/posts/:id', (req, res, ctx) => {
37-
const { id } = req.params as { id: string }
37+
const { id: idParam } = req.params as { id: string }
38+
const id = parseInt(idParam, 10)
3839
state = adapter.updateOne(state, {
3940
id,
4041
changes: { fetched_at: new Date().toUTCString() },

examples/publish-ci/vite/src/mocks/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const handlers = [
3434
}),
3535

3636
rest.get('/posts/:id', (req, res, ctx) => {
37-
const { id } = req.params as { id: string }
37+
const { id: idParam } = req.params as { id: string }
38+
const id = parseInt(idParam, 10)
3839
state = adapter.updateOne(state, {
3940
id,
4041
changes: { fetched_at: new Date().toUTCString() },

examples/query/react/kitchen-sink/src/mocks/handlers.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ export const handlers = [
6969
}),
7070

7171
rest.get('/posts/:id', (req, res, ctx) => {
72-
const { id } = req.params as { id: string };
72+
const { id: idParam } = req.params as { id: string }
73+
const id = parseInt(idParam, 10)
7374
state = adapter.updateOne(state, { id, changes: { fetched_at: new Date().toUTCString() } });
7475
return res(ctx.json(state.entities[id]), ctx.delay(400));
7576
}),
7677

7778
rest.put('/posts/:id', (req, res, ctx) => {
78-
const { id } = req.params as { id: string };
79+
const { id: idParam } = req.params as { id: string }
80+
const id = parseInt(idParam, 10)
7981
const changes = req.body as Partial<Post>;
8082

8183
state = adapter.updateOne(state, { id, changes });
@@ -84,7 +86,8 @@ export const handlers = [
8486
}),
8587

8688
rest.delete('/posts/:id', (req, res, ctx) => {
87-
const { id } = req.params as { id: string };
89+
const { id: idParam } = req.params as { id: string }
90+
const id = parseInt(idParam, 10)
8891

8992
state = adapter.removeOne(state, id);
9093

packages/rtk-query-codegen-openapi/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,5 @@ yalc.lock
137137
lib
138138
yarn.lock
139139
test/tmp/example.ts
140+
test/tmp/emptyApi.ts
141+
test/tmp/out.ts

packages/toolkit/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ Redux Toolkit includes these APIs:
6060
- `configureStore()`: wraps `createStore` to provide simplified configuration options and good defaults. It can automatically combine your slice reducers, add whatever Redux middleware you supply, includes `redux-thunk` by default, and enables use of the Redux DevTools Extension.
6161
- `createReducer()`: lets you supply a lookup table of action types to case reducer functions, rather than writing switch statements. In addition, it automatically uses the [`immer` library](https://github.com/mweststrate/immer) to let you write simpler immutable updates with normal mutative code, like `state.todos[3].completed = true`.
6262
- `createAction()`: generates an action creator function for the given action type string. The function itself has `toString()` defined, so that it can be used in place of the type constant.
63-
- `createSlice()`: combines `createReducer()` + `createAction()`. Accepts an object of reducer functions, a slice name, and an initial state value, and automatically generates a slice reducer with corresponding action creators and action types.
64-
- `createListenerMiddleware()`: lets you define "listener" entries that contain an "effect" callback with additional logic, and a way to specify when that callback should run based on dispatched actions or state changes. A lightweight alternative to Redux async middleware like sagas and observables.
63+
- `createSlice()`: combines `createReducer()` + `createAction()`. Accepts an object of reducer functions, a slice name, and an initial state value, and automatically generates a slice reducer with corresponding action creators and action types.
64+
- `createListenerMiddleware()`: lets you define "listener" entries that contain an "effect" callback with additional logic, and a way to specify when that callback should run based on dispatched actions or state changes. A lightweight alternative to Redux async middleware like sagas and observables.
6565
- `createAsyncThunk()`: accepts an action type string and a function that returns a promise, and generates a thunk that dispatches `pending/resolved/rejected` action types based on that promise
6666
- `createEntityAdapter()`: generates a set of reusable reducers and selectors to manage normalized data in the store
6767
- The `createSelector()` utility from the [Reselect](https://github.com/reduxjs/reselect) library, re-exported for ease of use.

packages/toolkit/src/entities/create_adapter.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,27 @@ import type {
33
Comparer,
44
IdSelector,
55
EntityAdapter,
6+
EntityId,
67
} from './models'
78
import { createInitialStateFactory } from './entity_state'
89
import { createSelectorsFactory } from './state_selectors'
910
import { createSortedStateAdapter } from './sorted_state_adapter'
1011
import { createUnsortedStateAdapter } from './unsorted_state_adapter'
1112

13+
export interface EntityAdapterOptions<T, Id extends EntityId> {
14+
selectId?: IdSelector<T, Id>
15+
sortComparer?: false | Comparer<T>
16+
}
17+
18+
export function createEntityAdapter<T, Id extends EntityId>(options: {
19+
selectId: IdSelector<T, Id>
20+
sortComparer?: false | Comparer<T>
21+
}): EntityAdapter<T, Id>
22+
23+
export function createEntityAdapter<T extends { id: EntityId }>(options?: {
24+
sortComparer?: false | Comparer<T>
25+
}): EntityAdapter<T, T['id']>
26+
1227
/**
1328
*
1429
* @param options
@@ -17,18 +32,18 @@ import { createUnsortedStateAdapter } from './unsorted_state_adapter'
1732
*/
1833
export function createEntityAdapter<T>(
1934
options: {
20-
selectId?: IdSelector<T>
35+
selectId?: IdSelector<T, EntityId>
2136
sortComparer?: false | Comparer<T>
2237
} = {}
23-
): EntityAdapter<T> {
24-
const { selectId, sortComparer }: EntityDefinition<T> = {
38+
): EntityAdapter<T, EntityId> {
39+
const { selectId, sortComparer }: EntityDefinition<T, EntityId> = {
2540
sortComparer: false,
2641
selectId: (instance: any) => instance.id,
2742
...options,
2843
}
2944

30-
const stateFactory = createInitialStateFactory<T>()
31-
const selectorsFactory = createSelectorsFactory<T>()
45+
const stateFactory = createInitialStateFactory<T, EntityId>()
46+
const selectorsFactory = createSelectorsFactory<T, EntityId>()
3247
const stateAdapter = sortComparer
3348
? createSortedStateAdapter(selectId, sortComparer)
3449
: createUnsortedStateAdapter(selectId)

packages/toolkit/src/entities/entity_state.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import type { EntityState } from './models'
1+
import type { EntityId, EntityState } from './models'
22

3-
export function getInitialEntityState<V>(): EntityState<V> {
3+
export function getInitialEntityState<T, Id extends EntityId>(): EntityState<
4+
T,
5+
Id
6+
> {
47
return {
58
ids: [],
69
entities: {},
710
}
811
}
912

10-
export function createInitialStateFactory<V>() {
11-
function getInitialState(): EntityState<V>
13+
export function createInitialStateFactory<T, Id extends EntityId>() {
14+
function getInitialState(): EntityState<T, Id>
1215
function getInitialState<S extends object>(
1316
additionalState: S
14-
): EntityState<V> & S
17+
): EntityState<T, Id> & S
1518
function getInitialState(additionalState: any = {}): any {
1619
return Object.assign(getInitialEntityState(), additionalState)
1720
}

0 commit comments

Comments
 (0)