node-consul-service 1.0.32 → 1.0.34
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/consul/dataLink.d.ts +6 -0
- package/dist/index.cjs.js +21 -2
- package/dist/index.esm.js +21 -3
- package/package.json +1 -1
|
@@ -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,6 +19494,7 @@ async function dataLink(data, schema) {
|
|
|
19478
19494
|
}
|
|
19479
19495
|
});
|
|
19480
19496
|
}
|
|
19497
|
+
// ✅ Fetch remaining from service
|
|
19481
19498
|
if (idsToFetch.length > 0) {
|
|
19482
19499
|
try {
|
|
19483
19500
|
const response = await callService(service, {
|
|
@@ -19496,9 +19513,10 @@ async function dataLink(data, schema) {
|
|
|
19496
19513
|
}
|
|
19497
19514
|
catch (error) {
|
|
19498
19515
|
console.error(`❌ Failed to fetch from ${service}${path}:`, JSON.stringify(error, Object.getOwnPropertyNames(error), 2));
|
|
19499
|
-
throw error;
|
|
19516
|
+
throw new DataLinkError(service, path, error);
|
|
19500
19517
|
}
|
|
19501
19518
|
}
|
|
19519
|
+
// ✅ Replace ids in result with full objects
|
|
19502
19520
|
for (const item of result) {
|
|
19503
19521
|
const value = item[filed];
|
|
19504
19522
|
if (typeof value === "string" ||
|
|
@@ -19522,6 +19540,7 @@ async function dataLink(data, schema) {
|
|
|
19522
19540
|
return isArray ? result : result[0];
|
|
19523
19541
|
}
|
|
19524
19542
|
|
|
19543
|
+
exports.DataLinkError = DataLinkError;
|
|
19525
19544
|
exports.callService = callService;
|
|
19526
19545
|
exports.dataLink = dataLink;
|
|
19527
19546
|
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,6 +19492,7 @@ async function dataLink(data, schema) {
|
|
|
19476
19492
|
}
|
|
19477
19493
|
});
|
|
19478
19494
|
}
|
|
19495
|
+
// ✅ Fetch remaining from service
|
|
19479
19496
|
if (idsToFetch.length > 0) {
|
|
19480
19497
|
try {
|
|
19481
19498
|
const response = await callService(service, {
|
|
@@ -19494,9 +19511,10 @@ async function dataLink(data, schema) {
|
|
|
19494
19511
|
}
|
|
19495
19512
|
catch (error) {
|
|
19496
19513
|
console.error(`❌ Failed to fetch from ${service}${path}:`, JSON.stringify(error, Object.getOwnPropertyNames(error), 2));
|
|
19497
|
-
throw error;
|
|
19514
|
+
throw new DataLinkError(service, path, error);
|
|
19498
19515
|
}
|
|
19499
19516
|
}
|
|
19517
|
+
// ✅ Replace ids in result with full objects
|
|
19500
19518
|
for (const item of result) {
|
|
19501
19519
|
const value = item[filed];
|
|
19502
19520
|
if (typeof value === "string" ||
|
|
@@ -19520,5 +19538,5 @@ async function dataLink(data, schema) {
|
|
|
19520
19538
|
return isArray ? result : result[0];
|
|
19521
19539
|
}
|
|
19522
19540
|
|
|
19523
|
-
export { callService, dataLink, deregisterService, getRandomServiceInstance, getServiceInstances, getServiceUrl, listServices, registerService };
|
|
19541
|
+
export { DataLinkError, callService, dataLink, deregisterService, getRandomServiceInstance, getServiceInstances, getServiceUrl, listServices, registerService };
|
|
19524
19542
|
//# sourceMappingURL=index.esm.js.map
|