-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
capacitor-preferences
driver (#283)
- Loading branch information
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })) | ||
); | ||
}, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }), | ||
}); | ||
}); |