index.d.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {JsonValue} from 'type-fest';
  2. declare namespace loadJsonFile {
  3. type Reviver = (this: unknown, key: string, value: any) => unknown;
  4. type BeforeParse = (data: string) => string;
  5. interface Options {
  6. /**
  7. Applies a function to the JSON string before parsing.
  8. */
  9. readonly beforeParse?: BeforeParse;
  10. /**
  11. Prescribes how the value originally produced by parsing is transformed, before being returned.
  12. See the [`JSON.parse` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter) for more.
  13. */
  14. readonly reviver?: Reviver;
  15. }
  16. }
  17. declare const loadJsonFile: {
  18. /**
  19. Read and parse a JSON file.
  20. Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors.
  21. @example
  22. ```
  23. import loadJsonFile = require('load-json-file');
  24. (async () => {
  25. const json = await loadJsonFile('foo.json');
  26. //=> {foo: true}
  27. })();
  28. ```
  29. */
  30. <T = JsonValue>(filePath: string, options?: loadJsonFile.Options): Promise<T>;
  31. /**
  32. Read and parse a JSON file.
  33. Strips UTF-8 BOM, uses graceful-fs, and throws more helpful JSON errors.
  34. @example
  35. ```
  36. import loadJsonFile = require('load-json-file');
  37. const json = loadJsonFile.sync('foo.json');
  38. //=> {foo: true}
  39. ```
  40. */
  41. sync<T = JsonValue>(filePath: string, options?: loadJsonFile.Options): T;
  42. };
  43. export = loadJsonFile;