@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.
@@ -7,8 +7,7 @@ import { EventEmitter } from 'events';
7
7
  import { ERROR_CODES as ERROR_CODES$1, ERROR_CATEGORY as ERROR_CATEGORY$1, API_ERROR_CODES as API_ERROR_CODES$1 } from '@plyaz/types/errors';
8
8
  import { OBSERVABILITY_METRICS } from '@plyaz/types/observability';
9
9
  import { clearEventEmitter, setEventEmitter, initializeGlobalErrorHandler } from '@plyaz/errors/middleware';
10
- import { STORE_KEYS, useRootStore, createStandaloneFeatureFlagStore } from '@plyaz/store';
11
- export { useRootStore } from '@plyaz/store';
10
+ import { STORE_KEYS, createStandaloneFeatureFlagStore } from '@plyaz/store';
12
11
  import { CORE_EVENTS as CORE_EVENTS$1, SERVICE_KEYS } from '@plyaz/types/core';
13
12
  import { uploadFile, uploadFiles, generateDocument, downloadFile, getSignedUrl, deleteFile, getFile } from '@plyaz/api';
14
13
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
@@ -2773,6 +2772,24 @@ var Core = class _Core {
2773
2772
  */
2774
2773
  this._rootStore = null;
2775
2774
  }
2775
+ static {
2776
+ /**
2777
+ * Injected store hook for frontend (set via setRootStoreHook before initialize).
2778
+ * This is used instead of directly importing from @plyaz/store to prevent
2779
+ * duplicate store instances when bundlers create multiple module copies.
2780
+ */
2781
+ this._injectedStoreHook = null;
2782
+ }
2783
+ /**
2784
+ * Set the root store hook for frontend use.
2785
+ * Must be called before Core.initialize() when running in browser.
2786
+ * PlyazProvider calls this automatically with the store prop.
2787
+ *
2788
+ * @param store - The useRootStore hook from @plyaz/store
2789
+ */
2790
+ static setRootStoreHook(store) {
2791
+ _Core._injectedStoreHook = store;
2792
+ }
2776
2793
  /**
2777
2794
  * Setup environment and context
2778
2795
  */
