normalize.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.normalizeArgs = exports.normalizeOptions = void 0;
  4. const isomorphic_node_1 = require("./isomorphic.node");
  5. /**
  6. * Normalizes Ono options, accounting for defaults and optional options.
  7. */
  8. function normalizeOptions(options) {
  9. options = options || {};
  10. return {
  11. concatMessages: options.concatMessages === undefined ? true : Boolean(options.concatMessages),
  12. format: options.format === undefined ? isomorphic_node_1.format
  13. : (typeof options.format === "function" ? options.format : false),
  14. };
  15. }
  16. exports.normalizeOptions = normalizeOptions;
  17. /**
  18. * Normalizes the Ono arguments, accounting for defaults, options, and optional arguments.
  19. */
  20. function normalizeArgs(args, options) {
  21. let originalError;
  22. let props;
  23. let formatArgs;
  24. let message = "";
  25. // Determine which arguments were actually specified
  26. if (typeof args[0] === "string") {
  27. formatArgs = args;
  28. }
  29. else if (typeof args[1] === "string") {
  30. if (args[0] instanceof Error) {
  31. originalError = args[0];
  32. }
  33. else {
  34. props = args[0];
  35. }
  36. formatArgs = args.slice(1);
  37. }
  38. else {
  39. originalError = args[0];
  40. props = args[1];
  41. formatArgs = args.slice(2);
  42. }
  43. // If there are any format arguments, then format the error message
  44. if (formatArgs.length > 0) {
  45. if (options.format) {
  46. message = options.format.apply(undefined, formatArgs);
  47. }
  48. else {
  49. message = formatArgs.join(" ");
  50. }
  51. }
  52. if (options.concatMessages && originalError && originalError.message) {
  53. // The inner-error's message will be added to the new message
  54. message += (message ? " \n" : "") + originalError.message;
  55. }
  56. return { originalError, props, message };
  57. }
  58. exports.normalizeArgs = normalizeArgs;
  59. //# sourceMappingURL=normalize.js.map