Skip to content

Commit

Permalink
feat: add capacitor-preferences driver (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcoru97 authored Aug 7, 2023
1 parent 022fc39 commit 0268049
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/content/6.drivers/capacitor-preferences.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Capacitor Preferences

Stores data via [Capacitor Preferences API](https://capacitorjs.com/docs/apis/preferences) on mobile devices or the local storage on the web.

To use this driver, you need to install and sync `@capacitor/preferences` inside your capacitor project:

::code-group

```sh [npm]
npm install @capacitor/preferences
npx cap sync
```

```sh [Yarn]
yarn add @capacitor/preferences
npx cap sync
```

```sh [pnpm]
pnpm add @capacitor/preferences
pnpm cap sync
```

::

Usage:

```js
import { createStorage } from "unstorage";
import capacitorPreferences from "unstorage/drivers/capacitor-preferences";

const storage = createStorage({
driver: capacitorPreferences({
base: "test",
}),
});
```

**Options:**

- `base`: Add `${base}:` to all keys to avoid collision
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@azure/identity": "^3.2.4",
"@azure/keyvault-secrets": "^4.7.0",
"@azure/storage-blob": "^12.15.0",
"@capacitor/preferences": "^5.0.6",
"@cloudflare/workers-types": "^4.20230801.0",
"@planetscale/database": "^1.10.0",
"@types/ioredis-mock": "^8.2.2",
Expand Down Expand Up @@ -101,6 +102,7 @@
"@azure/identity": "^3.2.3",
"@azure/keyvault-secrets": "^4.7.0",
"@azure/storage-blob": "^12.14.0",
"@capacitor/preferences": "^5.0.0",
"idb-keyval": "^6.2.1",
"@planetscale/database": "^1.8.0",
"@upstash/redis": "^1.22.0",
Expand All @@ -125,6 +127,9 @@
"@azure/storage-blob": {
"optional": true
},
"@capacitor/preferences": {
"optional": true
},
"@planetscale/database": {
"optional": true
},
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions src/drivers/capacitor-preferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Preferences } from "@capacitor/preferences";

import { defineDriver, joinKeys, normalizeKey } from "./utils";

const DRIVER_NAME = "capacitor-preferences";

export interface CapacitorPreferencesOptions {
base?: string;
}

export default defineDriver<CapacitorPreferencesOptions>((opts) => {
const base = normalizeKey(opts?.base || "");
const resolveKey = (key: string) => joinKeys(base, key);

return {
name: DRIVER_NAME,
options: opts,
hasItem(key) {
return Preferences.keys().then((r) => r.keys.includes(resolveKey(key)));
},
getItem(key) {
return Preferences.get({ key: resolveKey(key) }).then((r) => r.value);
},
getItemRaw(key) {
return Preferences.get({ key: resolveKey(key) }).then((r) => r.value);
},
setItem(key, value) {
return Preferences.set({ key: resolveKey(key), value });
},
setItemRaw(key, value) {
return Preferences.set({ key: resolveKey(key), value });
},
removeItem(key) {
return Preferences.remove({ key: resolveKey(key) });
},
async getKeys() {
const { keys } = await Preferences.keys();
return keys.map((key) => key.slice(base.length));
},
async clear(prefix) {
const { keys } = await Preferences.keys();
const _prefix = resolveKey(prefix || "");
await Promise.all(
keys
.filter((key) => key.startsWith(_prefix))
.map((key) => Preferences.remove({ key }))
);
},
};
});
40 changes: 40 additions & 0 deletions test/drivers/capacitor-preferences.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, vi } from "vitest";
import driver from "../../src/drivers/capacitor-preferences";
import { testDriver } from "./utils";
import { afterEach } from "node:test";

vi.mock("@capacitor/preferences", () => {
const data = new Map<string, string>();

const keys = vi.fn(() => Promise.resolve({ keys: Array.from(data.keys()) }));
const get = vi.fn(({ key }) =>
Promise.resolve({ value: data.get(key) ?? null })
);
const set = vi.fn(({ key, value }) => Promise.resolve(data.set(key, value)));
const remove = vi.fn(({ key }) => Promise.resolve(data.delete(key)));
const clear = vi.fn(() => Promise.resolve(data.clear()));

return {
Preferences: {
keys,
get,
set,
remove,
clear,
},
};
});

describe("drivers: capacitor-preferences", () => {
afterEach(() => {
vi.resetAllMocks();
});

testDriver({
driver: driver({}),
});

testDriver({
driver: driver({ base: "test" }),
});
});

0 comments on commit 0268049

Please sign in to comment.