node-consul-service 1.0.14 → 1.0.15

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.
@@ -0,0 +1,8 @@
1
+ interface LinkSchema {
2
+ filed: string;
3
+ service: string;
4
+ path: string;
5
+ cacheGetter?: (id: string) => Promise<any>;
6
+ }
7
+ export declare function dataLink(data: any[], schema: LinkSchema[]): Promise<any[]>;
8
+ export {};
package/dist/index.cjs.js CHANGED
@@ -19034,7 +19034,45 @@ async function callService(serviceName, options = {}) {
19034
19034
  return response.data;
19035
19035
  }
19036
19036
 
19037
+ async function dataLink(data, schema) {
19038
+ const result = [...data];
19039
+ await Promise.all(schema.map(async ({ filed, service, path, cacheGetter }) => {
19040
+ const uniqueIds = Array.from(new Set(data.map((item) => item[filed])));
19041
+ if (uniqueIds.length === 0)
19042
+ return;
19043
+ let cacheMap = new Map();
19044
+ let idsToFetch = uniqueIds;
19045
+ if (cacheGetter) {
19046
+ const cacheResults = await Promise.all(uniqueIds.map(id => cacheGetter(id)));
19047
+ idsToFetch = [];
19048
+ cacheResults.forEach((item, index) => {
19049
+ if (item) {
19050
+ cacheMap.set(uniqueIds[index], item);
19051
+ }
19052
+ else {
19053
+ idsToFetch.push(uniqueIds[index]);
19054
+ }
19055
+ });
19056
+ }
19057
+ if (idsToFetch.length > 0) {
19058
+ const apiData = await callService(service, {
19059
+ method: "POST",
19060
+ path,
19061
+ data: { ids: idsToFetch },
19062
+ });
19063
+ apiData.forEach((item) => cacheMap.set(item._id, item));
19064
+ }
19065
+ for (const item of result) {
19066
+ if (cacheMap.has(item[filed])) {
19067
+ item[filed] = cacheMap.get(item[filed]);
19068
+ }
19069
+ }
19070
+ }));
19071
+ return result;
19072
+ }
19073
+
19037
19074
  exports.callService = callService;
19075
+ exports.dataLink = dataLink;
19038
19076
  exports.deregisterService = deregisterService;
19039
19077
  exports.getRandomServiceInstance = getRandomServiceInstance;
19040
19078
  exports.getServiceInstances = getServiceInstances;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './consul/registry';
2
2
  export * from './consul/discovery';
3
3
  export * from './consul/httpCaller';
4
+ export * from './consul/dataLink';
package/dist/index.esm.js CHANGED
@@ -19032,5 +19032,42 @@ async function callService(serviceName, options = {}) {
19032
19032
  return response.data;
19033
19033
  }
19034
19034
 
19035
- export { callService, deregisterService, getRandomServiceInstance, getServiceInstances, getServiceUrl, listServices, registerService };
19035
+ async function dataLink(data, schema) {
19036
+ const result = [...data];
19037
+ await Promise.all(schema.map(async ({ filed, service, path, cacheGetter }) => {
19038
+ const uniqueIds = Array.from(new Set(data.map((item) => item[filed])));
19039
+ if (uniqueIds.length === 0)
19040
+ return;
19041
+ let cacheMap = new Map();
19042
+ let idsToFetch = uniqueIds;
19043
+ if (cacheGetter) {
19044
+ const cacheResults = await Promise.all(uniqueIds.map(id => cacheGetter(id)));
19045
+ idsToFetch = [];
19046
+ cacheResults.forEach((item, index) => {
19047
+ if (item) {
19048
+ cacheMap.set(uniqueIds[index], item);
19049
+ }
19050
+ else {
19051
+ idsToFetch.push(uniqueIds[index]);
19052
+ }
19053
+ });
19054
+ }
19055
+ if (idsToFetch.length > 0) {
19056
+ const apiData = await callService(service, {
19057
+ method: "POST",
19058
+ path,
19059
+ data: { ids: idsToFetch },
19060
+ });
19061
+ apiData.forEach((item) => cacheMap.set(item._id, item));
19062
+ }
19063
+ for (const item of result) {
19064
+ if (cacheMap.has(item[filed])) {
19065
+ item[filed] = cacheMap.get(item[filed]);
19066
+ }
19067
+ }
19068
+ }));
19069
+ return result;
19070
+ }
19071
+
19072
+ export { callService, dataLink, deregisterService, getRandomServiceInstance, getServiceInstances, getServiceUrl, listServices, registerService };
19036
19073
  //# sourceMappingURL=index.esm.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-consul-service",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",