node-consul-service 1.0.50 → 1.0.52

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 CHANGED
@@ -19060,7 +19060,13 @@ async function callService(serviceName, options = {}) {
19060
19060
  }
19061
19061
  }
19062
19062
 
19063
- // Custom Error Class
19063
+ function normalizeId(id) {
19064
+ if (id && typeof id === "object" && typeof id.toString === "function") {
19065
+ return id.toString();
19066
+ }
19067
+ return id;
19068
+ }
19069
+
19064
19070
  class DataLinkError extends Error {
19065
19071
  constructor(service, path, originalError) {
19066
19072
  super(`DataLinkError: Failed to fetch from ${service}:${path}`);
@@ -19073,31 +19079,36 @@ class DataLinkError extends Error {
19073
19079
  }
19074
19080
  }
19075
19081
  }
19076
- // Helper to normalize Mongo IDs or ObjectIds
19077
- function normalizeId(id) {
19078
- if (id && typeof id === "object" && typeof id.toString === "function") {
19079
- return id.toString();
19080
- }
19081
- return id;
19082
+ // 🛠 أدوات التعامل مع المسارات
19083
+ function getValueByPath(obj, path) {
19084
+ return path.split(".").reduce((acc, key) => acc === null || acc === void 0 ? void 0 : acc[key], obj);
19085
+ }
19086
+ function setValueByPath(obj, path, value) {
19087
+ const keys = path.split(".");
19088
+ const lastKey = keys.pop();
19089
+ const target = keys.reduce((acc, key) => {
19090
+ if (!acc[key])
19091
+ acc[key] = {};
19092
+ return acc[key];
19093
+ }, obj);
19094
+ if (lastKey)
19095
+ target[lastKey] = value;
19082
19096
  }
19083
- // ✅ Main function
19084
19097
  async function dataLink(data, schema) {
19085
19098
  const isArray = Array.isArray(data);
19086
19099
  const sourceData = isArray ? data : [data];
19087
- // Clone data to avoid mutating original
19088
19100
  const result = sourceData.map((item) => ({ ...item }));
19089
19101
  await Promise.all(schema.map(async ({ filed, service, path, headers, cacheGetter }) => {
19090
19102
  var _a;
19091
- // ✅ Safety check + trim path
19092
19103
  if (!path || !path.trim().startsWith("/")) {
19093
19104
  throw new DataLinkError(service, path, new Error(`Invalid path: "${path}". Path must start with "/"`));
19094
19105
  }
19095
19106
  const uniqueIds = Array.from(new Set(sourceData.flatMap((item) => {
19096
- const value = item[filed];
19097
- if (typeof value === "string" ||
19098
- (value &&
19099
- typeof value === "object" &&
19100
- typeof value.toString === "function")) {
19107
+ const value = getValueByPath(item, filed);
19108
+ if (typeof value === "string") {
19109
+ if (value.includes(",")) {
19110
+ return value.split(",").map((v) => normalizeId(v.trim()));
19111
+ }
19101
19112
  return [normalizeId(value)];
19102
19113
  }
19103
19114
  if (Array.isArray(value)) {
@@ -19108,13 +19119,17 @@ async function dataLink(data, schema) {
19108
19119
  typeof v.toString === "function"))
19109
19120
  .map((v) => normalizeId(v));
19110
19121
  }
19122
+ if (value &&
19123
+ typeof value === "object" &&
19124
+ typeof value.toString === "function") {
19125
+ return [normalizeId(value)];
19126
+ }
19111
19127
  return [];
19112
19128
  })));
19113
19129
  if (uniqueIds.length === 0)
19114
19130
  return;
19115
19131
  let cacheMap = new Map();
19116
19132
  let idsToFetch = uniqueIds;
19117
- // ✅ Check cache first
19118
19133
  if (cacheGetter) {
19119
19134
  const cacheResults = await cacheGetter(uniqueIds);
19120
19135
  idsToFetch = [];
@@ -19128,7 +19143,6 @@ async function dataLink(data, schema) {
19128
19143
  }
19129
19144
  });
19130
19145
  }
19131
- // ✅ Fetch missing items from service
19132
19146
  if (idsToFetch.length > 0) {
19133
19147
  try {
19134
19148
  const response = await callService(service, {
@@ -19152,24 +19166,34 @@ async function dataLink(data, schema) {
19152
19166
  throw new DataLinkError(service, path, error);
19153
19167
  }
19154
19168
  }
19155
- // ✅ Replace ids in result with full objects
19156
19169
  for (const item of result) {
19157
- const value = item[filed];
19158
- if (typeof value === "string" ||
19159
- (value &&
19160
- typeof value === "object" &&
19161
- typeof value.toString === "function")) {
19162
- const key = normalizeId(value);
19163
- if (cacheMap.has(key)) {
19164
- item[filed] = cacheMap.get(key);
19170
+ const value = getValueByPath(item, filed);
19171
+ if (typeof value === "string") {
19172
+ if (value.includes(",")) {
19173
+ const ids = value.split(",").map((v) => normalizeId(v.trim()));
19174
+ setValueByPath(item, filed, ids.map((id) => { var _a; return (_a = cacheMap.get(id)) !== null && _a !== void 0 ? _a : id; }));
19175
+ }
19176
+ else {
19177
+ const key = normalizeId(value);
19178
+ if (cacheMap.has(key)) {
19179
+ setValueByPath(item, filed, cacheMap.get(key));
19180
+ }
19165
19181
  }
19166
19182
  }
19167
19183
  else if (Array.isArray(value)) {
19168
- item[filed] = value.map((id) => {
19184
+ setValueByPath(item, filed, value.map((id) => {
19169
19185
  var _a;
19170
19186
  const key = normalizeId(id);
19171
19187
  return (_a = cacheMap.get(key)) !== null && _a !== void 0 ? _a : id;
19172
- });
19188
+ }));
19189
+ }
19190
+ else if (value &&
19191
+ typeof value === "object" &&
19192
+ typeof value.toString === "function") {
19193
+ const key = normalizeId(value);
19194
+ if (cacheMap.has(key)) {
19195
+ setValueByPath(item, filed, cacheMap.get(key));
19196
+ }
19173
19197
  }
19174
19198
  }
19175
19199
  }));
package/dist/index.esm.js CHANGED
@@ -19058,7 +19058,13 @@ async function callService(serviceName, options = {}) {
19058
19058
  }
19059
19059
  }
19060
19060
 
19061
- // Custom Error Class
19061
+ function normalizeId(id) {
19062
+ if (id && typeof id === "object" && typeof id.toString === "function") {
19063
+ return id.toString();
19064
+ }
19065
+ return id;
19066
+ }
19067
+
19062
19068
  class DataLinkError extends Error {
19063
19069
  constructor(service, path, originalError) {
19064
19070
  super(`DataLinkError: Failed to fetch from ${service}:${path}`);
@@ -19071,31 +19077,36 @@ class DataLinkError extends Error {
19071
19077
  }
19072
19078
  }
19073
19079
  }
19074
- // Helper to normalize Mongo IDs or ObjectIds
19075
- function normalizeId(id) {
19076
- if (id && typeof id === "object" && typeof id.toString === "function") {
19077
- return id.toString();
19078
- }
19079
- return id;
19080
+ // 🛠 أدوات التعامل مع المسارات
19081
+ function getValueByPath(obj, path) {
19082
+ return path.split(".").reduce((acc, key) => acc === null || acc === void 0 ? void 0 : acc[key], obj);
19083
+ }
19084
+ function setValueByPath(obj, path, value) {
19085
+ const keys = path.split(".");
19086
+ const lastKey = keys.pop();
19087
+ const target = keys.reduce((acc, key) => {
19088
+ if (!acc[key])
19089
+ acc[key] = {};
19090
+ return acc[key];
19091
+ }, obj);
19092
+ if (lastKey)
19093
+ target[lastKey] = value;
19080
19094
  }
19081
- // ✅ Main function
19082
19095
  async function dataLink(data, schema) {
19083
19096
  const isArray = Array.isArray(data);
19084
19097
  const sourceData = isArray ? data : [data];
19085
- // Clone data to avoid mutating original
19086
19098
  const result = sourceData.map((item) => ({ ...item }));
19087
19099
  await Promise.all(schema.map(async ({ filed, service, path, headers, cacheGetter }) => {
19088
19100
  var _a;
19089
- // ✅ Safety check + trim path
19090
19101
  if (!path || !path.trim().startsWith("/")) {
19091
19102
  throw new DataLinkError(service, path, new Error(`Invalid path: "${path}". Path must start with "/"`));
19092
19103
  }
19093
19104
  const uniqueIds = Array.from(new Set(sourceData.flatMap((item) => {
19094
- const value = item[filed];
19095
- if (typeof value === "string" ||
19096
- (value &&
19097
- typeof value === "object" &&
19098
- typeof value.toString === "function")) {
19105
+ const value = getValueByPath(item, filed);
19106
+ if (typeof value === "string") {
19107
+ if (value.includes(",")) {
19108
+ return value.split(",").map((v) => normalizeId(v.trim()));
19109
+ }
19099
19110
  return [normalizeId(value)];
19100
19111
  }
19101
19112
  if (Array.isArray(value)) {
@@ -19106,13 +19117,17 @@ async function dataLink(data, schema) {
19106
19117
  typeof v.toString === "function"))
19107
19118
  .map((v) => normalizeId(v));
19108
19119
  }
19120
+ if (value &&
19121
+ typeof value === "object" &&
19122
+ typeof value.toString === "function") {
19123
+ return [normalizeId(value)];
19124
+ }
19109
19125
  return [];
19110
19126
  })));
19111
19127
  if (uniqueIds.length === 0)
19112
19128
  return;
19113
19129
  let cacheMap = new Map();
19114
19130
  let idsToFetch = uniqueIds;
19115
- // ✅ Check cache first
19116
19131
  if (cacheGetter) {
19117
19132
  const cacheResults = await cacheGetter(uniqueIds);
19118
19133
  idsToFetch = [];
@@ -19126,7 +19141,6 @@ async function dataLink(data, schema) {
19126
19141
  }
19127
19142
  });
19128
19143
  }
19129
- // ✅ Fetch missing items from service
19130
19144
  if (idsToFetch.length > 0) {
19131
19145
  try {
19132
19146
  const response = await callService(service, {
@@ -19150,24 +19164,34 @@ async function dataLink(data, schema) {
19150
19164
  throw new DataLinkError(service, path, error);
19151
19165
  }
19152
19166
  }
19153
- // ✅ Replace ids in result with full objects
19154
19167
  for (const item of result) {
19155
- const value = item[filed];
19156
- if (typeof value === "string" ||
19157
- (value &&
19158
- typeof value === "object" &&
19159
- typeof value.toString === "function")) {
19160
- const key = normalizeId(value);
19161
- if (cacheMap.has(key)) {
19162
- item[filed] = cacheMap.get(key);
19168
+ const value = getValueByPath(item, filed);
19169
+ if (typeof value === "string") {
19170
+ if (value.includes(",")) {
19171
+ const ids = value.split(",").map((v) => normalizeId(v.trim()));
19172
+ setValueByPath(item, filed, ids.map((id) => { var _a; return (_a = cacheMap.get(id)) !== null && _a !== void 0 ? _a : id; }));
19173
+ }
19174
+ else {
19175
+ const key = normalizeId(value);
19176
+ if (cacheMap.has(key)) {
19177
+ setValueByPath(item, filed, cacheMap.get(key));
19178
+ }
19163
19179
  }
19164
19180
  }
19165
19181
  else if (Array.isArray(value)) {
19166
- item[filed] = value.map((id) => {
19182
+ setValueByPath(item, filed, value.map((id) => {
19167
19183
  var _a;
19168
19184
  const key = normalizeId(id);
19169
19185
  return (_a = cacheMap.get(key)) !== null && _a !== void 0 ? _a : id;
19170
- });
19186
+ }));
19187
+ }
19188
+ else if (value &&
19189
+ typeof value === "object" &&
19190
+ typeof value.toString === "function") {
19191
+ const key = normalizeId(value);
19192
+ if (cacheMap.has(key)) {
19193
+ setValueByPath(item, filed, cacheMap.get(key));
19194
+ }
19171
19195
  }
19172
19196
  }
19173
19197
  }));
@@ -0,0 +1 @@
1
+ export declare function normalizeId(id: any): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-consul-service",
3
- "version": "1.0.50",
3
+ "version": "1.0.52",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",