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

feat(Simulations): narrow down simulation payload types #52

Merged
merged 4 commits into from
Oct 9, 2024

Conversation

danbillson
Copy link
Contributor

@danbillson danbillson commented Oct 9, 2024

Based on some recommendations after creating #51 it is a much more common pattern in this lib to use discriminated unions for this use case, originally this would have look like this:

import {
  type AddressCreatedEvent,
  type AddressImportedEvent,
  ...
} from '../../notifications/events';

export type ISimulationResponse =
  | IAddressCreatedSimulationResponse
  | IAddressUpdatedSimulationResponse
  | ...

interface BaseSimulationResponse {
  id: string;
  status: Status;
  notification_setting_id: string;
  name: string;
  last_run_at?: string | null;
  created_at: string;
  updated_at: string;
}

export interface IAddressCreatedSimulationResponse extends BaseSimulationResponse {
  type: 'address.created';
  payload?: Partial<AddressCreatedEvent['data']> | null;
}

export interface IAddressUpdatedSimulationResponse extends BaseSimulationResponse {
  type: 'address.updated';
  payload?: Partial<AddressUpdatedEvent['data']> | null;
}

...

naturally this ended up being quite exhaustive when required for ISimulationResponse, CreateSimulationRequestBody and UpdateSimulationRequestBody so this looks at an intermediary by using a helper to generate the discriminated union based on a base interface


The main downside to using discriminated unions over generics as looked at in #51 is that there is no way to quickly type the get simulation request other than coercing it

@danbillson danbillson self-assigned this Oct 9, 2024
@danbillson danbillson requested a review from a team as a code owner October 9, 2024 09:39
@danbillson danbillson requested a review from samcolby October 9, 2024 09:39
type TransactionUpdatedEvent,
} from '../../notifications/events';

export interface EventPayloadMap {
Copy link

Choose a reason for hiding this comment

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

Do we need to export this? If so, would SimulationUpdatePayloadMap be clearer?

EventPayloadMap feels quite generic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, doesn't need to be exported, how about SimulationEventPayloadMap?

'report.updated': ReportUpdatedEvent;
}

export type DiscriminatedEventResponse<Base> = {
Copy link

Choose a reason for hiding this comment

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

Similar naming here, should we include Simulation somewhere?

notificationSettingId?: string;
name?: string;
status?: Status;
type?: IEventName | SimulationScenarioType;
Copy link

Choose a reason for hiding this comment

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

Another type here we should reenable.

id: string;
status: Status;
notification_setting_id: string;
name: string;
type: IEventName | SimulationScenarioType;
Copy link

Choose a reason for hiding this comment

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

Maybe another type here?

@danbillson danbillson merged commit 5536e93 into simulations Oct 9, 2024
1 check passed
@danbillson danbillson deleted the union-simulations branch October 9, 2024 12:50
danbillson added a commit that referenced this pull request Oct 14, 2024
* feat(Simulations): narrow down simulation payload types

* refactor(Simulations): rename event map

* fix(Simulations): Include scenarios in union

* refactor(Simulations): rename DiscriminatedEventResponse
danbillson added a commit that referenced this pull request Oct 15, 2024
* feat(SimulationTypes): Add simulation types resource

* chore: remove payment_method events

* feat(Simulations): Add simulation resource

* chore(Simulations): update request body types

* chore: remove unused import

* feat(Simulations): Add simulation runs resources

* feat(Simulations): Add simulation run events resources

* chore: clean up unused vars

* feat(Simulations): narrow down simulation payload types (#52)

* feat(Simulations): narrow down simulation payload types

* refactor(Simulations): rename event map

* fix(Simulations): Include scenarios in union

* refactor(Simulations): rename DiscriminatedEventResponse

* chore: bump version

* fix(Simulations): update imports

* chore: update import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants