glin-profanity
Version:
Glin-Profanity is a lightweight and efficient npm package designed to detect and filter profane language in text inputs across multiple languages. Whether you’re building a chat application, a comment section, or any platform where user-generated content
174 lines (170 loc) • 5.29 kB
text/typescript
import { F as FilterConfig, C as CheckProfanityResult } from '../types-B9c_ik4k.cjs';
export { L as Language } from '../types-B9c_ik4k.cjs';
/**
* Next.js Middleware and Utilities for glin-profanity
*
* Provides middleware, API route helpers, and React components
* for integrating profanity detection in Next.js applications.
*
* @example
* ```typescript
* // middleware.ts
* import { createProfanityMiddleware } from 'glin-profanity/nextjs';
*
* export const middleware = createProfanityMiddleware({
* paths: ['/api/chat', '/api/comments'],
* blockProfanity: true,
* });
*
* // API Route
* import { withProfanityCheck } from 'glin-profanity/nextjs';
*
* export const POST = withProfanityCheck(async (req, profanityResult) => {
* if (profanityResult.blocked) {
* return Response.json({ error: profanityResult.reason }, { status: 400 });
* }
* // Continue processing...
* });
* ```
*
* @packageDocumentation
* @module glin-profanity/nextjs
*/
/**
* Middleware configuration
*/
interface ProfanityMiddlewareConfig {
/** Paths to check (glob patterns supported) */
paths?: string[];
/** Paths to exclude */
excludePaths?: string[];
/** Block requests with profanity (return 400) */
blockProfanity?: boolean;
/** Add profanity result to request headers */
addHeaders?: boolean;
/** Custom response for blocked requests */
blockedResponse?: {
status?: number;
message?: string;
};
/** Fields in request body to check */
checkFields?: string[];
/** Filter configuration */
filterConfig?: Partial<FilterConfig>;
}
/**
* Profanity check result for middleware
*/
interface MiddlewareProfanityResult {
/** Whether the request was blocked */
blocked: boolean;
/** Reason for blocking (if blocked) */
reason?: string;
/** Profane words found */
profaneWords: string[];
/** Fields that contained profanity */
flaggedFields: string[];
/** Full check result */
checkResult?: CheckProfanityResult;
}
/**
* Creates Next.js middleware for profanity detection
*
* @example
* ```typescript
* // middleware.ts
* import { createProfanityMiddleware } from 'glin-profanity/nextjs';
*
* export const middleware = createProfanityMiddleware({
* paths: ['/api/chat', '/api/comments/*'],
* blockProfanity: true,
* checkFields: ['message', 'content', 'text'],
* });
*
* export const config = {
* matcher: ['/api/:path*'],
* };
* ```
*/
declare function createProfanityMiddleware(config?: ProfanityMiddlewareConfig): (request: Request) => Promise<Response>;
/**
* Higher-order function to wrap API route handlers with profanity checking
*
* @example
* ```typescript
* // app/api/chat/route.ts
* import { withProfanityCheck } from 'glin-profanity/nextjs';
*
* export const POST = withProfanityCheck(
* async (req, profanityResult) => {
* if (profanityResult.blocked) {
* return Response.json({ error: profanityResult.reason }, { status: 400 });
* }
*
* const { message } = await req.json();
* // Process message...
* return Response.json({ success: true });
* },
* {
* checkFields: ['message'],
* blockProfanity: false, // Handle manually
* }
* );
* ```
*/
declare function withProfanityCheck<T>(handler: (request: Request, profanityResult: MiddlewareProfanityResult) => Promise<T>, config?: {
checkFields?: string[];
blockProfanity?: boolean;
filterConfig?: Partial<FilterConfig>;
}): (request: Request) => Promise<T | Response>;
/**
* Server action wrapper for profanity checking
*
* @example
* ```typescript
* // actions.ts
* 'use server';
* import { createServerAction } from 'glin-profanity/nextjs';
*
* export const submitComment = createServerAction(
* async (data: { comment: string }) => {
* // Save to database
* await db.comments.create({ data });
* return { success: true };
* },
* {
* checkFields: ['comment'],
* onProfanity: (result) => {
* return { error: 'Please remove inappropriate language' };
* },
* }
* );
* ```
*/
declare function createServerAction<TInput extends Record<string, unknown>, TOutput>(action: (data: TInput) => Promise<TOutput>, config?: {
checkFields?: string[];
filterConfig?: Partial<FilterConfig>;
onProfanity?: (result: MiddlewareProfanityResult) => TOutput;
}): (data: TInput) => Promise<TOutput>;
/**
* Utility functions for use in API routes
*/
declare const profanityUtils: {
/**
* Quick check for profanity in a single string
*/
check(text: string, config?: Partial<FilterConfig>): CheckProfanityResult;
/**
* Check and optionally censor text
*/
censor(text: string, replacement?: string, config?: Partial<FilterConfig>): string;
/**
* Check multiple fields in an object
*/
checkObject(obj: Record<string, unknown>, fields: string[], config?: Partial<FilterConfig>): {
containsProfanity: boolean;
flaggedFields: string[];
profaneWords: string[];
};
};
export { CheckProfanityResult, FilterConfig, type MiddlewareProfanityResult, type ProfanityMiddlewareConfig, createProfanityMiddleware, createServerAction, profanityUtils, withProfanityCheck };