@softlock/sdk
Version:
Official Softlock SDK for access key validation and management
171 lines (167 loc) • 5.24 kB
TypeScript
/**
* 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 SoftlockMiddlewareOptions {
/** Custom error handler */
onError?: (error: string, req: any, res: any) => void;
/** Custom unauthorized handler */
onUnauthorized?: (req: any, res: any) => void;
/** Extract key from custom location */
extractKey?: (req: any) => string | null;
}
interface CacheEntry {
result: ValidationResult;
timestamp: number;
ttl: number;
}
interface ApiResponse<T = any> {
success: boolean;
data?: T;
error?: string;
}
interface ValidationApiResponse extends ApiResponse {
data?: {
valid: boolean;
key?: AccessKey;
user?: User;
};
}
declare class SoftlockError extends Error {
code?: string | undefined;
statusCode?: number | undefined;
constructor(message: string, code?: string | undefined, statusCode?: number | undefined);
}
declare class ValidationError extends SoftlockError {
constructor(message: string, code?: string);
}
declare class NetworkError extends SoftlockError {
constructor(message: string, statusCode?: number);
}
declare class ConfigurationError extends SoftlockError {
constructor(message: string);
}
/**
* Express middleware for protecting routes with Softlock access keys
*/
declare function createExpressMiddleware(options?: SoftlockMiddlewareOptions): (req: any, res: any, next: any) => Promise<any>;
/**
* Next.js middleware for protecting routes with Softlock access keys
*/
declare function createNextMiddleware(options?: SoftlockMiddlewareOptions): (request: any) => Promise<Response | undefined>;
/**
* Utility function to extract Softlock data from Next.js request headers
*/
declare function getSoftlockDataFromHeaders(headers: Headers): {
valid: boolean;
userId: string | undefined;
userTag: string | undefined;
keyId: string | undefined;
};
/**
* Higher-order function for protecting API routes
*/
declare function withSoftlockAuth(handler: (req: any, res: any) => Promise<any>, options?: SoftlockMiddlewareOptions): (req: any, res: any) => Promise<unknown>;
declare global {
namespace Express {
interface Request {
softlock?: {
valid: boolean;
key?: {
id: string;
status: string;
discord_user_id?: string;
discord_tag?: string;
created_at: string;
expires_at?: string;
};
user?: {
discordId?: string;
discordTag?: string;
};
};
}
}
}
export { ConfigurationError, NetworkError, SoftlockClient, SoftlockError, ValidationError, createExpressMiddleware, createNextMiddleware, getSoftlockClient, getSoftlockDataFromHeaders, initSoftlock, validateKey, withSoftlockAuth };
export type { AccessKey, ApiResponse, CacheEntry, SoftlockConfig, SoftlockMiddlewareOptions, User, ValidationApiResponse, ValidationResult };