UNPKG

@limitly/limitly-nextjs

Version:

Official Next.js SDK for Limitly - API Key management, plans, users and request validation optimized for server-side

81 lines 2.78 kB
import { HttpClient } from './client'; import { ApiKeysModule } from './modules/api-keys'; import { PlansModule } from './modules/plans'; import { UsersModule } from './modules/users'; import { ValidationModule } from './modules/validation'; import { LimitlyConfig, NextJsConfig } from './types'; /** * Main Limitly SDK client for Next.js * Optimized for server-side rendering and Next.js applications * * @example * ```typescript * import { Limitly } from '@limitly/limitly-nextjs'; * * const limitly = new Limitly({ * apiKey: 'your_limitly_api_key' * }); * * // Validate a request in a Next.js API route * export async function GET(request: Request) { * const apiKey = request.headers.get('authorization')?.replace('Bearer ', ''); * * if (!apiKey) { * return Response.json({ error: 'API Key required' }, { status: 401 }); * } * * const result = await limitly.validation.validate( * apiKey, * '/api/users', * 'GET' * ); * * if (!result.success) { * return Response.json({ error: 'Rate limit exceeded' }, { status: 429 }); * } * * return Response.json({ message: 'Request allowed' }); * } * ``` */ export declare class Limitly { readonly apiKeys: ApiKeysModule; readonly plans: PlansModule; readonly users: UsersModule; readonly validation: ValidationModule; private client; constructor(config: LimitlyConfig | NextJsConfig); /** * Gets the internal HTTP client * Useful for debugging and testing */ getClient(): HttpClient; /** * Creates a middleware function for Next.js API routes * @param options - Middleware configuration options * @returns Middleware function */ createMiddleware(options?: { apiKeyHeader?: string; onRateLimitExceeded?: (req: any, res: any) => void; onValidationError?: (req: any, res: any, error: any) => void; }): (req: any, res: any, next?: () => void) => Promise<any>; /** * Creates a Next.js API route handler with built-in rate limiting * @param handler - The API route handler function * @param options - Rate limiting options * @returns Wrapped API route handler */ withRateLimit(handler: (request: Request, ...args: any[]) => Promise<Response>, options?: { apiKeyHeader?: string; onRateLimitExceeded?: (request: Request) => Response; }): (request: Request, ...args: any[]) => Promise<Response>; } export * from './types'; export { HttpClient } from './client'; export { ApiKeysModule } from './modules/api-keys'; export { PlansModule } from './modules/plans'; export { UsersModule } from './modules/users'; export { ValidationModule } from './modules/validation'; export default Limitly; //# sourceMappingURL=index.d.ts.map