@plyaz/core 1.11.0 → 1.11.1

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.
@@ -2774,6 +2774,24 @@ var Core = class _Core {
2774
2774
  */
2775
2775
  this._rootStore = null;
2776
2776
  }
2777
+ static {
2778
+ /**
2779
+ * Injected store hook for frontend (set via setRootStoreHook before initialize).
2780
+ * This is used instead of directly importing from @plyaz/store to prevent
2781
+ * duplicate store instances when bundlers create multiple module copies.
2782
+ */
2783
+ this._injectedStoreHook = null;
2784
+ }
2785
+ /**
2786
+ * Set the root store hook for frontend use.
2787
+ * Must be called before Core.initialize() when running in browser.
2788
+ * PlyazProvider calls this automatically with the store prop.
2789
+ *
2790
+ * @param store - The useRootStore hook from @plyaz/store
2791
+ */
2792
+ static setRootStoreHook(store) {
2793
+ _Core._injectedStoreHook = store;
2794
+ }
2777
2795
  /**
2778
2796
  * Setup environment and context
2779
2797
  */
@@ -3684,8 +3702,14 @@ var Core = class _Core {
3684
3702
  static async initializeRootStore(config, verbose) {
3685
3703
  const isFrontend = typeof window !== "undefined";
3686
3704
  if (isFrontend) {
3687
- _Core.log("Using frontend root store (Zustand)", verbose);
3688
- _Core._rootStore = store.useRootStore;
3705
+ if (!_Core._injectedStoreHook) {
3706
+ throw new errors.CorePackageError(
3707
+ "Root store hook not set. Call Core.setRootStoreHook(useRootStore) before Core.initialize(), or use PlyazProvider with the store prop: <PlyazProvider store={useRootStore} ...>",
3708
+ types.ERROR_CODES.CLIENT_INITIALIZATION_FAILED
3709
+ );
3710
+ }
3711
+ _Core.log("Using frontend root store (Zustand) - injected via setRootStoreHook", verbose);
3712
+ _Core._rootStore = _Core._injectedStoreHook;
3689
3713
  } else {
3690
3714
  _Core.log("Creating backend composite store (in-memory)", verbose);
3691
3715
  const ServerErrorMiddleware = getCoreDependency("ServerErrorMiddleware");
@@ -7137,7 +7161,10 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
7137
7161
  const response = await this.apiClient.post(endpoint, data);
7138
7162
  if (!this.isResponseSuccess(response)) {
7139
7163
  const error = this.extractResponseError(response);
7140
- throw new Error(`Failed to send email: ${JSON.stringify(error)}`);
7164
+ throw new errors.CorePackageError(
7165
+ `Failed to send email: ${JSON.stringify(error)}`,
7166
+ types.ERROR_CODES.CORE_OPERATION_FAILED
7167
+ );
7141
7168
  }
7142
7169
  const result = this.unwrapResponseData(response.data);
7143
7170
  CoreEventManager.emit(`${this.eventPrefix}:email:sent`, { result });
@@ -8679,22 +8706,27 @@ async function initializeCore(config) {
8679
8706
  });
8680
8707
  }
8681
8708
  __name(initializeCore, "initializeCore");
8682
- function createStoreRegistry() {
8709
+ function createStoreRegistry(store) {
8683
8710
  return {
8684
8711
  getStore(key) {
8685
- const state = store.useRootStore.getState();
8712
+ const state = store.getState();
8686
8713
  if (!state) {
8687
8714
  logger4.warn(
8688
8715
  "Store state is undefined - store may not be hydrated yet. This can cause side effects if called during SSR or before initialization."
8689
8716
  );
8690
8717
  return void 0;
8691
8718
  }
8692
- return state[key];
8719
+ const slice = state[key];
8720
+ logger4.debug(`[StoreRegistry] getStore('${key}')`, {
8721
+ hasSlice: !!slice,
8722
+ sliceKeys: slice ? Object.keys(slice) : []
8723
+ });
8724
+ return slice;
8693
8725
  }
8694
8726
  };
8695
8727
  }
8696
8728
  __name(createStoreRegistry, "createStoreRegistry");
8697
- async function initializeServices(config) {
8729
+ async function initializeServices(config, store) {
8698
8730
  if (!config.services || config.services.length === 0) return;
8699
8731
  if (config.verbose) {
8700
8732
  globalThis.console.log("[PlyazProvider] Initializing domain services...");
@@ -8706,7 +8738,7 @@ async function initializeServices(config) {
8706
8738
  observability: config.observability,
8707
8739
  services: config.services,
8708
8740
  // Provide store registry for injecting stores into services
8709
- stores: createStoreRegistry()
8741
+ stores: createStoreRegistry(store)
8710
8742
  });
8711
8743
  if (config.verbose) {
8712
8744
  globalThis.console.log(
@@ -8743,6 +8775,7 @@ function createServicesObject(config, featureFlagStore) {
8743
8775
  __name(createServicesObject, "createServicesObject");
8744
8776
  function PlyazProvider({
8745
8777
  children,
8778
+ store,
8746
8779
  config,
8747
8780
  loading,
8748
8781
  error: errorComponent,
@@ -8756,8 +8789,9 @@ function PlyazProvider({
8756
8789
  const initialize = react.useCallback(async () => {
8757
8790
  try {
8758
8791
  setError(null);
8792
+ Core.setRootStoreHook(store);
8759
8793
  await initializeCore(config);
8760
- await initializeServices(config);
8794
+ await initializeServices(config, store);
8761
8795
  await initializeFeatureFlags(featureFlagStore);
8762
8796
  setIsReady(true);
8763
8797
  onReady?.(createServicesObject(config, featureFlagStore));
@@ -8767,7 +8801,7 @@ function PlyazProvider({
8767
8801
  onError?.(initError);
8768
8802
  globalThis.console.error("[PlyazProvider] Initialization failed:", initError);
8769
8803
  }
8770
- }, [config, featureFlagStore, onReady, onError]);
8804
+ }, [config, store, featureFlagStore, onReady, onError]);
8771
8805
  const reinitialize = react.useCallback(async () => {
8772
8806
  setIsReady(false);
8773
8807
  ServiceRegistry.disposeAll();
@@ -8816,8 +8850,9 @@ __name(PlyazProvider, "PlyazProvider");
8816
8850
  function usePlyaz() {
8817
8851
  const context = react.useContext(PlyazContext);
8818
8852
  if (!context) {
8819
- throw new Error(
8820
- "usePlyaz must be used within a PlyazProvider. Wrap your app with <PlyazProvider config={...}>...</PlyazProvider>"
8853
+ throw new errors.CorePackageError(
8854
+ "usePlyaz must be used within a PlyazProvider. Wrap your app with <PlyazProvider config={...}>...</PlyazProvider>",
8855
+ types.ERROR_CODES.CORE_PROVIDER_NOT_FOUND
8821
8856
  );
8822
8857
  }
8823
8858
  return context;
@@ -8826,7 +8861,10 @@ __name(usePlyaz, "usePlyaz");
8826
8861
  function useApi() {
8827
8862
  const { api, isReady } = usePlyaz();
8828
8863
  if (!isReady || !api) {
8829
- throw new Error("API client is not ready. Make sure PlyazProvider has finished initializing.");
8864
+ throw new errors.CorePackageError(
8865
+ "API client is not ready. Make sure PlyazProvider has finished initializing.",
8866
+ types.ERROR_CODES.CORE_PROVIDER_INITIALIZATION_FAILED
8867
+ );
8830
8868
  }
8831
8869
  return api;
8832
8870
  }
@@ -8869,7 +8907,10 @@ __name(useEnvironment, "useEnvironment");
8869
8907
  function useService(key) {
8870
8908
  const { getService, isReady } = usePlyaz();
8871
8909
  if (!isReady) {
8872
- throw new Error(`PlyazProvider not ready. Cannot get service '${key}'.`);
8910
+ throw new errors.CorePackageError(
8911
+ `PlyazProvider not ready. Cannot get service '${key}'.`,
8912
+ types.ERROR_CODES.CORE_PROVIDER_INITIALIZATION_FAILED
8913
+ );
8873
8914
  }
8874
8915
  return react.useMemo(() => getService(key), [getService, key]);
8875
8916
  }
@@ -9408,10 +9449,6 @@ function createFeatureFlagStoreConfig(options) {
9408
9449
  }
9409
9450
  __name(createFeatureFlagStoreConfig, "createFeatureFlagStoreConfig");
9410
9451
 
9411
- Object.defineProperty(exports, "useRootStore", {
9412
- enumerable: true,
9413
- get: function () { return store.useRootStore; }
9414
- });
9415
9452
  exports.ApiClientService = ApiClientService;
9416
9453
  exports.ApiProvider = ApiProvider;
9417
9454
  exports.BaseDomainService = BaseDomainService;