node-consul-service 1.0.50 → 1.0.51
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.cjs.js +28 -19
- package/dist/index.esm.js +28 -19
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -19060,7 +19060,6 @@ async function callService(serviceName, options = {}) {
|
|
|
19060
19060
|
}
|
|
19061
19061
|
}
|
|
19062
19062
|
|
|
19063
|
-
// ✅ Custom Error Class
|
|
19064
19063
|
class DataLinkError extends Error {
|
|
19065
19064
|
constructor(service, path, originalError) {
|
|
19066
19065
|
super(`DataLinkError: Failed to fetch from ${service}:${path}`);
|
|
@@ -19073,31 +19072,27 @@ class DataLinkError extends Error {
|
|
|
19073
19072
|
}
|
|
19074
19073
|
}
|
|
19075
19074
|
}
|
|
19076
|
-
// ✅ Helper to normalize Mongo IDs or ObjectIds
|
|
19077
19075
|
function normalizeId(id) {
|
|
19078
19076
|
if (id && typeof id === "object" && typeof id.toString === "function") {
|
|
19079
19077
|
return id.toString();
|
|
19080
19078
|
}
|
|
19081
19079
|
return id;
|
|
19082
19080
|
}
|
|
19083
|
-
// ✅ Main function
|
|
19084
19081
|
async function dataLink(data, schema) {
|
|
19085
19082
|
const isArray = Array.isArray(data);
|
|
19086
19083
|
const sourceData = isArray ? data : [data];
|
|
19087
|
-
// Clone data to avoid mutating original
|
|
19088
19084
|
const result = sourceData.map((item) => ({ ...item }));
|
|
19089
19085
|
await Promise.all(schema.map(async ({ filed, service, path, headers, cacheGetter }) => {
|
|
19090
19086
|
var _a;
|
|
19091
|
-
// ✅ Safety check + trim path
|
|
19092
19087
|
if (!path || !path.trim().startsWith("/")) {
|
|
19093
19088
|
throw new DataLinkError(service, path, new Error(`Invalid path: "${path}". Path must start with "/"`));
|
|
19094
19089
|
}
|
|
19095
19090
|
const uniqueIds = Array.from(new Set(sourceData.flatMap((item) => {
|
|
19096
19091
|
const value = item[filed];
|
|
19097
|
-
if (typeof value === "string"
|
|
19098
|
-
(value
|
|
19099
|
-
|
|
19100
|
-
|
|
19092
|
+
if (typeof value === "string") {
|
|
19093
|
+
if (value.includes(",")) {
|
|
19094
|
+
return value.split(",").map((v) => normalizeId(v.trim()));
|
|
19095
|
+
}
|
|
19101
19096
|
return [normalizeId(value)];
|
|
19102
19097
|
}
|
|
19103
19098
|
if (Array.isArray(value)) {
|
|
@@ -19108,13 +19103,18 @@ async function dataLink(data, schema) {
|
|
|
19108
19103
|
typeof v.toString === "function"))
|
|
19109
19104
|
.map((v) => normalizeId(v));
|
|
19110
19105
|
}
|
|
19106
|
+
if (value &&
|
|
19107
|
+
typeof value === "object" &&
|
|
19108
|
+
typeof value.toString === "function") {
|
|
19109
|
+
return [normalizeId(value)];
|
|
19110
|
+
}
|
|
19111
19111
|
return [];
|
|
19112
19112
|
})));
|
|
19113
|
+
console.log("🚀 ~ schema.map ~ uniqueIds:", uniqueIds);
|
|
19113
19114
|
if (uniqueIds.length === 0)
|
|
19114
19115
|
return;
|
|
19115
19116
|
let cacheMap = new Map();
|
|
19116
19117
|
let idsToFetch = uniqueIds;
|
|
19117
|
-
// ✅ Check cache first
|
|
19118
19118
|
if (cacheGetter) {
|
|
19119
19119
|
const cacheResults = await cacheGetter(uniqueIds);
|
|
19120
19120
|
idsToFetch = [];
|
|
@@ -19128,7 +19128,6 @@ async function dataLink(data, schema) {
|
|
|
19128
19128
|
}
|
|
19129
19129
|
});
|
|
19130
19130
|
}
|
|
19131
|
-
// ✅ Fetch missing items from service
|
|
19132
19131
|
if (idsToFetch.length > 0) {
|
|
19133
19132
|
try {
|
|
19134
19133
|
const response = await callService(service, {
|
|
@@ -19152,16 +19151,18 @@ async function dataLink(data, schema) {
|
|
|
19152
19151
|
throw new DataLinkError(service, path, error);
|
|
19153
19152
|
}
|
|
19154
19153
|
}
|
|
19155
|
-
// ✅ Replace ids in result with full objects
|
|
19156
19154
|
for (const item of result) {
|
|
19157
19155
|
const value = item[filed];
|
|
19158
|
-
if (typeof value === "string"
|
|
19159
|
-
(value
|
|
19160
|
-
|
|
19161
|
-
|
|
19162
|
-
|
|
19163
|
-
|
|
19164
|
-
|
|
19156
|
+
if (typeof value === "string") {
|
|
19157
|
+
if (value.includes(",")) {
|
|
19158
|
+
const ids = value.split(",").map((v) => normalizeId(v.trim()));
|
|
19159
|
+
item[filed] = ids.map((id) => { var _a; return (_a = cacheMap.get(id)) !== null && _a !== void 0 ? _a : id; });
|
|
19160
|
+
}
|
|
19161
|
+
else {
|
|
19162
|
+
const key = normalizeId(value);
|
|
19163
|
+
if (cacheMap.has(key)) {
|
|
19164
|
+
item[filed] = cacheMap.get(key);
|
|
19165
|
+
}
|
|
19165
19166
|
}
|
|
19166
19167
|
}
|
|
19167
19168
|
else if (Array.isArray(value)) {
|
|
@@ -19171,6 +19172,14 @@ async function dataLink(data, schema) {
|
|
|
19171
19172
|
return (_a = cacheMap.get(key)) !== null && _a !== void 0 ? _a : id;
|
|
19172
19173
|
});
|
|
19173
19174
|
}
|
|
19175
|
+
else if (value &&
|
|
19176
|
+
typeof value === "object" &&
|
|
19177
|
+
typeof value.toString === "function") {
|
|
19178
|
+
const key = normalizeId(value);
|
|
19179
|
+
if (cacheMap.has(key)) {
|
|
19180
|
+
item[filed] = cacheMap.get(key);
|
|
19181
|
+
}
|
|
19182
|
+
}
|
|
19174
19183
|
}
|
|
19175
19184
|
}));
|
|
19176
19185
|
return isArray ? result : result[0];
|
package/dist/index.esm.js
CHANGED
|
@@ -19058,7 +19058,6 @@ async function callService(serviceName, options = {}) {
|
|
|
19058
19058
|
}
|
|
19059
19059
|
}
|
|
19060
19060
|
|
|
19061
|
-
// ✅ Custom Error Class
|
|
19062
19061
|
class DataLinkError extends Error {
|
|
19063
19062
|
constructor(service, path, originalError) {
|
|
19064
19063
|
super(`DataLinkError: Failed to fetch from ${service}:${path}`);
|
|
@@ -19071,31 +19070,27 @@ class DataLinkError extends Error {
|
|
|
19071
19070
|
}
|
|
19072
19071
|
}
|
|
19073
19072
|
}
|
|
19074
|
-
// ✅ Helper to normalize Mongo IDs or ObjectIds
|
|
19075
19073
|
function normalizeId(id) {
|
|
19076
19074
|
if (id && typeof id === "object" && typeof id.toString === "function") {
|
|
19077
19075
|
return id.toString();
|
|
19078
19076
|
}
|
|
19079
19077
|
return id;
|
|
19080
19078
|
}
|
|
19081
|
-
// ✅ Main function
|
|
19082
19079
|
async function dataLink(data, schema) {
|
|
19083
19080
|
const isArray = Array.isArray(data);
|
|
19084
19081
|
const sourceData = isArray ? data : [data];
|
|
19085
|
-
// Clone data to avoid mutating original
|
|
19086
19082
|
const result = sourceData.map((item) => ({ ...item }));
|
|
19087
19083
|
await Promise.all(schema.map(async ({ filed, service, path, headers, cacheGetter }) => {
|
|
19088
19084
|
var _a;
|
|
19089
|
-
// ✅ Safety check + trim path
|
|
19090
19085
|
if (!path || !path.trim().startsWith("/")) {
|
|
19091
19086
|
throw new DataLinkError(service, path, new Error(`Invalid path: "${path}". Path must start with "/"`));
|
|
19092
19087
|
}
|
|
19093
19088
|
const uniqueIds = Array.from(new Set(sourceData.flatMap((item) => {
|
|
19094
19089
|
const value = item[filed];
|
|
19095
|
-
if (typeof value === "string"
|
|
19096
|
-
(value
|
|
19097
|
-
|
|
19098
|
-
|
|
19090
|
+
if (typeof value === "string") {
|
|
19091
|
+
if (value.includes(",")) {
|
|
19092
|
+
return value.split(",").map((v) => normalizeId(v.trim()));
|
|
19093
|
+
}
|
|
19099
19094
|
return [normalizeId(value)];
|
|
19100
19095
|
}
|
|
19101
19096
|
if (Array.isArray(value)) {
|
|
@@ -19106,13 +19101,18 @@ async function dataLink(data, schema) {
|
|
|
19106
19101
|
typeof v.toString === "function"))
|
|
19107
19102
|
.map((v) => normalizeId(v));
|
|
19108
19103
|
}
|
|
19104
|
+
if (value &&
|
|
19105
|
+
typeof value === "object" &&
|
|
19106
|
+
typeof value.toString === "function") {
|
|
19107
|
+
return [normalizeId(value)];
|
|
19108
|
+
}
|
|
19109
19109
|
return [];
|
|
19110
19110
|
})));
|
|
19111
|
+
console.log("🚀 ~ schema.map ~ uniqueIds:", uniqueIds);
|
|
19111
19112
|
if (uniqueIds.length === 0)
|
|
19112
19113
|
return;
|
|
19113
19114
|
let cacheMap = new Map();
|
|
19114
19115
|
let idsToFetch = uniqueIds;
|
|
19115
|
-
// ✅ Check cache first
|
|
19116
19116
|
if (cacheGetter) {
|
|
19117
19117
|
const cacheResults = await cacheGetter(uniqueIds);
|
|
19118
19118
|
idsToFetch = [];
|
|
@@ -19126,7 +19126,6 @@ async function dataLink(data, schema) {
|
|
|
19126
19126
|
}
|
|
19127
19127
|
});
|
|
19128
19128
|
}
|
|
19129
|
-
// ✅ Fetch missing items from service
|
|
19130
19129
|
if (idsToFetch.length > 0) {
|
|
19131
19130
|
try {
|
|
19132
19131
|
const response = await callService(service, {
|
|
@@ -19150,16 +19149,18 @@ async function dataLink(data, schema) {
|
|
|
19150
19149
|
throw new DataLinkError(service, path, error);
|
|
19151
19150
|
}
|
|
19152
19151
|
}
|
|
19153
|
-
// ✅ Replace ids in result with full objects
|
|
19154
19152
|
for (const item of result) {
|
|
19155
19153
|
const value = item[filed];
|
|
19156
|
-
if (typeof value === "string"
|
|
19157
|
-
(value
|
|
19158
|
-
|
|
19159
|
-
|
|
19160
|
-
|
|
19161
|
-
|
|
19162
|
-
|
|
19154
|
+
if (typeof value === "string") {
|
|
19155
|
+
if (value.includes(",")) {
|
|
19156
|
+
const ids = value.split(",").map((v) => normalizeId(v.trim()));
|
|
19157
|
+
item[filed] = ids.map((id) => { var _a; return (_a = cacheMap.get(id)) !== null && _a !== void 0 ? _a : id; });
|
|
19158
|
+
}
|
|
19159
|
+
else {
|
|
19160
|
+
const key = normalizeId(value);
|
|
19161
|
+
if (cacheMap.has(key)) {
|
|
19162
|
+
item[filed] = cacheMap.get(key);
|
|
19163
|
+
}
|
|
19163
19164
|
}
|
|
19164
19165
|
}
|
|
19165
19166
|
else if (Array.isArray(value)) {
|
|
@@ -19169,6 +19170,14 @@ async function dataLink(data, schema) {
|
|
|
19169
19170
|
return (_a = cacheMap.get(key)) !== null && _a !== void 0 ? _a : id;
|
|
19170
19171
|
});
|
|
19171
19172
|
}
|
|
19173
|
+
else if (value &&
|
|
19174
|
+
typeof value === "object" &&
|
|
19175
|
+
typeof value.toString === "function") {
|
|
19176
|
+
const key = normalizeId(value);
|
|
19177
|
+
if (cacheMap.has(key)) {
|
|
19178
|
+
item[filed] = cacheMap.get(key);
|
|
19179
|
+
}
|
|
19180
|
+
}
|
|
19172
19181
|
}
|
|
19173
19182
|
}));
|
|
19174
19183
|
return isArray ? result : result[0];
|