normalize-arguments.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const is_1 = require("@sindresorhus/is");
  4. const normalizeArguments = (options, defaults) => {
  5. if (is_1.default.null_(options.encoding)) {
  6. throw new TypeError('To get a Buffer, set `options.responseType` to `buffer` instead');
  7. }
  8. is_1.assert.any([is_1.default.string, is_1.default.undefined], options.encoding);
  9. is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.resolveBodyOnly);
  10. is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.methodRewriting);
  11. is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.isStream);
  12. is_1.assert.any([is_1.default.string, is_1.default.undefined], options.responseType);
  13. // `options.responseType`
  14. if (options.responseType === undefined) {
  15. options.responseType = 'text';
  16. }
  17. // `options.retry`
  18. const { retry } = options;
  19. if (defaults) {
  20. options.retry = { ...defaults.retry };
  21. }
  22. else {
  23. options.retry = {
  24. calculateDelay: retryObject => retryObject.computedValue,
  25. limit: 0,
  26. methods: [],
  27. statusCodes: [],
  28. errorCodes: [],
  29. maxRetryAfter: undefined
  30. };
  31. }
  32. if (is_1.default.object(retry)) {
  33. options.retry = {
  34. ...options.retry,
  35. ...retry
  36. };
  37. options.retry.methods = [...new Set(options.retry.methods.map(method => method.toUpperCase()))];
  38. options.retry.statusCodes = [...new Set(options.retry.statusCodes)];
  39. options.retry.errorCodes = [...new Set(options.retry.errorCodes)];
  40. }
  41. else if (is_1.default.number(retry)) {
  42. options.retry.limit = retry;
  43. }
  44. if (is_1.default.undefined(options.retry.maxRetryAfter)) {
  45. options.retry.maxRetryAfter = Math.min(
  46. // TypeScript is not smart enough to handle `.filter(x => is.number(x))`.
  47. // eslint-disable-next-line unicorn/no-fn-reference-in-iterator
  48. ...[options.timeout.request, options.timeout.connect].filter(is_1.default.number));
  49. }
  50. // `options.pagination`
  51. if (is_1.default.object(options.pagination)) {
  52. if (defaults) {
  53. options.pagination = {
  54. ...defaults.pagination,
  55. ...options.pagination
  56. };
  57. }
  58. const { pagination } = options;
  59. if (!is_1.default.function_(pagination.transform)) {
  60. throw new Error('`options.pagination.transform` must be implemented');
  61. }
  62. if (!is_1.default.function_(pagination.shouldContinue)) {
  63. throw new Error('`options.pagination.shouldContinue` must be implemented');
  64. }
  65. if (!is_1.default.function_(pagination.filter)) {
  66. throw new TypeError('`options.pagination.filter` must be implemented');
  67. }
  68. if (!is_1.default.function_(pagination.paginate)) {
  69. throw new Error('`options.pagination.paginate` must be implemented');
  70. }
  71. }
  72. // JSON mode
  73. if (options.responseType === 'json' && options.headers.accept === undefined) {
  74. options.headers.accept = 'application/json';
  75. }
  76. return options;
  77. };
  78. exports.default = normalizeArguments;