-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.ts
147 lines (135 loc) · 3.8 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { JSONSchemaType, ValidateFunction } from 'ajv'
import type { BaseConf } from './conf'
export type JSONSchema<T> = JSONSchemaType<T>
export type ValidateFn = ValidateFunction
export interface Serializer<T> {
/**
* Deserialize the config object from a UTF-8 string when reading the config file.
* @param raw UTF-8 encoded string.
*/
read: (raw: string) => T
/**
* Serialize the config object to a UTF-8 string when writing the config file.
* @param value The config object.
*/
write: (value: T) => string
}
export type Migration<T extends Record<string, any>> = {
/**
* Migration version. The initial version must be greater than `0`. A new
* version is defined on each migration and is incremented on the previous version.
*/
version: number
/**
* Migration hook. You can perform operations to update your configuration.
* @param instance config instance.
* @param currentVersion current version.
*/
hook: (instance: BaseConf<T>, currentVersion: number) => void
}
export type Options<T extends Record<string, any>> = {
/**
* The directory for storing your app's configuration file.
*
* @default app.getPath('userData')
*/
dir?: string
/**
* Configuration file name without extension.
*
* @default 'config'
*/
name?: string
/**
* Configuration file extension.
*
* @default '.json'
*/
ext?: string
/**
* Default config used if there are no existing config.
*/
defaults?: Readonly<T>
/**
* Provides functionality to serialize object types to UTF-8 strings and to
* deserialize UTF-8 strings into object types.
*
* By default, `JSON.stringify` is used for serialization and `JSON.parse` is
* used for deserialization.
*
* You would usually not need this, but it could be useful if you want to use
* a format other than JSON.
*/
serializer?: Serializer<T>
/**
* [JSON Schema](https://json-schema.org) to validate your config data.
*
* Under the hood, we use the [ajv](https://ajv.js.org/) JSON Schema
* validator to validate config data.
*
* @example
* ```
* import { Conf } from 'electron-conf/main'
*
* const schema = {
* type: 'object',
* properties: {
* foo: {
* type: 'string',
* maxLength: 10,
* nullable: true
* }
* }
* }
*
* const conf = new Conf({ schema })
* ```
*/
schema?: JSONSchema<T>
/**
* You can customize versions and perform operations to migrate configurations.
* When instantiated, it will be compared with the version number of the
* configuration file and a higher version migration operation will be performed.
*
* **Note:** The migration version must be greater than `0`. A new version is
* defined on each migration and is incremented on the previous version.
*
* @example
* ```
* import { Conf } from 'electron-conf/main'
*
* const migrations = [
* {
* version: 1,
* hook: (conf, version): void => {
* conf.set('foo', 'a')
* console.log(`migrate from ${version} to 1`) // migrate from 0 to 1
* }
* },
* {
* version: 2,
* hook: (conf, version): void => {
* conf.set('foo', 'b')
* console.log(`migrate from ${version} to 2`) // migrate from 1 to 2
* }
* }
* ]
*
* const conf = new Conf({ migrations })
* ```
*/
migrations?: Migration<T>[]
}
export type OnDidChangeCallback<T> = (newValue?: T, oldValue?: T) => void
export type OnDidAnyChangeCallback<T> = (
newValue?: Readonly<T>,
oldValue?: Readonly<T>
) => void
export type Unsubscribe = () => void
export type ConfOptions<T extends Record<string, any>> = Options<T>
interface IpcRenderer {
invoke(channel: string, ...args: any[]): Promise<any>
}
export interface ConfAPI {
ipcRenderer: IpcRenderer
}