@adventurelabs/scout-core 1.4.3 → 1.4.4
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/constants/env.d.ts +24 -0
- package/dist/constants/env.js +47 -0
- package/dist/constants/index.d.ts +1 -0
- package/dist/constants/index.js +1 -0
- package/dist/helpers/connectivity.d.ts +4 -1
- package/dist/helpers/connectivity.js +48 -0
- package/dist/helpers/db.js +3 -2
- package/dist/helpers/events.d.ts +4 -0
- package/dist/helpers/events.js +48 -0
- package/dist/helpers/sessions.d.ts +3 -1
- package/dist/helpers/sessions.js +48 -0
- package/dist/hooks/useScoutRefresh.js +3 -1
- package/dist/providers/ScoutRefreshProvider.js +4 -2
- package/dist/types/db.d.ts +6 -0
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This helper checks multiple possible environment variable names to support
|
|
3
|
+
* different frameworks and build tools:
|
|
4
|
+
* - NEXT_PUBLIC_* (Next.js convention, also works in other frameworks)
|
|
5
|
+
* - VITE_* (Vite convention)
|
|
6
|
+
* - REACT_APP_* (Create React App convention)
|
|
7
|
+
* - Direct access (for runtime configuration)
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Get an environment variable, checking multiple possible names
|
|
11
|
+
*
|
|
12
|
+
* @param name - The base name of the environment variable (without prefix)
|
|
13
|
+
* @param defaultValue - Optional default value if not found
|
|
14
|
+
* @returns The environment variable value or default
|
|
15
|
+
*/
|
|
16
|
+
export declare function getEnvVar(name: string, defaultValue?: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Get Supabase URL from environment variables
|
|
19
|
+
*/
|
|
20
|
+
export declare function getSupabaseUrl(): string;
|
|
21
|
+
/**
|
|
22
|
+
* Get Supabase anonymous key from environment variables
|
|
23
|
+
*/
|
|
24
|
+
export declare function getSupabaseAnonKey(): string;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This helper checks multiple possible environment variable names to support
|
|
3
|
+
* different frameworks and build tools:
|
|
4
|
+
* - NEXT_PUBLIC_* (Next.js convention, also works in other frameworks)
|
|
5
|
+
* - VITE_* (Vite convention)
|
|
6
|
+
* - REACT_APP_* (Create React App convention)
|
|
7
|
+
* - Direct access (for runtime configuration)
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Get an environment variable, checking multiple possible names
|
|
11
|
+
*
|
|
12
|
+
* @param name - The base name of the environment variable (without prefix)
|
|
13
|
+
* @param defaultValue - Optional default value if not found
|
|
14
|
+
* @returns The environment variable value or default
|
|
15
|
+
*/
|
|
16
|
+
export function getEnvVar(name, defaultValue = "") {
|
|
17
|
+
// Check multiple possible prefixes
|
|
18
|
+
const possibleNames = [
|
|
19
|
+
`NEXT_PUBLIC_${name}`, // Next.js (also works in other frameworks)
|
|
20
|
+
`VITE_${name}`, // Vite
|
|
21
|
+
`REACT_APP_${name}`, // Create React App
|
|
22
|
+
name, // Direct access (for runtime configuration)
|
|
23
|
+
];
|
|
24
|
+
// Try each possible name
|
|
25
|
+
for (const envName of possibleNames) {
|
|
26
|
+
if (typeof process !== "undefined" && process.env?.[envName]) {
|
|
27
|
+
return process.env[envName];
|
|
28
|
+
}
|
|
29
|
+
// Also check window object for runtime configuration (PWA use case)
|
|
30
|
+
if (typeof window !== "undefined" && window.__SCOUT_CONFIG__?.[name]) {
|
|
31
|
+
return window.__SCOUT_CONFIG__[name];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return defaultValue;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get Supabase URL from environment variables
|
|
38
|
+
*/
|
|
39
|
+
export function getSupabaseUrl() {
|
|
40
|
+
return getEnvVar("SUPABASE_URL", "");
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get Supabase anonymous key from environment variables
|
|
44
|
+
*/
|
|
45
|
+
export function getSupabaseAnonKey() {
|
|
46
|
+
return getEnvVar("SUPABASE_ANON_KEY", "");
|
|
47
|
+
}
|
package/dist/constants/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { IWebResponseCompatible } from "../types/requests";
|
|
2
|
-
import { IConnectivityWithCoordinates } from "../types/db";
|
|
2
|
+
import { IConnectivityWithCoordinates, IConnectivity, ConnectivityInsert, ConnectivityUpdate } from "../types/db";
|
|
3
|
+
import { SupabaseClient } from "@supabase/supabase-js";
|
|
3
4
|
export declare function server_get_connectivity_by_session_id(sessionId: number): Promise<IWebResponseCompatible<IConnectivityWithCoordinates[]>>;
|
|
4
5
|
export declare function server_get_connectivity_by_device_id(deviceId: number, timestamp: string): Promise<IWebResponseCompatible<IConnectivityWithCoordinates[]>>;
|
|
6
|
+
export declare function server_insert_connectivity(connectivity: ConnectivityInsert, client?: SupabaseClient): Promise<IWebResponseCompatible<IConnectivity | null>>;
|
|
7
|
+
export declare function server_update_connectivity(connectivityId: number, updates: ConnectivityUpdate, client?: SupabaseClient): Promise<IWebResponseCompatible<IConnectivity | null>>;
|
|
@@ -44,3 +44,51 @@ export async function server_get_connectivity_by_device_id(deviceId, timestamp)
|
|
|
44
44
|
});
|
|
45
45
|
return IWebResponse.success(sortedConnectivity).to_compatible();
|
|
46
46
|
}
|
|
47
|
+
// Insert a new connectivity record
|
|
48
|
+
export async function server_insert_connectivity(connectivity, client) {
|
|
49
|
+
const supabase = client || (await newServerClient());
|
|
50
|
+
const { data, error } = await supabase
|
|
51
|
+
.from("connectivity")
|
|
52
|
+
.insert([connectivity])
|
|
53
|
+
.select("*")
|
|
54
|
+
.single();
|
|
55
|
+
if (error) {
|
|
56
|
+
console.warn("Error inserting connectivity:", error.message);
|
|
57
|
+
return {
|
|
58
|
+
status: EnumWebResponse.ERROR,
|
|
59
|
+
msg: error.message,
|
|
60
|
+
data: null,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return IWebResponse.success(data).to_compatible();
|
|
64
|
+
}
|
|
65
|
+
// Update an existing connectivity record
|
|
66
|
+
export async function server_update_connectivity(connectivityId, updates, client) {
|
|
67
|
+
const supabase = client || (await newServerClient());
|
|
68
|
+
// Remove fields that shouldn't be updated
|
|
69
|
+
const updateData = { ...updates };
|
|
70
|
+
delete updateData.id;
|
|
71
|
+
delete updateData.inserted_at;
|
|
72
|
+
const { data, error } = await supabase
|
|
73
|
+
.from("connectivity")
|
|
74
|
+
.update(updateData)
|
|
75
|
+
.eq("id", connectivityId)
|
|
76
|
+
.select("*")
|
|
77
|
+
.single();
|
|
78
|
+
if (error) {
|
|
79
|
+
console.warn("Error updating connectivity:", error.message);
|
|
80
|
+
return {
|
|
81
|
+
status: EnumWebResponse.ERROR,
|
|
82
|
+
msg: error.message,
|
|
83
|
+
data: null,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (!data) {
|
|
87
|
+
return {
|
|
88
|
+
status: EnumWebResponse.ERROR,
|
|
89
|
+
msg: "Connectivity record not found or update failed",
|
|
90
|
+
data: null,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return IWebResponse.success(data).to_compatible();
|
|
94
|
+
}
|
package/dist/helpers/db.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { createClient } from "@supabase/supabase-js";
|
|
2
|
+
import { getSupabaseUrl, getSupabaseAnonKey } from "../constants/env";
|
|
2
3
|
export function createClientWithApiKey(user_api_key) {
|
|
3
|
-
const supabase_url =
|
|
4
|
-
const supabase_anon_key =
|
|
4
|
+
const supabase_url = getSupabaseUrl();
|
|
5
|
+
const supabase_anon_key = getSupabaseAnonKey();
|
|
5
6
|
if (!supabase_url || !supabase_anon_key) {
|
|
6
7
|
return null;
|
|
7
8
|
}
|
package/dist/helpers/events.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
import { IWebResponseCompatible } from "../types/requests";
|
|
2
2
|
import { EnumSessionsVisibility } from "../types/events";
|
|
3
|
+
import { IEvent, EventInsert, EventUpdate } from "../types/db";
|
|
4
|
+
import { SupabaseClient } from "@supabase/supabase-js";
|
|
3
5
|
export declare function server_get_total_events_by_herd(herd_id: number, sessions_visibility: EnumSessionsVisibility): Promise<IWebResponseCompatible<number>>;
|
|
6
|
+
export declare function server_insert_event(event: EventInsert, client?: SupabaseClient): Promise<IWebResponseCompatible<IEvent | null>>;
|
|
7
|
+
export declare function server_update_event(eventId: number, updates: EventUpdate, client?: SupabaseClient): Promise<IWebResponseCompatible<IEvent | null>>;
|
package/dist/helpers/events.js
CHANGED
|
@@ -22,3 +22,51 @@ export async function server_get_total_events_by_herd(herd_id, sessions_visibili
|
|
|
22
22
|
return IWebResponse.success(data || 0).to_compatible();
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
+
// Insert a new event
|
|
26
|
+
export async function server_insert_event(event, client) {
|
|
27
|
+
const supabase = client || (await newServerClient());
|
|
28
|
+
const { data, error } = await supabase
|
|
29
|
+
.from("events")
|
|
30
|
+
.insert([event])
|
|
31
|
+
.select("*")
|
|
32
|
+
.single();
|
|
33
|
+
if (error) {
|
|
34
|
+
console.warn("Error inserting event:", error.message);
|
|
35
|
+
return {
|
|
36
|
+
status: EnumWebResponse.ERROR,
|
|
37
|
+
msg: error.message,
|
|
38
|
+
data: null,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return IWebResponse.success(data).to_compatible();
|
|
42
|
+
}
|
|
43
|
+
// Update an existing event
|
|
44
|
+
export async function server_update_event(eventId, updates, client) {
|
|
45
|
+
const supabase = client || (await newServerClient());
|
|
46
|
+
// Remove fields that shouldn't be updated
|
|
47
|
+
const updateData = { ...updates };
|
|
48
|
+
delete updateData.id;
|
|
49
|
+
delete updateData.inserted_at;
|
|
50
|
+
const { data, error } = await supabase
|
|
51
|
+
.from("events")
|
|
52
|
+
.update(updateData)
|
|
53
|
+
.eq("id", eventId)
|
|
54
|
+
.select("*")
|
|
55
|
+
.single();
|
|
56
|
+
if (error) {
|
|
57
|
+
console.warn("Error updating event:", error.message);
|
|
58
|
+
return {
|
|
59
|
+
status: EnumWebResponse.ERROR,
|
|
60
|
+
msg: error.message,
|
|
61
|
+
data: null,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (!data) {
|
|
65
|
+
return {
|
|
66
|
+
status: EnumWebResponse.ERROR,
|
|
67
|
+
msg: "Event not found or update failed",
|
|
68
|
+
data: null,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return IWebResponse.success(data).to_compatible();
|
|
72
|
+
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { ISessionWithCoordinates, ISessionUsageOverTime, IEventAndTagsPrettyLocation } from "../types/db";
|
|
1
|
+
import { ISessionWithCoordinates, ISessionUsageOverTime, IEventAndTagsPrettyLocation, ISession, SessionInsert, SessionUpdate } from "../types/db";
|
|
2
2
|
import { IWebResponseCompatible } from "../types/requests";
|
|
3
3
|
import { SupabaseClient } from "@supabase/supabase-js";
|
|
4
4
|
export declare function server_get_session_by_id(sessionId: number, client?: SupabaseClient): Promise<IWebResponseCompatible<ISessionWithCoordinates | null>>;
|
|
5
5
|
export declare function server_get_session_usage_over_time_by_herd(herd_id: number, client?: SupabaseClient): Promise<IWebResponseCompatible<ISessionUsageOverTime>>;
|
|
6
6
|
export declare function server_get_session_usage_over_time_by_device(device_id: number, client?: SupabaseClient): Promise<IWebResponseCompatible<ISessionUsageOverTime>>;
|
|
7
7
|
export declare function server_get_events_and_tags_by_session_id(sessionId: number, limit?: number, offset?: number): Promise<IWebResponseCompatible<IEventAndTagsPrettyLocation[]>>;
|
|
8
|
+
export declare function server_insert_session(session: SessionInsert, client?: SupabaseClient): Promise<IWebResponseCompatible<ISession | null>>;
|
|
9
|
+
export declare function server_update_session(sessionId: number, updates: SessionUpdate, client?: SupabaseClient): Promise<IWebResponseCompatible<ISession | null>>;
|
package/dist/helpers/sessions.js
CHANGED
|
@@ -71,3 +71,51 @@ export async function server_get_events_and_tags_by_session_id(sessionId, limit
|
|
|
71
71
|
}
|
|
72
72
|
return IWebResponse.success(data || []).to_compatible();
|
|
73
73
|
}
|
|
74
|
+
// Insert a new session
|
|
75
|
+
export async function server_insert_session(session, client) {
|
|
76
|
+
const supabase = client || (await newServerClient());
|
|
77
|
+
const { data, error } = await supabase
|
|
78
|
+
.from("sessions")
|
|
79
|
+
.insert([session])
|
|
80
|
+
.select("*")
|
|
81
|
+
.single();
|
|
82
|
+
if (error) {
|
|
83
|
+
console.warn("Error inserting session:", error.message);
|
|
84
|
+
return {
|
|
85
|
+
status: EnumWebResponse.ERROR,
|
|
86
|
+
msg: error.message,
|
|
87
|
+
data: null,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return IWebResponse.success(data).to_compatible();
|
|
91
|
+
}
|
|
92
|
+
// Update an existing session
|
|
93
|
+
export async function server_update_session(sessionId, updates, client) {
|
|
94
|
+
const supabase = client || (await newServerClient());
|
|
95
|
+
// Remove fields that shouldn't be updated
|
|
96
|
+
const updateData = { ...updates };
|
|
97
|
+
delete updateData.id;
|
|
98
|
+
delete updateData.inserted_at;
|
|
99
|
+
const { data, error } = await supabase
|
|
100
|
+
.from("sessions")
|
|
101
|
+
.update(updateData)
|
|
102
|
+
.eq("id", sessionId)
|
|
103
|
+
.select("*")
|
|
104
|
+
.single();
|
|
105
|
+
if (error) {
|
|
106
|
+
console.warn("Error updating session:", error.message);
|
|
107
|
+
return {
|
|
108
|
+
status: EnumWebResponse.ERROR,
|
|
109
|
+
msg: error.message,
|
|
110
|
+
data: null,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (!data) {
|
|
114
|
+
return {
|
|
115
|
+
status: EnumWebResponse.ERROR,
|
|
116
|
+
msg: "Session not found or update failed",
|
|
117
|
+
data: null,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return IWebResponse.success(data).to_compatible();
|
|
121
|
+
}
|
|
@@ -7,6 +7,7 @@ import { server_load_herd_modules } from "../helpers/herds";
|
|
|
7
7
|
import { scoutCache } from "../helpers/cache";
|
|
8
8
|
import { EnumDataSource } from "../types/data_source";
|
|
9
9
|
import { createBrowserClient } from "@supabase/ssr";
|
|
10
|
+
import { getSupabaseUrl, getSupabaseAnonKey } from "../constants/env";
|
|
10
11
|
/**
|
|
11
12
|
* Hook for refreshing scout data with detailed timing measurements and cache-first loading
|
|
12
13
|
*
|
|
@@ -37,8 +38,9 @@ export function useScoutRefresh(options = {}) {
|
|
|
37
38
|
const store = useStore();
|
|
38
39
|
const refreshInProgressRef = useRef(false);
|
|
39
40
|
// Create Supabase client directly to avoid circular dependency
|
|
41
|
+
// Uses flexible environment variable helper for better PWA compatibility
|
|
40
42
|
const supabase = useMemo(() => {
|
|
41
|
-
return createBrowserClient(
|
|
43
|
+
return createBrowserClient(getSupabaseUrl(), getSupabaseAnonKey());
|
|
42
44
|
}, []);
|
|
43
45
|
// Refs to store timing measurements
|
|
44
46
|
const timingRefs = useRef({
|
|
@@ -3,6 +3,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
3
3
|
import { useScoutRefresh } from "../hooks/useScoutRefresh";
|
|
4
4
|
import { createContext, useContext, useMemo, useRef } from "react";
|
|
5
5
|
import { createBrowserClient } from "@supabase/ssr";
|
|
6
|
+
import { getSupabaseUrl, getSupabaseAnonKey } from "../constants/env";
|
|
6
7
|
// Create context for the Supabase client
|
|
7
8
|
const SupabaseContext = createContext(null);
|
|
8
9
|
const ConnectionStatusContext = createContext(null);
|
|
@@ -24,8 +25,9 @@ export function useConnectionStatus() {
|
|
|
24
25
|
}
|
|
25
26
|
export function ScoutRefreshProvider({ children }) {
|
|
26
27
|
// Use refs to store the URL and key to prevent unnecessary recreations
|
|
27
|
-
|
|
28
|
-
const
|
|
28
|
+
// Uses flexible environment variable helper for better PWA compatibility
|
|
29
|
+
const urlRef = useRef(getSupabaseUrl());
|
|
30
|
+
const anonKeyRef = useRef(getSupabaseAnonKey());
|
|
29
31
|
// Create a single Supabase client instance that only runs once
|
|
30
32
|
const supabaseClient = useMemo(() => {
|
|
31
33
|
console.log("[ScoutRefreshProvider] Creating Supabase client");
|
package/dist/types/db.d.ts
CHANGED
|
@@ -54,6 +54,12 @@ export type PartInsert = Database["public"]["Tables"]["parts"]["Insert"];
|
|
|
54
54
|
export type VersionsSoftwareInsert = Database["public"]["Tables"]["versions_software"]["Insert"];
|
|
55
55
|
export type ArtifactInsert = Database["public"]["Tables"]["artifacts"]["Insert"];
|
|
56
56
|
export type PinInsert = Database["public"]["Tables"]["pins"]["Insert"];
|
|
57
|
+
export type SessionInsert = Database["public"]["Tables"]["sessions"]["Insert"];
|
|
58
|
+
export type SessionUpdate = Database["public"]["Tables"]["sessions"]["Update"];
|
|
59
|
+
export type ConnectivityInsert = Database["public"]["Tables"]["connectivity"]["Insert"];
|
|
60
|
+
export type ConnectivityUpdate = Database["public"]["Tables"]["connectivity"]["Update"];
|
|
61
|
+
export type EventInsert = Database["public"]["Tables"]["events"]["Insert"];
|
|
62
|
+
export type EventUpdate = Database["public"]["Tables"]["events"]["Update"];
|
|
57
63
|
export type IEventWithTags = Database["public"]["CompositeTypes"]["event_with_tags"] & {
|
|
58
64
|
earthranger_url: string | null;
|
|
59
65
|
file_path: string | null;
|