aio-capacitor 2.2.2 → 2.2.3

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.
Files changed (3) hide show
  1. package/index.d.ts +5 -3
  2. package/index.js +54 -4
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -65,14 +65,16 @@ export declare class AIOCapacitor {
65
65
  fileName: string;
66
66
  }[])>;
67
67
  openFile: (filePath: string) => Promise<void>;
68
- readFile: (path: string, options?: {
68
+ isFileExist: (path: string, location?: 'Data' | 'Documents' | 'Cache') => Promise<boolean>;
69
+ readFile: (path: string, def: any, options?: {
69
70
  location?: 'Data' | 'Documents' | 'Cache';
70
71
  utf8?: boolean;
72
+ writeDef?: boolean;
71
73
  }) => Promise<any>;
72
- writeFile: (data: string, path: string, options?: {
74
+ writeFile: (data: any, path: string, options?: {
73
75
  location?: 'Data' | 'Documents' | 'Cache';
74
76
  utf8?: boolean;
75
- }) => Promise<any>;
77
+ }) => Promise<string>;
76
78
  save: (data: string | File | Blob, path: string, options?: {
77
79
  location?: 'Data' | 'Documents' | 'Cache';
78
80
  utf8?: boolean;
package/index.js CHANGED
@@ -198,12 +198,38 @@ export class AIOCapacitor {
198
198
  alert(`Open file error:\n${String(error)}`);
199
199
  }
200
200
  });
201
- this.readFile = (path, options) => __awaiter(this, void 0, void 0, function* () {
202
- const res = yield Filesystem.readFile({ path, directory: this.getDirectory(options === null || options === void 0 ? void 0 : options.location), encoding: (options === null || options === void 0 ? void 0 : options.utf8) ? Encoding.UTF8 : undefined });
203
- return res.data;
201
+ this.isFileExist = (path, location) => __awaiter(this, void 0, void 0, function* () {
202
+ try {
203
+ yield Filesystem.stat({ path, directory: this.getDirectory(location) });
204
+ return true;
205
+ }
206
+ catch (_a) {
207
+ return false;
208
+ }
209
+ });
210
+ this.readFile = (path, def, options) => __awaiter(this, void 0, void 0, function* () {
211
+ const location = options === null || options === void 0 ? void 0 : options.location;
212
+ const isJson = typeof def !== 'string';
213
+ if (!(yield this.isFileExist(path, location))) {
214
+ if (options === null || options === void 0 ? void 0 : options.writeDef) {
215
+ yield this.writeFile(isJson ? JSON.stringify(def) : def, path, { location, utf8: isJson ? true : options === null || options === void 0 ? void 0 : options.utf8 });
216
+ }
217
+ return def;
218
+ }
219
+ const res = yield Filesystem.readFile({
220
+ path, directory: this.getDirectory(location),
221
+ encoding: isJson || (options === null || options === void 0 ? void 0 : options.utf8) ? Encoding.UTF8 : undefined
222
+ });
223
+ return isJson ? JSON.parse(res.data) : res.data;
204
224
  });
205
225
  this.writeFile = (data, path, options) => __awaiter(this, void 0, void 0, function* () {
206
- return Filesystem.writeFile({ path, data, directory: this.getDirectory(options === null || options === void 0 ? void 0 : options.location), encoding: (options === null || options === void 0 ? void 0 : options.utf8) ? Encoding.UTF8 : undefined });
226
+ const res = yield Filesystem.writeFile({
227
+ path,
228
+ data: typeof data === 'string' ? data : JSON.stringify(data),
229
+ directory: this.getDirectory(options === null || options === void 0 ? void 0 : options.location),
230
+ encoding: (options === null || options === void 0 ? void 0 : options.utf8) ? Encoding.UTF8 : undefined
231
+ });
232
+ return res.uri;
207
233
  });
208
234
  this.save = (data, path, options) => __awaiter(this, void 0, void 0, function* () {
209
235
  try {
@@ -693,3 +719,27 @@ export class NotificationService {
693
719
  this.init(actionCallBack);
694
720
  }
695
721
  }
722
+ class AppStorage {
723
+ constructor(props) {
724
+ this.cap = new AIOCapacitor();
725
+ this.save = (data) => __awaiter(this, void 0, void 0, function* () {
726
+ try {
727
+ yield this.cap.writeFile(JSON.stringify(data), this.fileName, { location: this.location, utf8: true });
728
+ }
729
+ catch (err) {
730
+ alert(`AppStorage save error: ${err.message}`);
731
+ }
732
+ });
733
+ this.load = (def) => __awaiter(this, void 0, void 0, function* () {
734
+ try {
735
+ const data = yield this.cap.readFile(this.fileName, { location: this.location, utf8: true });
736
+ }
737
+ catch (err) {
738
+ console.error('storageLoad error:', err);
739
+ return def;
740
+ }
741
+ });
742
+ this.fileName = props.fileName;
743
+ this.location = props.location;
744
+ }
745
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-capacitor",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",