@plyaz/core 1.5.20 → 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.
@@ -4627,6 +4627,43 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4627
4627
  }
4628
4628
  }
4629
4629
  // ─────────────────────────────────────────────────────────────────────────
4630
+ // Response Unwrapping
4631
+ // ─────────────────────────────────────────────────────────────────────────
4632
+ /**
4633
+ * Unwrap response data if responseDataKey is configured.
4634
+ *
4635
+ * Handles wrapped API responses (e.g., SuccessResponseStandard) by extracting
4636
+ * the actual data from a nested property. Supports nested keys with dot notation.
4637
+ *
4638
+ * @param data - Raw response data from fetcher
4639
+ * @returns Unwrapped data or original data if no key configured
4640
+ *
4641
+ * @example
4642
+ * ```typescript
4643
+ * // Simple key: responseDataKey: 'data'
4644
+ * // Input: { success: true, message: '...', data: [...], codeStatus: 200 }
4645
+ * // Output: [...]
4646
+ *
4647
+ * // Nested key: responseDataKey: 'data.items'
4648
+ * // Input: { data: { items: [...], total: 100 } }
4649
+ * // Output: [...]
4650
+ * ```
4651
+ */
4652
+ unwrapResponseData(data) {
4653
+ const key = this.config.responseDataKey;
4654
+ if (!key) return data;
4655
+ const keys = key.split(".");
4656
+ let result = data;
4657
+ for (const k of keys) {
4658
+ if (result && typeof result === "object" && k in result) {
4659
+ result = result[k];
4660
+ } else {
4661
+ return data;
4662
+ }
4663
+ }
4664
+ return result;
4665
+ }
4666
+ // ─────────────────────────────────────────────────────────────────────────
4630
4667
  // Generic CRUD Operations
4631
4668
  // ─────────────────────────────────────────────────────────────────────────
4632
4669
  /**
@@ -4691,7 +4728,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4691
4728
  }
4692
4729
  );
4693
4730
  }
4694
- const entities = (response.data ?? []).map((dto) => this.mapper.toDomain(dto));
4731
+ const rawData = this.unwrapResponseData(response.data);
4732
+ const entities = (rawData ?? []).map((dto) => this.mapper.toDomain(dto));
4695
4733
  this.syncEntitiesToStore(entities);
4696
4734
  await this.afterFetchAll?.(entities, query);
4697
4735
  this.emitEvent("fetched", {
@@ -4743,7 +4781,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4743
4781
  cause: response.error ?? void 0
4744
4782
  });
4745
4783
  }
4746
- const entity = response.data ? this.mapper.toDomain(response.data) : null;
4784
+ const rawData = this.unwrapResponseData(response.data);
4785
+ const entity = rawData ? this.mapper.toDomain(rawData) : null;
4747
4786
  if (entity) {
4748
4787
  await this.afterFetchById?.(entity);
4749
4788
  }
@@ -4825,7 +4864,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4825
4864
  }
4826
4865
  );
4827
4866
  }
4828
- const entity = this.mapper.toDomain(response.data);
4867
+ const rawData = this.unwrapResponseData(response.data);
4868
+ const entity = this.mapper.toDomain(rawData);
4829
4869
  let storeState;
4830
4870
  if (isOptimistic && optimisticEntity) {
4831
4871
  const tempId = optimisticEntity.id;
@@ -4918,7 +4958,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
4918
4958
  }
4919
4959
  );
4920
4960
  }
4921
- const serverEntity = this.mapper.toDomain(response.data);
4961
+ const rawData = this.unwrapResponseData(response.data);
4962
+ const serverEntity = this.mapper.toDomain(rawData);
4922
4963
  let finalEntity = serverEntity;
4923
4964
  if (isOptimistic && optimisticStoreState) {
4924
4965
  finalEntity = this.resolveConflict({ id, ...data }, serverEntity);
@@ -5937,20 +5978,23 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
5937
5978
  super({
5938
5979
  serviceName: "ExampleFrontendService",
5939
5980
  supportedRuntimes: ["frontend"],
5981
+ // API client config - uses injected options or creates from apiBasePath
5982
+ apiClientConfig: options?.apiClient?.options ?? { baseURL: apiBasePath },
5940
5983
  serviceConfig: {
5941
5984
  enabled: true,
5942
5985
  apiBasePath,
5943
5986
  autoFetch: false,
5944
5987
  pollingInterval: 0,
5945
5988
  ...config,
5989
+ // Unwrap SuccessResponseStandard: { success, message, data, codeStatus }
5990
+ // Base class will extract 'data' property automatically
5991
+ responseDataKey: "data",
5946
5992
  // Fetchers - using apiClient directly for testing/example purposes
5947
5993
  // In production, these would be imported from @plyaz/api services
5948
5994
  // Note: Validation is handled by validator class, mapping by mapper class
5949
5995
  fetchers: {
5950
5996
  fetchAll: /* @__PURE__ */ __name(async (query) => {
5951
- return this.apiClient.get(apiBasePath, {
5952
- params: query
5953
- });
5997
+ return this.apiClient.get(apiBasePath, { params: query });
5954
5998
  }, "fetchAll"),
5955
5999
  fetchById: /* @__PURE__ */ __name(async (id) => {
5956
6000
  return this.apiClient.get(`${apiBasePath}/${id}`);
@@ -5960,10 +6004,7 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
5960
6004
  }, "create"),
5961
6005
  update: /* @__PURE__ */ __name(async (payload) => {
5962
6006
  const { id, data } = payload;
5963
- return this.apiClient.patch(
5964
- `${apiBasePath}/${id}`,
5965
- data
5966
- );
6007
+ return this.apiClient.patch(`${apiBasePath}/${id}`, data);
5967
6008
  }, "update"),
5968
6009
  delete: /* @__PURE__ */ __name(async (id) => {
5969
6010
  return this.apiClient.delete(`${apiBasePath}/${id}`);