@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.
package/dist/index.js CHANGED
@@ -37353,6 +37353,43 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37353
37353
  }
37354
37354
  }
37355
37355
  // ─────────────────────────────────────────────────────────────────────────
37356
+ // Response Unwrapping
37357
+ // ─────────────────────────────────────────────────────────────────────────
37358
+ /**
37359
+ * Unwrap response data if responseDataKey is configured.
37360
+ *
37361
+ * Handles wrapped API responses (e.g., SuccessResponseStandard) by extracting
37362
+ * the actual data from a nested property. Supports nested keys with dot notation.
37363
+ *
37364
+ * @param data - Raw response data from fetcher
37365
+ * @returns Unwrapped data or original data if no key configured
37366
+ *
37367
+ * @example
37368
+ * ```typescript
37369
+ * // Simple key: responseDataKey: 'data'
37370
+ * // Input: { success: true, message: '...', data: [...], codeStatus: 200 }
37371
+ * // Output: [...]
37372
+ *
37373
+ * // Nested key: responseDataKey: 'data.items'
37374
+ * // Input: { data: { items: [...], total: 100 } }
37375
+ * // Output: [...]
37376
+ * ```
37377
+ */
37378
+ unwrapResponseData(data) {
37379
+ const key = this.config.responseDataKey;
37380
+ if (!key) return data;
37381
+ const keys = key.split(".");
37382
+ let result2 = data;
37383
+ for (const k of keys) {
37384
+ if (result2 && typeof result2 === "object" && k in result2) {
37385
+ result2 = result2[k];
37386
+ } else {
37387
+ return data;
37388
+ }
37389
+ }
37390
+ return result2;
37391
+ }
37392
+ // ─────────────────────────────────────────────────────────────────────────
37356
37393
  // Generic CRUD Operations
37357
37394
  // ─────────────────────────────────────────────────────────────────────────
37358
37395
  /**
@@ -37417,7 +37454,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37417
37454
  }
37418
37455
  );
37419
37456
  }
37420
- const entities = (response.data ?? []).map((dto) => this.mapper.toDomain(dto));
37457
+ const rawData = this.unwrapResponseData(response.data);
37458
+ const entities = (rawData ?? []).map((dto) => this.mapper.toDomain(dto));
37421
37459
  this.syncEntitiesToStore(entities);
37422
37460
  await this.afterFetchAll?.(entities, query);
37423
37461
  this.emitEvent("fetched", {
@@ -37469,7 +37507,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37469
37507
  cause: response.error ?? void 0
37470
37508
  });
37471
37509
  }
37472
- const entity = response.data ? this.mapper.toDomain(response.data) : null;
37510
+ const rawData = this.unwrapResponseData(response.data);
37511
+ const entity = rawData ? this.mapper.toDomain(rawData) : null;
37473
37512
  if (entity) {
37474
37513
  await this.afterFetchById?.(entity);
37475
37514
  }
@@ -37551,7 +37590,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37551
37590
  }
37552
37591
  );
37553
37592
  }
37554
- const entity = this.mapper.toDomain(response.data);
37593
+ const rawData = this.unwrapResponseData(response.data);
37594
+ const entity = this.mapper.toDomain(rawData);
37555
37595
  let storeState;
37556
37596
  if (isOptimistic && optimisticEntity) {
37557
37597
  const tempId = optimisticEntity.id;
@@ -37644,7 +37684,8 @@ var BaseFrontendDomainService = class extends BaseDomainService {
37644
37684
  }
37645
37685
  );
37646
37686
  }
37647
- const serverEntity = this.mapper.toDomain(response.data);
37687
+ const rawData = this.unwrapResponseData(response.data);
37688
+ const serverEntity = this.mapper.toDomain(rawData);
37648
37689
  let finalEntity = serverEntity;
37649
37690
  if (isOptimistic && optimisticStoreState) {
37650
37691
  finalEntity = this.resolveConflict({ id, ...data }, serverEntity);
@@ -45070,14 +45111,15 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
45070
45111
  autoFetch: false,
45071
45112
  pollingInterval: 0,
45072
45113
  ...config,
45114
+ // Unwrap SuccessResponseStandard: { success, message, data, codeStatus }
45115
+ // Base class will extract 'data' property automatically
45116
+ responseDataKey: "data",
45073
45117
  // Fetchers - using apiClient directly for testing/example purposes
45074
45118
  // In production, these would be imported from @plyaz/api services
45075
45119
  // Note: Validation is handled by validator class, mapping by mapper class
45076
45120
  fetchers: {
45077
45121
  fetchAll: /* @__PURE__ */ __name(async (query) => {
45078
- return this.apiClient.get(apiBasePath, {
45079
- params: query
45080
- });
45122
+ return this.apiClient.get(apiBasePath, { params: query });
45081
45123
  }, "fetchAll"),
45082
45124
  fetchById: /* @__PURE__ */ __name(async (id) => {
45083
45125
  return this.apiClient.get(`${apiBasePath}/${id}`);
@@ -45087,10 +45129,7 @@ var FrontendExampleDomainService = class _FrontendExampleDomainService extends B
45087
45129
  }, "create"),
45088
45130
  update: /* @__PURE__ */ __name(async (payload) => {
45089
45131
  const { id, data } = payload;
45090
- return this.apiClient.patch(
45091
- `${apiBasePath}/${id}`,
45092
- data
45093
- );
45132
+ return this.apiClient.patch(`${apiBasePath}/${id}`, data);
45094
45133
  }, "update"),
45095
45134
  delete: /* @__PURE__ */ __name(async (id) => {
45096
45135
  return this.apiClient.delete(`${apiBasePath}/${id}`);