import { NextFunction, ParseDataConfig, Request, Response, VkrunParseData } from "../types"; export declare class ParseDataSetup implements VkrunParseData { private readonly urlencoded; private readonly params; private readonly query; private readonly json; private readonly formData; private readonly escapeSQL; private readonly rawBody; private readonly maxBytes; private readonly type?; private readonly verify?; private readonly inflate; constructor(config?: ParseDataConfig); handle(request: Request, response: Response, next: NextFunction): Promise; } /** * @function parseData * * ### 🧩 Overview * The `parseData` middleware is responsible for **interpreting incoming HTTP request data** — * including the body, query string, route parameters, and optional security filters. * * It automatically detects the `Content-Type` header and transforms the raw incoming data into a * structured JavaScript object (`req.body`, `req.query`, `req.params`), similar to Express' body parsers, * but with **more flexibility**, **no dependencies**, and **built-in security**. * * --- * * ### ⚙️ Supported Parsing Types * * | Source | Description | Default | * |---------|--------------|----------| * | **JSON** | Parses `application/json` requests. | ✅ | * | **URL-encoded** | Parses `application/x-www-form-urlencoded` requests. | ✅ | * | **Multipart form data** | Parses `multipart/form-data` (supports file uploads). | ✅ | * | **Query parameters** | Parses query strings from the URL (`?key=value`). | ✅ | * | **Route parameters** | Parses parameters from the route (`/users/:id`). | ✅ | * * --- * * ### 🛡️ Security Features * * - **SQL Escaping**: Prevents injection attempts by sanitizing special characters in request data. * - **Verification Callback**: Runs immediately after reading the raw body, allowing you to * verify HMAC signatures or webhook authenticity before parsing. * - **Body Size Limit**: Rejects requests that exceed a configurable maximum payload size. * - **Automatic Decompression**: Supports gzip, deflate, and Brotli compressed bodies. * * --- * * ### 🔧 Configuration Groups * * `parseData` accepts an optional `ParseDataConfig` object divided into three semantic sections: * * ```ts * { * parse?: { json?: boolean, urlencoded?: boolean, formData?: boolean, query?: boolean, params?: boolean }, * security?: { escapeSQL?: boolean, verify?: (req, rawBody, encoding?) => void }, * body?: { raw?: boolean, limit?: number, type?: string | RegExp, inflate?: boolean } * } * ``` * * #### ⚙️ Example * ```ts * import { App, parseData } from "vkrun"; * * const app = App(); * * app.use(parseData({ * parse: { json: true, urlencoded: false }, // Disable URL-encoded parsing * security: { escapeSQL: true }, // Enable SQL injection protection * body: { limit: 5 * 1024 * 1024 }, // Limit body to 5MB * })); * * app.post("/user", (req, res) => { * console.log(req.body); // Safely parsed data * res.status(200).json({ received: true }); * }); * ``` * * --- * * ### 🧠 Advanced Options * * #### `body.raw` * Stores the unparsed raw body (`Buffer`) in `req.rawBody`. * Useful for validating webhooks or digital signatures. * * ```ts * app.use(parseData({ body: { raw: true } })); * app.post("/webhook", (req, res) => { * console.log(req.rawBody.toString()); // raw payload * }); * ``` * * #### `body.limit` * Defines the maximum allowed request size (in bytes). * Exceeding this limit triggers a `413 Payload Too Large` response. * * ```ts * app.use(parseData({ body: { limit: 1024 * 1024 } })); // 1 MB * ``` * * #### `body.type` * Restricts body parsing to specific MIME types. * If the request’s `Content-Type` doesn’t match, only `query` and `params` will be parsed. * * ```ts * // Only process XML webhooks * app.use(parseData({ body: { type: "application/xml" } })); * ``` * * **Supports:** * - `string`: `"application/json"` — direct match * - `RegExp`: `/^text\//` — pattern match * * #### `body.inflate` * Enables automatic decompression for requests with: * - `Content-Encoding: gzip` * - `Content-Encoding: deflate` * - `Content-Encoding: br` * * ```ts * // Disable inflate if you expect compressed payloads handled externally * app.use(parseData({ body: { inflate: false } })); * ``` * * --- * * ### 🚨 Error Handling * * The middleware returns standardized HTTP responses for common parsing errors: * * | Error | Status | Message | * |--------|---------|----------| * | Body exceeds limit | **413** | `Payload Too Large` | * | Unsupported encoding | **415** | `Unsupported Content-Encoding` | * | Invalid gzip/deflate data | **400** | `Invalid Compressed Data` | * | Invalid verification | **400** | `Invalid Request Verification` | * | Generic parse error | **400** | `Invalid Request Data` | * * --- * * ### 🧪 Examples * * #### Parse All Data * ```ts * app.use(parseData()); * app.post("/test/:id", (req, res) => { * res.json({ * params: req.params, * query: req.query, * body: req.body, * }); * }); * ``` * * #### Use Route-Level Parsing * ```ts * router.post("/upload", parseData({ body: { type: "multipart/form-data" } }), (req, res) => { * res.status(200).send(`Received ${req.files?.length} file(s)`); * }); * ``` * * --- * * @function parseData * @param {ParseDataConfig} [config] - Optional configuration to control parsing behavior. * @returns {VkrunParseData} Middleware instance implementing `handle(req, res, next)`. * * @see ParseDataConfig for detailed option breakdown. * @example * // Basic usage * const app = App(); * app.use(parseData()); * * // Advanced usage * app.use(parseData({ * body: { type: /^text\//, limit: 2 * 1024 * 1024 }, * security: { escapeSQL: true } * })); */ export declare const parseData: (config?: ParseDataConfig) => VkrunParseData;