Skip to content

Commit 109c922

Browse files
authored
enhance: Remove _TYPE suffix from actionTypes (#3244)
1 parent 43a955c commit 109c922

37 files changed

+267
-169
lines changed

.changeset/dry-carrots-hammer.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@data-client/test': patch
3+
---
4+
5+
Support [actionTypes](https://dataclient.io/docs/api/Actions) without \_TYPE suffix
6+

.changeset/nasty-roses-ring.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
'@data-client/react': patch
3+
'@data-client/core': patch
4+
---
5+
6+
Add [actionTypes](https://dataclient.io/docs/api/Actions) without \_TYPE suffix
7+
8+
(Not breaking - we keep the old actionTypes name as well.)
9+
10+
```ts title="Before"
11+
import type { Manager, Middleware } from '@data-client/react';
12+
import { actionTypes } from '@data-client/react';
13+
14+
export default class LoggingManager implements Manager {
15+
middleware: Middleware = controller => next => async action => {
16+
switch (action.type) {
17+
case actionTypes.SET_RESPONSE_TYPE:
18+
console.info(
19+
`${action.endpoint.name} ${JSON.stringify(action.response)}`,
20+
);
21+
default:
22+
return next(action);
23+
}
24+
};
25+
26+
cleanup() {}
27+
}
28+
```
29+
30+
```ts title="After"
31+
import type { Manager, Middleware } from '@data-client/react';
32+
import { actionTypes } from '@data-client/react';
33+
34+
export default class LoggingManager implements Manager {
35+
middleware: Middleware = controller => next => async action => {
36+
switch (action.type) {
37+
case actionTypes.SET_RESPONSE:
38+
console.info(
39+
`${action.endpoint.name} ${JSON.stringify(action.response)}`,
40+
);
41+
default:
42+
return next(action);
43+
}
44+
};
45+
46+
cleanup() {}
47+
}
48+
```

docs/core/api/Actions.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface FetchMeta {
3030
}
3131

3232
interface FetchAction {
33-
type: typeof FETCH_TYPE;
33+
type: typeof actionTypes.FETCH;
3434
endpoint: Endpoint;
3535
args: readonly [...Parameters<Endpoint>];
3636
key: string;
@@ -45,7 +45,7 @@ Comes from [Controller.fetch()](./Controller.md#fetch), [Controller.fetchIfStale
4545

4646
```ts
4747
interface SetAction {
48-
type: typeof SET_TYPE;
48+
type: typeof actionTypes.SET;
4949
schema: Queryable;
5050
args: readonly any[];
5151
meta: ActionMeta;
@@ -59,7 +59,7 @@ Comes from [Controller.set()](./Controller.md#set)
5959

6060
```ts
6161
interface SetResponseAction {
62-
type: typeof SET_RESPONSE_TYPE;
62+
type: typeof actionTypes.SET_RESPONSE;
6363
endpoint: Endpoint;
6464
args: readonly any[];
6565
key: string;
@@ -75,7 +75,7 @@ Comes from [Controller.setResponse()](./Controller.md#setResponse), [NetworkMana
7575

7676
```ts
7777
interface ResetAction {
78-
type: typeof RESET_TYPE;
78+
type: typeof actionTypes.RESET;
7979
date: number;
8080
}
8181
```
@@ -86,7 +86,7 @@ Comes from [Controller.resetEntireStore()](./Controller.md#resetEntireStore)
8686

8787
```ts
8888
interface SubscribeAction {
89-
type: typeof SUBSCRIBE_TYPE;
89+
type: typeof actionTypes.SUBSCRIBE;
9090
endpoint: Endpoint;
9191
args: readonly any[];
9292
key: string;
@@ -99,7 +99,7 @@ Comes from [Controller.subscribe()](./Controller.md#subscribe), [useSubscription
9999

100100
```ts
101101
interface UnsubscribeAction {
102-
type: typeof UNSUBSCRIBE_TYPE;
102+
type: typeof actionTypes.UNSUBSCRIBE;
103103
endpoint: Endpoint;
104104
args: readonly any[];
105105
key: string;
@@ -112,7 +112,7 @@ Comes from [Controller.unsubscribe()](./Controller.md#unsubscribe), [useSubscrip
112112

113113
```ts
114114
interface InvalidateAction {
115-
type: typeof INVALIDATE_TYPE;
115+
type: typeof actionTypes.INVALIDATE;
116116
key: string;
117117
}
118118
```
@@ -123,7 +123,7 @@ Comes from [Controller.invalidate()](./Controller.md#invalidate)
123123

124124
```ts
125125
interface InvalidateAllAction {
126-
type: typeof INVALIDATEALL_TYPE;
126+
type: typeof actionTypes.INVALIDATEALL;
127127
testKey: (key: string) => boolean;
128128
}
129129
```
@@ -134,7 +134,7 @@ Comes from [Controller.invalidateAll()](./Controller.md#invalidateAll)
134134

135135
```ts
136136
interface ExpireAllAction {
137-
type: typeof EXPIREALL_TYPE;
137+
type: typeof actionTypes.EXPIREALL;
138138
testKey: (key: string) => boolean;
139139
}
140140
```

docs/core/api/Controller.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ import type { EndpointInterface } from '@data-client/endpoint';
556556
export default class MyManager implements Manager {
557557
middleware: Middleware = controller => {
558558
return next => async action => {
559-
if (action.type === actionTypes.FETCH_TYPE) {
559+
if (action.type === actionTypes.FETCH) {
560560
console.log('The existing response of the requested fetch');
561561
console.log(
562562
controller.getResponse(

docs/core/api/Manager.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ import { actionTypes } from '@data-client/react';
272272
export default class LoggingManager implements Manager {
273273
middleware: Middleware = controller => next => async action => {
274274
switch (action.type) {
275-
case actionTypes.SET_RESPONSE_TYPE:
275+
case actionTypes.SET_RESPONSE:
276276
if (action.endpoint.sideEffect) {
277277
console.info(
278278
`${action.endpoint.name} ${JSON.stringify(action.response)}`,
@@ -326,12 +326,12 @@ export default class CustomSubsManager implements Manager {
326326

327327
middleware: Middleware = controller => next => async action => {
328328
switch (action.type) {
329-
case actionTypes.SUBSCRIBE_TYPE:
330-
case actionTypes.UNSUBSCRIBE_TYPE:
329+
case actionTypes.SUBSCRIBE:
330+
case actionTypes.UNSUBSCRIBE:
331331
const { schema } = action.endpoint;
332332
// only process registered entities
333333
if (schema && isEntity(schema) && schema.key in this.entities) {
334-
if (action.type === actionTypes.SUBSCRIBE_TYPE) {
334+
if (action.type === actionTypes.SUBSCRIBE) {
335335
this.subscribe(schema.key, action.args[0]?.product_id);
336336
} else {
337337
this.unsubscribe(schema.key, action.args[0]?.product_id);
@@ -357,5 +357,5 @@ export default class CustomSubsManager implements Manager {
357357
By `return Promise.resolve();` instead of calling `next(action)`, we prevent managers listed
358358
after this one from seeing that [action](./Actions.md).
359359

360-
Types: `FETCH_TYPE`, `SET_TYPE`, `SET_RESPONSE_TYPE`, `RESET_TYPE`, `SUBSCRIBE_TYPE`,
361-
`UNSUBSCRIBE_TYPE`, `INVALIDATE_TYPE`, `INVALIDATEALL_TYPE`, `EXPIREALL_TYPE`
360+
Types: `FETCH`, `SET`, `SET_RESPONSE`, `RESET`, `SUBSCRIBE`,
361+
`UNSUBSCRIBE`, `INVALIDATE`, `INVALIDATEALL`, `EXPIREALL`

docs/core/api/NetworkManager.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Default:
5050

5151
```ts
5252
skipLogging(action: ActionTypes) {
53-
return action.type === FETCH_TYPE && action.meta.key in this.fetched;
53+
return action.type === FETCH && action.meta.key in this.fetched;
5454
}
5555
```
5656

docs/core/concepts/managers.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ we can maintain fresh data when the data updates are independent of user action.
7878
price, or a real-time collaborative editor.
7979

8080
```typescript
81-
import type { Manager, Middleware, ActionTypes } from '@data-client/react';
82-
import { Controller, actionTypes } from '@data-client/react';
81+
import { type Manager, type Middleware, Controller } from '@data-client/react';
8382
import type { Entity } from '@data-client/rest';
8483

8584
export default class StreamManager implements Manager {

packages/core/src/actionTypes.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
export const FETCH_TYPE = 'rdc/fetch' as const;
2-
export const SET_TYPE = 'rdc/set' as const;
3-
export const SET_RESPONSE_TYPE = 'rdc/setresponse' as const;
4-
export const OPTIMISTIC_TYPE = 'rdc/optimistic' as const;
5-
export const RESET_TYPE = 'rdc/reset' as const;
6-
export const SUBSCRIBE_TYPE = 'rdc/subscribe' as const;
7-
export const UNSUBSCRIBE_TYPE = 'rdc/unsubscribe' as const;
8-
export const INVALIDATE_TYPE = 'rdc/invalidate' as const;
9-
export const INVALIDATEALL_TYPE = 'rdc/invalidateall' as const;
10-
export const EXPIREALL_TYPE = 'rdc/expireall' as const;
11-
export const GC_TYPE = 'rdc/gc' as const;
1+
export const FETCH = 'rdc/fetch' as const;
2+
export const SET = 'rdc/set' as const;
3+
export const SET_RESPONSE = 'rdc/setresponse' as const;
4+
export const OPTIMISTIC = 'rdc/optimistic' as const;
5+
export const RESET = 'rdc/reset' as const;
6+
export const SUBSCRIBE = 'rdc/subscribe' as const;
7+
export const UNSUBSCRIBE = 'rdc/unsubscribe' as const;
8+
export const INVALIDATE = 'rdc/invalidate' as const;
9+
export const INVALIDATEALL = 'rdc/invalidateall' as const;
10+
export const EXPIREALL = 'rdc/expireall' as const;
11+
export const GC = 'rdc/gc' as const;
12+
13+
export const FETCH_TYPE = FETCH;
14+
export const SET_TYPE = SET;
15+
export const SET_RESPONSE_TYPE = SET_RESPONSE;
16+
export const OPTIMISTIC_TYPE = OPTIMISTIC;
17+
export const RESET_TYPE = RESET;
18+
export const SUBSCRIBE_TYPE = SUBSCRIBE;
19+
export const UNSUBSCRIBE_TYPE = UNSUBSCRIBE;
20+
export const INVALIDATE_TYPE = INVALIDATE;
21+
export const INVALIDATEALL_TYPE = INVALIDATEALL;
22+
export const EXPIREALL_TYPE = EXPIREALL;
23+
export const GC_TYPE = GC;

packages/core/src/actions.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import type {
77
} from '@data-client/normalizr';
88

99
import type {
10-
SET_TYPE,
11-
RESET_TYPE,
12-
FETCH_TYPE,
13-
SUBSCRIBE_TYPE,
14-
UNSUBSCRIBE_TYPE,
15-
INVALIDATE_TYPE,
16-
GC_TYPE,
17-
OPTIMISTIC_TYPE,
18-
INVALIDATEALL_TYPE,
19-
EXPIREALL_TYPE,
20-
SET_RESPONSE_TYPE,
10+
SET,
11+
RESET,
12+
FETCH,
13+
SUBSCRIBE,
14+
UNSUBSCRIBE,
15+
INVALIDATE,
16+
GC,
17+
OPTIMISTIC,
18+
INVALIDATEALL,
19+
EXPIREALL,
20+
SET_RESPONSE,
2121
} from './actionTypes.js';
2222
import type { EndpointUpdateFunction } from './controller/types.js';
2323

@@ -37,7 +37,7 @@ export interface ActionMeta {
3737

3838
/** Action for Controller.set() */
3939
export interface SetAction<S extends Queryable = any> {
40-
type: typeof SET_TYPE;
40+
type: typeof SET;
4141
schema: S;
4242
args: readonly any[];
4343
meta: ActionMeta;
@@ -48,7 +48,7 @@ export interface SetAction<S extends Queryable = any> {
4848
export interface SetResponseActionBase<
4949
E extends EndpointAndUpdate<E> = EndpointDefault,
5050
> {
51-
type: typeof SET_RESPONSE_TYPE;
51+
type: typeof SET_RESPONSE;
5252
endpoint: E;
5353
args: readonly any[];
5454
key: string;
@@ -81,7 +81,7 @@ export interface FetchMeta {
8181

8282
/** Action for Controller.fetch() */
8383
export interface FetchAction<E extends EndpointAndUpdate<E> = EndpointDefault> {
84-
type: typeof FETCH_TYPE;
84+
type: typeof FETCH;
8585
endpoint: E;
8686
args: readonly [...Parameters<E>];
8787
key: string;
@@ -93,7 +93,7 @@ export interface FetchAction<E extends EndpointAndUpdate<E> = EndpointDefault> {
9393
export interface OptimisticAction<
9494
E extends EndpointAndUpdate<E> = EndpointDefault,
9595
> {
96-
type: typeof OPTIMISTIC_TYPE;
96+
type: typeof OPTIMISTIC;
9797
endpoint: E;
9898
args: readonly any[];
9999
key: string;
@@ -106,7 +106,7 @@ export interface OptimisticAction<
106106
export interface SubscribeAction<
107107
E extends EndpointAndUpdate<E> = EndpointDefault,
108108
> {
109-
type: typeof SUBSCRIBE_TYPE;
109+
type: typeof SUBSCRIBE;
110110
endpoint: E;
111111
args: readonly any[];
112112
key: string;
@@ -116,38 +116,38 @@ export interface SubscribeAction<
116116
export interface UnsubscribeAction<
117117
E extends EndpointAndUpdate<E> = EndpointDefault,
118118
> {
119-
type: typeof UNSUBSCRIBE_TYPE;
119+
type: typeof UNSUBSCRIBE;
120120
endpoint: E;
121121
args: readonly any[];
122122
key: string;
123123
}
124124

125125
/* EXPIRY */
126126
export interface ExpireAllAction {
127-
type: typeof EXPIREALL_TYPE;
127+
type: typeof EXPIREALL;
128128
testKey: (key: string) => boolean;
129129
}
130130

131131
/* INVALIDATE */
132132
export interface InvalidateAllAction {
133-
type: typeof INVALIDATEALL_TYPE;
133+
type: typeof INVALIDATEALL;
134134
testKey: (key: string) => boolean;
135135
}
136136

137137
export interface InvalidateAction {
138-
type: typeof INVALIDATE_TYPE;
138+
type: typeof INVALIDATE;
139139
key: string;
140140
}
141141

142142
/* RESET */
143143
export interface ResetAction {
144-
type: typeof RESET_TYPE;
144+
type: typeof RESET;
145145
date: number;
146146
}
147147

148148
/* GC */
149149
export interface GCAction {
150-
type: typeof GC_TYPE;
150+
type: typeof GC;
151151
entities: [string, string][];
152152
endpoints: string[];
153153
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { EXPIREALL_TYPE } from '../../actionTypes.js';
1+
import { EXPIREALL } from '../../actionTypes.js';
22
import type { ExpireAllAction } from '../../types.js';
33

44
export function createExpireAll(
55
testKey: (key: string) => boolean,
66
): ExpireAllAction {
77
return {
8-
type: EXPIREALL_TYPE,
8+
type: EXPIREALL,
99
testKey,
1010
};
1111
}

0 commit comments

Comments
 (0)