@@ -3683,8 +3700,14 @@ var Core = class _Core {
3683
3700
  static async initializeRootStore(config, verbose) {
3684
3701
  const isFrontend = typeof window !== "undefined";
3685
3702
  if (isFrontend) {
3686
- _Core.log("Using frontend root store (Zustand)", verbose);
3687
- _Core._rootStore = useRootStore;
3703
+ if (!_Core._injectedStoreHook) {
3704
+ throw new CorePackageError(
3705
+ "Root store hook not set. Call Core.setRootStoreHook(useRootStore) before Core.initialize(), or use PlyazProvider with the store prop: <PlyazProvider store={useRootStore} ...>",
3706
+ ERROR_CODES.CLIENT_INITIALIZATION_FAILED
3707
+ );
3708
+ }
3709
+ _Core.log("Using frontend root store (Zustand) - injected via setRootStoreHook", verbose);
3710
+ _Core._rootStore = _Core._injectedStoreHook;
3688
3711
  } else {
3689
3712
  _Core.log("Creating backend composite store (in-memory)", verbose);
3690
3713
  const ServerErrorMiddleware = getCoreDependency("ServerErrorMiddleware");
@@ -7136,7 +7159,10 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
7136
7159
  const response = await this.apiClient.post(endpoint, data);
7137
7160
  if (!this.isResponseSuccess(response)) {
7138
7161
  const error = this.extractResponseError(response);
7139
- throw new Error(`Failed to send email: ${JSON.stringify(error)}`);
7162
+ throw new CorePackageError(
7163
+ `Failed to send email: ${JSON.stringify(error)}`,
7164
+ ERROR_CODES.CORE_OPERATION_FAILED
7165
+ );
7140
7166
  }
7141
7167
  const result = this.unwrapResponseData(response.data);
7142
7168
  CoreEventManager.emit(`${this.eventPrefix}:email:sent`, { result });
@@ -8678,22 +8704,27 @@ async function initializeCore(config) {
8678
8704
  });
8679
8705
  }
8680
8706
  __name(initializeCore, "initializeCore");
8681
- function createStoreRegistry() {
8707
+ function createStoreRegistry(store) {
8682
8708
  return {
8683
8709
  getStore(key) {
8684
- const state = useRootStore.getState();
8710
+ const state = store.getState();
8685
8711
  if (!state) {
8686
8712
  logger4.warn(
8687
8713
  "Store state is undefined - store may not be hydrated yet. This can cause side effects if called during SSR or before initialization."
8688
8714
  );
8689
8715
  return void 0;
8690
8716
  }
8691
- return state[key];
8717
+ const slice = state[key];
8718
+ logger4.debug(`[StoreRegistry] getStore('${key}')`, {
8719
+ hasSlice: !!slice,
8720
+ sliceKeys: slice ? Object.keys(slice) : []
8721
+ });
8722
+ return slice;
8692
8723
  }
8693
8724
  };
8694
8725
  }
8695
8726
  __name(createStoreRegistry, "createStoreRegistry");
8696
- async function initializeServices(config) {
8727
+ async function initializeServices(config, store) {
8697
8728
  if (!config.services || config.services.length === 0) return;
8698
8729
  if (config.verbose) {
8699
8730
  globalThis.console.log("[PlyazProvider] Initializing domain services...");
@@ -8705,7 +8736,7 @@ async function initializeServices(config) {
8705
8736
  observability: config.observability,
8706
8737
  services: config.services,
8707
8738
  // Provide store registry for injecting stores into services
8708
- stores: createStoreRegistry()
8739
+ stores: createStoreRegistry(store)
8709
8740
  });
8710
8741
  if (config.verbose) {
8711
8742
  globalThis.console.log(
@@ -8742,6 +8773,7 @@ function createServicesObject(config, featureFlagStore) {
8742
8773
  __name(createServicesObject, "createServicesObject");
8743
8774
  function PlyazProvider({
8744
8775
  children,
8776
+ store,
8745
8777
  config,
8746
8778
  loading,
8747
8779
  error: errorComponent,
@@ -8755,8 +8787,9 @@ function PlyazProvider({
8755
8787
  const initialize = useCallback(async () => {
8756
8788
  try {
8757
8789
  setError(null);
8790
+ Core.setRootStoreHook(store);
8758
8791
  await initializeCore(config);
8759
- await initializeServices(config);
8792
+ await initializeServices(config, store);
8760
8793
  await initializeFeatureFlags(featureFlagStore);
8761
8794
  setIsReady(true);
8762
8795
  onReady?.(createServicesObject(config, featureFlagStore));
@@ -8766,7 +8799,7 @@ function PlyazProvider({
8766
8799
  onError?.(initError);
8767
8800
  globalThis.console.error("[PlyazProvider] Initialization failed:", initError);
8768
8801
  }
8769
- }, [config, featureFlagStore, onReady, onError]);
8802
+ }, [config, store, featureFlagStore, onReady, onError]);
8770
8803
  const reinitialize = useCallback(async () => {
8771
8804
  setIsReady(false);
8772
8805
  ServiceRegistry.disposeAll();
@@ -8815,8 +8848,9 @@ __name(PlyazProvider, "PlyazProvider");
8815
8848
  function usePlyaz() {
8816
8849
  const context = useContext(PlyazContext);
8817
8850
  if (!context) {
8818
- throw new Error(
8819
- "usePlyaz must be used within a PlyazProvider. Wrap your app with <PlyazProvider config={...}>...</PlyazProvider>"
8851
+ throw new CorePackageError(
8852
+ "usePlyaz must be used within a PlyazProvider. Wrap your app with <PlyazProvider config={...}>...</PlyazProvider>",
8853
+ ERROR_CODES.CORE_PROVIDER_NOT_FOUND
8820
8854
  );
8821
8855
  }
8822
8856
  return context;
@@ -8825,7 +8859,10 @@ __name(usePlyaz, "usePlyaz");
8825
8859
  function useApi() {
8826
8860
  const { api, isReady } = usePlyaz();
8827
8861
  if (!isReady || !api) {
8828
- throw new Error("API client is not ready. Make sure PlyazProvider has finished initializing.");
8862
+ throw new CorePackageError(
8863
+ "API client is not ready. Make sure PlyazProvider has finished initializing.",
8864
+ ERROR_CODES.CORE_PROVIDER_INITIALIZATION_FAILED
8865
+ );
8829
8866
  }
8830
8867
  return api;
8831
8868
  }
@@ -8868,7 +8905,10 @@ __name(useEnvironment, "useEnvironment");
8868
8905
  function useService(key) {
8869
8906
  const { getService, isReady } = usePlyaz();
8870
8907
  if (!isReady) {
8871
- throw new Error(`PlyazProvider not ready. Cannot get service '${key}'.`);
8908
+ throw new CorePackageError(
8909
+ `PlyazProvider not ready. Cannot get service '${key}'.`,
8910
+ ERROR_CODES.CORE_PROVIDER_INITIALIZATION_FAILED
8911
+ );
8872
8912
  }
8873
8913
  return useMemo(() => getService(key), [getService, key]);
8874
8914
  }