UNPKG

@alessiofrittoli/next-api

Version:
74 lines (70 loc) 3.64 kB
import { NextRequest } from 'next/server'; import * as _alessiofrittoli_web_utils from '@alessiofrittoli/web-utils'; import { A as Api } from '../types-BCw6mdsz.mjs'; import '../page-DAsxsEeP.mjs'; import 'next'; import 'fs'; import 'node:stream/web'; import '@alessiofrittoli/stream-reader/types'; /** * Reads and parses the JSON body from a given resource. * * @template T The expected type of the parsed JSON body. * * @param body The request or response object containing the JSON body. * @param clone If `false`, reads the Body without cloning. Default: `false`. * @returns A Promise that resolves to the parsed JSON body of type `T`, or `null` if an error occurs. */ declare const readJsonBody: <T extends Api.Route.RequestBody = Api.Route.RequestBody>(body: Request | Api.Route.Request<T> | Response, clone?: boolean) => Promise<T | null>; /** * Interface representing a map of form data values where the key is a string * and the value can be a single form entry value or an array of form entry values. */ interface DefaultFormDataReadMap { [x: string]: FormDataEntryValue | FormDataEntryValue[]; } /** * A callback type for processing form data entries when read. * This function is called for each key-value pair in the form data. * * @template I The type of the form data map. * @template OK The key of the form data entry. * * @param key The key of the form data entry. * @param value The value associated with the key. * @returns The processed value for the form data entry. */ type OnRead<I extends Record<string, unknown | unknown[]> = DefaultFormDataReadMap, O extends Record<string, unknown | unknown[]> = I, IK extends keyof I = keyof I, OK extends keyof O = keyof O> = (key: OK, value: I[IK]) => O[OK]; /** * Reads and processes form data from a request or response body. * It extracts the form data and optionally processes each entry using a provided `onRead` callback. * * @template T The type of the form data map (defaults to `DefaultFormDataReadMap`). * * @param body The body of the request or response, from which the form data is to be read. * @param clone (Optional) Whether to clone the body before reading the form data. Default: `false`. * @param onRead (Optional) An optional callback function to process the form data entry. * * @returns A map of form data entries, where the keys are the field names and the values are the field values. */ declare const readFormDataBody: <I extends Record<string, unknown | unknown[]> = DefaultFormDataReadMap, O extends Record<string, unknown | unknown[]> = I>(body: Request | Api.Route.Request | Response, clone?: boolean, onRead?: OnRead<I, O>) => Promise<_alessiofrittoli_web_utils.TypedMap<O, true, keyof O>>; /** * Extracts and returns all the files from the form data in a request or response body. * * This function reads the form data, filters out entries that are instances of `File`, * and returns only the file entries. * * @param body The body of the request or response from which the files should be extracted. * @param clone (Optional) Whether to clone the body before reading the form data. Default: `false`. * * @returns A promise that resolves to an array of `File` objects extracted from the form data. */ declare const getRequestFiles: (body: Request | Api.Route.Request | Response, clone?: boolean) => Promise<File[]>; /** * Get request IP address. * * @param request The NextRequest Instance. * @returns The Request IP address. */ declare const getRequestIp: (request?: NextRequest) => Promise<string | null>; export { type OnRead, getRequestFiles, getRequestIp, readFormDataBody, readJsonBody };