1
- import type { StorageItems , SettingsKey } from '@/types/storage'
1
+ import type {
2
+ SettingsKey ,
3
+ SettingItems ,
4
+ SettingsExportKey ,
5
+ SettingsExportItems ,
6
+ } from '@/types/storage'
2
7
import type { StorageOnChangeCallback } from '@/utils/storage'
3
8
4
- import { SETTINGS_DEFAULT } from '@/constants/settings/default'
9
+ import {
10
+ SETTINGS_DEFAULT ,
11
+ SETTINGS_DEFAULT_KEYS ,
12
+ } from '@/constants/settings/default'
5
13
6
14
import { WebExtStorage } from '@/utils/storage'
7
15
16
+ const SETTINGS_EXPORT_KEYS = [
17
+ '_migrate_version' ,
18
+ ...SETTINGS_DEFAULT_KEYS ,
19
+ ] as SettingsExportKey [ ]
20
+
8
21
/**
9
22
* 設定を取得
10
23
*/
11
24
export type SettingsGetFunction = {
12
25
/** すべての設定を取得 */
13
- ( ) : Promise < {
14
- [ key in SettingsKey ] : StorageItems [ key ]
15
- } >
26
+ ( ) : Promise < SettingItems >
16
27
17
28
/** 1つの設定を取得 */
18
- < Key extends SettingsKey > ( key : Key ) : Promise < StorageItems [ Key ] >
29
+ < Key extends SettingsKey > ( key : Key ) : Promise < SettingItems [ Key ] >
19
30
20
31
/** 複数の設定を取得 */
21
32
< Keys extends SettingsKey [ ] > (
22
33
...keys : Keys
23
34
) : Promise < {
24
- [ key in Keys [ number ] ] : StorageItems [ key ]
35
+ [ key in Keys [ number ] ] : SettingItems [ key ]
25
36
} >
26
37
}
27
38
@@ -30,7 +41,7 @@ export type SettingsGetFunction = {
30
41
*/
31
42
export type SettingsSetFunction = < Key extends SettingsKey > (
32
43
key : Key ,
33
- value : StorageItems [ Key ] | null | undefined
44
+ value : SettingItems [ Key ] | null | undefined
34
45
) => Promise < void >
35
46
36
47
/**
@@ -68,9 +79,23 @@ export type SettingsOnChangeFunction = <Key extends SettingsKey>(
68
79
*/
69
80
export type SettingsWatch = < Key extends SettingsKey > (
70
81
key : Key ,
71
- callback : ( value : StorageItems [ Key ] ) => void
82
+ callback : ( value : SettingItems [ Key ] ) => void
72
83
) => ( ) => void
73
84
85
+ /**
86
+ * 設定をインポート
87
+ */
88
+ export type SettingsImportFunction = {
89
+ ( values : string | SettingsExportItems ) : Promise < void >
90
+ }
91
+
92
+ /**
93
+ * 設定をエクスポート
94
+ */
95
+ export type SettingsExportFunction = {
96
+ ( ) : Promise < SettingsExportItems >
97
+ }
98
+
74
99
export class WebExtSettings {
75
100
#storage: WebExtStorage
76
101
@@ -97,9 +122,7 @@ export class WebExtSettings {
97
122
Object . entries ( values ) . map ( ( [ key , val ] ) => {
98
123
return [ key , val ?? SETTINGS_DEFAULT [ key as SettingsKey ] ]
99
124
} )
100
- ) as {
101
- [ key in SettingsKey ] : StorageItems [ key ]
102
- }
125
+ ) as SettingItems
103
126
104
127
return keys . length
105
128
? items
@@ -111,7 +134,7 @@ export class WebExtSettings {
111
134
112
135
readonly remove : SettingsRemoveFunction = ( ...keys : SettingsKey [ ] ) => {
113
136
if ( ! keys . length ) {
114
- keys = Object . keys ( SETTINGS_DEFAULT ) as SettingsKey [ ]
137
+ keys = SETTINGS_DEFAULT_KEYS
115
138
}
116
139
117
140
return this . #storage. remove ( ...keys )
@@ -121,7 +144,7 @@ export class WebExtSettings {
121
144
...keys : SettingsKey [ ]
122
145
) => {
123
146
if ( ! keys . length ) {
124
- keys = Object . keys ( SETTINGS_DEFAULT ) as SettingsKey [ ]
147
+ keys = SETTINGS_DEFAULT_KEYS
125
148
}
126
149
127
150
return this . #storage. getBytesInUse ( ...keys )
@@ -140,4 +163,30 @@ export class WebExtSettings {
140
163
141
164
return ( ) => removeListener ( )
142
165
}
166
+
167
+ readonly import : SettingsImportFunction = async ( values ) => {
168
+ const object =
169
+ typeof values === 'string'
170
+ ? ( JSON . parse ( values ) as SettingsExportItems )
171
+ : values
172
+
173
+ const entries = (
174
+ Object . entries ( object ) as [
175
+ SettingsExportKey ,
176
+ SettingsExportItems [ SettingsExportKey ] ,
177
+ ] [ ]
178
+ ) . filter ( ( [ key ] ) => SETTINGS_EXPORT_KEYS . includes ( key ) )
179
+
180
+ await Promise . all (
181
+ entries . map ( ( [ key , value ] ) => {
182
+ return this . #storage. set ( key , value )
183
+ } )
184
+ )
185
+ }
186
+
187
+ readonly export : SettingsExportFunction = ( ) => {
188
+ return this . #storage. get (
189
+ ...SETTINGS_EXPORT_KEYS
190
+ ) as Promise < SettingsExportItems >
191
+ }
143
192
}
0 commit comments