@capacitor/preferences 4.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +299 -0
- package/CapacitorPreferences.podspec +17 -0
- package/LICENSE +23 -0
- package/README.md +259 -0
- package/android/build.gradle +57 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/java/com/capacitorjs/plugins/preferences/Preferences.java +45 -0
- package/android/src/main/java/com/capacitorjs/plugins/preferences/PreferencesConfiguration.java +18 -0
- package/android/src/main/java/com/capacitorjs/plugins/preferences/PreferencesPlugin.java +129 -0
- package/dist/docs.json +348 -0
- package/dist/esm/definitions.d.ts +135 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +17 -0
- package/dist/esm/web.js +71 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +87 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +90 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/Preferences.swift +64 -0
- package/ios/Plugin/PreferencesPlugin.h +10 -0
- package/ios/Plugin/PreferencesPlugin.m +15 -0
- package/ios/Plugin/PreferencesPlugin.swift +106 -0
- package/package.json +83 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,WAAW,GAAG,cAAc,CAAoB,aAAa,EAAE;IACnE,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,cAAc,EAAE,CAAC;CAC7D,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,WAAW,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { PreferencesPlugin } from './definitions';\n\nconst Preferences = registerPlugin<PreferencesPlugin>('Preferences', {\n web: () => import('./web').then(m => new m.PreferencesWeb()),\n});\n\nexport * from './definitions';\nexport { Preferences };\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { WebPlugin } from '@capacitor/core';
|
|
2
|
+
import type { PreferencesPlugin, ConfigureOptions, GetOptions, GetResult, SetOptions, RemoveOptions, KeysResult, MigrateResult } from './definitions';
|
|
3
|
+
export declare class PreferencesWeb extends WebPlugin implements PreferencesPlugin {
|
|
4
|
+
private group;
|
|
5
|
+
configure({ group }: ConfigureOptions): Promise<void>;
|
|
6
|
+
get(options: GetOptions): Promise<GetResult>;
|
|
7
|
+
set(options: SetOptions): Promise<void>;
|
|
8
|
+
remove(options: RemoveOptions): Promise<void>;
|
|
9
|
+
keys(): Promise<KeysResult>;
|
|
10
|
+
clear(): Promise<void>;
|
|
11
|
+
migrate(): Promise<MigrateResult>;
|
|
12
|
+
removeOld(): Promise<void>;
|
|
13
|
+
private get impl();
|
|
14
|
+
private get prefix();
|
|
15
|
+
private rawKeys;
|
|
16
|
+
private applyPrefix;
|
|
17
|
+
}
|
package/dist/esm/web.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { WebPlugin } from '@capacitor/core';
|
|
2
|
+
export class PreferencesWeb extends WebPlugin {
|
|
3
|
+
constructor() {
|
|
4
|
+
super(...arguments);
|
|
5
|
+
this.group = 'CapacitorStorage';
|
|
6
|
+
}
|
|
7
|
+
async configure({ group }) {
|
|
8
|
+
if (typeof group === 'string') {
|
|
9
|
+
this.group = group;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
async get(options) {
|
|
13
|
+
const value = this.impl.getItem(this.applyPrefix(options.key));
|
|
14
|
+
return { value };
|
|
15
|
+
}
|
|
16
|
+
async set(options) {
|
|
17
|
+
this.impl.setItem(this.applyPrefix(options.key), options.value);
|
|
18
|
+
}
|
|
19
|
+
async remove(options) {
|
|
20
|
+
this.impl.removeItem(this.applyPrefix(options.key));
|
|
21
|
+
}
|
|
22
|
+
async keys() {
|
|
23
|
+
const keys = this.rawKeys().map(k => k.substring(this.prefix.length));
|
|
24
|
+
return { keys };
|
|
25
|
+
}
|
|
26
|
+
async clear() {
|
|
27
|
+
for (const key of this.rawKeys()) {
|
|
28
|
+
this.impl.removeItem(key);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async migrate() {
|
|
32
|
+
var _a;
|
|
33
|
+
const migrated = [];
|
|
34
|
+
const existing = [];
|
|
35
|
+
const oldprefix = '_cap_';
|
|
36
|
+
const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);
|
|
37
|
+
for (const oldkey of keys) {
|
|
38
|
+
const key = oldkey.substring(oldprefix.length);
|
|
39
|
+
const value = (_a = this.impl.getItem(oldkey)) !== null && _a !== void 0 ? _a : '';
|
|
40
|
+
const { value: currentValue } = await this.get({ key });
|
|
41
|
+
if (typeof currentValue === 'string') {
|
|
42
|
+
existing.push(key);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
await this.set({ key, value });
|
|
46
|
+
migrated.push(key);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { migrated, existing };
|
|
50
|
+
}
|
|
51
|
+
async removeOld() {
|
|
52
|
+
const oldprefix = '_cap_';
|
|
53
|
+
const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);
|
|
54
|
+
for (const oldkey of keys) {
|
|
55
|
+
this.impl.removeItem(oldkey);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
get impl() {
|
|
59
|
+
return window.localStorage;
|
|
60
|
+
}
|
|
61
|
+
get prefix() {
|
|
62
|
+
return this.group === 'NativeStorage' ? '' : `${this.group}.`;
|
|
63
|
+
}
|
|
64
|
+
rawKeys() {
|
|
65
|
+
return Object.keys(this.impl).filter(k => k.indexOf(this.prefix) === 0);
|
|
66
|
+
}
|
|
67
|
+
applyPrefix(key) {
|
|
68
|
+
return this.prefix + key;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=web.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAa5C,MAAM,OAAO,cAAe,SAAQ,SAAS;IAA7C;;QACU,UAAK,GAAG,kBAAkB,CAAC;IA+ErC,CAAC;IA7EQ,KAAK,CAAC,SAAS,CAAC,EAAE,KAAK,EAAoB;QAChD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;YAC7B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;SACpB;IACH,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,OAAmB;QAClC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;QAE/D,OAAO,EAAE,KAAK,EAAE,CAAC;IACnB,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,OAAmB;QAClC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IAClE,CAAC;IAEM,KAAK,CAAC,MAAM,CAAC,OAAsB;QACxC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;IACtD,CAAC;IAEM,KAAK,CAAC,IAAI;QACf,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC,GAAG,CAAC,C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AE,CAAC;IAChC,CAAC;IAEM,KAAK,CAAC,SAAS;QACpB,MAAM,SAAS,GAAG,OAAO,CAAC;QAC1B,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;QAC5E,KAAK,MAAM,MAAM,IAAI,IAAI,EAAE;YACzB,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;SAC9B;IACH,CAAC;IAED,IAAY,IAAI;QACd,OAAO,MAAM,CAAC,YAAY,CAAC;IAC7B,CAAC;IAED,IAAY,MAAM;QAChB,OAAO,IAAI,CAAC,KAAK,KAAK,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,GAAG,CAAC;IAChE,CAAC;IAEO,OAAO;QACb,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC1E,CAAC;IAEO,WAAW,CAAC,GAAW;QAC7B,OAAO,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;IAC3B,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type {\n PreferencesPlugin,\n ConfigureOptions,\n GetOptions,\n GetResult,\n SetOptions,\n RemoveOptions,\n KeysResult,\n MigrateResult,\n} from './definitions';\n\nexport class PreferencesWeb extends WebPlugin implements PreferencesPlugin {\n private group = 'CapacitorStorage';\n\n public async configure({ group }: ConfigureOptions): Promise<void> {\n if (typeof group === 'string') {\n this.group = group;\n }\n }\n\n public async get(options: GetOptions): Promise<GetResult> {\n const value = this.impl.getItem(this.applyPrefix(options.key));\n\n return { value };\n }\n\n public async set(options: SetOptions): Promise<void> {\n this.impl.setItem(this.applyPrefix(options.key), options.value);\n }\n\n public async remove(options: RemoveOptions): Promise<void> {\n this.impl.removeItem(this.applyPrefix(options.key));\n }\n\n public async keys(): Promise<KeysResult> {\n const keys = this.rawKeys().map(k => k.substring(this.prefix.length));\n\n return { keys };\n }\n\n public async clear(): Promise<void> {\n for (const key of this.rawKeys()) {\n this.impl.removeItem(key);\n }\n }\n\n public async migrate(): Promise<MigrateResult> {\n const migrated: string[] = [];\n const existing: string[] = [];\n const oldprefix = '_cap_';\n const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);\n\n for (const oldkey of keys) {\n const key = oldkey.substring(oldprefix.length);\n const value = this.impl.getItem(oldkey) ?? '';\n const { value: currentValue } = await this.get({ key });\n\n if (typeof currentValue === 'string') {\n existing.push(key);\n } else {\n await this.set({ key, value });\n migrated.push(key);\n }\n }\n\n return { migrated, existing };\n }\n\n public async removeOld(): Promise<void> {\n const oldprefix = '_cap_';\n const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);\n for (const oldkey of keys) {\n this.impl.removeItem(oldkey);\n }\n }\n\n private get impl(): Storage {\n return window.localStorage;\n }\n\n private get prefix(): string {\n return this.group === 'NativeStorage' ? '' : `${this.group}.`;\n }\n\n private rawKeys(): string[] {\n return Object.keys(this.impl).filter(k => k.indexOf(this.prefix) === 0);\n }\n\n private applyPrefix(key: string) {\n return this.prefix + key;\n }\n}\n"]}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@capacitor/core');
|
|
6
|
+
|
|
7
|
+
const Preferences = core.registerPlugin('Preferences', {
|
|
8
|
+
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.PreferencesWeb()),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
class PreferencesWeb extends core.WebPlugin {
|
|
12
|
+
constructor() {
|
|
13
|
+
super(...arguments);
|
|
14
|
+
this.group = 'CapacitorStorage';
|
|
15
|
+
}
|
|
16
|
+
async configure({ group }) {
|
|
17
|
+
if (typeof group === 'string') {
|
|
18
|
+
this.group = group;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async get(options) {
|
|
22
|
+
const value = this.impl.getItem(this.applyPrefix(options.key));
|
|
23
|
+
return { value };
|
|
24
|
+
}
|
|
25
|
+
async set(options) {
|
|
26
|
+
this.impl.setItem(this.applyPrefix(options.key), options.value);
|
|
27
|
+
}
|
|
28
|
+
async remove(options) {
|
|
29
|
+
this.impl.removeItem(this.applyPrefix(options.key));
|
|
30
|
+
}
|
|
31
|
+
async keys() {
|
|
32
|
+
const keys = this.rawKeys().map(k => k.substring(this.prefix.length));
|
|
33
|
+
return { keys };
|
|
34
|
+
}
|
|
35
|
+
async clear() {
|
|
36
|
+
for (const key of this.rawKeys()) {
|
|
37
|
+
this.impl.removeItem(key);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async migrate() {
|
|
41
|
+
var _a;
|
|
42
|
+
const migrated = [];
|
|
43
|
+
const existing = [];
|
|
44
|
+
const oldprefix = '_cap_';
|
|
45
|
+
const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);
|
|
46
|
+
for (const oldkey of keys) {
|
|
47
|
+
const key = oldkey.substring(oldprefix.length);
|
|
48
|
+
const value = (_a = this.impl.getItem(oldkey)) !== null && _a !== void 0 ? _a : '';
|
|
49
|
+
const { value: currentValue } = await this.get({ key });
|
|
50
|
+
if (typeof currentValue === 'string') {
|
|
51
|
+
existing.push(key);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
await this.set({ key, value });
|
|
55
|
+
migrated.push(key);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { migrated, existing };
|
|
59
|
+
}
|
|
60
|
+
async removeOld() {
|
|
61
|
+
const oldprefix = '_cap_';
|
|
62
|
+
const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);
|
|
63
|
+
for (const oldkey of keys) {
|
|
64
|
+
this.impl.removeItem(oldkey);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
get impl() {
|
|
68
|
+
return window.localStorage;
|
|
69
|
+
}
|
|
70
|
+
get prefix() {
|
|
71
|
+
return this.group === 'NativeStorage' ? '' : `${this.group}.`;
|
|
72
|
+
}
|
|
73
|
+
rawKeys() {
|
|
74
|
+
return Object.keys(this.impl).filter(k => k.indexOf(this.prefix) === 0);
|
|
75
|
+
}
|
|
76
|
+
applyPrefix(key) {
|
|
77
|
+
return this.prefix + key;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
82
|
+
__proto__: null,
|
|
83
|
+
PreferencesWeb: PreferencesWeb
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
exports.Preferences = Preferences;
|
|
87
|
+
//# sourceMappingURL=plugin.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst Preferences = registerPlugin('Preferences', {\n web: () => import('./web').then(m => new m.PreferencesWeb()),\n});\nexport * from './definitions';\nexport { Preferences };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class PreferencesWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this.group = 'CapacitorStorage';\n }\n async configure({ group }) {\n if (typeof group === 'string') {\n this.group = group;\n }\n }\n async get(options) {\n const value = this.impl.getItem(this.applyPrefix(options.key));\n return { value };\n }\n async set(options) {\n this.impl.setItem(this.applyPrefix(options.key), options.value);\n }\n async remove(options) {\n this.impl.removeItem(this.applyPrefix(options.key));\n }\n async keys() {\n const keys = this.rawKeys().map(k => k.substring(this.prefix.length));\n return { keys };\n }\n async clear() {\n for (const key of this.rawKeys()) {\n this.impl.removeItem(key);\n }\n }\n async migrate() {\n var _a;\n const migrated = [];\n const existing = [];\n const oldprefix = '_cap_';\n const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);\n for (const oldkey of keys) {\n const key = oldkey.substring(oldprefix.length);\n const value = (_a = this.impl.getItem(oldkey)) !== null && _a !== void 0 ? _a : '';\n const { value: currentValue } = await this.get({ key });\n if (typeof currentValue === 'string') {\n existing.push(key);\n }\n else {\n await this.set({ key, value });\n migrated.push(key);\n }\n }\n return { migrated, existing };\n }\n async removeOld() {\n const oldprefix = '_cap_';\n const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);\n for (const oldkey of keys) {\n this.impl.removeItem(oldkey);\n }\n }\n get impl() {\n return window.localStorage;\n }\n get prefix() {\n return this.group === 'NativeStorage' ? '' : `${this.group}.`;\n }\n rawKeys() {\n return Object.keys(this.impl).filter(k => k.indexOf(this.prefix) === 0);\n }\n applyPrefix(key) {\n return this.prefix + key;\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;;;AACK,MAAC,WAAW,GAAGA,mBAAc,CAAC,aAAa,EAAE;AAClD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,cAAc,EAAE,CAAC;AAChE,CAAC;;ACFM,MAAM,cAAc,SAASC,cAAS,CAAC;AAC9C,IAAI,WAAW,GAAG;AAClB,QAAQ,KAAK,CAAC,GAAG,SAAS,CAAC,CAAC;AAC5B,QAAQ,IAAI,CAAC,KAAK,GAAG,kBAAkB,CAAC;AACxC,KAAK;AACL,IAAI,MAAM,SAAS,CAAC,EAAE,KAAK,EAAE,EAAE;AAC/B,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AACvC,YAAY,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;AAC/B,SAAS;AACT,KAAK;AACL,IAAI,MAAM,GA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UAAU,CAAC,MAAM,CAAC,CAAC;AACzC,SAAS;AACT,KAAK;AACL,IAAI,IAAI,IAAI,GAAG;AACf,QAAQ,OAAO,MAAM,CAAC,YAAY,CAAC;AACnC,KAAK;AACL,IAAI,IAAI,MAAM,GAAG;AACjB,QAAQ,OAAO,IAAI,CAAC,KAAK,KAAK,eAAe,GAAG,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACtE,KAAK;AACL,IAAI,OAAO,GAAG;AACd,QAAQ,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AAChF,KAAK;AACL,IAAI,WAAW,CAAC,GAAG,EAAE;AACrB,QAAQ,OAAO,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;AACjC,KAAK;AACL;;;;;;;;;"}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
var capacitorPreferences = (function (exports, core) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const Preferences = core.registerPlugin('Preferences', {
|
|
5
|
+
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.PreferencesWeb()),
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
class PreferencesWeb extends core.WebPlugin {
|
|
9
|
+
constructor() {
|
|
10
|
+
super(...arguments);
|
|
11
|
+
this.group = 'CapacitorStorage';
|
|
12
|
+
}
|
|
13
|
+
async configure({ group }) {
|
|
14
|
+
if (typeof group === 'string') {
|
|
15
|
+
this.group = group;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async get(options) {
|
|
19
|
+
const value = this.impl.getItem(this.applyPrefix(options.key));
|
|
20
|
+
return { value };
|
|
21
|
+
}
|
|
22
|
+
async set(options) {
|
|
23
|
+
this.impl.setItem(this.applyPrefix(options.key), options.value);
|
|
24
|
+
}
|
|
25
|
+
async remove(options) {
|
|
26
|
+
this.impl.removeItem(this.applyPrefix(options.key));
|
|
27
|
+
}
|
|
28
|
+
async keys() {
|
|
29
|
+
const keys = this.rawKeys().map(k => k.substring(this.prefix.length));
|
|
30
|
+
return { keys };
|
|
31
|
+
}
|
|
32
|
+
async clear() {
|
|
33
|
+
for (const key of this.rawKeys()) {
|
|
34
|
+
this.impl.removeItem(key);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async migrate() {
|
|
38
|
+
var _a;
|
|
39
|
+
const migrated = [];
|
|
40
|
+
const existing = [];
|
|
41
|
+
const oldprefix = '_cap_';
|
|
42
|
+
const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);
|
|
43
|
+
for (const oldkey of keys) {
|
|
44
|
+
const key = oldkey.substring(oldprefix.length);
|
|
45
|
+
const value = (_a = this.impl.getItem(oldkey)) !== null && _a !== void 0 ? _a : '';
|
|
46
|
+
const { value: currentValue } = await this.get({ key });
|
|
47
|
+
if (typeof currentValue === 'string') {
|
|
48
|
+
existing.push(key);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
await this.set({ key, value });
|
|
52
|
+
migrated.push(key);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return { migrated, existing };
|
|
56
|
+
}
|
|
57
|
+
async removeOld() {
|
|
58
|
+
const oldprefix = '_cap_';
|
|
59
|
+
const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);
|
|
60
|
+
for (const oldkey of keys) {
|
|
61
|
+
this.impl.removeItem(oldkey);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
get impl() {
|
|
65
|
+
return window.localStorage;
|
|
66
|
+
}
|
|
67
|
+
get prefix() {
|
|
68
|
+
return this.group === 'NativeStorage' ? '' : `${this.group}.`;
|
|
69
|
+
}
|
|
70
|
+
rawKeys() {
|
|
71
|
+
return Object.keys(this.impl).filter(k => k.indexOf(this.prefix) === 0);
|
|
72
|
+
}
|
|
73
|
+
applyPrefix(key) {
|
|
74
|
+
return this.prefix + key;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
79
|
+
__proto__: null,
|
|
80
|
+
PreferencesWeb: PreferencesWeb
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
exports.Preferences = Preferences;
|
|
84
|
+
|
|
85
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
86
|
+
|
|
87
|
+
return exports;
|
|
88
|
+
|
|
89
|
+
})({}, capacitorExports);
|
|
90
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst Preferences = registerPlugin('Preferences', {\n web: () => import('./web').then(m => new m.PreferencesWeb()),\n});\nexport * from './definitions';\nexport { Preferences };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class PreferencesWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this.group = 'CapacitorStorage';\n }\n async configure({ group }) {\n if (typeof group === 'string') {\n this.group = group;\n }\n }\n async get(options) {\n const value = this.impl.getItem(this.applyPrefix(options.key));\n return { value };\n }\n async set(options) {\n this.impl.setItem(this.applyPrefix(options.key), options.value);\n }\n async remove(options) {\n this.impl.removeItem(this.applyPrefix(options.key));\n }\n async keys() {\n const keys = this.rawKeys().map(k => k.substring(this.prefix.length));\n return { keys };\n }\n async clear() {\n for (const key of this.rawKeys()) {\n this.impl.removeItem(key);\n }\n }\n async migrate() {\n var _a;\n const migrated = [];\n const existing = [];\n const oldprefix = '_cap_';\n const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);\n for (const oldkey of keys) {\n const key = oldkey.substring(oldprefix.length);\n const value = (_a = this.impl.getItem(oldkey)) !== null && _a !== void 0 ? _a : '';\n const { value: currentValue } = await this.get({ key });\n if (typeof currentValue === 'string') {\n existing.push(key);\n }\n else {\n await this.set({ key, value });\n migrated.push(key);\n }\n }\n return { migrated, existing };\n }\n async removeOld() {\n const oldprefix = '_cap_';\n const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);\n for (const oldkey of keys) {\n this.impl.removeItem(oldkey);\n }\n }\n get impl() {\n return window.localStorage;\n }\n get prefix() {\n return this.group === 'NativeStorage' ? '' : `${this.group}.`;\n }\n rawKeys() {\n return Object.keys(this.impl).filter(k => k.indexOf(this.prefix) === 0);\n }\n applyPrefix(key) {\n return this.prefix + key;\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,WAAW,GAAGA,mBAAc,CAAC,aAAa,EAAE;IAClD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,cAAc,EAAE,CAAC;IAChE,CAAC;;ICFM,MAAM,cAAc,SAASC,cAAS,CAAC;IAC9C,IAAI,WAAW,GAAG;IAClB,QAAQ,KAAK,CAAC,GAAG,SAAS,CAAC,CAAC;IAC5B,QAAQ,IAAI,CAAC,KAAK,GAAG,kBAAkB,CAAC;IACxC,KAAK;IACL,IAAI,MAAM,SAAS,CAAC,EAAE,KAAK,EAAE,EAAE;IAC/B,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;IACvC,YAAY,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IAC/B,SAAS;IACT,KAAK;IACL,IAAI,MAAM,GAAG,CAAC,OAAO,EAAE;IACvB,QAAQ,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;IACvE,QAAQ,OAAO,EAAE,KAAK,EAAE,CAAC;IACzB,KAAK;IACL,IAAI,MAAM,GAAG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GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACtE,KAAK;IACL,IAAI,OAAO,GAAG;IACd,QAAQ,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAChF,KAAK;IACL,IAAI,WAAW,CAAC,GAAG,EAAE;IACrB,QAAQ,OAAO,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;IACjC,KAAK;IACL;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3
|
+
<plist version="1.0">
|
|
4
|
+
<dict>
|
|
5
|
+
<key>CFBundleDevelopmentRegion</key>
|
|
6
|
+
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
|
7
|
+
<key>CFBundleExecutable</key>
|
|
8
|
+
<string>$(EXECUTABLE_NAME)</string>
|
|
9
|
+
<key>CFBundleIdentifier</key>
|
|
10
|
+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
|
11
|
+
<key>CFBundleInfoDictionaryVersion</key>
|
|
12
|
+
<string>6.0</string>
|
|
13
|
+
<key>CFBundleName</key>
|
|
14
|
+
<string>$(PRODUCT_NAME)</string>
|
|
15
|
+
<key>CFBundlePackageType</key>
|
|
16
|
+
<string>FMWK</string>
|
|
17
|
+
<key>CFBundleShortVersionString</key>
|
|
18
|
+
<string>1.0</string>
|
|
19
|
+
<key>CFBundleVersion</key>
|
|
20
|
+
<string>$(CURRENT_PROJECT_VERSION)</string>
|
|
21
|
+
<key>NSPrincipalClass</key>
|
|
22
|
+
<string></string>
|
|
23
|
+
</dict>
|
|
24
|
+
</plist>
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
public struct PreferencesConfiguration {
|
|
4
|
+
public enum Group {
|
|
5
|
+
case named(String), cordovaNativeStorage
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let group: Group
|
|
9
|
+
|
|
10
|
+
public init(for group: Group = .named("CapacitorStorage")) {
|
|
11
|
+
self.group = group
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public class Preferences {
|
|
16
|
+
private let configuration: PreferencesConfiguration
|
|
17
|
+
|
|
18
|
+
private var defaults: UserDefaults {
|
|
19
|
+
return UserDefaults.standard
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private var prefix: String {
|
|
23
|
+
switch configuration.group {
|
|
24
|
+
case .cordovaNativeStorage:
|
|
25
|
+
return ""
|
|
26
|
+
case let .named(group):
|
|
27
|
+
return group + "."
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private var rawKeys: [String] {
|
|
32
|
+
return defaults.dictionaryRepresentation().keys.filter { $0.hasPrefix(prefix) }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public init(with configuration: PreferencesConfiguration) {
|
|
36
|
+
self.configuration = configuration
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public func get(by key: String) -> String? {
|
|
40
|
+
return defaults.string(forKey: applyPrefix(to: key))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public func set(_ value: String, for key: String) {
|
|
44
|
+
defaults.set(value, forKey: applyPrefix(to: key))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public func remove(by key: String) {
|
|
48
|
+
defaults.removeObject(forKey: applyPrefix(to: key))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public func removeAll() {
|
|
52
|
+
for key in rawKeys {
|
|
53
|
+
defaults.removeObject(forKey: key)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public func keys() -> [String] {
|
|
58
|
+
return rawKeys.map { String($0.dropFirst(prefix.count)) }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private func applyPrefix(to key: String) -> String {
|
|
62
|
+
return prefix + key
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
#import <UIKit/UIKit.h>
|
|
2
|
+
|
|
3
|
+
//! Project version number for Plugin.
|
|
4
|
+
FOUNDATION_EXPORT double PluginVersionNumber;
|
|
5
|
+
|
|
6
|
+
//! Project version string for Plugin.
|
|
7
|
+
FOUNDATION_EXPORT const unsigned char PluginVersionString[];
|
|
8
|
+
|
|
9
|
+
// In this header, you should import all the public headers of your framework using statements like #import <Plugin/PublicHeader.h>
|
|
10
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
#import <Capacitor/Capacitor.h>
|
|
3
|
+
|
|
4
|
+
// Define the plugin using the CAP_PLUGIN Macro, and
|
|
5
|
+
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
|
|
6
|
+
CAP_PLUGIN(PreferencesPlugin, "Preferences",
|
|
7
|
+
CAP_PLUGIN_METHOD(configure, CAPPluginReturnPromise);
|
|
8
|
+
CAP_PLUGIN_METHOD(get, CAPPluginReturnPromise);
|
|
9
|
+
CAP_PLUGIN_METHOD(set, CAPPluginReturnPromise);
|
|
10
|
+
CAP_PLUGIN_METHOD(remove, CAPPluginReturnPromise);
|
|
11
|
+
CAP_PLUGIN_METHOD(keys, CAPPluginReturnPromise);
|
|
12
|
+
CAP_PLUGIN_METHOD(clear, CAPPluginReturnPromise);
|
|
13
|
+
CAP_PLUGIN_METHOD(migrate, CAPPluginReturnPromise);
|
|
14
|
+
CAP_PLUGIN_METHOD(removeOld, CAPPluginReturnPromise);
|
|
15
|
+
)
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc(PreferencesPlugin)
|
|
5
|
+
public class PreferencesPlugin: CAPPlugin {
|
|
6
|
+
private var preferences = Preferences(with: PreferencesConfiguration())
|
|
7
|
+
|
|
8
|
+
@objc func configure(_ call: CAPPluginCall) {
|
|
9
|
+
let group = call.getString("group")
|
|
10
|
+
let configuration: PreferencesConfiguration
|
|
11
|
+
|
|
12
|
+
if let group = group {
|
|
13
|
+
if group == "NativeStorage" {
|
|
14
|
+
configuration = PreferencesConfiguration(for: .cordovaNativeStorage)
|
|
15
|
+
} else {
|
|
16
|
+
configuration = PreferencesConfiguration(for: .named(group))
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
configuration = PreferencesConfiguration()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
preferences = Preferences(with: configuration)
|
|
23
|
+
call.resolve()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@objc func get(_ call: CAPPluginCall) {
|
|
27
|
+
guard let key = call.getString("key") else {
|
|
28
|
+
call.reject("Must provide a key")
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let value = preferences.get(by: key)
|
|
33
|
+
|
|
34
|
+
call.resolve([
|
|
35
|
+
"value": value as Any
|
|
36
|
+
])
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@objc func set(_ call: CAPPluginCall) {
|
|
40
|
+
guard let key = call.getString("key") else {
|
|
41
|
+
call.reject("Must provide a key")
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
let value = call.getString("value", "")
|
|
45
|
+
|
|
46
|
+
preferences.set(value, for: key)
|
|
47
|
+
call.resolve()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@objc func remove(_ call: CAPPluginCall) {
|
|
51
|
+
guard let key = call.getString("key") else {
|
|
52
|
+
call.reject("Must provide a key")
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
preferences.remove(by: key)
|
|
57
|
+
call.resolve()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@objc func keys(_ call: CAPPluginCall) {
|
|
61
|
+
let keys = preferences.keys()
|
|
62
|
+
|
|
63
|
+
call.resolve([
|
|
64
|
+
"keys": keys
|
|
65
|
+
])
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@objc func clear(_ call: CAPPluginCall) {
|
|
69
|
+
preferences.removeAll()
|
|
70
|
+
call.resolve()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@objc func migrate(_ call: CAPPluginCall) {
|
|
74
|
+
var migrated: [String] = []
|
|
75
|
+
var existing: [String] = []
|
|
76
|
+
let oldPrefix = "_cap_"
|
|
77
|
+
let oldKeys = UserDefaults.standard.dictionaryRepresentation().keys.filter { $0.hasPrefix(oldPrefix) }
|
|
78
|
+
|
|
79
|
+
for oldKey in oldKeys {
|
|
80
|
+
let key = String(oldKey.dropFirst(oldPrefix.count))
|
|
81
|
+
let value = UserDefaults.standard.string(forKey: oldKey) ?? ""
|
|
82
|
+
let currentValue = preferences.get(by: key)
|
|
83
|
+
|
|
84
|
+
if currentValue == nil {
|
|
85
|
+
preferences.set(value, for: key)
|
|
86
|
+
migrated.append(key)
|
|
87
|
+
} else {
|
|
88
|
+
existing.append(key)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
call.resolve([
|
|
93
|
+
"migrated": migrated,
|
|
94
|
+
"existing": existing
|
|
95
|
+
])
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
@objc func removeOld(_ call: CAPPluginCall) {
|
|
99
|
+
let oldPrefix = "_cap_"
|
|
100
|
+
let oldKeys = UserDefaults.standard.dictionaryRepresentation().keys.filter { $0.hasPrefix(oldPrefix) }
|
|
101
|
+
for oldKey in oldKeys {
|
|
102
|
+
UserDefaults.standard.removeObject(forKey: oldKey)
|
|
103
|
+
}
|
|
104
|
+
call.resolve()
|
|
105
|
+
}
|
|
106
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@capacitor/preferences",
|
|
3
|
+
"version": "4.0.0-beta.0",
|
|
4
|
+
"description": "The Preferences API provides a simple key/value persistent store for lightweight data.",
|
|
5
|
+
"main": "dist/plugin.cjs.js",
|
|
6
|
+
"module": "dist/esm/index.js",
|
|
7
|
+
"types": "dist/esm/index.d.ts",
|
|
8
|
+
"unpkg": "dist/plugin.js",
|
|
9
|
+
"files": [
|
|
10
|
+
"android/src/main/",
|
|
11
|
+
"android/build.gradle",
|
|
12
|
+
"dist/",
|
|
13
|
+
"ios/Plugin/",
|
|
14
|
+
"CapacitorPreferences.podspec"
|
|
15
|
+
],
|
|
16
|
+
"author": "Ionic <hi@ionicframework.com>",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/ionic-team/capacitor-plugins"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/ionic-team/capacitor-plugins/issues"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"capacitor",
|
|
27
|
+
"plugin",
|
|
28
|
+
"native"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
|
|
32
|
+
"verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -destination generic/platform=iOS && cd ..",
|
|
33
|
+
"verify:android": "cd android && ./gradlew clean build test && cd ..",
|
|
34
|
+
"verify:web": "npm run build",
|
|
35
|
+
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
|
|
36
|
+
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
|
|
37
|
+
"eslint": "eslint . --ext ts",
|
|
38
|
+
"prettier": "prettier \"**/*.{css,html,ts,js,java}\"",
|
|
39
|
+
"swiftlint": "node-swiftlint",
|
|
40
|
+
"docgen": "docgen --api PreferencesPlugin --output-readme README.md --output-json dist/docs.json",
|
|
41
|
+
"build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.js",
|
|
42
|
+
"clean": "rimraf ./dist",
|
|
43
|
+
"watch": "tsc --watch",
|
|
44
|
+
"prepublishOnly": "npm run build",
|
|
45
|
+
"publish:cocoapod": "pod trunk push ./CapacitorPreferences.podspec --allow-warnings"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@capacitor/android": "next",
|
|
49
|
+
"@capacitor/core": "next",
|
|
50
|
+
"@capacitor/docgen": "0.0.18",
|
|
51
|
+
"@capacitor/ios": "next",
|
|
52
|
+
"@ionic/eslint-config": "^0.3.0",
|
|
53
|
+
"@ionic/prettier-config": "~1.0.1",
|
|
54
|
+
"@ionic/swiftlint-config": "^1.1.2",
|
|
55
|
+
"eslint": "^7.11.0",
|
|
56
|
+
"prettier": "~2.3.0",
|
|
57
|
+
"prettier-plugin-java": "~1.0.2",
|
|
58
|
+
"rimraf": "^3.0.0",
|
|
59
|
+
"rollup": "^2.29.0",
|
|
60
|
+
"swiftlint": "^1.0.1",
|
|
61
|
+
"typescript": "~4.1.5"
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"@capacitor/core": "next"
|
|
65
|
+
},
|
|
66
|
+
"prettier": "@ionic/prettier-config",
|
|
67
|
+
"swiftlint": "@ionic/swiftlint-config",
|
|
68
|
+
"eslintConfig": {
|
|
69
|
+
"extends": "@ionic/eslint-config/recommended"
|
|
70
|
+
},
|
|
71
|
+
"capacitor": {
|
|
72
|
+
"ios": {
|
|
73
|
+
"src": "ios"
|
|
74
|
+
},
|
|
75
|
+
"android": {
|
|
76
|
+
"src": "android"
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"publishConfig": {
|
|
80
|
+
"access": "public"
|
|
81
|
+
},
|
|
82
|
+
"gitHead": "fd7db8285f72990522da0adc889514c9804b6dae"
|
|
83
|
+
}
|