@datadome/module-nextjs
Version:
DataDome module for Next.js applications
150 lines • 5.3 kB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
/**
* Request properties to be sent to the Protection API for validation
*/
export interface RequestData {
Accept?: string | null;
AcceptCharset?: string | null;
AcceptEncoding?: string | null;
AcceptLanguage?: string | null;
APIConnectionState?: string | null;
AuthorizationLen?: number | null;
CacheControl?: string | null;
ClientID?: string | null;
Connection?: string | null;
ContentType?: string | null;
CookiesLen?: number | null;
From?: string | null;
GraphQLOperationType?: GraphQLType | null;
GraphQLOperationName?: string | null;
GraphQLOperationCount?: number | null;
HeadersList?: string | null;
Host?: string | null;
IP: string;
JA4?: string | null;
Key: string;
Method?: string | null;
ModuleVersion?: string | null;
Origin?: string | null;
Port?: number | null;
PostParamLen?: string | null;
Pragma?: string | null;
Protocol?: string | null;
Referer?: string | null;
Request?: string | null;
RequestModuleName?: string | null;
SecCHDeviceMemory?: string | null;
SecCHUA?: string | null;
SecCHUAArch?: string | null;
SecCHUAFullVersionList?: string | null;
SecCHUAMobile?: string | null;
SecCHUAModel?: string | null;
SecCHUAPlatform?: string | null;
SecFetchDest?: string | null;
SecFetchMode?: string | null;
SecFetchSite?: string | null;
SecFetchUser?: string | null;
ServerHostname?: string | null;
ServerName?: string | null;
ServerRegion?: string | null;
TimeRequest?: number | null;
TrueClientIP?: string | null;
UserAgent?: string | null;
Via?: string | null;
'X-Real-IP'?: string | null;
'X-Requested-With'?: string | null;
XForwardedForIP?: string | null;
}
export interface Logger {
debug: (message: string) => void;
info: (message: string) => void;
warn: (message: string) => void;
error: (message: string) => void;
}
/**
* The options to customize the {@link DataDomeMiddleware} behavior
*/
export interface DataDomeMiddlewareOptions {
enableGraphQLSupport?: boolean;
endpointHost?: string;
urlPatternExclusion?: string;
logger?: Logger;
timeout?: number;
}
export type GraphQLType = 'query' | 'mutation' | 'subscription';
export interface GraphQLData {
name: string;
type: GraphQLType;
count: number;
}
/**
* The DataDome's middleware that will be used to protect the traffic.
*/
export declare class DataDomeMiddleware {
serverSideKey: string;
moduleName: string;
moduleVersion: string;
logger: Logger;
maximumBodySize: number;
endpointHost: string;
enableGraphQLSupport: boolean;
urlPatternExclusion: RegExp;
timeout: number;
constructor(serverSideKey: string, parameters?: DataDomeMiddlewareOptions);
/**
* Extract the fingerprint from the request, build a request to the Protection API, and returns the response
* @memberof DataDomeMiddleware
* @param req - The incoming request.
* @returns The response of the Protection API.
*/
handleRequest(req: NextRequest, res?: NextResponse): Promise<NextResponse>;
/**
* @private
* @param req - The incoming request.
* @returns Returns true if the request must be protected. It returns false otherwise.
*/
private isRequestProtected;
/**
* This function extracts the information from the incoming request and returns the body payload for the Protection API.
* @private
* @param key - The server-side key of the user.
* @param req - The incoming request.
* @returns The {@link RequestData} payload for the Protection API.
*/
private buildRequestPayload;
/**
* This function performs the request to the Protection API and returns its result.
* @private
* @param req - The incoming request.
* @param requestData - The truncated body payload for the Protection API.
* @returns It returns the response of the Protection API, or `null` if an error occured.
*/
private sendRequest;
/**
* This functions interprets the response of the Protection API and returns a NextResponse.
* @param req - The incoming request.
* @param res - The outcoming next response.
* @param datadomeRes - The response from the Protection API.
* @returns It returns the {@link NextResponse} for the middleware.
*/
private handleResponse;
/**
* Parse and set DataDome cookies with all their attributes.
*
* This method extracts cookies from DataDome's response and sets them using Next.js
* cookies API, which automatically generates the x-middleware-set-cookie header
* needed for the middleware chain and browser.
*
* @private
* @param res - The NextResponse object
* @param cookieHeader - The set-cookie header value from DataDome
*/
private setDataDomeCookies;
/**
* This function is used to truncate the fields of the payload of the Protection API.
* @param requestData - The payload for the Protection API.
* @returns The truncated payload for the Protection API.
*/
private truncateRequestData;
}
//# sourceMappingURL=middleware.d.ts.map