* Asserts that the handler object is a request handler. * * @param {object} handler * @param {string} method * @param {string} [upgrade] * @returns {asserts handler is import('../api/api-request').RequestHandler}
(handler, method, upgrade)
| 565 | * @returns {asserts handler is import('../api/api-request').RequestHandler} |
| 566 | */ |
| 567 | function assertRequestHandler (handler, method, upgrade) { |
| 568 | if (!handler || typeof handler !== 'object') { |
| 569 | throw new InvalidArgumentError('handler must be an object') |
| 570 | } |
| 571 | |
| 572 | if (typeof handler.onRequestStart !== 'function') { |
| 573 | throw new InvalidArgumentError('invalid onRequestStart method') |
| 574 | } |
| 575 | |
| 576 | if (typeof handler.onResponseError !== 'function') { |
| 577 | throw new InvalidArgumentError('invalid onResponseError method') |
| 578 | } |
| 579 | |
| 580 | if (typeof handler.onBodySent !== 'function' && handler.onBodySent !== undefined) { |
| 581 | throw new InvalidArgumentError('invalid onBodySent method') |
| 582 | } |
| 583 | |
| 584 | if (typeof handler.onRequestSent !== 'function' && handler.onRequestSent !== undefined) { |
| 585 | throw new InvalidArgumentError('invalid onRequestSent method') |
| 586 | } |
| 587 | |
| 588 | if (upgrade || method === 'CONNECT') { |
| 589 | if (typeof handler.onRequestUpgrade !== 'function') { |
| 590 | throw new InvalidArgumentError('invalid onRequestUpgrade method') |
| 591 | } |
| 592 | } else { |
| 593 | if (typeof handler.onResponseStart !== 'function') { |
| 594 | throw new InvalidArgumentError('invalid onResponseStart method') |
| 595 | } |
| 596 | |
| 597 | if (typeof handler.onResponseData !== 'function') { |
| 598 | throw new InvalidArgumentError('invalid onResponseData method') |
| 599 | } |
| 600 | |
| 601 | if (typeof handler.onResponseEnd !== 'function') { |
| 602 | throw new InvalidArgumentError('invalid onResponseEnd method') |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * A body is disturbed if it has been read from and it cannot be re-used without |