isomorphic.node.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import * as util from "util";
  2. import { getDeepKeys } from "./to-json";
  3. // The `inspect()` method is actually a Symbol, not a string key.
  4. // https://nodejs.org/api/util.html#util_util_inspect_custom
  5. const inspectMethod = util.inspect.custom || Symbol.for("nodejs.util.inspect.custom");
  6. /**
  7. * Ono supports Node's `util.format()` formatting for error messages.
  8. *
  9. * @see https://nodejs.org/api/util.html#util_util_format_format_args
  10. */
  11. export const format = util.format;
  12. /**
  13. * Adds an `inspect()` method to support Node's `util.inspect()` function.
  14. *
  15. * @see https://nodejs.org/api/util.html#util_util_inspect_custom
  16. */
  17. export function addInspectMethod(newError) {
  18. // @ts-expect-error - TypeScript doesn't support symbol indexers
  19. newError[inspectMethod] = inspect;
  20. }
  21. /**
  22. * Returns a representation of the error for Node's `util.inspect()` method.
  23. *
  24. * @see https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects
  25. */
  26. function inspect() {
  27. // HACK: We have to cast the objects to `any` so we can use symbol indexers.
  28. // see https://github.com/Microsoft/TypeScript/issues/1863
  29. let pojo = {};
  30. let error = this;
  31. for (let key of getDeepKeys(error)) {
  32. let value = error[key];
  33. pojo[key] = value;
  34. }
  35. // Don't include the `inspect()` method on the output object,
  36. // otherwise it will cause `util.inspect()` to go into an infinite loop
  37. // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
  38. delete pojo[inspectMethod];
  39. return pojo;
  40. }
  41. //# sourceMappingURL=isomorphic.node.js.map