Skip to content

Commit c8ba775

Browse files
author
Krzysztof Borowy
committed
examples
1 parent fdca26e commit c8ba775

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
test-name: [lint, ts]
14-
workspace: [default-storage-backend, core]
14+
workspace: [default-storage-backend, api]
1515
steps:
1616
- name: Checkout
1717
uses: actions/checkout@v3
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { StorageExtension } from "../src";
2+
3+
export interface MyExampleExtension extends StorageExtension {
4+
double: (num: number) => Promise<number>;
5+
6+
uppercase: (text: string) => string;
7+
8+
key: string;
9+
}
10+
11+
export class ExampleExtension implements MyExampleExtension {
12+
key = "my-example-storage";
13+
14+
double = async (num: number) => num * 2;
15+
16+
uppercase = (text: string): string => text.toUpperCase();
17+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { AsyncStorage, StorageKeys, StorageModel } from "../src";
2+
import { ExampleExtension, MyExampleExtension } from "./ExampleExtension";
3+
4+
type MyModel = StorageModel<{
5+
age: number;
6+
name: string;
7+
likes: boolean[];
8+
}>;
9+
10+
// @ts-ignore
11+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
12+
class ExampleStorage implements AsyncStorage<MyModel, MyExampleExtension> {
13+
private storage: MyModel = {
14+
age: null,
15+
name: null,
16+
likes: null,
17+
};
18+
19+
getItem = async <K extends keyof MyModel>(key: K): Promise<MyModel[K]> => {
20+
return this.storage[key];
21+
};
22+
23+
setItem = async <K extends StorageKeys<MyModel>>(
24+
key: K,
25+
value: MyModel[K]
26+
): Promise<void> => {
27+
this.storage[key] = value;
28+
};
29+
30+
removeItem = async <K extends StorageKeys<MyModel>>(
31+
key: K
32+
): Promise<void> => {
33+
this.storage[key] = null;
34+
};
35+
36+
getMany = async <K extends StorageKeys<MyModel>>(
37+
keys: K[]
38+
): Promise<{ [k in K]: MyModel[k] }> => {
39+
return keys.reduce((entries, key) => {
40+
return {
41+
...entries,
42+
[key]: this.storage[key] ?? null,
43+
};
44+
}, {} as { [k in K]: MyModel[k] });
45+
};
46+
47+
setMany = async <K extends StorageKeys<MyModel>>(entries: {
48+
[k in K]: MyModel[k];
49+
}): Promise<void> => {
50+
Object.entries(entries).forEach((entry) => {
51+
const key = entry[0] as K;
52+
this.storage[key] = entry[1] as MyModel[K];
53+
});
54+
};
55+
56+
removeMany = async <K extends StorageKeys<MyModel>>(
57+
keys: K[]
58+
): Promise<void> => {
59+
keys.forEach((k) => {
60+
this.storage[k] = null;
61+
});
62+
};
63+
64+
clear = async (): Promise<void> => {
65+
this.storage.age = null;
66+
this.storage.name = null;
67+
this.storage.likes = null;
68+
};
69+
70+
ext: MyExampleExtension = new ExampleExtension();
71+
}

0 commit comments

Comments
 (0)