UNPKG

2.75 kBTypeScriptView Raw
1// Type definitions for jsonfile 6.0
2// Project: https://github.com/jprichardson/node-jsonfile#readme
3// Definitions by: Daniel Bowring <https://github.com/dbowring>
4// BendingBender <https://github.com/BendingBender>
5// Piotr Błażejewicz <https://github.com/peterblazejewicz>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
8/// <reference types="node"/>
9
10import { Url } from 'url';
11import {
12 PathLike,
13 readFile as fsReadFile,
14 readFileSync as fsReadFileSync,
15 writeFile as fsWriteFile,
16 writeFileSync as fsWriteFileSync,
17} from 'fs';
18
19export type Path = PathLike | Url;
20
21export interface FS {
22 readFile: typeof fsReadFile;
23 readFileSync: typeof fsReadFileSync;
24 writeFile: typeof fsWriteFile;
25 writeFileSync: typeof fsWriteFileSync;
26}
27
28export type JFReadOptions =
29 | {
30 encoding?: string | null | undefined;
31 flag?: string | undefined;
32 throws?: boolean | undefined;
33 fs?: FS | undefined;
34 reviver?: ((key: any, value: any) => any) | undefined;
35 }
36 | string
37 | null
38 | undefined;
39
40export type JFWriteOptions =
41 | {
42 encoding?: string | null | undefined;
43 mode?: string | number | undefined;
44 flag?: string | undefined;
45 fs?: FS | undefined;
46 EOL?: string | undefined;
47 spaces?: string | number | undefined;
48 replacer?: ((key: string, value: any) => any) | undefined;
49 }
50 | string
51 | null;
52
53export type ReadCallback = (err: NodeJS.ErrnoException | null, data: any) => void;
54export type WriteCallback = (err: NodeJS.ErrnoException) => void;
55
56/**
57 * @see {@link https://github.com/jprichardson/node-jsonfile#readfilefilename-options-callback}
58 */
59export function readFile(file: Path, options: JFReadOptions, callback: ReadCallback): void;
60export function readFile(file: Path, callback: ReadCallback): void;
61export function readFile(file: Path, options?: JFReadOptions): Promise<any>;
62
63/**
64 * @see {@link https://github.com/jprichardson/node-jsonfile#readfilesyncfilename-options}
65 */
66export function readFileSync(file: Path, options?: JFReadOptions): any;
67
68/**
69 * @see {@link https://github.com/jprichardson/node-jsonfile#writefilefilename-obj-options-callback}
70 */
71export function writeFile(file: Path, obj: any, options: JFWriteOptions, callback: WriteCallback): void;
72export function writeFile(file: Path, obj: any, callback: WriteCallback): void;
73export function writeFile(file: Path, obj: any, options?: JFWriteOptions): Promise<void>;
74
75/**
76 * @see {@link https://github.com/jprichardson/node-jsonfile#writefilesyncfilename-obj-options}
77 */
78export function writeFileSync(file: Path, obj: any, options?: JFWriteOptions): void;