@plyaz/core 1.5.21 → 1.6.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.
@@ -4629,6 +4629,43 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4629
4629
  }
4630
4630
  }
4631
4631
  // ─────────────────────────────────────────────────────────────────────────
4632
+ // Response Unwrapping
4633
+ // ─────────────────────────────────────────────────────────────────────────
4634
+ /**
4635
+ * Unwrap response data if responseDataKey is configured.
4636
+ *
4637
+ * Handles wrapped API responses (e.g., SuccessResponseStandard) by extracting
4638
+ * the actual data from a nested property. Supports nested keys with dot notation.
4639
+ *
4640
+ * @param data - Raw response data from fetcher
4641
+ * @returns Unwrapped data or original data if no key configured
4642
+ *
4643
+ * @example
4644
+ * ```typescript
4645
+ * // Simple key: responseDataKey: 'data'
4646
+ * // Input: { success: true, message: '...', data: [...], codeStatus: 200 }
4647
+ * // Output: [...]
4648
+ *
4649
+ * // Nested key: responseDataKey: 'data.items'
4650
+ * // Input: { data: { items: [...], total: 100 } }
4651
+ * // Output: [...]
4652
+ * ```
4653
+ */
4654
+ unwrapResponseData(data) {
4655
+ const key = this.config.responseDataKey;
4656
+ if (!key) return data;
4657
+ const keys = key.split(".");
4658
+ let result = data;
4659
+ for (const k of keys) {
4660
+ if (result && typeof result === "object" && k in result) {
4661
+ result = result[k];
4662
+ } else {
4663
+ return data;
4664
+ }
4665
+ }
4666
+ return result;
4667
+ }
4668
+ // ─────────────────────────────────────────────────────────────────────────
4632
4669
  // Generic CRUD Operations
4633
4670
  // ─────────────────────────────────────────────────────────────────────────
4634
4671
  /**
@@ -4693,7 +4730,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4693
4730
  }
4694
4731
  );
4695
4732
  }
4696
- const entities = (response.data ?? []).map((dto) => this.mapper.toDomain(dto));
4733
+ const rawData = this.unwrapResponseData(response.data);
4734
+ const entities = (rawData ?? []).map((dto) => this.mapper.toDomain(dto));
4697
4735
  this.syncEntitiesToStore(entities);
4698
4736
  await this.afterFetchAll?.(entities, query);
4699
4737
  this.emitEvent("fetched", {
@@ -4745,7 +4783,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4745
4783
  cause: response.error ?? void 0
4746
4784
  });
4747
4785
  }
4748
- const entity = response.data ? this.mapper.toDomain(response.data) : null;
4786
+ const rawData = this.unwrapResponseData(response.data);
4787
+ const entity = rawData ? this.mapper.toDomain(rawData) : null;
4749
4788
  if (entity) {
4750
4789
  await this.afterFetchById?.(entity);
4751
4790
  }
@@ -4827,7 +4866,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4827
4866
  }
4828
4867
  );
4829
4868
  }
4830
- const entity = this.mapper.toDomain(response.data);
4869
+ const rawData = this.unwrapResponseData(response.data);
4870
+ const entity = this.mapper.toDomain(rawData);
4831
4871
  let storeState;
4832
4872
  if (isOptimistic && optimisticEntity) {
4833
4873
  const tempId = optimisticEntity.id;
@@ -4920,7 +4960,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4920
4960
  }
4921
4961
  );
4922
4962
  }
4923
- const serverEntity = this.mapper.toDomain(response.data);
4963
+ const rawData = this.unwrapResponseData(response.data);
4964
+ const serverEntity = this.mapper.toDomain(rawData);
4924
4965
  let finalEntity = serverEntity;
4925
4966
  if (isOptimistic && optimisticStoreState) {
4926
4967
  finalEntity = this.resolveConflict({ id, ...data }, serverEntity);
@@ -5947,14 +5988,15 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
5947
5988
  autoFetch: false,
5948
5989
  pollingInterval: 0,
5949
5990
  ...config,
5991
+ // Unwrap SuccessResponseStandard: { success, message, data, codeStatus }
5992
+ // Base class will extract 'data' property automatically
5993
+ responseDataKey: "data",
5950
5994
  // Fetchers - using apiClient directly for testing/example purposes
5951
5995
  // In production, these would be imported from @plyaz/api services
5952
5996
  // Note: Validation is handled by validator class, mapping by mapper class
5953
5997
  fetchers: {
5954
5998
  fetchAll: /* @__PURE__ */ __name(async (query) => {
5955
- return this.apiClient.get(apiBasePath, {
5956
- params: query
5957
- });
5999
+ return this.apiClient.get(apiBasePath, { params: query });
5958
6000
  }, "fetchAll"),
5959
6001
  fetchById: /* @__PURE__ */ __name(async (id) => {
5960
6002
  return this.apiClient.get(`${apiBasePath}/${id}`);
@@ -5964,10 +6006,7 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
5964
6006
  }, "create"),
5965
6007
  update: /* @__PURE__ */ __name(async (payload) => {
5966
6008
  const { id, data } = payload;
5967
- return this.apiClient.patch(
5968
- `${apiBasePath}/${id}`,
5969
- data
5970
- );
6009
+ return this.apiClient.patch(`${apiBasePath}/${id}`, data);
5971
6010
  }, "update"),
5972
6011
  delete: /* @__PURE__ */ __name(async (id) => {
5973
6012
  return this.apiClient.delete(`${apiBasePath}/${id}`);