Skip to content

Commit

Permalink
enhance: Remove _TYPE suffix from actionTypes (#3244)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker authored Oct 13, 2024
1 parent 43a955c commit 109c922
Show file tree
Hide file tree
Showing 37 changed files with 267 additions and 169 deletions.
6 changes: 6 additions & 0 deletions .changeset/dry-carrots-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@data-client/test': patch
---

Support [actionTypes](https://dataclient.io/docs/api/Actions) without \_TYPE suffix

48 changes: 48 additions & 0 deletions .changeset/nasty-roses-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
'@data-client/react': patch
'@data-client/core': patch
---

Add [actionTypes](https://dataclient.io/docs/api/Actions) without \_TYPE suffix

(Not breaking - we keep the old actionTypes name as well.)

```ts title="Before"
import type { Manager, Middleware } from '@data-client/react';
import { actionTypes } from '@data-client/react';

export default class LoggingManager implements Manager {
middleware: Middleware = controller => next => async action => {
switch (action.type) {
case actionTypes.SET_RESPONSE_TYPE:
console.info(
`${action.endpoint.name} ${JSON.stringify(action.response)}`,
);
default:
return next(action);
}
};

cleanup() {}
}
```

```ts title="After"
import type { Manager, Middleware } from '@data-client/react';
import { actionTypes } from '@data-client/react';

export default class LoggingManager implements Manager {
middleware: Middleware = controller => next => async action => {
switch (action.type) {
case actionTypes.SET_RESPONSE:
console.info(
`${action.endpoint.name} ${JSON.stringify(action.response)}`,
);
default:
return next(action);
}
};

cleanup() {}
}
```
18 changes: 9 additions & 9 deletions docs/core/api/Actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface FetchMeta {
}

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

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

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

```ts
interface ResetAction {
type: typeof RESET_TYPE;
type: typeof actionTypes.RESET;
date: number;
}
```
Expand All @@ -86,7 +86,7 @@ Comes from [Controller.resetEntireStore()](./Controller.md#resetEntireStore)

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

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

```ts
interface InvalidateAction {
type: typeof INVALIDATE_TYPE;
type: typeof actionTypes.INVALIDATE;
key: string;
}
```
Expand All @@ -123,7 +123,7 @@ Comes from [Controller.invalidate()](./Controller.md#invalidate)

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

```ts
interface ExpireAllAction {
type: typeof EXPIREALL_TYPE;
type: typeof actionTypes.EXPIREALL;
testKey: (key: string) => boolean;
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/core/api/Controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ import type { EndpointInterface } from '@data-client/endpoint';
export default class MyManager implements Manager {
middleware: Middleware = controller => {
return next => async action => {
if (action.type === actionTypes.FETCH_TYPE) {
if (action.type === actionTypes.FETCH) {
console.log('The existing response of the requested fetch');
console.log(
controller.getResponse(
Expand Down
12 changes: 6 additions & 6 deletions docs/core/api/Manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ import { actionTypes } from '@data-client/react';
export default class LoggingManager implements Manager {
middleware: Middleware = controller => next => async action => {
switch (action.type) {
case actionTypes.SET_RESPONSE_TYPE:
case actionTypes.SET_RESPONSE:
if (action.endpoint.sideEffect) {
console.info(
`${action.endpoint.name} ${JSON.stringify(action.response)}`,
Expand Down Expand Up @@ -326,12 +326,12 @@ export default class CustomSubsManager implements Manager {

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

Types: `FETCH_TYPE`, `SET_TYPE`, `SET_RESPONSE_TYPE`, `RESET_TYPE`, `SUBSCRIBE_TYPE`,
`UNSUBSCRIBE_TYPE`, `INVALIDATE_TYPE`, `INVALIDATEALL_TYPE`, `EXPIREALL_TYPE`
Types: `FETCH`, `SET`, `SET_RESPONSE`, `RESET`, `SUBSCRIBE`,
`UNSUBSCRIBE`, `INVALIDATE`, `INVALIDATEALL`, `EXPIREALL`
2 changes: 1 addition & 1 deletion docs/core/api/NetworkManager.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Default:

```ts
skipLogging(action: ActionTypes) {
return action.type === FETCH_TYPE && action.meta.key in this.fetched;
return action.type === FETCH && action.meta.key in this.fetched;
}
```

Expand Down
3 changes: 1 addition & 2 deletions docs/core/concepts/managers.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ we can maintain fresh data when the data updates are independent of user action.
price, or a real-time collaborative editor.

```typescript
import type { Manager, Middleware, ActionTypes } from '@data-client/react';
import { Controller, actionTypes } from '@data-client/react';
import { type Manager, type Middleware, Controller } from '@data-client/react';
import type { Entity } from '@data-client/rest';

export default class StreamManager implements Manager {
Expand Down
34 changes: 23 additions & 11 deletions packages/core/src/actionTypes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
export const FETCH_TYPE = 'rdc/fetch' as const;
export const SET_TYPE = 'rdc/set' as const;
export const SET_RESPONSE_TYPE = 'rdc/setresponse' as const;
export const OPTIMISTIC_TYPE = 'rdc/optimistic' as const;
export const RESET_TYPE = 'rdc/reset' as const;
export const SUBSCRIBE_TYPE = 'rdc/subscribe' as const;
export const UNSUBSCRIBE_TYPE = 'rdc/unsubscribe' as const;
export const INVALIDATE_TYPE = 'rdc/invalidate' as const;
export const INVALIDATEALL_TYPE = 'rdc/invalidateall' as const;
export const EXPIREALL_TYPE = 'rdc/expireall' as const;
export const GC_TYPE = 'rdc/gc' as const;
export const FETCH = 'rdc/fetch' as const;
export const SET = 'rdc/set' as const;
export const SET_RESPONSE = 'rdc/setresponse' as const;
export const OPTIMISTIC = 'rdc/optimistic' as const;
export const RESET = 'rdc/reset' as const;
export const SUBSCRIBE = 'rdc/subscribe' as const;
export const UNSUBSCRIBE = 'rdc/unsubscribe' as const;
export const INVALIDATE = 'rdc/invalidate' as const;
export const INVALIDATEALL = 'rdc/invalidateall' as const;
export const EXPIREALL = 'rdc/expireall' as const;
export const GC = 'rdc/gc' as const;

export const FETCH_TYPE = FETCH;
export const SET_TYPE = SET;
export const SET_RESPONSE_TYPE = SET_RESPONSE;
export const OPTIMISTIC_TYPE = OPTIMISTIC;
export const RESET_TYPE = RESET;
export const SUBSCRIBE_TYPE = SUBSCRIBE;
export const UNSUBSCRIBE_TYPE = UNSUBSCRIBE;
export const INVALIDATE_TYPE = INVALIDATE;
export const INVALIDATEALL_TYPE = INVALIDATEALL;
export const EXPIREALL_TYPE = EXPIREALL;
export const GC_TYPE = GC;
44 changes: 22 additions & 22 deletions packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import type {
} from '@data-client/normalizr';

import type {
SET_TYPE,
RESET_TYPE,
FETCH_TYPE,
SUBSCRIBE_TYPE,
UNSUBSCRIBE_TYPE,
INVALIDATE_TYPE,
GC_TYPE,
OPTIMISTIC_TYPE,
INVALIDATEALL_TYPE,
EXPIREALL_TYPE,
SET_RESPONSE_TYPE,
SET,
RESET,
FETCH,
SUBSCRIBE,
UNSUBSCRIBE,
INVALIDATE,
GC,
OPTIMISTIC,
INVALIDATEALL,
EXPIREALL,
SET_RESPONSE,
} from './actionTypes.js';
import type { EndpointUpdateFunction } from './controller/types.js';

Expand All @@ -37,7 +37,7 @@ export interface ActionMeta {

/** Action for Controller.set() */
export interface SetAction<S extends Queryable = any> {
type: typeof SET_TYPE;
type: typeof SET;
schema: S;
args: readonly any[];
meta: ActionMeta;
Expand All @@ -48,7 +48,7 @@ export interface SetAction<S extends Queryable = any> {
export interface SetResponseActionBase<
E extends EndpointAndUpdate<E> = EndpointDefault,
> {
type: typeof SET_RESPONSE_TYPE;
type: typeof SET_RESPONSE;
endpoint: E;
args: readonly any[];
key: string;
Expand Down Expand Up @@ -81,7 +81,7 @@ export interface FetchMeta {

/** Action for Controller.fetch() */
export interface FetchAction<E extends EndpointAndUpdate<E> = EndpointDefault> {
type: typeof FETCH_TYPE;
type: typeof FETCH;
endpoint: E;
args: readonly [...Parameters<E>];
key: string;
Expand All @@ -93,7 +93,7 @@ export interface FetchAction<E extends EndpointAndUpdate<E> = EndpointDefault> {
export interface OptimisticAction<
E extends EndpointAndUpdate<E> = EndpointDefault,
> {
type: typeof OPTIMISTIC_TYPE;
type: typeof OPTIMISTIC;
endpoint: E;
args: readonly any[];
key: string;
Expand All @@ -106,7 +106,7 @@ export interface OptimisticAction<
export interface SubscribeAction<
E extends EndpointAndUpdate<E> = EndpointDefault,
> {
type: typeof SUBSCRIBE_TYPE;
type: typeof SUBSCRIBE;
endpoint: E;
args: readonly any[];
key: string;
Expand All @@ -116,38 +116,38 @@ export interface SubscribeAction<
export interface UnsubscribeAction<
E extends EndpointAndUpdate<E> = EndpointDefault,
> {
type: typeof UNSUBSCRIBE_TYPE;
type: typeof UNSUBSCRIBE;
endpoint: E;
args: readonly any[];
key: string;
}

/* EXPIRY */
export interface ExpireAllAction {
type: typeof EXPIREALL_TYPE;
type: typeof EXPIREALL;
testKey: (key: string) => boolean;
}

/* INVALIDATE */
export interface InvalidateAllAction {
type: typeof INVALIDATEALL_TYPE;
type: typeof INVALIDATEALL;
testKey: (key: string) => boolean;
}

export interface InvalidateAction {
type: typeof INVALIDATE_TYPE;
type: typeof INVALIDATE;
key: string;
}

/* RESET */
export interface ResetAction {
type: typeof RESET_TYPE;
type: typeof RESET;
date: number;
}

/* GC */
export interface GCAction {
type: typeof GC_TYPE;
type: typeof GC;
entities: [string, string][];
endpoints: string[];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/controller/actions/createExpireAll.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { EXPIREALL_TYPE } from '../../actionTypes.js';
import { EXPIREALL } from '../../actionTypes.js';
import type { ExpireAllAction } from '../../types.js';

export function createExpireAll(
testKey: (key: string) => boolean,
): ExpireAllAction {
return {
type: EXPIREALL_TYPE,
type: EXPIREALL,
testKey,
};
}
4 changes: 2 additions & 2 deletions packages/core/src/controller/actions/createFetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EndpointInterface, NI } from '@data-client/normalizr';

import { FETCH_TYPE } from '../../actionTypes.js';
import { FETCH } from '../../actionTypes.js';
import type { FetchAction, FetchMeta } from '../../types.js';
import { EndpointUpdateFunction } from '../types.js';

Expand All @@ -26,7 +26,7 @@ export function createFetch<
};

return {
type: FETCH_TYPE,
type: FETCH,
key: endpoint.key(...args),
args,
endpoint,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/controller/actions/createInvalidate.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { EndpointInterface } from '@data-client/normalizr';

import { INVALIDATE_TYPE } from '../../actionTypes.js';
import { INVALIDATE } from '../../actionTypes.js';
import type { InvalidateAction } from '../../types.js';

export function createInvalidate<E extends EndpointInterface>(
endpoint: E,
{ args }: { args: readonly [...Parameters<E>] },
): InvalidateAction {
return {
type: INVALIDATE_TYPE,
type: INVALIDATE,
key: endpoint.key(...args),
};
}
Loading

0 comments on commit 109c922

Please sign in to comment.