node-consul-service 1.0.33 → 1.0.35

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.
@@ -6,5 +6,11 @@ interface LinkSchema {
6
6
  headers?: AxiosRequestHeaders;
7
7
  cacheGetter?: (ids: string[]) => Promise<(any | null)[]>;
8
8
  }
9
+ export declare class DataLinkError extends Error {
10
+ service: string;
11
+ path: string;
12
+ originalError: any;
13
+ constructor(service: string, path: string, originalError: any);
14
+ }
9
15
  export declare function dataLink(data: any[] | any, schema: LinkSchema[]): Promise<any[] | any>;
10
16
  export {};
package/dist/index.cjs.js CHANGED
@@ -19430,16 +19430,31 @@ async function callService(serviceName, options = {}) {
19430
19430
  }
19431
19431
  }
19432
19432
 
19433
+ // ✅ Custom Error Class
19434
+ class DataLinkError extends Error {
19435
+ constructor(service, path, originalError) {
19436
+ super(`DataLinkError: Failed to fetch from ${service}:${path}`);
19437
+ this.name = "DataLinkError";
19438
+ this.service = service;
19439
+ this.path = path;
19440
+ this.originalError = originalError;
19441
+ if (Error.captureStackTrace) {
19442
+ Error.captureStackTrace(this, DataLinkError);
19443
+ }
19444
+ }
19445
+ }
19446
+ // ✅ Helper to normalize Mongo IDs or ObjectIds
19433
19447
  function normalizeId(id) {
19434
19448
  if (id && typeof id === "object" && typeof id.toString === "function") {
19435
19449
  return id.toString();
19436
19450
  }
19437
19451
  return id;
19438
19452
  }
