UNPKG

3.95 kBTypeScriptView Raw
1export type JSONValue = boolean | number | string | null | JSONArray | JSONObject;
2export interface JSONArray extends Array<JSONValue> {
3}
4export interface JSONObject {
5 [key: string]: JSONValue | undefined;
6}
7type Defined<T> = T extends undefined ? never : T;
8type Options<TJSONObject extends JSONObject> = {
9 badJsonDefault?: TJSONObject;
10 jsonParseErrorDefault?: TJSONObject;
11 cantReadFileDefault?: TJSONObject;
12 ensureDir?: boolean;
13 default?: TJSONObject;
14 json5?: boolean;
15 space?: number;
16 addNewLineAtEOF?: boolean;
17};
18/**
19 * The JsonFile class represents the contents of json file.
20 *
21 * It's polymorphic on "JSONObject", which is a simple type representing
22 * and object with string keys and either objects or primitive types as values.
23 * @type {[type]}
24 */
25export default class JsonFile<TJSONObject extends JSONObject> {
26 file: string;
27 options: Options<TJSONObject>;
28 static read: typeof read;
29 static readAsync: typeof readAsync;
30 static parseJsonString: typeof parseJsonString;
31 static writeAsync: typeof writeAsync;
32 static getAsync: typeof getAsync;
33 static setAsync: typeof setAsync;
34 static mergeAsync: typeof mergeAsync;
35 static deleteKeyAsync: typeof deleteKeyAsync;
36 static deleteKeysAsync: typeof deleteKeysAsync;
37 static rewriteAsync: typeof rewriteAsync;
38 constructor(file: string, options?: Options<TJSONObject>);
39 read(options?: Options<TJSONObject>): TJSONObject;
40 readAsync(options?: Options<TJSONObject>): Promise<TJSONObject>;
41 writeAsync(object: TJSONObject, options?: Options<TJSONObject>): Promise<TJSONObject>;
42 parseJsonString(json: string, options?: Options<TJSONObject>): TJSONObject;
43 getAsync<K extends keyof TJSONObject, TDefault extends TJSONObject[K] | null>(key: K, defaultValue: TDefault, options?: Options<TJSONObject>): Promise<Defined<TJSONObject[K]> | TDefault>;
44 setAsync(key: string, value: unknown, options?: Options<TJSONObject>): Promise<TJSONObject>;
45 mergeAsync(sources: Partial<TJSONObject> | Partial<TJSONObject>[], options?: Options<TJSONObject>): Promise<TJSONObject>;
46 deleteKeyAsync(key: string, options?: Options<TJSONObject>): Promise<TJSONObject>;
47 deleteKeysAsync(keys: string[], options?: Options<TJSONObject>): Promise<TJSONObject>;
48 rewriteAsync(options?: Options<TJSONObject>): Promise<TJSONObject>;
49 _getOptions(options?: Options<TJSONObject>): Options<TJSONObject>;
50}
51declare function read<TJSONObject extends JSONObject>(file: string, options?: Options<TJSONObject>): TJSONObject;
52declare function readAsync<TJSONObject extends JSONObject>(file: string, options?: Options<TJSONObject>): Promise<TJSONObject>;
53declare function parseJsonString<TJSONObject extends JSONObject>(json: string, options?: Options<TJSONObject>, fileName?: string): TJSONObject;
54declare function getAsync<TJSONObject extends JSONObject, K extends keyof TJSONObject, DefaultValue>(file: string, key: K, defaultValue: DefaultValue, options?: Options<TJSONObject>): Promise<any>;
55declare function writeAsync<TJSONObject extends JSONObject>(file: string, object: TJSONObject, options?: Options<TJSONObject>): Promise<TJSONObject>;
56declare function setAsync<TJSONObject extends JSONObject>(file: string, key: string, value: unknown, options?: Options<TJSONObject>): Promise<TJSONObject>;
57declare function mergeAsync<TJSONObject extends JSONObject>(file: string, sources: Partial<TJSONObject> | Partial<TJSONObject>[], options?: Options<TJSONObject>): Promise<TJSONObject>;
58declare function deleteKeyAsync<TJSONObject extends JSONObject>(file: string, key: string, options?: Options<TJSONObject>): Promise<TJSONObject>;
59declare function deleteKeysAsync<TJSONObject extends JSONObject>(file: string, keys: string[], options?: Options<TJSONObject>): Promise<TJSONObject>;
60declare function rewriteAsync<TJSONObject extends JSONObject>(file: string, options?: Options<TJSONObject>): Promise<TJSONObject>;
61export {};