node-consul-service 1.0.49 → 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.
@@ -4,5 +4,6 @@ export interface ConsulClientOptions {
4
4
  port?: number;
5
5
  secure?: boolean;
6
6
  }
7
+ export declare function toBoolean(value?: string | boolean | null): boolean;
7
8
  export declare function initClient({ host, port, secure, }: ConsulClientOptions): void;
8
9
  export declare function getClient(): Consul;
package/dist/index.cjs.js CHANGED
@@ -15,12 +15,18 @@ var events = require('events');
15
15
 
16
16
  // src/lib/client.ts
17
17
  let consulClient = null;
18
+ function toBoolean(value) {
19
+ if (typeof value === "boolean")
20
+ return value;
21
+ if (typeof value === "string")
22
+ return value.toLowerCase() === "true";
23
+ return false;
24
+ }
18
25
  function initClient({ host = "127.0.0.1", port = 8500, secure = false, }) {
19
- console.log("🚀 ~ secure:", secure, typeof secure, Boolean(secure), typeof Boolean(secure));
20
26
  consulClient = new Consul({
21
27
  host: host !== null && host !== void 0 ? host : "127.0.0.1",
22
28
  port: port !== null && port !== void 0 ? port : 8500,
23
- secure: Boolean(secure),
29
+ secure: toBoolean(secure),
24
30
  });
25
31
  consulClient.agent.self((err) => {
26
32
  if (err) {
@@ -19054,7 +19060,6 @@ async function callService(serviceName, options = {}) {
19054
19060
  }
19055
19061
  }
19056
19062
 
19057
- // ✅ Custom Error Class
19058
19063
  class DataLinkError extends Error {
19059
19064
  constructor(service, path, originalError) {
19060
19065
  super(`DataLinkError: Failed to fetch from ${service}:${path}`);
@@ -19067,31 +19072,27 @@ class DataLinkError extends Error {
19067
19072
  }
19068
19073
  }
19069
19074
  }
19070
- // ✅ Helper to normalize Mongo IDs or ObjectIds
19071
19075
  function normalizeId(id) {
19072
19076
  if (id && typeof id === "object" && typeof id.toString === "function") {
19073
19077
  return id.toString();
19074
19078
  }
19075
19079
  return id;
19076
19080
  }
19077
- // ✅ Main function
19078
19081
  async function dataLink(data, schema) {
19079
19082
  const isArray = Array.isArray(data);
19080
19083
  const sourceData = isArray ? data : [data];
19081
- // Clone data to avoid mutating original
19082
19084
  const result = sourceData.map((item) => ({ ...item }));
19083
19085
  await Promise.all(schema.map(async ({ filed, service, path, headers, cacheGetter }) => {
19084
19086
  var _a;
19085
- // ✅ Safety check + trim path
19086
19087
  if (!path || !path.trim().startsWith("/")) {
19087
19088
  throw new DataLinkError(service, path, new Error(`Invalid path: "${path}". Path must start with "/"`));
19088
19089
  }
19089
19090
  const uniqueIds = Array.from(new Set(sourceData.flatMap((item) => {
19090
19091
  const value = item[filed];
19091
- if (typeof value === "string" ||
19092
- (value &&
19093
- typeof value === "object" &&
19094
- typeof value.toString === "function")) {
19092
+ if (typeof value === "string") {
19093
+ if (value.includes(",")) {
19094
+ return value.split(",").map((v) => normalizeId(v.trim()));
19095
+ }
19095
19096
  return [normalizeId(value)];
19096
19097
  }
19097
19098
  if (Array.isArray(value)) {
@@ -19102,13 +19103,18 @@ async function dataLink(data, schema) {
19102
19103
  typeof v.toString === "function"))
19103
19104
  .map((v) => normalizeId(v));
19104
19105
  }
19106
+ if (value &&
19107
+ typeof value === "object" &&
19108
+ typeof value.toString === "function") {
19109
+ return [normalizeId(value)];
19110
+ }
19105
19111
  return [];
19106
19112
  })));
19113
+ console.log("🚀 ~ schema.map ~ uniqueIds:", uniqueIds);
19107
19114
  if (uniqueIds.length === 0)
19108
19115
  return;
19109
19116
  let cacheMap = new Map();
19110
19117
  let idsToFetch = uniqueIds;
19111
- // ✅ Check cache first
19112
19118
  if (cacheGetter) {
19113
19119
  const cacheResults = await cacheGetter(uniqueIds);
19114
19120
  idsToFetch = [];
@@ -19122,7 +19128,6 @@ async function dataLink(data, schema) {
19122
19128
  }
19123
19129
  });
19124
19130
  }
19125
- // ✅ Fetch missing items from service
19126
19131
  if (idsToFetch.length > 0) {
19127
19132
  try {
19128
19133
  const response = await callService(service, {
@@ -19146,16 +19151,18 @@ async function dataLink(data, schema) {
19146
19151
  throw new DataLinkError(service, path, error);
19147
19152
  }
19148
19153
  }
19149
- // ✅ Replace ids in result with full objects
19150
19154
  for (const item of result) {
19151
19155
  const value = item[filed];
19152
- if (typeof value === "string" ||
19153
- (value &&
19154
- typeof value === "object" &&
19155
- typeof value.toString === "function")) {
19156
- const key = normalizeId(value);
19157
- if (cacheMap.has(key)) {
19158
- item[filed] = cacheMap.get(key);
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
+ }
19159
19166
  }
19160
19167
  }
19161
19168
  else if (Array.isArray(value)) {
@@ -19165,6 +19172,14 @@ async function dataLink(data, schema) {
19165
19172
  return (_a = cacheMap.get(key)) !== null && _a !== void 0 ? _a : id;
19166
19173
  });
19167
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
+ }
19168
19183
  }
19169
19184
  }));
19170
19185
  return isArray ? result : result[0];
@@ -19181,4 +19196,5 @@ exports.getServiceUrl = getServiceUrl;
19181
19196
  exports.initClient = initClient;
19182
19197
  exports.listServices = listServices;
19183
19198
  exports.registerService = registerService;
19199
+ exports.toBoolean = toBoolean;
19184
19200
  //# sourceMappingURL=index.cjs.js.map
package/dist/index.esm.js CHANGED
@@ -13,12 +13,18 @@ import { EventEmitter } from 'events';
13
13
 
14
14
  // src/lib/client.ts
15
15
  let consulClient = null;
16
+ function toBoolean(value) {
17
+ if (typeof value === "boolean")
18
+ return value;
19
+ if (typeof value === "string")
20
+ return value.toLowerCase() === "true";
21
+ return false;
22
+ }
16
23
  function initClient({ host = "127.0.0.1", port = 8500, secure = false, }) {
17
- console.log("🚀 ~ secure:", secure, typeof secure, Boolean(secure), typeof Boolean(secure));
18
24
  consulClient = new Consul({
19
25
  host: host !== null && host !== void 0 ? host : "127.0.0.1",
20
26
  port: port !== null && port !== void 0 ? port : 8500,
21
- secure: Boolean(secure),
27
+ secure: toBoolean(secure),
22
28
  });
23
29
  consulClient.agent.self((err) => {
24
30
  if (err) {
@@ -19052,7 +19058,6 @@ async function callService(serviceName, options = {}) {
19052
19058
  }
19053
19059
  }
19054
19060
 
19055
- // ✅ Custom Error Class
19056
19061
  class DataLinkError extends Error {
19057
19062
  constructor(service, path, originalError) {
19058
19063
  super(`DataLinkError: Failed to fetch from ${service}:${path}`);
@@ -19065,31 +19070,27 @@ class DataLinkError extends Error {
19065
19070
  }
19066
19071
  }
19067
19072
  }
19068
- // ✅ Helper to normalize Mongo IDs or ObjectIds
19069
19073
  function normalizeId(id) {
19070
19074
  if (id && typeof id === "object" && typeof id.toString === "function") {
19071
19075
  return id.toString();
19072
19076
  }
19073
19077
  return id;
19074
19078
  }
19075
- // ✅ Main function
19076
19079
  async function dataLink(data, schema) {
19077
19080
  const isArray = Array.isArray(data);
19078
19081
  const sourceData = isArray ? data : [data];
19079
- // Clone data to avoid mutating original
19080
19082
  const result = sourceData.map((item) => ({ ...item }));
19081
19083
  await Promise.all(schema.map(async ({ filed, service, path, headers, cacheGetter }) => {
19082
19084
  var _a;
19083
- // ✅ Safety check + trim path
19084
19085
  if (!path || !path.trim().startsWith("/")) {
19085
19086
  throw new DataLinkError(service, path, new Error(`Invalid path: "${path}". Path must start with "/"`));
19086
19087
  }
19087
19088
  const uniqueIds = Array.from(new Set(sourceData.flatMap((item) => {
19088
19089
  const value = item[filed];
19089
- if (typeof value === "string" ||
19090
- (value &&
19091
- typeof value === "object" &&
19092
- typeof value.toString === "function")) {
19090
+ if (typeof value === "string") {
19091
+ if (value.includes(",")) {
19092
+ return value.split(",").map((v) => normalizeId(v.trim()));
19093
+ }
19093
19094
  return [normalizeId(value)];
19094
19095
  }
19095
19096
  if (Array.isArray(value)) {
@@ -19100,13 +19101,18 @@ async function dataLink(data, schema) {
19100
19101
  typeof v.toString === "function"))
19101
19102
  .map((v) => normalizeId(v));
19102
19103
  }
19104
+ if (value &&
19105
+ typeof value === "object" &&
19106
+ typeof value.toString === "function") {
19107
+ return [normalizeId(value)];
19108
+ }
19103
19109
  return [];
19104
19110
  })));
19111
+ console.log("🚀 ~ schema.map ~ uniqueIds:", uniqueIds);
19105
19112
  if (uniqueIds.length === 0)
19106
19113
  return;
19107
19114
  let cacheMap = new Map();
19108
19115
  let idsToFetch = uniqueIds;
19109
- // ✅ Check cache first
19110
19116
  if (cacheGetter) {
19111
19117
  const cacheResults = await cacheGetter(uniqueIds);
19112
19118
  idsToFetch = [];
@@ -19120,7 +19126,6 @@ async function dataLink(data, schema) {
19120
19126
  }
19121
19127
  });
19122
19128
  }
19123
- // ✅ Fetch missing items from service
19124
19129
  if (idsToFetch.length > 0) {
19125
19130
  try {
19126
19131
  const response = await callService(service, {
@@ -19144,16 +19149,18 @@ async function dataLink(data, schema) {
19144
19149
  throw new DataLinkError(service, path, error);
19145
19150
  }
19146
19151
  }
19147
- // ✅ Replace ids in result with full objects
19148
19152
  for (const item of result) {
19149
19153
  const value = item[filed];
19150
- if (typeof value === "string" ||
19151
- (value &&
19152
- typeof value === "object" &&
19153
- typeof value.toString === "function")) {
19154
- const key = normalizeId(value);
19155
- if (cacheMap.has(key)) {
19156
- item[filed] = cacheMap.get(key);
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
+ }
19157
19164
  }
19158
19165
  }
19159
19166
  else if (Array.isArray(value)) {
@@ -19163,10 +19170,18 @@ async function dataLink(data, schema) {
19163
19170
  return (_a = cacheMap.get(key)) !== null && _a !== void 0 ? _a : id;
19164
19171
  });
19165
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
+ }
19166
19181
  }
19167
19182
  }));
19168
19183
  return isArray ? result : result[0];
19169
19184
  }
19170
19185
 
19171
- export { DataLinkError, callService, dataLink, deregisterService, getClient, getRandomServiceInstance, getServiceInstances, getServiceUrl, initClient, listServices, registerService };
19186
+ export { DataLinkError, callService, dataLink, deregisterService, getClient, getRandomServiceInstance, getServiceInstances, getServiceUrl, initClient, listServices, registerService, toBoolean };
19172
19187
  //# 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.49",
3
+ "version": "1.0.51",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",