index.d.ts 2.3 KB

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