@plyaz/core 1.8.1 → 1.8.3

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.
package/dist/index.js CHANGED
@@ -34979,10 +34979,16 @@ var ServiceRegistry = class _ServiceRegistry {
34979
34979
  return initPromise;
34980
34980
  }
34981
34981
  /** Build stores for service injection */
34982
- static buildStoresForService(config) {
34982
+ // eslint-disable-next-line complexity
34983
+ static buildStoresForService(config, entry) {
34983
34984
  const allKeys = /* @__PURE__ */ new Set();
34984
34985
  if (config.store) {
34985
34986
  allKeys.add(config.store);
34987
+ } else if ("primaryStoreKey" in entry.service && typeof entry.service.primaryStoreKey === "string") {
34988
+ allKeys.add(entry.service.primaryStoreKey);
34989
+ _ServiceRegistry.logger.debug(
34990
+ `Auto-resolved store key '${entry.service.primaryStoreKey}' from service class`
34991
+ );
34986
34992
  }
34987
34993
  if (config.readStores) {
34988
34994
  config.readStores.forEach((key) => allKeys.add(key));
@@ -35020,7 +35026,7 @@ var ServiceRegistry = class _ServiceRegistry {
35020
35026
  const observability = _ServiceRegistry.buildObservabilityConfig(config, entry);
35021
35027
  const storage = await _ServiceRegistry.buildStorageConfig(config, entry);
35022
35028
  const notifications = await _ServiceRegistry.buildNotificationsConfig(config, entry);
35023
- const stores = _ServiceRegistry.buildStoresForService(config);
35029
+ const stores = _ServiceRegistry.buildStoresForService(config, entry);
35024
35030
  let observabilityInstance = observability?.instance;
35025
35031
  if (observability?.dedicated && observability.config) {
35026
35032
  observabilityInstance = await _ServiceRegistry.createDedicatedObservability(
@@ -36549,6 +36555,13 @@ var Core = class _Core {
36549
36555
  /** Create fetch flags function */
36550
36556
  static createFetchFlagsFn(config, verbose) {
36551
36557
  return async () => {
36558
+ if (config?.provider !== "api") {
36559
+ _Core.log(
36560
+ `Feature flags using ${config?.provider ?? "default"} provider (no API fetch)`,
36561
+ verbose
36562
+ );
36563
+ return config?.defaults ?? {};
36564
+ }
36552
36565
  try {
36553
36566
  const client = ApiClientService.getClient();
36554
36567
  const endpoint = config?.apiEndpoint ?? "/feature-flags";
@@ -36568,6 +36581,7 @@ var Core = class _Core {
36568
36581
  /**
36569
36582
  * Initialize feature flags slice within root store
36570
36583
  */
36584
+ // eslint-disable-next-line complexity
36571
36585
  static async initializeFeatureFlags(config, verbose) {
36572
36586
  if (config?.enabled === false) {
36573
36587
  _Core.log("Feature flags disabled by configuration", verbose);
@@ -36580,11 +36594,16 @@ var Core = class _Core {
36580
36594
  );
36581
36595
  }
36582
36596
  _Core._flagConfig = config ?? {};
36583
- _Core.log("Initializing feature flags from root store...", verbose);
36597
+ const isApiProvider = config?.provider === "api";
36598
+ _Core.log(
36599
+ `Initializing feature flags from root store (provider: ${config?.provider ?? "default"})...`,
36600
+ verbose
36601
+ );
36584
36602
  const state = _Core._rootStore.getState();
36585
36603
  await state.featureFlags.initialize({
36586
36604
  defaults: _Core._flagConfig.defaults,
36587
- polling: _Core._flagConfig.polling,
36605
+ // Only enable polling for API provider
36606
+ polling: isApiProvider ? _Core._flagConfig.polling : void 0,
36588
36607
  fetchFn: _Core.createFetchFlagsFn(_Core._flagConfig, verbose),
36589
36608
  onFlagChange: _Core._flagConfig.onFlagChange,
36590
36609
  onError: _Core._flagConfig.onError
@@ -36991,13 +37010,14 @@ var BaseDomainService = class {
36991
37010
  );
36992
37011
  }
36993
37012
  };
36994
- var BaseFrontendDomainService = class extends BaseDomainService {
37013
+ var BaseFrontendDomainService = class _BaseFrontendDomainService extends BaseDomainService {
36995
37014
  // ─────────────────────────────────────────────────────────────────────────
36996
37015
  // Constructor
36997
37016
  // ─────────────────────────────────────────────────────────────────────────
36998
37017
  // eslint-disable-next-line complexity
36999
37018
  constructor(config) {
37000
- super(config);
37019
+ const resolvedConfig = _BaseFrontendDomainService.resolveApiClientConfig(config);
37020
+ super(resolvedConfig);
37001
37021
  // ─────────────────────────────────────────────────────────────────────────
37002
37022
  // Store Properties
37003
37023
  // ─────────────────────────────────────────────────────────────────────────
@@ -37023,6 +37043,11 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37023
37043
  const serviceConfig = config.serviceConfig;
37024
37044
  if (serviceConfig.store !== void 0) {
37025
37045
  this.primaryStoreKey = serviceConfig.store;
37046
+ } else {
37047
+ const staticKey = this.constructor.primaryStoreKey;
37048
+ if (staticKey) {
37049
+ this.primaryStoreKey = staticKey;
37050
+ }
37026
37051
  }
37027
37052
  if (serviceConfig.readStores !== void 0) {
37028
37053
  this.readStoreKeys = Array.from(
@@ -37041,9 +37066,12 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37041
37066
  this._primaryStore = primaryStore;
37042
37067
  this.logDebug(`Connected primary store: '${this.primaryStoreKey}'`);
37043
37068
  } else {
37044
- this.logWarn(
37045
- `Primary store '${this.primaryStoreKey}' not found. Store mutations (setData/updateData/setLoading) will be disabled. Configure stores in PlyazProvider to enable store integration.`
37046
- );
37069
+ const isServer = typeof globalThis.window === "undefined";
37070
+ if (!isServer) {
37071
+ this.logWarn(
37072
+ `Primary store '${this.primaryStoreKey}' not found. Store mutations (setData/updateData/setLoading) will be disabled. Configure stores in PlyazProvider to enable store integration.`
37073
+ );
37074
+ }
37047
37075
  }
37048
37076
  }
37049
37077
  for (const key of this.readStoreKeys) {
@@ -37068,6 +37096,66 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37068
37096
  static {
37069
37097
  __name(this, "BaseFrontendDomainService");
37070
37098
  }
37099
+ /**
37100
+ * Auto-resolve apiClientConfig from serviceConfig.apiBasePath if not explicitly provided.
37101
+ * This reduces boilerplate in child services - they only need to set apiBasePath in config.
37102
+ *
37103
+ * Priority:
37104
+ * 1. Explicit apiClientConfig.baseURL (full control)
37105
+ * 2. serviceConfig.apiBasePath (auto-constructed, merged with injected options)
37106
+ * 3. undefined (no API client)
37107
+ *
37108
+ * When auto-constructing, merges injected API options (headers, timeout, etc.)
37109
+ * with the service's apiBasePath as baseURL.
37110
+ */
37111
+ static resolveApiClientConfig(config) {
37112
+ if (config.apiClientConfig?.baseURL) {
37113
+ return config;
37114
+ }
37115
+ const apiBasePath = config.serviceConfig.apiBasePath;
37116
+ if (apiBasePath) {
37117
+ const injectedOptions = config.injected?.api ? {} : config.apiClientConfig ?? {};
37118
+ return {
37119
+ ...config,
37120
+ apiClientConfig: {
37121
+ ...injectedOptions,
37122
+ baseURL: apiBasePath
37123
+ // Service's apiBasePath always takes precedence
37124
+ }
37125
+ };
37126
+ }
37127
+ return config;
37128
+ }
37129
+ /**
37130
+ * Initialize service and wait for API client to be ready.
37131
+ * Call this in child service's static create() method after constructing the instance.
37132
+ *
37133
+ * @param service - The service instance to initialize
37134
+ * @returns The initialized service (same instance, for chaining)
37135
+ *
37136
+ * @example
37137
+ * ```typescript
37138
+ * static async create(config, options): Promise<MyService> {
37139
+ * const service = new MyService(config, options);
37140
+ * return this.initializeService(service);
37141
+ * }
37142
+ * ```
37143
+ */
37144
+ static async initializeService(service) {
37145
+ await service.ensureApiClientInitialized();
37146
+ return service;
37147
+ }
37148
+ /**
37149
+ * Ensure service is ready for operations.
37150
+ * Checks enabled/available status AND waits for API client initialization.
37151
+ * Call this at the start of all CRUD methods.
37152
+ *
37153
+ * @throws CorePackageError if service is not enabled or available
37154
+ */
37155
+ async ensureReady() {
37156
+ this.assertReady();
37157
+ await this.ensureApiClientInitialized();
37158
+ }
37071
37159
  // ─────────────────────────────────────────────────────────────────────────
37072
37160
  // Store Management (Public API)
37073
37161
  // ─────────────────────────────────────────────────────────────────────────
@@ -37540,7 +37628,7 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37540
37628
  */
37541
37629
  // eslint-disable-next-line complexity
37542
37630
  async fetchAll(query, options) {
37543
- this.assertReady();
37631
+ await this.ensureReady();
37544
37632
  const startTime = Date.now();
37545
37633
  if (!this.config.fetchers?.fetchAll) {
37546
37634
  throw new errors.CorePackageError(
@@ -37625,7 +37713,7 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37625
37713
  */
37626
37714
  // eslint-disable-next-line complexity
37627
37715
  async fetchById(id, options) {
37628
- this.assertReady();
37716
+ await this.ensureReady();
37629
37717
  const startTime = Date.now();
37630
37718
  if (!this.config.fetchers?.fetchById) {
37631
37719
  throw new errors.CorePackageError(
@@ -37686,7 +37774,7 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37686
37774
  */
37687
37775
  // eslint-disable-next-line complexity, max-lines-per-function
37688
37776
  async create(data, options) {
37689
- this.assertReady();
37777
+ await this.ensureReady();
37690
37778
  const startTime = Date.now();
37691
37779
  if (!this.config.fetchers?.create) {
37692
37780
  throw new errors.CorePackageError(
@@ -37779,7 +37867,7 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37779
37867
  */
37780
37868
  // eslint-disable-next-line complexity, max-lines-per-function
37781
37869
  async update(id, data, options) {
37782
- this.assertReady();
37870
+ await this.ensureReady();
37783
37871
  const startTime = Date.now();
37784
37872
  if (!this.config.fetchers?.update) {
37785
37873
  throw new errors.CorePackageError(
@@ -37868,7 +37956,7 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37868
37956
  */
37869
37957
  // eslint-disable-next-line complexity
37870
37958
  async delete(id, options) {
37871
- this.assertReady();
37959
+ await this.ensureReady();
37872
37960
  const startTime = Date.now();
37873
37961
  if (!this.config.fetchers?.delete) {
37874
37962
  throw new errors.CorePackageError(
@@ -45238,14 +45326,11 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
45238
45326
  // ─────────────────────────────────────────────────────────────────────────
45239
45327
  // Constructor
45240
45328
  // ─────────────────────────────────────────────────────────────────────────
45241
- // eslint-disable-next-line complexity
45242
45329
  constructor(config = {}, options) {
45243
45330
  const apiBasePath = config.apiBasePath ?? "/api/examples";
45244
45331
  super({
45245
45332
  serviceName: "ExampleFrontendService",
45246
45333
  supportedRuntimes: ["frontend"],
45247
- // API client config - uses injected options or creates from apiBasePath
45248
- apiClientConfig: options?.apiClient?.options ?? { baseURL: apiBasePath },
45249
45334
  serviceConfig: {
45250
45335
  enabled: true,
45251
45336
  apiBasePath,
@@ -45309,10 +45394,6 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
45309
45394
  * Required by BaseFrontendDomainService
45310
45395
  */
45311
45396
  this.eventPrefix = "example";
45312
- /**
45313
- * Primary store key - the store this service can mutate
45314
- */
45315
- this.primaryStoreKey = store.STORE_KEYS.EXAMPLE;
45316
45397
  /**
45317
45398
  * Read-only store keys - inherits error and featureFlags from base
45318
45399
  * No need to redeclare them - they're always included by default
@@ -45335,6 +45416,14 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
45335
45416
  // ─────────────────────────────────────────────────────────────────────────
45336
45417
  this.serviceKey = core.SERVICE_KEYS.EXAMPLE_FRONTEND;
45337
45418
  }
45419
+ static {
45420
+ /**
45421
+ * Primary store key for this service.
45422
+ * Used by ServiceRegistry to auto-inject the store if not specified in config.
45423
+ * Also used by base class constructor to set instance primaryStoreKey.
45424
+ */
45425
+ this.primaryStoreKey = store.STORE_KEYS.EXAMPLE;
45426
+ }
45338
45427
  /**
45339
45428
  * Factory method for ServiceRegistry auto-initialization.
45340
45429
  * Creates and initializes the service instance.