@liquidcommercedev/rmn-sdk 1.4.6-beta.3 → 1.4.6-beta.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,30 @@
1
- import type { IFireEventParams } from './event.interface';
1
+ import type { IEventMap, IRegisterSpotParams } from './event.interface';
2
2
  export declare class EventService {
3
- fireEvent({ event, eventUrl }: IFireEventParams): Promise<void>;
3
+ private static instance;
4
+ private pubSub;
5
+ private intersectionObserver;
6
+ private activeSpots;
7
+ private constructor();
8
+ static getInstance(): EventService;
9
+ registerSpot({ placementId, element, spot }: IRegisterSpotParams): void;
10
+ unregisterSpot(spotId: string): void;
11
+ private handleIntersectionObserver;
12
+ subscribe<K extends keyof IEventMap>(eventType: K, callback: (data: IEventMap[K]) => void): () => void;
13
+ publish<K extends keyof IEventMap>(eventType: K, data: IEventMap[K]): void;
14
+ /**
15
+ * Fires an event using the navigator.sendBeacon method and redirects the user if the event is a click event.
16
+ *
17
+ * @param {IFireEventParams} params - The parameters for firing the event.
18
+ * @param {RMN_SPOT_EVENT} params.event - The event type.
19
+ * @param {string} params.eventUrl - The URL to which the event is sent.
20
+ * @returns {Promise<void>} - A promise that resolves when the event is fired.
21
+ */
22
+ private fireEvent;
23
+ /**
24
+ * Extracts and decodes a URL from a base64-encoded query parameter.
25
+ *
26
+ * @param {string} url - The URL containing the base64-encoded query parameter.
27
+ * @returns {string} - The decoded URL or an empty string if decoding fails.
28
+ */
4
29
  private getRedirectUrlFromPayload;
5
30
  }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Callback function type for event subscribers
3
+ * @template T The type of data the callback receives
4
+ */
5
+ export type PSCallback<T> = (data: T) => void;
6
+ /**
7
+ * Function type for unsubscribing from an event
8
+ */
9
+ export type PSUnsubscribe = () => void;
10
+ /**
11
+ * PubSub class
12
+ * Manages event subscriptions and publications
13
+ * @template IEventMap A record type defining the structure of events and their data
14
+ */
15
+ export declare class PubSub<IEventMap> {
16
+ /**
17
+ * Object to store subscribers for each event type
18
+ */
19
+ private subscribers;
20
+ /**
21
+ * Subscribe to an event
22
+ * @param eventType - The type of event to subscribe to
23
+ * @param callback - The function to be called when the event is published
24
+ * @returns A function to unsubscribe from the event
25
+ *
26
+ * @Example:
27
+ * const unsubscribe = pubSub.subscribe('userLogin', (data) => {
28
+ * console.log(`User ${data.username} logged in`);
29
+ * });
30
+ */
31
+ subscribe<K extends keyof IEventMap>(eventType: K, callback: PSCallback<IEventMap[K]>): PSUnsubscribe;
32
+ /**
33
+ * Publish an event
34
+ * @param eventType - The type of event to publish
35
+ * @param data - The data to be passed to the event subscribers
36
+ *
37
+ * @Example:
38
+ * pubSub.publish('userLogin', { username: 'john_doe', timestamp: Date.now() });
39
+ */
40
+ publish<K extends keyof IEventMap>(eventType: K, data: IEventMap[K]): void;
41
+ }
42
+ /**
43
+ * Usage Example:
44
+ *
45
+ * interface IEventMap {
46
+ * userLogin: { username: string; timestamp: number };
47
+ * pageView: { url: string; timestamp: number };
48
+ * }
49
+ *
50
+ * const pubSub = new PubSub<IEventMap>();
51
+ *
52
+ * // Subscribe to events
53
+ * const unsubscribeLogin = pubSub.subscribe('userLogin', (data) => {
54
+ * console.log(`User ${data.username} logged in at ${new Date(data.timestamp)}`);
55
+ * });
56
+ *
57
+ * pubSub.subscribe('pageView', (data) => {
58
+ * console.log(`Page ${data.url} viewed at ${new Date(data.timestamp)}`);
59
+ * });
60
+ *
61
+ * // Publish events
62
+ * pubSub.publish('userLogin', { username: 'john_doe', timestamp: Date.now() });
63
+ * pubSub.publish('pageView', { url: '/home', timestamp: Date.now() });
64
+ *
65
+ * // Unsubscribe from an event
66
+ * unsubscribeLogin();
67
+ */
@@ -1,5 +1,5 @@
1
1
  import type { RMN_SPOT_EVENT, RMN_SPOT_TYPE } from 'enums';
