Skip to content

Commit

Permalink
feat(@kvs/localstorage): migrate to Schema type
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 8, 2020
1 parent 84d75f4 commit 0c84640
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
26 changes: 24 additions & 2 deletions packages/localstorage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,30 @@ Install with [npm](https://www.npmjs.com/):

## Usage

- [ ] Write usage instructions

```ts
import assert from "assert";
import { kvsLocalStorage } from "@kvs/localstorage";
(async () => {
type StorageSchema = {
a1: string;
b2: number;
c3: boolean;
};
const storage = await kvsLocalStorage<StorageSchema>({
name: "test",
version: 1
});
await storage.set("a1", "string");
await storage.set("b2", 42);
await storage.set("c3", false);
const a1 = await storage.get("a1");
const b2 = await storage.get("b2");
const c3 = await storage.get("c3");
assert.strictEqual(a1, "string");
assert.strictEqual(b2, 42);
assert.strictEqual(c3, false);
})();
```
## Changelog

See [Releases page](https://github.com/azu/kvs/releases).
Expand Down
15 changes: 9 additions & 6 deletions packages/localstorage/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { JsonValue, KvsStorage, kvsStorage, KVSStorageKey } from "@kvs/storage";
import { JsonValue, KvsStorage, kvsStorage } from "@kvs/storage";
import { KVS, KVSOptions } from "@kvs/types";

export type KvsLocalStorage<K extends KVSStorageKey, V extends JsonValue> = KVS<K, V>;
export type KvsLocalStorageOptions<K extends KVSStorageKey, V extends JsonValue> = KVSOptions<K, V> & {
export type KvsLocalStorageSchema = {
[index: string]: JsonValue;
};
export type KvsLocalStorage<Schema extends KvsLocalStorageSchema> = KVS<Schema>;
export type KvsLocalStorageOptions<Schema extends KvsLocalStorageSchema> = KVSOptions<Schema> & {
kvsVersionKey?: string;
};
export const kvsLocalStorage = async <K extends KVSStorageKey, V extends JsonValue>(
options: KvsLocalStorageOptions<K, V>
): Promise<KvsStorage<K, V>> => {
export const kvsLocalStorage = async <Schema extends KvsLocalStorageSchema>(
options: KvsLocalStorageOptions<Schema>
): Promise<KvsStorage<Schema>> => {
return kvsStorage({
...options,
storage: window.localStorage
Expand Down
3 changes: 2 additions & 1 deletion packages/storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Install with [npm](https://www.npmjs.com/):
## Usage

```ts
import assert from "assert";
import { kvsStorage } from "@kvs/storage";
(async () => {
type StorageSchema = {
Expand All @@ -34,7 +35,7 @@ import { kvsStorage } from "@kvs/storage";
assert.strictEqual(a1, "string");
assert.strictEqual(b2, 42);
assert.strictEqual(c3, false);
})()
})();
```

## Changelog
Expand Down

0 comments on commit 0c84640

Please sign in to comment.