@nizza/core 0.5.0-beta.9 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ import { AddToCartActionFactory, HooksInitConfig, HookState } from '../types';
2
+ export declare const addToCartMessages: {
3
+ errorAddingToCart: string;
4
+ errorOpeningVariationSelector: string;
5
+ errorOpeningNewTab: string;
6
+ successAddingToCart: string;
7
+ };
8
+ /**
9
+ * Executes the add to cart hooks.
10
+ * @async
11
+ * @param {HooksInitConfig} config - The initial configuration for the add to cart operation.
12
+ */
13
+ export declare function runAddToCartHooks<ContextData extends Record<string, any>>(config: HooksInitConfig<ContextData>): Promise<void>;
14
+ /**
15
+ * Creates an action factory based on the current state.
16
+ * @param {HookState} state - The current state of the add to cart operation.
17
+ * @returns {AddToCartActionFactory} The created action factory.
18
+ */
19
+ export declare function createActionFactory(state: HookState): AddToCartActionFactory;
@@ -0,0 +1 @@
1
+ export * from './hooks';
@@ -10,6 +10,7 @@
10
10
  export declare const EventBusWildcards: {
11
11
  readonly ALL: "*";
12
12
  readonly CORE: "core:**";
13
+ readonly ADD_TO_CART_HOOKS: "core:addToCartHooks:**";
13
14
  readonly LIVE_STREAMING_EVENT: "event:**";
14
15
  readonly EVENT_CHAT: "event:chat:**";
15
16
  readonly EVENT_PRODUCTS: "event:products:**";
@@ -31,4 +32,6 @@ export declare const EventBusWildcards: {
31
32
  */
32
33
  export declare const EventNames: {
33
34
  readonly EVENT_STATUS_CHANGED: "event:statusChanged";
35
+ /** AddToCartHook events */
36
+ readonly ATCH_ACTION_EXECUTED: "core:addToCartHooks:actionExecuted";
34
37
  };
@@ -1,5 +1,6 @@
1
1
  export declare enum NizzaVariationTypes {
2
- COMBO = "Combo"
2
+ COMBO = "Combo",
3
+ BADGE = "Badged"
3
4
  }
4
5
  export declare enum NizzaProductRequestOriginTypes {
5
6
  VTEX = "VTEX",
@@ -1,4 +1,5 @@
1
1
  export * from './accounts';
2
+ export * from './add-to-cart';
2
3
  export * from './constants';
3
4
  export * from './domain';
4
5
  export * from './errors';
@@ -1,8 +1,27 @@
1
- import { StoreApi, UseBoundStore } from 'zustand';
2
- import { NizzaConfig, NizzaProductDataSource, NizzaState, NizzaStore } from '../types';
3
- export declare let nizza: NizzaStore;
4
- export declare let useNizza: UseBoundStore<StoreApi<NizzaState>>;
5
- export declare function nizzaInit(): NizzaStore;
6
- export declare function getInstance(): NizzaStore;
7
- export declare function configure(config: NizzaConfig): Promise<void>;
8
- export declare function setProductDataSource(dataSource: NizzaProductDataSource): void;
1
+ import { NizzaAccount } from '../accounts';
2
+ import { BroadcastChannelEventBus } from '../event-buses';
3
+ import { NizzaStore as INizzaStore, NizzaConfig, NizzaProductDataSource } from '../types';
4
+ import { Logger } from '../utils/logger';
5
+ export declare class NizzaStore implements INizzaStore {
6
+ private static _instance;
7
+ readonly version = "1.0.5";
8
+ readonly instanceId: string;
9
+ private _config;
10
+ private _account?;
11
+ private _productDataSource;
12
+ private _eventBus;
13
+ readonly log: Logger;
14
+ private constructor();
15
+ static instance(): NizzaStore;
16
+ configure(config: NizzaConfig): Promise<void>;
17
+ private setAccount;
18
+ setProductDataSource(dataSource: NizzaProductDataSource): void;
19
+ get config(): NizzaConfig | null;
20
+ get account(): NizzaAccount | undefined;
21
+ get accountName(): string | undefined;
22
+ get productDataSource(): NizzaProductDataSource | undefined;
23
+ get eventBus(): BroadcastChannelEventBus;
24
+ }
25
+ export declare const nizza: NizzaStore;
26
+ export declare const logger: import("loglevel").Logger;
27
+ export declare const coreLogger: import("loglevel").Logger;
@@ -1,2 +1,4 @@
1
1
  import { NizzaStore } from '../types';
2
- export declare const nizza: NizzaStore;
2
+ export declare const nizza: import("./NizzaStore").NizzaStore | NizzaStore;
3
+ export declare const logger: import("loglevel").Logger;
4
+ export declare const coreLogger: import("loglevel").Logger;
@@ -0,0 +1,7 @@
1
+ import { NizzaConfig, NizzaProductDataSource, NizzaStore } from '../types';
2
+ export declare const configure: (config: NizzaConfig) => Promise<void>;
3
+ export declare const setProductDataSource: (dataSource: NizzaProductDataSource) => void;
4
+ export declare const nizza: import("./NizzaStore").NizzaStore | NizzaStore;
5
+ export declare const useNizza: any;
6
+ export declare const logger: import("loglevel").Logger;
7
+ export declare const coreLogger: import("loglevel").Logger;
@@ -1,2 +1,3 @@
1
1
  export * from './NizzaStore';
2
2
  export * from './helpers';
3
+ export * from './useNizza';
@@ -0,0 +1,11 @@
1
+ import { NizzaStore } from './NizzaStore';
2
+ /**
3
+ * Custom hook for selecting and observing specific properties from NizzaStore.
4
+ *
5
+ * @param selector - A function that receives the NizzaStore instance and returns the selected part.
6
+ * @returns The selected store part, being observed for changes.
7
+ *
8
+ * @example
9
+ * const accountName = useNizza(store => store.accountName);
10
+ */
11
+ export declare function useNizza<T>(selector: (store: NizzaStore) => T): T;
@@ -0,0 +1,122 @@
1
+ import { NizzaProduct } from './nizza';
2
+ export type HookName = 'transform' | 'validate' | 'end';
3
+ /**
4
+ * ============ Action types ============
5
+ */
6
+ export declare enum AddToCartActionTypes {
7
+ OPEN_TAB = "openTab",
8
+ OPEN_VARIATION_SELECTOR = "openVariationSelector",
9
+ ADD_TO_CART = "addToCart"
10
+ }
11
+ export type AddToCartCreateAction<T> = (data: T) => AddToCartAction;
12
+ export type AddToCartCreateActionExec<T> = (data: T) => any;
13
+ export type AddToCartAction = {
14
+ type: AddToCartActionTypes;
15
+ exec: () => any;
16
+ };
17
+ /**
18
+ * ============ Factory types ============
19
+ */
20
+ /**
21
+ * The keys must be coincide with "AddToCartHookActionTypes" enum values
22
+ */
23
+ export type AddToCartActionFactory = {
24
+ openTab: AddToCartCreateAction<string>;
25
+ openVariationSelector: AddToCartCreateAction<boolean>;
26
+ addToCart: AddToCartCreateAction<{
27
+ product: NizzaProduct;
28
+ quantity?: number;
29
+ }>;
30
+ };
31
+ export type AddToCartActionFactoryMap = {
32
+ openTab: AddToCartCreateActionExec<string>;
33
+ openVariationSelector: AddToCartCreateActionExec<boolean>;
34
+ addToCart: AddToCartCreateActionExec<{
35
+ product: NizzaProduct;
36
+ quantity?: number;
37
+ }>;
38
+ };
39
+ type AddToCartCreateNotify = (message: string) => void;
40
+ export type AddToCartNotifyFactory = {
41
+ error: AddToCartCreateNotify;
42
+ success: AddToCartCreateNotify;
43
+ };
44
+ /**
45
+ * ============ Context types ============
46
+ */
47
+ type TransformHookContext<T = Record<string, any>> = {
48
+ product: NizzaProduct;
49
+ data: T;
50
+ };
51
+ type ValidateHookContext<T = Record<string, any>> = {
52
+ product: NizzaProduct;
53
+ defaultAction: AddToCartAction;
54
+ action: AddToCartActionFactory;
55
+ data: T;
56
+ };
57
+ type EndHookContext<T = Record<string, any>> = {
58
+ error: Error | string | null | unknown;
59
+ notify: AddToCartNotifyFactory;
60
+ actionExecuted: AddToCartAction;
61
+ data: T;
62
+ };
63
+ /**
64
+ * ============ Execution types ============
65
+ */
66
+ /**
67
+ * Context for the add to cart operation.
68
+ */
69
+ export type HookContext<T = Record<string, any>> = {
70
+ /** Indicates if the variation selector is open. */
71
+ isVariationSelectorOpen: boolean;
72
+ /** The quantity of the product to add to cart. */
73
+ quantity?: number;
74
+ data?: T;
75
+ };
76
+ /**
77
+ * Represents the combined state of the add to cart operation.
78
+ */
79
+ export type HookState<ContextData = Record<string, any>> = HooksInitConfig<ContextData> & {
80
+ action: AddToCartAction;
81
+ };
82
+ export type AddToCartHookStrategy<ContextData = Record<string, any>> = {
83
+ validate: (state: HookState<ContextData>) => AddToCartAction;
84
+ };
85
+ /**
86
+ * Initial configuration for the add to cart operation.
87
+ */
88
+ export type HooksInitConfig<ContextData = Record<string, any>> = {
89
+ /** The product to be added to the cart. */
90
+ product: NizzaProduct;
91
+ hookStrategy: AddToCartHookStrategy<ContextData>;
92
+ /** A map of action factories for different add to cart actions. */
93
+ actionFactory: AddToCartActionFactoryMap;
94
+ /** Factory for creating notification actions. */
95
+ notifyFactory: AddToCartNotifyFactory;
96
+ /** Context for the add to cart operation. */
97
+ context: HookContext<ContextData>;
98
+ /** User-defined configuration for add to cart behavior. */
99
+ userAddToCartConfig?: AddToCartConfig;
100
+ };
101
+ /**
102
+ * ============ Event types ============
103
+ */
104
+ export type ActionExecutedPayload = {
105
+ actionType: AddToCartActionTypes;
106
+ product: NizzaProduct;
107
+ success: boolean;
108
+ error: Error | string | null | unknown;
109
+ };
110
+ /**
111
+ * ============ Main types ============
112
+ */
113
+ interface AddToCartHooks<ContextData = Record<string, any>> {
114
+ transform: (context: TransformHookContext<ContextData>) => NizzaProduct | Promise<NizzaProduct>;
115
+ validate: (context: ValidateHookContext<ContextData>) => AddToCartAction | Promise<AddToCartAction>;
116
+ end: (context: EndHookContext<ContextData>) => void | boolean | Promise<void | boolean>;
117
+ }
118
+ export interface AddToCartConfig {
119
+ hooks?: Partial<AddToCartHooks>;
120
+ actionFactory?: Partial<AddToCartActionFactoryMap>;
121
+ }
122
+ export {};
@@ -1,4 +1,5 @@
1
1
  import * as ApiGraphqlTypes from './api-graphql-types';
2
+ export * from './add-to-cart-hooks';
2
3
  export * from './api';
3
4
  export * from './appModule';
4
5
  export * from './i18n';
@@ -1,4 +1,3 @@
1
- import { StoreApi, UseBoundStore } from 'zustand';
2
1
  import { NizzaAccount } from '../../accounts';
3
2
  import { BroadcastChannelEventBus } from '../../event-buses';
4
3
  import { Logger } from '../../utils';
@@ -7,18 +6,14 @@ export interface GlobalStoreConfig {
7
6
  account: string;
8
7
  getProductDataSource?: () => Promise<any> | any;
9
8
  }
10
- export interface GlobalState {
11
- config: GlobalStoreConfig | null;
12
- account?: any;
13
- productDataSource?: any;
14
- }
15
9
  export interface GlobalStore {
16
10
  version: string;
17
- store: UseBoundStore<StoreApi<any>>;
18
11
  instanceId: string;
12
+ config: GlobalStoreConfig | null;
13
+ account?: any;
14
+ productDataSource?: any;
19
15
  log: Logger;
20
16
  eventBus: BroadcastChannelEventBus;
21
- state: GlobalState;
22
17
  configure: (config: GlobalStoreConfig) => Promise<void>;
23
18
  setProductDataSource: (dataSource: any) => void;
24
19
  }
@@ -26,14 +21,11 @@ export interface NizzaConfig extends GlobalStoreConfig {
26
21
  account: string;
27
22
  getProductDataSource?: () => Promise<NizzaProductDataSource> | NizzaProductDataSource;
28
23
  }
29
- export interface NizzaState extends GlobalState {
24
+ export interface NizzaStore extends GlobalStore {
30
25
  config: NizzaConfig | null;
31
26
  account?: NizzaAccount;
27
+ accountName?: string;
32
28
  productDataSource?: NizzaProductDataSource;
33
- }
34
- export interface NizzaStore extends GlobalStore {
35
- store: UseBoundStore<StoreApi<NizzaState>>;
36
- state: NizzaState;
37
29
  configure: (config: NizzaConfig) => Promise<void>;
38
30
  setProductDataSource: (dataSource: NizzaProductDataSource) => void;
39
31
  }
@@ -1,7 +1,7 @@
1
1
  import { NizzaProduct } from './product';
2
2
  export type NizzaAddToCartHandlerParams = {
3
3
  product: NizzaProduct;
4
- quantity: number;
4
+ quantity?: number;
5
5
  };
6
6
  export type NizzaAddToCartHandler = (params: NizzaAddToCartHandlerParams) => Promise<void>;
7
7
  export type NizzaSetUTMHandler = (utm: string) => Promise<void>;
@@ -0,0 +1 @@
1
+ export {};
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nizza/core",
3
- "version": "0.5.0-beta.9",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.cjs",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nizza/core",
3
- "version": "0.5.0-beta.9",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.cjs",