@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.
@@ -4798,6 +4798,43 @@ var init_BaseFrontendDomainService = __esm({
4798
4798
  }
4799
4799
  }
4800
4800
  // ─────────────────────────────────────────────────────────────────────────
4801
+ // Response Unwrapping
4802
+ // ─────────────────────────────────────────────────────────────────────────
4803
+ /**
4804
+ * Unwrap response data if responseDataKey is configured.
4805
+ *
4806
+ * Handles wrapped API responses (e.g., SuccessResponseStandard) by extracting
4807
+ * the actual data from a nested property. Supports nested keys with dot notation.
4808
+ *
4809
+ * @param data - Raw response data from fetcher
4810
+ * @returns Unwrapped data or original data if no key configured
4811
+ *
4812
+ * @example
4813
+ * ```typescript
4814
+ * // Simple key: responseDataKey: 'data'
4815
+ * // Input: { success: true, message: '...', data: [...], codeStatus: 200 }
4816
+ * // Output: [...]
4817
+ *
4818
+ * // Nested key: responseDataKey: 'data.items'
4819
+ * // Input: { data: { items: [...], total: 100 } }
4820
+ * // Output: [...]
4821
+ * ```
4822
+ */
4823
+ unwrapResponseData(data) {
4824
+ const key = this.config.responseDataKey;
4825
+ if (!key) return data;
4826
+ const keys = key.split(".");
4827
+ let result = data;
4828
+ for (const k of keys) {
4829
+ if (result && typeof result === "object" && k in result) {
4830
+ result = result[k];
4831
+ } else {
4832
+ return data;
4833
+ }
4834
+ }
4835
+ return result;
4836
+ }
4837
+ // ─────────────────────────────────────────────────────────────────────────
4801
4838
  // Generic CRUD Operations
4802
4839
  // ─────────────────────────────────────────────────────────────────────────
4803
4840
  /**
@@ -4862,7 +4899,8 @@ var init_BaseFrontendDomainService = __esm({
4862
4899
  }
4863
4900
  );
4864
4901
  }
4865
- const entities = (response.data ?? []).map((dto) => this.mapper.toDomain(dto));
4902
+ const rawData = this.unwrapResponseData(response.data);
4903
+ const entities = (rawData ?? []).map((dto) => this.mapper.toDomain(dto));
4866
4904
  this.syncEntitiesToStore(entities);
4867
4905
  await this.afterFetchAll?.(entities, query);
4868
4906
  this.emitEvent("fetched", {
@@ -4914,7 +4952,8 @@ var init_BaseFrontendDomainService = __esm({
4914
4952
  cause: response.error ?? void 0
4915
4953
  });
4916
4954
  }
4917
- const entity = response.data ? this.mapper.toDomain(response.data) : null;
4955
+ const rawData = this.unwrapResponseData(response.data);
4956
+ const entity = rawData ? this.mapper.toDomain(rawData) : null;
4918
4957
  if (entity) {
4919
4958
  await this.afterFetchById?.(entity);
4920
4959
  }
@@ -4996,7 +5035,8 @@ var init_BaseFrontendDomainService = __esm({
4996
5035
  }
4997
5036
  );
4998
5037
  }
4999
- const entity = this.mapper.toDomain(response.data);
5038
+ const rawData = this.unwrapResponseData(response.data);
5039
+ const entity = this.mapper.toDomain(rawData);
5000
5040
  let storeState;
5001
5041
  if (isOptimistic && optimisticEntity) {
5002
5042
  const tempId = optimisticEntity.id;
@@ -5089,7 +5129,8 @@ var init_BaseFrontendDomainService = __esm({
5089
5129
  }
5090
5130
  );
5091
5131
  }
5092
- const serverEntity = this.mapper.toDomain(response.data);
5132
+ const rawData = this.unwrapResponseData(response.data);
5133
+ const serverEntity = this.mapper.toDomain(rawData);
5093
5134
  let finalEntity = serverEntity;
5094
5135
  if (isOptimistic && optimisticStoreState) {
5095
5136
  finalEntity = this.resolveConflict({ id, ...data }, serverEntity);
@@ -7570,14 +7611,15 @@ var init_FrontendExampleDomainService = __esm({
7570
7611
  autoFetch: false,
7571
7612
  pollingInterval: 0,
7572
7613
  ...config,
7614
+ // Unwrap SuccessResponseStandard: { success, message, data, codeStatus }
7615
+ // Base class will extract 'data' property automatically
7616
+ responseDataKey: "data",
7573
7617
  // Fetchers - using apiClient directly for testing/example purposes
7574
7618
  // In production, these would be imported from @plyaz/api services
7575
7619
  // Note: Validation is handled by validator class, mapping by mapper class
7576
7620
  fetchers: {
7577
7621
  fetchAll: /* @__PURE__ */ __name(async (query) => {
7578
- return this.apiClient.get(apiBasePath, {
7579
- params: query
7580
- });
7622
+ return this.apiClient.get(apiBasePath, { params: query });
7581
7623
  }, "fetchAll"),
7582
7624
  fetchById: /* @__PURE__ */ __name(async (id) => {
7583
7625
  return this.apiClient.get(`${apiBasePath}/${id}`);
@@ -7587,10 +7629,7 @@ var init_FrontendExampleDomainService = __esm({
7587
7629
  }, "create"),
7588
7630
  update: /* @__PURE__ */ __name(async (payload) => {
7589
7631
  const { id, data } = payload;
7590
- return this.apiClient.patch(
7591
- `${apiBasePath}/${id}`,
7592
- data
7593
- );
7632
+ return this.apiClient.patch(`${apiBasePath}/${id}`, data);
7594
7633
  }, "update"),
7595
7634
  delete: /* @__PURE__ */ __name(async (id) => {
7596
7635
  return this.apiClient.delete(`${apiBasePath}/${id}`);