react-query-external-sync
Version:
A tool for syncing React Query state to an external Dev Tools
112 lines (111 loc) • 5.21 kB
TypeScript
import { QueryClient } from "@tanstack/react-query";
import { type AsyncStorageStatic } from "./hooks/useDynamicAsyncStorageQueries";
import { MmkvStorage } from "./hooks/useDynamicMmkvQueries";
import { type StorageInterface } from "./utils/storageHandlers";
import { PlatformOS } from "./platformUtils";
/**
* SecureStore static interface (from expo-secure-store)
*/
interface SecureStoreStatic {
getItemAsync: (key: string) => Promise<string | null>;
setItemAsync?: (key: string, value: string) => Promise<void>;
deleteItemAsync?: (key: string) => Promise<void>;
}
interface useSyncQueriesExternalProps {
queryClient: QueryClient;
deviceName: string;
/**
* A unique identifier for this device that persists across app restarts.
* This is crucial for proper device tracking, especially if you have multiple devices of the same type.
* If you only have one iOS and one Android device, you can use 'ios' and 'android'.
* For multiple devices of the same type, ensure this ID is unique and persistent.
*/
deviceId: string;
extraDeviceInfo?: Record<string, string>;
/**
* Additional environment variables to include beyond the automatically collected EXPO_PUBLIC_ variables.
* The hook automatically collects all EXPO_PUBLIC_ prefixed environment variables.
* Use this parameter to add any additional env vars you want to send to the dashboard.
*/
envVariables?: Record<string, string>;
socketURL: string;
platform: PlatformOS;
/**
* Enable/disable logging for debugging purposes
* @default false
*/
enableLogs?: boolean;
/**
* Whether the app is running on a physical device or an emulator/simulator
* This can affect how the socket URL is constructed, especially on Android
* @default false
*/
isDevice?: boolean;
/**
* Storage instances for different storage types
* When provided, these will automatically enable both external sync AND storage monitoring
*
* - mmkvStorage: MMKV storage instance (enables ['#storage', 'mmkv', 'key'] queries + monitoring)
* - asyncStorage: AsyncStorage instance (enables ['#storage', 'async', 'key'] queries + monitoring)
* - secureStorage: SecureStore instance (enables ['#storage', 'secure', 'key'] queries + monitoring)
* - secureStorageKeys: Array of SecureStore keys to monitor (required when using secureStorage)
* - secureStoragePollInterval: Polling interval for SecureStore monitoring (default: 1000ms)
*/
storage?: StorageInterface;
mmkvStorage?: StorageInterface | MmkvStorage;
asyncStorage?: StorageInterface | AsyncStorageStatic;
secureStorage?: StorageInterface | SecureStoreStatic;
secureStorageKeys?: string[];
secureStoragePollInterval?: number;
}
/**
* Hook used by mobile devices to sync query state with the external dashboard
*
* Handles:
* - Connection to the socket server
* - Responding to dashboard requests
* - Processing query actions from the dashboard
* - Sending query state updates to the dashboard
* - Automatically collecting all EXPO_PUBLIC_ environment variables
* - Merging additional user-provided environment variables
* - Supporting multiple storage types (MMKV, AsyncStorage, SecureStore)
* - Integrated storage monitoring (automatically monitors storage when instances are provided)
*
* @example
* // Basic usage with MMKV only (legacy)
* useSyncQueriesExternal({
* queryClient,
* socketURL: 'http://localhost:42831',
* deviceName: 'iOS Simulator',
* platform: 'ios',
* deviceId: 'ios-sim-1',
* storage: mmkvStorage, // Your MMKV instance
* });
*
* @example
* // Advanced usage with MMKV, AsyncStorage, and SecureStore
* // This automatically enables both external sync AND storage monitoring
* import AsyncStorage from '@react-native-async-storage/async-storage';
* import * as SecureStore from 'expo-secure-store';
* import { storage as mmkvStorage } from '~/lib/storage/mmkv';
*
* useSyncQueriesExternal({
* queryClient,
* socketURL: 'http://localhost:42831',
* deviceName: 'iOS Simulator',
* platform: 'ios',
* deviceId: 'ios-sim-1',
* mmkvStorage: mmkvStorage, // Enables ['#storage', 'mmkv', 'key'] queries + MMKV monitoring
* asyncStorage: AsyncStorage, // Enables ['#storage', 'async', 'key'] queries + AsyncStorage monitoring
* secureStorage: SecureStore, // Enables ['#storage', 'secure', 'key'] queries + SecureStore monitoring
* secureStorageKeys: ['sessionToken', 'auth.session', 'auth.email'], // Required for SecureStore monitoring
* enableLogs: true,
* });
*/
export declare function useSyncQueriesExternal({ queryClient, deviceName, socketURL, extraDeviceInfo, envVariables, platform, deviceId, enableLogs, isDevice, storage, mmkvStorage, asyncStorage, secureStorage, secureStorageKeys, secureStoragePollInterval, }: useSyncQueriesExternalProps): {
connect: () => void;
disconnect: () => void;
isConnected: boolean;
socket: import("socket.io-client").Socket<import("@socket.io/component-emitter").DefaultEventsMap, import("@socket.io/component-emitter").DefaultEventsMap>;
};
export {};