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

Update event payloads #14

Merged
merged 9 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to

### TBA

## [v5.0.0] - 2021-01-04

- Updated event types and payloads in Vendor Communicator

## [v4.1.0] - 2022-12-23

- Added Continue to Cart Event to the Vendor Communicator
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postmessage-communicator",
"version": "4.1.0",
"version": "5.0.0",
"description": "[![Release](https://img.shields.io/github/v/release/wayfair-incubator/oss-template?display_name=tag)](CHANGELOG.md) [![Lint](https://github.com/wayfair-incubator/oss-template/actions/workflows/lint.yml/badge.svg?branch=main)](https://github.com/wayfair-incubator/oss-template/actions/workflows/lint.yml) [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.0-4baaaa.svg)](CODE_OF_CONDUCT.md) [![Maintainer](https://img.shields.io/badge/Maintainer-Wayfair-7F187F)](https://wayfair.github.io)",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/classes/ClientCommunicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export class ClientCommunicator extends Communicator {
this.post({type: ClientEvent.RefreshWithToken, payload: token});
}
addToCartStatus(payload: AddToCartStatusPayload): void {
this.post({type: ClientEvent.AddToCartStatus, payload})
this.post({type: ClientEvent.AddToCartStatus, payload});
}
}
110 changes: 74 additions & 36 deletions src/classes/VendorCommunicator.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,75 @@
import {Communicator} from './Communicator';

export enum VendorEvent {
AddToCart = 'AddToCart',
AppInitialized = 'AppInitialized',
ContactDesigner = 'ContactDesigner',
DirtyStateChanged = 'DirtyStateChanged',
iFrameLoaded = 'iFrameLoaded',
ProjectDeleted = 'ProjectDeleted',
iFrameRedirect = 'iFrameRedirect',
DirtyStateChanged = 'DirtyStateChanged',
ProjectSaved = 'ProjectSaved',
TrackingEvent = 'TrackingEvent',
ProjectDeleted = 'ProjectDeleted',
ContactDesigner = 'ContactDesigner',
AddToCart = 'AddToCart',
ContinueToCart = 'ContinueToCart',
TokenRefreshRequested = 'TokenRefreshRequested',
UnauthorizedToken = 'UnauthorizedToken',
ContinueToCart = 'ContinueToCart',
TrackingEvent = 'TrackingEvent',
}

interface Metadata {
source?: string;
interface CommonPayload {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@NothingButThyme I took a pass at creating general/common interfaces that are extended for specific events. This commit is a proposal for us to discuss, not intended to unilaterally override the commit you pushed up (didn't think a whole separate PR for it was warranted). Thoughts appreciated!

The main point to get across here is the requirement for token and customerId be included in the majority of event payloads (excluding non-customer-centric events like tracking) so we can validate the token and make sure it's intended for the right customer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The changes look good. I don't have any objections.

I am curious what future-proofing is being accomplished by CommonMedata vs CabinetsMetadata, because 'source' and 'area' seem like they would apply to other things besides cabinets.

message?: string;
token: string;
customerId: string;
}

interface CommonMetadata {
title: string;
brand: string;
style: string;
color: string;
thumbnailUrl: string;
price: number;
retailPrice?: number;
thumbnailUrl?: string;
[key: string]: string | number | undefined;
}

interface CabinetsMetadata extends CommonMetadata {
source?: string;
area?: number;
price?: number;
}

interface EventPayload {
interface SnapshotPayload<M extends CommonMetadata> extends CommonPayload {
schema: string;
token: string;
customerId: string;
projectId: string;
versionId: string;
metadata: Metadata;
bom?: any;
metadata: M;
bom: string;
}

interface TrackingEvent {
interface ProjectSavedPayload extends CommonPayload {
projectId: string;
versionId?: string;
}

interface ProjectDeletedPayload extends CommonPayload {
projectId: string;
}

interface DirtyStateChangedPayload extends CommonPayload {
projectId: string;
isDirty: boolean;
}

// TODO: (IW) This is pretty vendor-specific, should be typed to vendor apps
interface iFrameRedirectPayload extends CommonPayload {
app: string;
key?: string | number;
}

interface TokenRefreshRequestedPayload extends CommonPayload {
requestId: string;
}

interface TrackingEventPayload {
actionName: string;
actionData: Record<string, unknown>;
}
Expand All @@ -47,47 +81,51 @@ export class VendorCommunicator extends Communicator {
this.origin = origin;
}

addToCart(payload: EventPayload): void {
this.post({type: VendorEvent.AddToCart, payload});
appInitialized(message: string): void {
this.post({type: VendorEvent.AppInitialized, payload: message});
}

appInitialized(): void {
this.post({type: VendorEvent.AddToCart});
iframeLoaded(message: string): void {
this.post({type: VendorEvent.iFrameLoaded, payload: message});
}

contactDesigner(payload: EventPayload): void {
this.post({type: VendorEvent.ContactDesigner, payload});
iframeRedirect(payload: iFrameRedirectPayload): void {
this.post({type: VendorEvent.iFrameRedirect, payload});
}

dirtyStateChanged(payload: DirtyStateChangedPayload): void {
this.post({type: VendorEvent.DirtyStateChanged, payload});
}

dirtyStateChanged(): void {
this.post({type: VendorEvent.DirtyStateChanged});
projectSaved(payload: ProjectSavedPayload): void {
this.post({type: VendorEvent.ProjectSaved, payload});
}

iframeLoaded(): void {
this.post({type: VendorEvent.iFrameLoaded});
projectDeleted(payload: ProjectDeletedPayload): void {
this.post({type: VendorEvent.ProjectDeleted, payload});
}

projectSaved(): void {
this.post({type: VendorEvent.ProjectSaved});
contactDesigner(payload: SnapshotPayload<CabinetsMetadata>): void {
this.post({type: VendorEvent.ContactDesigner, payload});
}

projectDeleted(): void {
this.post({type: VendorEvent.ProjectDeleted});
addToCart(payload: SnapshotPayload<CabinetsMetadata>): void {
this.post({type: VendorEvent.AddToCart, payload});
}

trackEvent(payload: TrackingEvent): void {
this.post({type: VendorEvent.TrackingEvent, payload});
continueToCart(payload: SnapshotPayload<CabinetsMetadata>): void {
this.post({type: VendorEvent.ContinueToCart, payload});
}

tokenRefreshRequested(): void {
this.post({type: VendorEvent.TokenRefreshRequested});
tokenRefreshRequested(payload: TokenRefreshRequestedPayload): void {
this.post({type: VendorEvent.TokenRefreshRequested, payload});
}

unauthorizedToken(error: string): void {
this.post({type: VendorEvent.UnauthorizedToken, payload: error});
}

continueToCart(payload: EventPayload): void {
this.post({type: VendorEvent.ContinueToCart, payload})
trackEvent(payload: TrackingEventPayload): void {
this.post({type: VendorEvent.TrackingEvent, payload});
}
}