Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly type actions #4105

Merged
merged 3 commits into from
Nov 26, 2023
Merged

Properly type actions #4105

merged 3 commits into from
Nov 26, 2023

Conversation

ivarnakken
Copy link
Member

Description

Properly type the callAPI and promise middleware

It is now possible to type API request!


Type announcement, article, comment and company actions

Result

No visual changes except this tiny fix:

Before After
image image

Testing

  • I have thoroughly tested my changes.

Tested all actions that were typed, even though that should not have broken anything.

@ivarnakken ivarnakken added types Pull requests that improve or fix types review-needed Pull requests that need review labels Aug 26, 2023
@ivarnakken ivarnakken requested a review from a team August 26, 2023 14:42
@ivarnakken ivarnakken self-assigned this Aug 26, 2023
@linear
Copy link

linear bot commented Aug 26, 2023

ABA-541 Type the call api

It works. Almost done 😬

@ivarnakken ivarnakken requested a review from eikhr September 2, 2023 12:09
@eikhr
Copy link
Member

eikhr commented Sep 5, 2023

Hmm, there's something strange here. It seems like the provided T to callApi gets used to set the type of action.type, which is a string id (ish) for that specific action. But in the typed actions you provide the entity type fetched? This seems unintentional.

Also I think the Action-type you have changed here is used for other things beside just API-calls, meaning the types will be wrong. A new API-call specific action-type would probably be a good idea

@ivarnakken ivarnakken added the technical-debt Pull requests that reduces technical debt label Oct 12, 2023
@ivarnakken
Copy link
Member Author

Hmm, there's something strange here. It seems like the provided T to callApi gets used to set the type of action.type, which is a string id (ish) for that specific action. But in the typed actions you provide the entity type fetched? This seems unintentional.

Yes, that's strange. The provided T is the type of the entities returned. It should be typed like this:

export type Action<T = unknown> = {
  type: unknown;
  payload?: {
    actionGrant?: ActionGrant;
    entities?: T;
    next?: string;
    previous?: string;
    result?: ID;
  };
};

Also I think the Action-type you have changed here is used for other things beside just API-calls, meaning the types will be wrong. A new API-call specific action-type would probably be a good idea

Not sure if I'm following you here. The Action type is only used to type actions?

@eikhr
Copy link
Member

eikhr commented Oct 18, 2023

Not sure if I'm following you here. The Action type is only used to type actions?

Yes, but not only API-call actions. It is f.ex. used in ToastActions.ts for the removeToast action, which has a payload consisting of only an id. This doesn't fit your new Action type, but did fit the previous one which allowed for any payload.

I'm just saying you should take your new type, call it f.ex. APIAction and use it for all the API-actions, while letting other types of actions keep on using the old type.

@ivarnakken
Copy link
Member Author

Not sure if I'm following you here. The Action type is only used to type actions?

Yes, but not only API-call actions. It is f.ex. used in ToastActions.ts for the removeToast action, which has a payload consisting of only an id. This doesn't fit your new Action type, but did fit the previous one which allowed for any payload.

I'm just saying you should take your new type, call it f.ex. APIAction and use it for all the API-actions, while letting other types of actions keep on using the old type.

Yes but all actions are currently wrong and have to be refactored.

This PR just makes it possible to actually type them

@eikhr
Copy link
Member

eikhr commented Oct 18, 2023

Not sure if I'm following you here. The Action type is only used to type actions?

Yes, but not only API-call actions. It is f.ex. used in ToastActions.ts for the removeToast action, which has a payload consisting of only an id. This doesn't fit your new Action type, but did fit the previous one which allowed for any payload.
I'm just saying you should take your new type, call it f.ex. APIAction and use it for all the API-actions, while letting other types of actions keep on using the old type.

Yes but all actions are currently wrong and have to be refactored.

This PR just makes it possible to actually type them

The deleteToast action is perfectly fine? Why would it need entities in it, it simply removes the toast with the given id from the redux state

@eikhr
Copy link
Member

eikhr commented Oct 18, 2023

Hmm, there's something strange here. It seems like the provided T to callApi gets used to set the type of action.type, which is a string id (ish) for that specific action. But in the typed actions you provide the entity type fetched? This seems unintentional.

Yes, that's strange. The provided T is the type of the entities returned. It should be typed like this:

export type Action<T = unknown> = {
  type: unknown;
  payload?: {
    actionGrant?: ActionGrant;
    entities?: T;
    next?: string;
    previous?: string;
    result?: ID;
  };
};

Regarding this part, I think you may have forgotten to push the latest changes? In the diff right now the action type looks like this:

export type Action<T = unknown> = {
  type: T;
  payload?: {
    actionGrant?: ActionGrant;
    entities?: Record<string, unknown>;
    next?: string;
    previous?: string;
    result?: ID;
  };
};

It should be more like what you posted over, except that entities is actually a map of entity type names and a map of entityId to the entity (this is because it is run through normalizr):
Screenshot 2023-10-18 at 16 31 12

It you look in app/store/models/entities.ts I have actually created a helper type for this. NormalizedEntityPayload<EntityKeys> will give you the type of the entities-field containing all the entity types specified in EntityKeys.

@ivarnakken ivarnakken force-pushed the ivarnakken/aba-541-type-the-call-api branch from 6996a21 to d5c108e Compare October 26, 2023 16:05
@eikhr eikhr force-pushed the ivarnakken/aba-541-type-the-call-api branch 3 times, most recently from 96b7c94 to d1d28d5 Compare November 25, 2023 22:34
@eikhr eikhr force-pushed the ivarnakken/aba-541-type-the-call-api branch 5 times, most recently from 43b3288 to 7d11555 Compare November 25, 2023 22:57
It is now possible to type API request!

Co-authored-by: Eik Hvattum Røgeberg <[email protected]>
@eikhr eikhr force-pushed the ivarnakken/aba-541-type-the-call-api branch 2 times, most recently from 504d6aa to 17aca50 Compare November 26, 2023 00:52
@eikhr
Copy link
Member

eikhr commented Nov 26, 2023

I think this works well now! I suggest we merge this, and then add types to the remaining actions as they are needed. They will default to unknown.

@eikhr eikhr force-pushed the ivarnakken/aba-541-type-the-call-api branch from 17aca50 to d3e71b2 Compare November 26, 2023 00:56
Copy link
Member

@eikhr eikhr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good now!
But maybe someone else should review as well, as I wrote quite a bit of it:)

@eikhr eikhr requested review from a team and ollfkaih November 26, 2023 13:43
Copy link
Contributor

@ollfkaih ollfkaih left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eikhr eikhr merged commit e6e720f into master Nov 26, 2023
2 checks passed
@eikhr eikhr deleted the ivarnakken/aba-541-type-the-call-api branch November 26, 2023 17:11
@ivarnakken ivarnakken mentioned this pull request Feb 4, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review-needed Pull requests that need review technical-debt Pull requests that reduces technical debt types Pull requests that improve or fix types
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants