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