index.d.ts (2286B)
1 import { Readable } from 'stream'; 2 3 declare namespace getRawBody { 4 export type Encoding = string | true; 5 6 export interface Options { 7 /** 8 * The expected length of the stream. 9 */ 10 length?: number | string | null; 11 /** 12 * The byte limit of the body. This is the number of bytes or any string 13 * format supported by `bytes`, for example `1000`, `'500kb'` or `'3mb'`. 14 */ 15 limit?: number | string | null; 16 /** 17 * The encoding to use to decode the body into a string. By default, a 18 * `Buffer` instance will be returned when no encoding is specified. Most 19 * likely, you want `utf-8`, so setting encoding to `true` will decode as 20 * `utf-8`. You can use any type of encoding supported by `iconv-lite`. 21 */ 22 encoding?: Encoding | null; 23 } 24 25 export interface RawBodyError extends Error { 26 /** 27 * The limit in bytes. 28 */ 29 limit?: number; 30 /** 31 * The expected length of the stream. 32 */ 33 length?: number; 34 expected?: number; 35 /** 36 * The received bytes. 37 */ 38 received?: number; 39 /** 40 * The encoding. 41 */ 42 encoding?: string; 43 /** 44 * The corresponding status code for the error. 45 */ 46 status: number; 47 statusCode: number; 48 /** 49 * The error type. 50 */ 51 type: string; 52 } 53 } 54 55 /** 56 * Gets the entire buffer of a stream either as a `Buffer` or a string. 57 * Validates the stream's length against an expected length and maximum 58 * limit. Ideal for parsing request bodies. 59 */ 60 declare function getRawBody( 61 stream: Readable, 62 callback: (err: getRawBody.RawBodyError, body: Buffer) => void 63 ): void; 64 65 declare function getRawBody( 66 stream: Readable, 67 options: (getRawBody.Options & { encoding: getRawBody.Encoding }) | getRawBody.Encoding, 68 callback: (err: getRawBody.RawBodyError, body: string) => void 69 ): void; 70 71 declare function getRawBody( 72 stream: Readable, 73 options: getRawBody.Options, 74 callback: (err: getRawBody.RawBodyError, body: Buffer) => void 75 ): void; 76 77 declare function getRawBody( 78 stream: Readable, 79 options: (getRawBody.Options & { encoding: getRawBody.Encoding }) | getRawBody.Encoding 80 ): Promise<string>; 81 82 declare function getRawBody( 83 stream: Readable, 84 options?: getRawBody.Options 85 ): Promise<Buffer>; 86 87 export = getRawBody;