@plyaz/core 1.5.21 → 1.7.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.
@@ -2564,16 +2564,32 @@ var Core = class _Core {
2564
2564
  */
2565
2565
  this._logger = null;
2566
2566
  }
2567
+ static {
2568
+ /**
2569
+ * Logger transport config (set during initialize)
2570
+ */
2571
+ this._loggerTransport = "pino";
2572
+ }
2567
2573
  /**
2568
2574
  * Get or create the Core logger
2569
2575
  */
2570
2576
  static get logger() {
2571
2577
  _Core._logger ??= new logger$1.PackageLogger({
2572
2578
  packageName: "core",
2573
- service: "Core"
2579
+ service: "Core",
2580
+ transport: _Core._loggerTransport
2574
2581
  });
2575
2582
  return _Core._logger;
2576
2583
  }
2584
+ /**
2585
+ * Configure logger transport (call before first log)
2586
+ */
2587
+ static configureLogger(transport) {
2588
+ if (transport && transport !== _Core._loggerTransport) {
2589
+ _Core._loggerTransport = transport;
2590
+ _Core._logger = null;
2591
+ }
2592
+ }
2577
2593
  /**
2578
2594
  * Log a message during initialization.
2579
2595
  * Uses PackageLogger, respects verbose flag.
@@ -2865,6 +2881,7 @@ var Core = class _Core {
2865
2881
  * Initialize all core services
2866
2882
  */
2867
2883
  static async initialize(options = {}) {
2884
+ _Core.configureLogger(options.logger?.transport);
2868
2885
  if (_Core.initialized) {
2869
2886
  _Core.log("Already initialized, returning existing services", options.verbose);
2870
2887
  return _Core._coreServices;
@@ -4629,6 +4646,43 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4629
4646
  }
4630
4647
  }
4631
4648
  // ─────────────────────────────────────────────────────────────────────────
4649
+ // Response Unwrapping
4650
+ // ─────────────────────────────────────────────────────────────────────────
4651
+ /**
4652
+ * Unwrap response data if responseDataKey is configured.
4653
+ *
4654
+ * Handles wrapped API responses (e.g., SuccessResponseStandard) by extracting
4655
+ * the actual data from a nested property. Supports nested keys with dot notation.
4656
+ *
4657
+ * @param data - Raw response data from fetcher
4658
+ * @returns Unwrapped data or original data if no key configured
4659
+ *
4660
+ * @example
4661
+ * ```typescript
4662
+ * // Simple key: responseDataKey: 'data'
4663
+ * // Input: { success: true, message: '...', data: [...], codeStatus: 200 }
4664
+ * // Output: [...]
4665
+ *
4666
+ * // Nested key: responseDataKey: 'data.items'
4667
+ * // Input: { data: { items: [...], total: 100 } }
4668
+ * // Output: [...]
4669
+ * ```
4670
+ */
4671
+ unwrapResponseData(data) {
4672
+ const key = this.config.responseDataKey;
4673
+ if (!key) return data;
4674
+ const keys = key.split(".");
4675
+ let result = data;
4676
+ for (const k of keys) {
4677
+ if (result && typeof result === "object" && k in result) {
4678
+ result = result[k];
4679
+ } else {
4680
+ return data;
4681
+ }
4682
+ }
4683
+ return result;
4684
+ }
4685
+ // ─────────────────────────────────────────────────────────────────────────
4632
4686
  // Generic CRUD Operations
4633
4687
  // ─────────────────────────────────────────────────────────────────────────
4634
4688
  /**
@@ -4693,7 +4747,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4693
4747
  }
4694
4748
  );
4695
4749
  }
4696
- const entities = (response.data ?? []).map((dto) => this.mapper.toDomain(dto));
4750
+ const rawData = this.unwrapResponseData(response.data);
4751
+ const entities = (rawData ?? []).map((dto) => this.mapper.toDomain(dto));
4697
4752
  this.syncEntitiesToStore(entities);
4698
4753
  await this.afterFetchAll?.(entities, query);
4699
4754
  this.emitEvent("fetched", {
@@ -4745,7 +4800,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4745
4800
  cause: response.error ?? void 0
4746
4801
  });
4747
4802
  }
4748
- const entity = response.data ? this.mapper.toDomain(response.data) : null;
4803
+ const rawData = this.unwrapResponseData(response.data);
4804
+ const entity = rawData ? this.mapper.toDomain(rawData) : null;
4749
4805
  if (entity) {
4750
4806
  await this.afterFetchById?.(entity);
4751
4807
  }
@@ -4827,7 +4883,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4827
4883
  }
4828
4884
  );
4829
4885
  }
4830
- const entity = this.mapper.toDomain(response.data);
4886
+ const rawData = this.unwrapResponseData(response.data);
4887
+ const entity = this.mapper.toDomain(rawData);
4831
4888
  let storeState;
4832
4889
  if (isOptimistic && optimisticEntity) {
4833
4890
  const tempId = optimisticEntity.id;
@@ -4920,7 +4977,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4920
4977
  }
4921
4978
  );
4922
4979
  }
4923
- const serverEntity = this.mapper.toDomain(response.data);
4980
+ const rawData = this.unwrapResponseData(response.data);
4981
+ const serverEntity = this.mapper.toDomain(rawData);
4924
4982
  let finalEntity = serverEntity;
4925
4983
  if (isOptimistic && optimisticStoreState) {
4926
4984
  finalEntity = this.resolveConflict({ id, ...data }, serverEntity);
@@ -5947,14 +6005,15 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
5947
6005
  autoFetch: false,
5948
6006
  pollingInterval: 0,
5949
6007
  ...config,
6008
+ // Unwrap SuccessResponseStandard: { success, message, data, codeStatus }
6009
+ // Base class will extract 'data' property automatically
6010
+ responseDataKey: "data",
5950
6011
  // Fetchers - using apiClient directly for testing/example purposes
5951
6012
  // In production, these would be imported from @plyaz/api services
5952
6013
  // Note: Validation is handled by validator class, mapping by mapper class
5953
6014
  fetchers: {
5954
6015
  fetchAll: /* @__PURE__ */ __name(async (query) => {
5955
- return this.apiClient.get(apiBasePath, {
5956
- params: query
5957
- });
6016
+ return this.apiClient.get(apiBasePath, { params: query });
5958
6017
  }, "fetchAll"),
5959
6018
  fetchById: /* @__PURE__ */ __name(async (id) => {
5960
6019
  return this.apiClient.get(`${apiBasePath}/${id}`);
@@ -5964,10 +6023,7 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
5964
6023
  }, "create"),
5965
6024
  update: /* @__PURE__ */ __name(async (payload) => {
5966
6025
  const { id, data } = payload;
5967
- return this.apiClient.patch(
5968
- `${apiBasePath}/${id}`,
5969
- data
5970
- );
6026
+ return this.apiClient.patch(`${apiBasePath}/${id}`, data);
5971
6027
  }, "update"),
5972
6028
  delete: /* @__PURE__ */ __name(async (id) => {
5973
6029
  return this.apiClient.delete(`${apiBasePath}/${id}`);