19453
+ // ✅ Main function
19439
19454
  async function dataLink(data, schema) {
19440
19455
  const isArray = Array.isArray(data);
19441
19456
  const sourceData = isArray ? data : [data];
19442
- // ننسخ كل عنصر بدون التأثير على الأصل
19457
+ // Clone data to avoid mutating original
19443
19458
  const result = sourceData.map((item) => ({ ...item }));
19444
19459
  await Promise.all(schema.map(async ({ filed, service, path, headers, cacheGetter }) => {
19445
19460
  var _a;
@@ -19465,6 +19480,7 @@ async function dataLink(data, schema) {
19465
19480
  return;
19466
19481
  let cacheMap = new Map();
19467
19482
  let idsToFetch = uniqueIds;
19483
+ // ✅ Check cache first
19468
19484
  if (cacheGetter) {
19469
19485
  const cacheResults = await cacheGetter(uniqueIds);
19470
19486
  idsToFetch = [];
@@ -19478,7 +19494,12 @@ async function dataLink(data, schema) {
19478
19494
  }
19479
19495
  });
19480
19496
  }
19497
+ // ✅ Validate path
19481
19498
  if (idsToFetch.length > 0) {
19499
+ if (!path.startsWith("/")) {
19500
+ throw new DataLinkError(service, path, new Error(`Invalid path: "${path}". Path must start with "/"`));
19501
+ }
19502
+ // ✅ Fetch remaining from service
19482
19503
  try {
19483
19504
  const response = await callService(service, {
19484
19505
  method: "POST",
@@ -19495,10 +19516,10 @@ async function dataLink(data, schema) {
19495
19516
  }
19496
19517
  }
19497
19518
  catch (error) {
19498
- console.error(`❌ Failed to fetch from ${service}${path}:`, JSON.stringify(error, Object.getOwnPropertyNames(error), 2));
19499
- return error;
19519
+ throw new DataLinkError(service, path, error);
19500
19520
  }
19501
19521
  }
19522
+ // ✅ Replace ids in result with full objects
19502
19523
  for (const item of result) {
19503
19524
  const value = item[filed];
19504
19525
  if (typeof value === "string" ||
@@ -19522,6 +19543,7 @@ async function dataLink(data, schema) {
19522
19543
  return isArray ? result : result[0];
19523
19544
  }
19524
19545
 
19546
+ exports.DataLinkError = DataLinkError;
19525
19547
  exports.callService = callService;
19526
19548
  exports.dataLink = dataLink;
19527
19549
  exports.deregisterService = deregisterService;
package/dist/index.esm.js CHANGED
@@ -19428,16 +19428,31 @@ async function callService(serviceName, options = {}) {
19428
19428
  }
19429
19429
  }
19430
19430
 
19431
+ // ✅ Custom Error Class
19432
+ class DataLinkError extends Error {
19433
+ constructor(service, path, originalError) {
19434
+ super(`DataLinkError: Failed to fetch from ${service}:${path}`);
19435
+ this.name = "DataLinkError";
19436
+ this.service = service;
19437
+ this.path = path;
19438
+ this.originalError = originalError;
19439
+ if (Error.captureStackTrace) {
19440
+ Error.captureStackTrace(this, DataLinkError);
19441
+ }
19442
+ }
19443
+ }
19444
+ // ✅ Helper to normalize Mongo IDs or ObjectIds
19431
19445
  function normalizeId(id) {
19432
19446
  if (id && typeof id === "object" && typeof id.toString === "function") {
19433
19447
  return id.toString();
19434
19448
  }
19435
19449
  return id;
19436
19450
  }
19451
+ // ✅ Main function
19437
19452
  async function dataLink(data, schema) {
19438
19453
  const isArray = Array.isArray(data);
19439
19454
  const sourceData = isArray ? data : [data];
19440
- // ننسخ كل عنصر بدون التأثير على الأصل
19455
+ // Clone data to avoid mutating original
19441
19456
  const result = sourceData.map((item) => ({ ...item }));
19442
19457
  await Promise.all(schema.map(async ({ filed, service, path, headers, cacheGetter }) => {
19443
19458
  var _a;
@@ -19463,6 +19478,7 @@ async function dataLink(data, schema) {
19463
19478
  return;
19464
19479
  let cacheMap = new Map();
19465
19480
  let idsToFetch = uniqueIds;
19481
+ // ✅ Check cache first
19466
19482
  if (cacheGetter) {
19467
19483
  const cacheResults = await cacheGetter(uniqueIds);
19468
19484
  idsToFetch = [];
@@ -19476,7 +19492,12 @@ async function dataLink(data, schema) {
19476
19492
  }
19477
19493
  });
19478
19494
  }
19495
+ // ✅ Validate path
19479
19496
  if (idsToFetch.length > 0) {
19497
+ if (!path.startsWith("/")) {
19498
+ throw new DataLinkError(service, path, new Error(`Invalid path: "${path}". Path must start with "/"`));
19499
+ }
19500
+ // ✅ Fetch remaining from service
19480
19501
  try {
19481
19502
  const response = await callService(service, {
19482
19503
  method: "POST",
@@ -19493,10 +19514,10 @@ async function dataLink(data, schema) {
19493
19514
  }
19494
19515
  }
19495
19516
  catch (error) {
19496
- console.error(`❌ Failed to fetch from ${service}${path}:`, JSON.stringify(error, Object.getOwnPropertyNames(error), 2));
19497
- return error;
19517
+ throw new DataLinkError(service, path, error);
19498
19518
  }
19499
19519
  }
19520
+ // ✅ Replace ids in result with full objects
19500
19521
  for (const item of result) {
19501
19522
  const value = item[filed];
19502
19523
  if (typeof value === "string" ||
@@ -19520,5 +19541,5 @@ async function dataLink(data, schema) {
19520
19541
  return isArray ? result : result[0];
19521
19542
  }
19522
19543
 
19523
- export { callService, dataLink, deregisterService, getRandomServiceInstance, getServiceInstances, getServiceUrl, listServices, registerService };
19544
+ export { DataLinkError, callService, dataLink, deregisterService, getRandomServiceInstance, getServiceInstances, getServiceUrl, listServices, registerService };
19524
19545
  //# sourceMappingURL=index.esm.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-consul-service",
3
- "version": "1.0.33",
3
+ "version": "1.0.35",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",