node-consul-service 1.0.59 → 1.0.61

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.
@@ -3,14 +3,10 @@ interface LinkSchema {
3
3
  field: string;
4
4
  service: string;
5
5
  path: string;
6
- headers?: AxiosRequestHeaders;
6
+ headers?: AxiosRequestHeaders | {
7
+ [key: string]: string | string[] | number | boolean | undefined;
8
+ };
7
9
  cacheGetter?: (ids: string[]) => Promise<(any | null)[]>;
8
10
  }
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
- }
15
11
  export declare function dataLink(data: any[] | any, schema: LinkSchema[]): Promise<any[] | any>;
16
12
  export {};
package/dist/index.cjs.js CHANGED
@@ -19057,6 +19057,11 @@ async function callService(serviceName, options = {}) {
19057
19057
  }
19058
19058
  }
19059
19059
 
19060
+ // 🛠 أدوات التعامل مع المسارات
19061
+ function getValueByPath(obj, path) {
19062
+ return path.split(".").reduce((acc, key) => acc === null || acc === void 0 ? void 0 : acc[key], obj);
19063
+ }
19064
+
19060
19065
  function normalizeId(id) {
19061
19066
  if (id && typeof id === "object" && typeof id.toString === "function") {
19062
19067
  return id.toString();
@@ -19064,6 +19069,69 @@ function normalizeId(id) {
19064
19069
  return id;
19065
19070
  }
19066
19071
 
19072
+ function setValueByPath(obj, path, value) {
19073
+ const keys = path.split(".");
19074
+ const lastKey = keys.pop();
19075
+ const target = keys.reduce((acc, key) => {
19076
+ if (!acc[key])
19077
+ acc[key] = {};
19078
+ return acc[key];
19079
+ }, obj);
19080
+ if (lastKey)
19081
+ target[lastKey] = value;
19082
+ }
19083
+
19084
+ function applyResolvedValues(item, field, cacheMap) {
19085
+ const value = getValueByPath(item, field);
19086
+ // ✅ تجاهل الحقول غير الموجودة أو الفارغة تمامًا
19087
+ if (value === undefined || value === null)
19088
+ return;
19089
+ if (typeof value === "string") {
19090
+ if (value.includes(",")) {
19091
+ const ids = value.split(",").map((v) => normalizeId(v.trim()));
19092
+ setValueByPath(item, field, ids.map((id) => { var _a; return (_a = cacheMap.get(id)) !== null && _a !== void 0 ? _a : id; }));
19093
+ }
19094
+ else {
19095
+ const id = normalizeId(value);
19096
+ if (cacheMap.has(id))
19097
+ setValueByPath(item, field, cacheMap.get(id));
19098
+ }
19099
+ }
19100
+ else if (Array.isArray(value)) {
19101
+ setValueByPath(item, field, value.map((id) => {
19102
+ var _a;
19103
+ const key = normalizeId(id);
19104
+ return (_a = cacheMap.get(key)) !== null && _a !== void 0 ? _a : id;
19105
+ }));
19106
+ }
19107
+ else if (value &&
19108
+ typeof value === "object" &&
19109
+ typeof value.toString === "function") {
19110
+ const key = normalizeId(value);
19111
+ if (cacheMap.has(key))
19112
+ setValueByPath(item, field, cacheMap.get(key));
19113
+ }
19114
+ }
19115
+
19116
+ function extractIdsFromField(item, field) {
19117
+ const value = getValueByPath(item, field);
19118
+ if (typeof value === "string") {
19119
+ return value.includes(",")
19120
+ ? value.split(",").map((v) => normalizeId(v.trim()))
19121
+ : [normalizeId(value)];
19122
+ }
19123
+ if (Array.isArray(value)) {
19124
+ return value
19125
+ .filter((v) => typeof v === "string" ||
19126
+ (v && typeof v === "object" && typeof v.toString === "function"))
19127
+ .map((v) => normalizeId(v));
19128
+ }
19129
+ if (value && typeof value === "object" && typeof value.toString === "function") {
19130
+ return [normalizeId(value)];
19131
+ }
19132
+ return [];
19133
+ }
19134
+
19067
19135
  class DataLinkError extends Error {
19068
19136
  constructor(service, path, originalError) {
19069
19137
  super(`DataLinkError: Failed to fetch from ${service}:${path}`);
@@ -19076,129 +19144,70 @@ class DataLinkError extends Error {
19076
19144
  }
19077
19145
  }
19078
19146
  }
19079
- // 🛠 أدوات التعامل مع المسارات
19080
- function getValueByPath(obj, path) {
19081
- return path.split(".").reduce((acc, key) => acc === null || acc === void 0 ? void 0 : acc[key], obj);
19147
+ async function fetchItemsFromService(service, path, ids, headers) {
19148
+ if (!path.startsWith("/")) {
19149
+ throw new DataLinkError(service, path, new Error("Path must start with '/'"));
19150
+ }
19151
+ try {
19152
+ const response = await callService(service, {
19153
+ method: "POST",
19154
+ path,
19155
+ headers,
19156
+ data: { ids },
19157
+ });
19158
+ if (!(response === null || response === void 0 ? void 0 : response.success)) {
19159
+ throw new DataLinkError(service, path, response);
19160
+ }
19161
+ return Array.isArray(response.data) ? response.data : [];
19162
+ }
19163
+ catch (error) {
19164
+ throw new DataLinkError(service, path, error);
19165
+ }
19082
19166
  }
19083
- function setValueByPath(obj, path, value) {
19084
- const keys = path.split(".");
19085
- const lastKey = keys.pop();
19086
- const target = keys.reduce((acc, key) => {
19087
- if (!acc[key])
19088
- acc[key] = {};
19089
- return acc[key];
19090
- }, obj);
19091
- if (lastKey)
19092
- target[lastKey] = value;
19167
+
19168
+ async function getCachedAndUncachedItems(ids, cacheGetter) {
19169
+ const cacheMap = new Map();
19170
+ let idsToFetch = ids;
19171
+ if (cacheGetter) {
19172
+ const results = await cacheGetter(ids);
19173
+ idsToFetch = [];
19174
+ results.forEach((item, index) => {
19175
+ const id = ids[index];
19176
+ if (item) {
19177
+ cacheMap.set(id, item);
19178
+ }
19179
+ else {
19180
+ idsToFetch.push(id);
19181
+ }
19182
+ });
19183
+ }
19184
+ return { cacheMap, idsToFetch };
19093
19185
  }
19186
+
19094
19187
  async function dataLink(data, schema) {
19095
19188
  const isArray = Array.isArray(data);
19096
19189
  const sourceData = isArray ? data : [data];
19097
19190
  const result = sourceData.map((item) => ({ ...item }));
19098
- await Promise.all(schema.map(async ({ field, service, path, headers, cacheGetter }) => {
19099
- if (!path || !path.trim().startsWith("/")) {
19100
- throw new DataLinkError(service, path, new Error(`Invalid path: "${path}". Path must start with "/"`));
19101
- }
19102
- const uniqueIds = Array.from(new Set(sourceData.flatMap((item) => {
19103
- const value = getValueByPath(item, field);
19104
- if (typeof value === "string") {
19105
- if (value.includes(",")) {
19106
- return value.split(",").map((v) => normalizeId(v.trim()));
19107
- }
19108
- return [normalizeId(value)];
19109
- }
19110
- if (Array.isArray(value)) {
19111
- return value
19112
- .filter((v) => typeof v === "string" ||
19113
- (v &&
19114
- typeof v === "object" &&
19115
- typeof v.toString === "function"))
19116
- .map((v) => normalizeId(v));
19117
- }
19118
- if (value &&
19119
- typeof value === "object" &&
19120
- typeof value.toString === "function") {
19121
- return [normalizeId(value)];
19122
- }
19123
- return [];
19124
- })));
19125
- if (uniqueIds.length === 0)
19126
- return;
19127
- let cacheMap = new Map();
19128
- let idsToFetch = uniqueIds;
19129
- if (cacheGetter) {
19130
- const cacheResults = await cacheGetter(uniqueIds);
19131
- idsToFetch = [];
19132
- cacheResults.forEach((item, index) => {
19133
- const id = uniqueIds[index];
19134
- if (item) {
19135
- cacheMap.set(id, item);
19136
- }
19137
- else {
19138
- idsToFetch.push(id);
19139
- }
19140
- });
19141
- }
19191
+ for (const { field, service, path, headers, cacheGetter } of schema) {
19192
+ const allIds = Array.from(new Set(sourceData.flatMap((item) => extractIdsFromField(item, field))));
19193
+ if (allIds.length === 0)
19194
+ continue;
19195
+ const { cacheMap, idsToFetch } = await getCachedAndUncachedItems(allIds, cacheGetter);
19142
19196
  if (idsToFetch.length > 0) {
19143
- try {
19144
- const response = await callService(service, {
19145
- method: "POST",
19146
- path,
19147
- headers,
19148
- data: { ids: idsToFetch },
19149
- });
19150
- if ((response === null || response === void 0 ? void 0 : response.success) && Array.isArray(response.data)) {
19151
- for (const item of response.data) {
19152
- if (item === null || item === void 0 ? void 0 : item._id) {
19153
- cacheMap.set(normalizeId(item._id), item);
19154
- }
19155
- }
19156
- }
19157
- if (!(response === null || response === void 0 ? void 0 : response.success)) {
19158
- throw new DataLinkError(service, path, response);
19197
+ const fetched = await fetchItemsFromService(service, path, idsToFetch, headers);
19198
+ for (const item of fetched) {
19199
+ if (item === null || item === void 0 ? void 0 : item._id) {
19200
+ cacheMap.set(normalizeId(item._id), item);
19159
19201
  }
19160
19202
  }
19161
- catch (error) {
19162
- throw new DataLinkError(service, path, error);
19163
- }
19164
19203
  }
19165
19204
  for (const item of result) {
19166
- const value = getValueByPath(item, field);
19167
- if (!value)
19168
- continue;
19169
- if (typeof value === "string") {
19170
- if (value.includes(",")) {
19171
- const ids = value.split(",").map((v) => normalizeId(v.trim()));
19172
- setValueByPath(item, field, 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, field, cacheMap.get(key));
19178
- }
19179
- }
19180
- }
19181
- else if (Array.isArray(value)) {
19182
- setValueByPath(item, field, value.map((id) => {
19183
- var _a;
19184
- const key = normalizeId(id);
19185
- return (_a = cacheMap.get(key)) !== null && _a !== void 0 ? _a : id;
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, field, cacheMap.get(key));
19194
- }
19195
- }
19205
+ applyResolvedValues(item, field, cacheMap);
19196
19206
  }
19197
- }));
19207
+ }
19198
19208
  return isArray ? result : result[0];
19199
19209
  }
19200
19210
 
19201
- exports.DataLinkError = DataLinkError;
19202
19211
  exports.callService = callService;
19203
19212
  exports.dataLink = dataLink;
19204
19213
  exports.deregisterService = deregisterService;
package/dist/index.esm.js CHANGED
@@ -19055,6 +19055,11 @@ async function callService(serviceName, options = {}) {
19055
19055
  }
19056
19056
  }
19057
19057
 
19058
+ // 🛠 أدوات التعامل مع المسارات
19059
+ function getValueByPath(obj, path) {
19060
+ return path.split(".").reduce((acc, key) => acc === null || acc === void 0 ? void 0 : acc[key], obj);
19061
+ }
19062
+
19058
19063
  function normalizeId(id) {
19059
19064
  if (id && typeof id === "object" && typeof id.toString === "function") {
19060
19065
  return id.toString();
@@ -19062,6 +19067,69 @@ function normalizeId(id) {
19062
19067
  return id;
19063
19068
  }
19064
19069
 
19070
+ function setValueByPath(obj, path, value) {
19071
+ const keys = path.split(".");
19072
+ const lastKey = keys.pop();
19073
+ const target = keys.reduce((acc, key) => {
19074
+ if (!acc[key])
19075
+ acc[key] = {};
19076
+ return acc[key];
19077
+ }, obj);
19078
+ if (lastKey)
19079
+ target[lastKey] = value;
19080
+ }
19081
+
19082
+ function applyResolvedValues(item, field, cacheMap) {
19083
+ const value = getValueByPath(item, field);
19084
+ // ✅ تجاهل الحقول غير الموجودة أو الفارغة تمامًا
19085
+ if (value === undefined || value === null)
19086
+ return;
19087
+ if (typeof value === "string") {
19088
+ if (value.includes(",")) {
19089
+ const ids = value.split(",").map((v) => normalizeId(v.trim()));
19090
+ setValueByPath(item, field, ids.map((id) => { var _a; return (_a = cacheMap.get(id)) !== null && _a !== void 0 ? _a : id; }));
19091
+ }
19092
+ else {
19093
+ const id = normalizeId(value);
19094
+ if (cacheMap.has(id))
19095
+ setValueByPath(item, field, cacheMap.get(id));
19096
+ }
19097
+ }
19098
+ else if (Array.isArray(value)) {
19099
+ setValueByPath(item, field, value.map((id) => {
19100
+ var _a;
19101
+ const key = normalizeId(id);
19102
+ return (_a = cacheMap.get(key)) !== null && _a !== void 0 ? _a : id;
19103
+ }));
19104
+ }
19105
+ else if (value &&
19106
+ typeof value === "object" &&
19107
+ typeof value.toString === "function") {
19108
+ const key = normalizeId(value);
19109
+ if (cacheMap.has(key))
19110
+ setValueByPath(item, field, cacheMap.get(key));
19111
+ }
19112
+ }
19113
+
19114
+ function extractIdsFromField(item, field) {
19115
+ const value = getValueByPath(item, field);
19116
+ if (typeof value === "string") {
19117
+ return value.includes(",")
19118
+ ? value.split(",").map((v) => normalizeId(v.trim()))
19119
+ : [normalizeId(value)];
19120
+ }
19121
+ if (Array.isArray(value)) {
19122
+ return value
19123
+ .filter((v) => typeof v === "string" ||
19124
+ (v && typeof v === "object" && typeof v.toString === "function"))
19125
+ .map((v) => normalizeId(v));
19126
+ }
19127
+ if (value && typeof value === "object" && typeof value.toString === "function") {
19128
+ return [normalizeId(value)];
19129
+ }
19130
+ return [];
19131
+ }
19132
+
19065
19133
  class DataLinkError extends Error {
19066
19134
  constructor(service, path, originalError) {
19067
19135
  super(`DataLinkError: Failed to fetch from ${service}:${path}`);
@@ -19074,127 +19142,69 @@ class DataLinkError extends Error {
19074
19142
  }
19075
19143
  }
19076
19144
  }
19077
- // 🛠 أدوات التعامل مع المسارات
19078
- function getValueByPath(obj, path) {
19079
- return path.split(".").reduce((acc, key) => acc === null || acc === void 0 ? void 0 : acc[key], obj);
19145
+ async function fetchItemsFromService(service, path, ids, headers) {
19146
+ if (!path.startsWith("/")) {
19147
+ throw new DataLinkError(service, path, new Error("Path must start with '/'"));
19148
+ }
19149
+ try {
19150
+ const response = await callService(service, {
19151
+ method: "POST",
19152
+ path,
19153
+ headers,
19154
+ data: { ids },
19155
+ });
19156
+ if (!(response === null || response === void 0 ? void 0 : response.success)) {
19157
+ throw new DataLinkError(service, path, response);
19158
+ }
19159
+ return Array.isArray(response.data) ? response.data : [];
19160
+ }
19161
+ catch (error) {
19162
+ throw new DataLinkError(service, path, error);
19163
+ }
19080
19164
  }
19081
- function setValueByPath(obj, path, value) {
19082
- const keys = path.split(".");
19083
- const lastKey = keys.pop();
19084
- const target = keys.reduce((acc, key) => {
19085
- if (!acc[key])
19086
- acc[key] = {};
19087
- return acc[key];
19088
- }, obj);
19089
- if (lastKey)
19090
- target[lastKey] = value;
19165
+
19166
+ async function getCachedAndUncachedItems(ids, cacheGetter) {
19167
+ const cacheMap = new Map();
19168
+ let idsToFetch = ids;
19169
+ if (cacheGetter) {
19170
+ const results = await cacheGetter(ids);
19171
+ idsToFetch = [];
19172
+ results.forEach((item, index) => {
19173
+ const id = ids[index];
19174
+ if (item) {
19175
+ cacheMap.set(id, item);
19176
+ }
19177
+ else {
19178
+ idsToFetch.push(id);
19179
+ }
19180
+ });
19181
+ }
19182
+ return { cacheMap, idsToFetch };
19091
19183
  }
19184
+
19092
19185
  async function dataLink(data, schema) {
19093
19186
  const isArray = Array.isArray(data);
19094
19187
  const sourceData = isArray ? data : [data];
19095
19188
  const result = sourceData.map((item) => ({ ...item }));
19096
- await Promise.all(schema.map(async ({ field, service, path, headers, cacheGetter }) => {
19097
- if (!path || !path.trim().startsWith("/")) {
19098
- throw new DataLinkError(service, path, new Error(`Invalid path: "${path}". Path must start with "/"`));
19099
- }
19100
- const uniqueIds = Array.from(new Set(sourceData.flatMap((item) => {
19101
- const value = getValueByPath(item, field);
19102
- if (typeof value === "string") {
19103
- if (value.includes(",")) {
19104
- return value.split(",").map((v) => normalizeId(v.trim()));
19105
- }
19106
- return [normalizeId(value)];
19107
- }
19108
- if (Array.isArray(value)) {
19109
- return value
19110
- .filter((v) => typeof v === "string" ||
19111
- (v &&
19112
- typeof v === "object" &&
19113
- typeof v.toString === "function"))
19114
- .map((v) => normalizeId(v));
19115
- }
19116
- if (value &&
19117
- typeof value === "object" &&
19118
- typeof value.toString === "function") {
19119
- return [normalizeId(value)];
19120
- }
19121
- return [];
19122
- })));
19123
- if (uniqueIds.length === 0)
19124
- return;
19125
- let cacheMap = new Map();
19126
- let idsToFetch = uniqueIds;
19127
- if (cacheGetter) {
19128
- const cacheResults = await cacheGetter(uniqueIds);
19129
- idsToFetch = [];
19130
- cacheResults.forEach((item, index) => {
19131
- const id = uniqueIds[index];
19132
- if (item) {
19133
- cacheMap.set(id, item);
19134
- }
19135
- else {
19136
- idsToFetch.push(id);
19137
- }
19138
- });
19139
- }
19189
+ for (const { field, service, path, headers, cacheGetter } of schema) {
19190
+ const allIds = Array.from(new Set(sourceData.flatMap((item) => extractIdsFromField(item, field))));
19191
+ if (allIds.length === 0)
19192
+ continue;
19193
+ const { cacheMap, idsToFetch } = await getCachedAndUncachedItems(allIds, cacheGetter);
19140
19194
  if (idsToFetch.length > 0) {
19141
- try {
19142
- const response = await callService(service, {
19143
- method: "POST",
19144
- path,
19145
- headers,
19146
- data: { ids: idsToFetch },
19147
- });
19148
- if ((response === null || response === void 0 ? void 0 : response.success) && Array.isArray(response.data)) {
19149
- for (const item of response.data) {
19150
- if (item === null || item === void 0 ? void 0 : item._id) {
19151
- cacheMap.set(normalizeId(item._id), item);
19152
- }
19153
- }
19154
- }
19155
- if (!(response === null || response === void 0 ? void 0 : response.success)) {
19156
- throw new DataLinkError(service, path, response);
19195
+ const fetched = await fetchItemsFromService(service, path, idsToFetch, headers);
19196
+ for (const item of fetched) {
19197
+ if (item === null || item === void 0 ? void 0 : item._id) {
19198
+ cacheMap.set(normalizeId(item._id), item);
19157
19199
  }
19158
19200
  }
19159
- catch (error) {
19160
- throw new DataLinkError(service, path, error);
19161
- }
19162
19201
  }
19163
19202
  for (const item of result) {
19164
- const value = getValueByPath(item, field);
19165
- if (!value)
19166
- continue;
19167
- if (typeof value === "string") {
19168
- if (value.includes(",")) {
19169
- const ids = value.split(",").map((v) => normalizeId(v.trim()));
19170
- setValueByPath(item, field, ids.map((id) => { var _a; return (_a = cacheMap.get(id)) !== null && _a !== void 0 ? _a : id; }));
19171
- }
19172
- else {
19173
- const key = normalizeId(value);
19174
- if (cacheMap.has(key)) {
19175
- setValueByPath(item, field, cacheMap.get(key));
19176
- }
19177
- }
19178
- }
19179
- else if (Array.isArray(value)) {
19180
- setValueByPath(item, field, value.map((id) => {
19181
- var _a;
19182
- const key = normalizeId(id);
19183
- return (_a = cacheMap.get(key)) !== null && _a !== void 0 ? _a : id;
19184
- }));
19185
- }
19186
- else if (value &&
19187
- typeof value === "object" &&
19188
- typeof value.toString === "function") {
19189
- const key = normalizeId(value);
19190
- if (cacheMap.has(key)) {
19191
- setValueByPath(item, field, cacheMap.get(key));
19192
- }
19193
- }
19203
+ applyResolvedValues(item, field, cacheMap);
19194
19204
  }
19195
- }));
19205
+ }
19196
19206
  return isArray ? result : result[0];
19197
19207
  }
19198
19208
 
19199
- export { DataLinkError, callService, dataLink, deregisterService, getClient, getRandomServiceInstance, getServiceInstances, getServiceUrl, initClient, listServices, registerService, toBoolean };
19209
+ export { callService, dataLink, deregisterService, getClient, getRandomServiceInstance, getServiceInstances, getServiceUrl, initClient, listServices, registerService, toBoolean };
19200
19210
  //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ export declare function applyResolvedValues(item: any, field: string, cacheMap: Map<string, any>): void;
@@ -0,0 +1 @@
1
+ export declare function extractIdsFromField(item: any, field: string): string[];
@@ -0,0 +1,8 @@
1
+ import { AxiosRequestHeaders } from "axios";
2
+ export declare class DataLinkError extends Error {
3
+ service: string;
4
+ path: string;
5
+ originalError: any;
6
+ constructor(service: string, path: string, originalError: any);
7
+ }
8
+ export declare function fetchItemsFromService(service: string, path: string, ids: string[], headers?: AxiosRequestHeaders): Promise<any[]>;
@@ -0,0 +1,4 @@
1
+ export declare function getCachedAndUncachedItems(ids: string[], cacheGetter?: (ids: string[]) => Promise<(any | null)[]>): Promise<{
2
+ cacheMap: Map<string, any>;
3
+ idsToFetch: string[];
4
+ }>;
@@ -0,0 +1 @@
1
+ export declare function getValueByPath(obj: any, path: string): any;
@@ -0,0 +1 @@
1
+ export declare function setValueByPath(obj: any, path: string, value: any): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-consul-service",
3
- "version": "1.0.59",
3
+ "version": "1.0.61",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",