@adventurelabs/scout-core 1.0.47 → 1.0.49

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.
@@ -1,6 +1,6 @@
1
1
  import { IApiKeyScout } from "../types/db";
2
- export declare function test_api_key_loading(device_id: string): Promise<boolean>;
3
- export declare function server_list_api_keys(device_id: string): Promise<IApiKeyScout[]>;
4
- export declare function server_list_api_keys_batch(device_ids: string[]): Promise<{
2
+ export declare function test_api_key_loading(device_id: number): Promise<boolean>;
3
+ export declare function server_list_api_keys(device_id: number): Promise<IApiKeyScout[]>;
4
+ export declare function server_list_api_keys_batch(device_ids: number[]): Promise<{
5
5
  [device_id: number]: IApiKeyScout[];
6
6
  }>;
@@ -25,6 +25,7 @@ export async function server_list_api_keys(device_id) {
25
25
  return [];
26
26
  const data_to_return = [];
27
27
  for (let i = 0; i < data.length; i++) {
28
+ // Parse the JSON string from the text array
28
29
  const converted_data = JSON.parse(data[i]);
29
30
  data_to_return.push(converted_data);
30
31
  }
@@ -32,49 +33,70 @@ export async function server_list_api_keys(device_id) {
32
33
  }
33
34
  export async function server_list_api_keys_batch(device_ids) {
34
35
  const startTime = Date.now();
35
- // CRITICAL: Check for UUIDs being passed instead of device IDs
36
- const invalidIds = device_ids.filter((id) => !/^\d+$/.test(id));
37
- if (invalidIds.length > 0) {
38
- console.error(`[CRITICAL] UUIDs detected instead of device IDs:`, invalidIds);
39
- console.error(`[CRITICAL] Valid device IDs should be:`, device_ids.filter((id) => /^\d+$/.test(id)));
40
- return {};
41
- }
42
36
  const supabase = await newServerClient();
43
- const { data, error } = await supabase.rpc("load_api_keys_batch", {
44
- device_ids: device_ids,
45
- });
46
- if (error) {
47
- console.error(`[API Keys Batch] Database error:`, error.message);
37
+ // Check if the batch function exists by trying a simple call
38
+ try {
39
+ const { data, error } = await supabase.rpc("load_api_keys_batch", {
40
+ device_ids: device_ids,
41
+ });
42
+ if (error) {
43
+ // Check if it's a "function does not exist" error
44
+ if (error.message.includes("function") &&
45
+ error.message.includes("does not exist")) {
46
+ console.log(`[API Keys Batch] Batch function not deployed, using individual calls...`);
47
+ }
48
+ else {
49
+ console.error(`[API Keys Batch] Database error:`, error.message);
50
+ }
51
+ console.log(`[API Keys Batch] Falling back to individual calls...`);
52
+ // Fallback to individual API key loading
53
+ const result = {};
54
+ const promises = device_ids.map(async (device_id) => {
55
+ try {
56
+ const api_keys = await server_list_api_keys(device_id);
57
+ result[device_id] = api_keys;
58
+ }
59
+ catch (err) {
60
+ console.warn(`[API Keys Batch] Failed for device ${device_id}:`, err);
61
+ result[device_id] = [];
62
+ }
63
+ });
64
+ await Promise.all(promises);
65
+ return result;
66
+ }
67
+ if (!data) {
68
+ return {};
69
+ }
70
+ const result = {};
71
+ // Group API keys by device_id
72
+ data.forEach((item) => {
73
+ const device_id = item.device_id;
74
+ if (!result[device_id]) {
75
+ result[device_id] = [];
76
+ }
77
+ result[device_id].push({
78
+ id: item.api_key_id.toString(),
79
+ key: item.api_key_key,
80
+ });
81
+ });
82
+ return result;
83
+ }
84
+ catch (err) {
85
+ console.error(`[API Keys Batch] Unexpected error:`, err);
48
86
  console.log(`[API Keys Batch] Falling back to individual calls...`);
49
87
  // Fallback to individual API key loading
50
88
  const result = {};
51
89
  const promises = device_ids.map(async (device_id) => {
52
90
  try {
53
91
  const api_keys = await server_list_api_keys(device_id);
54
- result[parseInt(device_id)] = api_keys;
92
+ result[device_id] = api_keys;
55
93
  }
56
94
  catch (err) {
57
95
  console.warn(`[API Keys Batch] Failed for device ${device_id}:`, err);
58
- result[parseInt(device_id)] = [];
96
+ result[device_id] = [];
59
97
  }
60
98
  });
61
99
  await Promise.all(promises);
62
100
  return result;
63
101
  }
64
- if (!data) {
65
- return {};
66
- }
67
- const result = {};
68
- // Group API keys by device_id
69
- data.forEach((item) => {
70
- const device_id = item.device_id;
71
- if (!result[device_id]) {
72
- result[device_id] = [];
73
- }
74
- result[device_id].push({
75
- id: item.api_key_id.toString(),
76
- key: item.api_key_key,
77
- });
78
- });
79
- return result;
80
102
  }
@@ -6,7 +6,7 @@ export declare function server_delete_tags_by_ids(tag_ids: number[]): Promise<IW
6
6
  export declare function server_update_tags(tags: ITag[]): Promise<IWebResponseCompatible<ITag[]>>;
7
7
  export declare function server_get_more_events_with_tags_by_herd(herd_id: number, offset: number, page_count?: number): Promise<IWebResponseCompatible<IEventWithTags[]>>;
8
8
  export declare function server_get_events_and_tags_for_device(device_id: number, limit?: number): Promise<IWebResponseCompatible<IEventWithTags[]>>;
9
- export declare function server_get_events_and_tags_for_devices_batch(device_ids: string[], limit?: number): Promise<IWebResponseCompatible<{
9
+ export declare function server_get_events_and_tags_for_devices_batch(device_ids: number[], limit?: number): Promise<IWebResponseCompatible<{
10
10
  [device_id: number]: IEventWithTags[];
11
11
  }>>;
12
12
  export declare function get_event_and_tags_by_event_id(event_id: number): Promise<IWebResponseCompatible<IEventWithTags>>;
@@ -167,13 +167,6 @@ export async function server_get_events_and_tags_for_device(device_id, limit = 3
167
167
  }
168
168
  export async function server_get_events_and_tags_for_devices_batch(device_ids, limit = 1) {
169
169
  const startTime = Date.now();
170
- // CRITICAL: Check for UUIDs being passed instead of device IDs
171
- const invalidIds = device_ids.filter((id) => !/^\d+$/.test(id));
172
- if (invalidIds.length > 0) {
173
- console.error(`[CRITICAL] UUIDs detected instead of device IDs:`, invalidIds);
174
- console.error(`[CRITICAL] Valid device IDs should be:`, device_ids.filter((id) => /^\d+$/.test(id)));
175
- return IWebResponse.success({}).to_compatible();
176
- }
177
170
  const supabase = await newServerClient();
178
171
  // Use single RPC call for all devices
179
172
  const { data, error } = await supabase.rpc("get_events_and_tags_for_devices_batch", {
@@ -187,19 +180,18 @@ export async function server_get_events_and_tags_for_devices_batch(device_ids, l
187
180
  const result = {};
188
181
  const promises = device_ids.map(async (device_id) => {
189
182
  try {
190
- const device_id_num = parseInt(device_id);
191
- const events_response = await server_get_events_and_tags_for_device(device_id_num, limit);
183
+ const events_response = await server_get_events_and_tags_for_device(device_id, limit);
192
184
  if (events_response.status === EnumWebResponse.SUCCESS &&
193
185
  events_response.data) {
194
- result[device_id_num] = events_response.data;
186
+ result[device_id] = events_response.data;
195
187
  }
196
188
  else {
197
- result[device_id_num] = [];
189
+ result[device_id] = [];
198
190
  }
199
191
  }
200
192
  catch (err) {
201
193
  console.warn(`[Events Batch] Failed for device ${device_id}:`, err);
202
- result[parseInt(device_id)] = [];
194
+ result[device_id] = [];
203
195
  }
204
196
  });
205
197
  await Promise.all(promises);
@@ -740,13 +740,13 @@ export declare function useSupabase(): SupabaseClient<Database, "public", {
740
740
  };
741
741
  load_api_keys: {
742
742
  Args: {
743
- id_of_device: string;
743
+ id_of_device: number;
744
744
  };
745
745
  Returns: string[];
746
746
  };
747
747
  load_api_keys_batch: {
748
748
  Args: {
749
- device_ids: string[];
749
+ device_ids: number[];
750
750
  };
751
751
  Returns: {
752
752
  device_id: number;
@@ -756,7 +756,7 @@ export declare function useSupabase(): SupabaseClient<Database, "public", {
756
756
  };
757
757
  get_events_and_tags_for_devices_batch: {
758
758
  Args: {
759
- device_ids: string[];
759
+ device_ids: number[];
760
760
  limit_per_device: number;
761
761
  };
762
762
  Returns: {
@@ -731,13 +731,13 @@ export declare function newServerClient(): Promise<import("@supabase/supabase-js
731
731
  };
732
732
  load_api_keys: {
733
733
  Args: {
734
- id_of_device: string;
734
+ id_of_device: number;
735
735
  };
736
736
  Returns: string[];
737
737
  };
738
738
  load_api_keys_batch: {
739
739
  Args: {
740
- device_ids: string[];
740
+ device_ids: number[];
741
741
  };
742
742
  Returns: {
743
743
  device_id: number;
@@ -747,7 +747,7 @@ export declare function newServerClient(): Promise<import("@supabase/supabase-js
747
747
  };
748
748
  get_events_and_tags_for_devices_batch: {
749
749
  Args: {
750
- device_ids: string[];
750
+ device_ids: number[];
751
751
  limit_per_device: number;
752
752
  };
753
753
  Returns: {
@@ -61,17 +61,7 @@ export class HerdModule {
61
61
  if (new_devices.length > 0) {
62
62
  const batchStartTime = Date.now();
63
63
  try {
64
- const device_ids = new_devices.map((device) => (device.id ?? 0).toString());
65
- // CRITICAL: Check for UUIDs being passed instead of device IDs
66
- const invalidDeviceIds = device_ids.filter((id) => !/^\d+$/.test(id));
67
- if (invalidDeviceIds.length > 0) {
68
- console.error(`[CRITICAL] UUIDs detected in HerdModule:`, invalidDeviceIds);
69
- console.error(`[CRITICAL] Device objects:`, new_devices.map((d) => ({ id: d.id, created_by: d.created_by })));
70
- // Filter out invalid IDs
71
- const validDeviceIds = device_ids.filter((id) => /^\d+$/.test(id));
72
- device_ids.length = 0;
73
- device_ids.push(...validDeviceIds);
74
- }
64
+ const device_ids = new_devices.map((device) => device.id ?? 0);
75
65
  // Load API keys and events in parallel
76
66
  const [api_keys_batch, events_response] = await Promise.all([
77
67
  server_list_api_keys_batch(device_ids),
@@ -798,13 +798,13 @@ export type Database = {
798
798
  };
799
799
  load_api_keys: {
800
800
  Args: {
801
- id_of_device: string;
801
+ id_of_device: number;
802
802
  };
803
803
  Returns: string[];
804
804
  };
805
805
  load_api_keys_batch: {
806
806
  Args: {
807
- device_ids: string[];
807
+ device_ids: number[];
808
808
  };
809
809
  Returns: {
810
810
  device_id: number;
@@ -814,7 +814,7 @@ export type Database = {
814
814
  };
815
815
  get_events_and_tags_for_devices_batch: {
816
816
  Args: {
817
- device_ids: string[];
817
+ device_ids: number[];
818
818
  limit_per_device: number;
819
819
  };
820
820
  Returns: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adventurelabs/scout-core",
3
- "version": "1.0.47",
3
+ "version": "1.0.49",
4
4
  "description": "Core utilities and helpers for Adventure Labs Scout applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",