response-interceptor.d.ts 1.2 KB

123456789101112131415161718192021222324252627
  1. import type * as http from 'node:http';
  2. type Interceptor<TReq = http.IncomingMessage, TRes = http.ServerResponse> = (buffer: Buffer, proxyRes: http.IncomingMessage, req: TReq, res: TRes) => Promise<Buffer | string>;
  3. /**
  4. * Intercept responses from upstream.
  5. * Automatically decompress (deflate, gzip, brotli, zstd).
  6. * Give developer the opportunity to modify intercepted Buffer and http.ServerResponse
  7. *
  8. * NOTE: must set options.selfHandleResponse=true (prevent automatic call of res.end())
  9. *
  10. * @example
  11. *
  12. * ```ts
  13. * createProxyMiddleware({
  14. * target: 'http://example.com',
  15. * selfHandleResponse: true, // MUST set selfHandleResponse=true
  16. * on: {
  17. * proxyRes: responseInterceptor(async (buffer, proxyRes, req, res) => {
  18. * // modify intercepted buffer and return modified buffer
  19. * const modifiedBuffer = Buffer.from(buffer.toString().replace(/Example/g, 'Demo'), 'utf8');
  20. * return modifiedBuffer;
  21. * }),
  22. * }
  23. * });
  24. * ```
  25. */
  26. export declare function responseInterceptor<TReq extends http.IncomingMessage = http.IncomingMessage, TRes extends http.ServerResponse = http.ServerResponse>(interceptor: Interceptor<TReq, TRes>): (proxyRes: http.IncomingMessage, req: TReq, res: TRes) => Promise<void>;
  27. export {};