2
- import type { SpotIdentifierType, SpotVariantType } from 'modules/selection';
2
+ import type { PlacementIdType, SpotVariantType } from 'modules/selection';
3
3
  import type { RmnFilterType, RmnSpotType } from 'types';
4
4
  export interface ISpotSelectionParams {
5
5
  url?: string;
@@ -11,6 +11,7 @@ export interface ISpotEvent {
11
11
  url: string;
12
12
  }
13
13
  export interface ISpot {
14
+ id: string;
14
15
  events: ISpotEvent[];
15
16
  spot: RMN_SPOT_TYPE;
16
17
  variant: SpotVariantType;
@@ -30,7 +31,7 @@ export interface ISpot {
30
31
  mobileSecondaryImage?: string;
31
32
  productUpcs?: string[];
32
33
  }
33
- export type ISpots = Record<SpotIdentifierType, ISpot[]>;
34
+ export type ISpots = Record<PlacementIdType, ISpot[]>;
34
35
  export interface ISelectionService {
35
36
  spotSelection(data: ISpotSelectionParams): Promise<ISpots>;
36
37
  }
@@ -2,12 +2,13 @@ import type { RMN_FILTER_PROPERTIES, RMN_SPOT_TYPE } from 'enums';
2
2
  export type RmnFilterType = {
3
3
  [key in RMN_FILTER_PROPERTIES]?: string[];
4
4
  };
5
+ export type PlacementIdType = RMN_SPOT_TYPE | `${RMN_SPOT_TYPE}${number}` | string;
5
6
  export type SpotFilterType = {
7
+ placementId?: PlacementIdType;
6
8
  spot: RMN_SPOT_TYPE | string;
7
- count: number;
9
+ count?: number;
8
10
  exactMatch?: string;
9
11
  } & Omit<RmnFilterType, RMN_FILTER_PROPERTIES.KEYWORDS>;
10
- export type SpotIdentifierType = RMN_SPOT_TYPE | `${RMN_SPOT_TYPE}${number}`;
11
12
  export type RmnSpotType = RMN_SPOT_TYPE | string | SpotFilterType;
12
13
  type RBSpotTypeKeys = keyof {
13
14
  [K in keyof typeof RMN_SPOT_TYPE as K extends `RB_${string}` ? K : never]: (typeof RMN_SPOT_TYPE)[K];
@@ -1,30 +1,45 @@
1
1
  import type { IAuthCredentials } from 'modules/auth';
2
- import type { IInjectSpotElement, IInjectSpotElementConfig, IRmnCreateSpotElementConfig } from 'modules/element';
2
+ import type { IInjectSpotElementParams, IRmnCreateSpotElementConfig } from 'modules/element';
3
+ import { EventService } from 'modules/event';
3
4
  import type { ISpot, ISpots, ISpotSelectionParams } from 'modules/selection';
4
5
  import type { IRmnClient, IRmnConfig } from 'types';
5
6
  export declare class LiquidCommerceRmnClient implements IRmnClient {
6
7
  private readonly selectionService;
7
8
  private readonly elementService;
9
+ private readonly eventService;
8
10
  constructor(auth: IAuthCredentials);
9
11
  /**
10
12
  * Makes a selection request on our server based on the provided data.
11
13
  *
12
14
  * To create a spot html element, use the RmnCreateSpotElement function.
13
15
  *
14
- * @param {ISpotSelectionParams} data - Spots selection parameters.
16
+ * @param {ISpotSelectionParams} params - Spots selection parameters.
15
17
  *
16
18
  * @return {Promise<ISpots>} - The spots response object.
17
19
  */
18
- spotSelection(data: ISpotSelectionParams): Promise<ISpots>;
20
+ spotSelection(params: ISpotSelectionParams): Promise<ISpots>;
19
21
  /**
20
22
  * Injects the spot elements into their provided placement.
21
23
  *
22
- * @param {IInjectSpotElement[]} data - The spot element's data.
23
- * @param {IInjectSpotElementConfig} config - The configuration object.
24
+ * @param {IInjectSpotElementParams} params - Parameters for injecting spot elements.
24
25
  *
25
26
  * @return {Promise<void>} - A promise that resolves when the spot elements are injected.
26
27
  */
27
- injectSpotElement(data: IInjectSpotElement[], config?: IInjectSpotElementConfig): Promise<void>;
28
+ injectSpotElement(params: IInjectSpotElementParams): Promise<void>;
29
+ /**
30
+ * Returns the event manager instance.
31
+ *
32
+ * @return {EventService} - The event manager instance.
33
+ */
34
+ eventManager(): EventService;
35
+ /**
36
+ * Makes a selection request on our server based on the provided data.
37
+ *
38
+ * @param {IInjectSpotElementParams} params - Parameters for injecting spot elements.
39
+ *
40
+ * @return {Promise<ISpots>} - The spots response object.
41
+ */
42
+ private spotSelectionRequest;
28
43
  /**
29
44
  * Injects a carousel element with the provided spots into the placement.
30
45
  *
@@ -46,14 +61,17 @@ export declare class LiquidCommerceRmnClient implements IRmnClient {
46
61
  * @return {void}
47
62
  */
48
63
  private injectOneSpotElement;
64
+ private eventSpotElement;
49
65
  /**
50
- * Normalizes the spot type data by adding a number suffix to the spot type.
66
+ * Prevents duplicate placement ids in the inject data.
67
+ *
68
+ * @param {IInjectSpotElement[]} inject - The inject data.
51
69
  *
52
- * @param {IInjectSpotElement[]} spots - The spot type data.
70
+ * @throws {Error} - If a duplicate placement id is found.
53
71
  *
54
- * @return {IInjectSpotElement[]} - The normalized spot type data.
72
+ * @return {void}
55
73
  */
56
- private normalizeDataSpotType;
74
+ private preventDuplicateSpotPlacementIds;
57
75
  }
58
76
  /**
59
77
  * Creates a new instance of the RmnClient.
@@ -1,12 +1,15 @@
1
- export type { IInjectSpotElement, IInjectSpotElementConfig, IRmnCreateSpotElementConfig, } from 'modules/element';
1
+ import type { EventService } from './modules/event';
2
+ export type { IInjectSpotElement, IInjectSpotElementConfig, IInjectSpotElementParams, IRmnCreateSpotElementConfig, ISpotColors, ISpotOverlay, } from 'modules/element';
3
+ export type { CarouselNavPositionType, ICarouselButtonOptions, ICarouselDotOptions, ICarouselOptions, } from 'modules/element/component/carousel';
2
4
  export type { ISpots, RmnFilterType, RmnSpotType } from 'modules/selection';
3
5
  export { ISpot, ISpotEvent, ISpotSelectionParams } from 'modules/selection';
4
6
  import type { RMN_ENV } from 'enums';
5
- import type { IInjectSpotElement, IInjectSpotElementConfig } from 'modules/element';
7
+ import type { IInjectSpotElementParams } from 'modules/element';
6
8
  import type { ISpots, ISpotSelectionParams } from 'modules/selection';
7
9
  export interface IRmnClient {
8
- spotSelection(data: ISpotSelectionParams): Promise<ISpots>;
9
- injectSpotElement(spots: IInjectSpotElement[], config?: IInjectSpotElementConfig): Promise<void>;
10
+ spotSelection(params: ISpotSelectionParams): Promise<ISpots>;
11
+ injectSpotElement(params: IInjectSpotElementParams): Promise<void>;
12
+ eventManager(): EventService;
10
13
  }
11
14
  export interface IRmnConfig {
12
15
  env: RMN_ENV;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@liquidcommercedev/rmn-sdk",
3
3
  "description": "LiquidCommerce RMN SDK",
4
4
  "author": "LiquidCommerce Tech",
5
- "version": "1.4.6-beta.3",
5
+ "version": "1.4.6-beta.5",
6
6
  "homepage": "https://docs.liquidcommerce.co/rmn-sdk",
7
7
  "main": "./dist/index.cjs",
8
8
  "module": "./dist/index.esm.js",