logPolicy.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See License.txt in the project root for license information.
  3. import * as tslib_1 from "tslib";
  4. import { BaseRequestPolicy } from "./requestPolicy";
  5. export function logPolicy(logger) {
  6. if (logger === void 0) { logger = console.log; }
  7. return {
  8. create: function (nextPolicy, options) {
  9. return new LogPolicy(nextPolicy, options, logger);
  10. }
  11. };
  12. }
  13. var LogPolicy = /** @class */ (function (_super) {
  14. tslib_1.__extends(LogPolicy, _super);
  15. function LogPolicy(nextPolicy, options, logger) {
  16. if (logger === void 0) { logger = console.log; }
  17. var _this = _super.call(this, nextPolicy, options) || this;
  18. _this.logger = logger;
  19. return _this;
  20. }
  21. LogPolicy.prototype.sendRequest = function (request) {
  22. var _this = this;
  23. return this._nextPolicy.sendRequest(request).then(function (response) { return logResponse(_this, response); });
  24. };
  25. return LogPolicy;
  26. }(BaseRequestPolicy));
  27. export { LogPolicy };
  28. function logResponse(policy, response) {
  29. policy.logger(">> Request: " + JSON.stringify(response.request, undefined, 2));
  30. policy.logger(">> Response status code: " + response.status);
  31. var responseBody = response.bodyAsText;
  32. policy.logger(">> Body: " + responseBody);
  33. return Promise.resolve(response);
  34. }
  35. //# sourceMappingURL=logPolicy.js.map