UNPKG

@softlock/sdk

Version:

Official Softlock SDK for access key validation and management

248 lines (242 loc) 7.72 kB
import * as react_jsx_runtime from 'react/jsx-runtime'; import React$1 from 'react'; /** * Configuration for the Softlock client */ interface SoftlockConfig$1 { /** Your Softlock project/tenant ID */ tenantId: string; /** Base URL for your Softlock instance (defaults to official hosted service) */ baseUrl?: string; /** API key for server-side validation (optional for client-side) */ apiKey?: string; /** Cache TTL in milliseconds (default: 5 minutes) */ cacheTtl?: number; /** Enable debug logging */ debug?: boolean; } /** * Core Softlock client for access key validation */ declare class SoftlockClient { private config; private cache; constructor(config: SoftlockConfig$1); /** * Validate an access key */ validateKey(keyValue: string): Promise<any>; /** * Clear validation cache */ clearCache(): void; /** * Update configuration */ updateConfig(newConfig: Partial<SoftlockConfig$1>): void; /** * Get current configuration (without sensitive data) */ getConfig(): Omit<SoftlockConfig$1, 'apiKey'>; } /** * Initialize the default Softlock client */ declare function initSoftlock(config: SoftlockConfig$1): SoftlockClient; /** * Get the default client instance */ declare function getSoftlockClient(): SoftlockClient; /** * Validate a key using the default client */ declare function validateKey(keyValue: string): Promise<any>; interface SoftlockConfig { /** Your Softlock project/tenant ID */ tenantId: string; /** Base URL for your Softlock instance (defaults to official hosted service) */ baseUrl?: string; /** API key for server-side validation (optional for client-side) */ apiKey?: string; /** Cache TTL in milliseconds (default: 5 minutes) */ cacheTtl?: number; /** Enable debug logging */ debug?: boolean; } interface AccessKey { id: string; key_value: string; status: 'active' | 'revoked' | 'expired'; discord_user_id?: string; discord_tag?: string; created_at: string; expires_at?: string; used_at?: string; } interface ValidationResult { valid: boolean; key?: AccessKey; error?: string; cached?: boolean; } interface User { discordId?: string; discordTag?: string; userId?: string; } interface UseAccessKeyOptions { /** Auto-validate on mount */ autoValidate?: boolean; /** Refetch interval in milliseconds */ refetchInterval?: number; /** Disable caching for this hook */ disableCache?: boolean; } interface UseAccessKeyResult { /** Validation result */ result: ValidationResult | null; /** Loading state */ loading: boolean; /** Error state */ error: string | null; /** Manually validate a key */ validate: (key: string) => Promise<ValidationResult>; /** Clear current validation */ clear: () => void; /** Retry last validation */ retry: () => void; } interface UseUserAccessOptions { /** Auto-refresh access data */ autoRefresh?: boolean; /** Refresh interval in milliseconds */ refreshInterval?: number; } interface UseUserAccessResult { /** User data */ user: User | null; /** Loading state */ loading: boolean; /** Error state */ error: string | null; /** Manually refresh access */ refreshAccess: () => Promise<void>; } interface UseAccessGuardOptions { /** Fallback component for invalid access */ fallback?: React.ReactNode; /** Auto-validate on mount */ autoValidate?: boolean; } interface UseAccessGuardResult { /** Whether access is granted */ hasAccess: boolean; /** Loading state */ loading: boolean; /** Error state */ error: string | null; /** Validation result */ result: ValidationResult | null; } /** * React hook for access key validation */ declare function useAccessKey(initialKey?: string, options?: UseAccessKeyOptions): UseAccessKeyResult; /** * Hook for managing user access state */ declare function useUserAccess(userId?: string): { hasAccess: boolean | null; loading: boolean; error: string | null; checkAccess: (key: string) => Promise<ValidationResult>; revokeAccess: () => void; userKey: string | undefined; validationResult: ValidationResult | null; }; /** * Hook for protecting components based on access key validation */ declare function useAccessGuard(keyValue?: string, options?: { redirectUrl?: string; onUnauthorized?: () => void; fallback?: React.ComponentType; }): { isAuthorized: boolean | null; loading: boolean; error: string | null; checkKey: (key: string) => Promise<ValidationResult>; validationResult: ValidationResult | null; }; /** * Props for AccessKeyValidator component */ interface AccessKeyValidatorProps { /** Initial key value */ initialKey?: string; /** Placeholder text for input */ placeholder?: string; /** Custom validation button text */ validateButtonText?: string; /** Custom clear button text */ clearButtonText?: string; /** Show clear button */ showClearButton?: boolean; /** Auto-validate on input change */ autoValidate?: boolean; /** Debounce delay for auto-validation (ms) */ debounceDelay?: number; /** Custom CSS classes */ className?: string; /** Called when validation completes */ onValidation?: (result: ValidationResult) => void; /** Called when key changes */ onKeyChange?: (key: string) => void; /** Disable the input */ disabled?: boolean; } /** * Component for validating access keys with built-in UI */ declare function AccessKeyValidator({ initialKey, placeholder, validateButtonText, clearButtonText, showClearButton, autoValidate, debounceDelay, className, onValidation, onKeyChange, disabled, }: AccessKeyValidatorProps): react_jsx_runtime.JSX.Element; /** * Props for AccessGuard component */ interface AccessGuardProps { /** Access key to validate */ keyValue?: string; /** Children to render when access is granted */ children: React$1.ReactNode; /** Component to render when access is denied */ fallback?: React$1.ReactNode; /** Component to render while loading */ loadingComponent?: React$1.ReactNode; /** URL to redirect to when access is denied */ redirectUrl?: string; /** Called when access is denied */ onUnauthorized?: () => void; /** Custom CSS classes */ className?: string; } /** * Component that conditionally renders children based on access key validation */ declare function AccessGuard({ keyValue, children, fallback, loadingComponent, redirectUrl, onUnauthorized, className, }: AccessGuardProps): react_jsx_runtime.JSX.Element; /** * Props for AccessStatus component */ interface AccessStatusProps { /** Access key to check */ keyValue?: string; /** Show detailed key information */ showDetails?: boolean; /** Auto-refresh interval (ms) */ refreshInterval?: number; /** Custom CSS classes */ className?: string; } /** * Component that displays the current access status */ declare function AccessStatus({ keyValue, showDetails, refreshInterval, className, }: AccessStatusProps): react_jsx_runtime.JSX.Element; export { AccessGuard, AccessKeyValidator, AccessStatus, SoftlockClient, getSoftlockClient, initSoftlock, useAccessGuard, useAccessKey, useUserAccess, validateKey }; export type { AccessGuardProps, AccessKey, AccessKeyValidatorProps, AccessStatusProps, SoftlockConfig, UseAccessGuardOptions, UseAccessGuardResult, UseAccessKeyOptions, UseAccessKeyResult, UseUserAccessOptions, UseUserAccessResult, User, ValidationResult };