Skip to content

Commit 7480535

Browse files
author
Krzysztof Borowy
authored
Merge pull request #186 from react-native-community/improve-ts
* Improve types for Factory, Async storage and Storage backends * Docs: Core * Docs: Legacy * Docs: Writing own storage backend * Make sure the packages can be published correctly
2 parents 7531bc4 + 5a110c3 commit 7480535

21 files changed

+623
-207
lines changed

.npmignore

-85
This file was deleted.

README.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Async Storage
22

3-
A storage system for React Native.
3+
A persistent data storage solution for your next mobile, web or desktop application.
44

55
## Work in progress
66

@@ -10,8 +10,22 @@ If you're looking for published and operational Async Storage version, please ch
1010

1111
If you'd like to see the progress of v2, [please visit Project page.](https://github.com/react-native-community/async-storage/projects/1)
1212

13-
## Running Examples
13+
## Features
14+
15+
todo
16+
17+
## Documentation
18+
19+
- [Creating a custom Storage backend](./packages/core/docs/Writing_Storage_Backend.md)
20+
21+
## Available storage backends
22+
23+
- [Legacy](./packages/storage-legacy/README.md)
24+
25+
26+
## License
27+
28+
MIT
1429

15-
### React Native
1630

1731

examples/mobile/.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{
66
"alias": {
77
"@react-native-community/async-storage": "../../packages/core/build",
8-
"@react-native-community/async-storage-legacy": "../../packages/storage-legacy/build"
8+
"@react-native-community/async-storage-backend-legacy": "../../packages/storage-legacy/build"
99
},
1010
"cwd": "babelrc"
1111
}

examples/mobile/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ $ yarn start
2828

2929
Let me know about any issue found or feedback you have at [Async-Storage issue page](https://github.com/react-native-community/async-storage/issues).
3030
Please mark it as `examples` label.
31+
32+
## License
33+
34+
MIT

examples/mobile/src/legacy/storage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import LegacyStorage from '@react-native-community/async-storage-legacy';
1+
import LegacyStorage from '@react-native-community/async-storage-backend-legacy';
22
import AsyncStorageFactory from '@react-native-community/async-storage';
33

44

lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"packages": [
33
"core",
4-
"storages/*"
4+
"storages-*"
55
],
66
"version": "independent",
77
"npmClient": "yarn",

packages/core/.npmignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Core
2+
node_modules
3+
yarn.lock
4+
src/
5+
__tests__/
6+
CONTRIBUTING.md
7+
CODE_OF_CONDUCT.md
8+
9+
10+
########## Trash ##########
11+
.DS_Store
12+
.DS_Store?
13+
*.DS_Store
14+
coverage.android.json
15+
coverage.ios.json
16+
coverage
17+
npm-debug.log
18+
.github
19+
._*
20+
.Spotlight-V100
21+
.Trashes
22+
ehthumbs.db
23+
Thumbs.dbandroid/gradle
24+
.idea
25+
bin/test.js
26+
codorials
27+
.vscode
28+
.nyc_output
29+
yarn-error.log
30+

packages/core/README.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Async Storage Core
2+
3+
Main, public-facing components of Async Storage. The core module contains a factory to create an `AsyncStorage` instance
4+
and `IStorageBackend`, that needs to be implemented by any Backend Storage in order to be compatible.
5+
6+
## Install
7+
8+
```bash
9+
$ yarn install @react-native-community/async-storage
10+
```
11+
12+
## API
13+
14+
15+
### `AsyncStorageFactory`
16+
17+
A factory module for `AsyncStorage` instance with selected storage backend attached.
18+
19+
20+
```typescript
21+
// storage.js
22+
23+
import ASFactory from '@react-native-community/async-storage'
24+
25+
// use any available Storage Backend
26+
const storageBackend = new StorageBackend();
27+
28+
const mobileStorage = ASFactory.create(storageBackend, options);
29+
30+
export default mobileStorage;
31+
```
32+
33+
34+
**Factory options**
35+
36+
`AsyncStorageFactory.create` accepts an options object, that enables additional features.
37+
38+
39+
- *logger*
40+
41+
```typescript
42+
type logger = ((action: LoggerAction) => void) | boolean;
43+
```
44+
45+
Used to log `AsyncStorage` method calls and used arguments.
46+
You can provide your own implementation or use provided default logger (enabled by default in DEV mode).
47+
48+
49+
- *errorHandler*
50+
51+
```typescript
52+
type errorHandler = ((error: Error | string) => void) | boolean;
53+
````
54+
55+
Used to report any errors thrown.
56+
You can provide your own implementation or use provided default error handler (enabled by default in DEV mode).
57+
58+
59+
**Providing a storage model**
60+
61+
If you know the structure of the stored data upfront, you can use the full potential of the type system, by providing a type argument to `AsyncStorageFactory.create<T>` method.
62+
63+
64+
```typescript
65+
import ASFactory from '@react-native-community/async-storage'
66+
67+
type StorageModel = {
68+
user: {
69+
name: string;
70+
age: number;
71+
};
72+
preferences: {
73+
darkModeEnabled: boolean;
74+
};
75+
subscribedChannels: Array<string>;
76+
onboardingCompleted: boolean;
77+
};
78+
79+
// use any available Storage Backend
80+
const storageBackend = new StorageBackend();
81+
82+
83+
const storage = ASFactory.create<StorageModel>(storageBackend);
84+
85+
```
86+
87+
88+
### `IStorageBackend`
89+
90+
In order to let `AsyncStorage` use a storage backend, it has to implement this interface.
91+
Contains basic set method of methods to get, set and remove data, return already used keys or drop the whole storage.
92+
93+
94+
```typescript
95+
96+
import {
97+
IStorageBackend,
98+
} from '@react-native-community/async-storage';
99+
100+
type Model = {
101+
count: number
102+
user: {
103+
name: string,
104+
rating: number
105+
}
106+
}
107+
108+
class MyStorageSolution implements IStorageBackend<Model> {
109+
// implement necessary methods
110+
}
111+
112+
```
113+
114+
115+
116+
## License
117+
118+
MIT
119+

packages/core/__tests__/AsyncStorage.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ describe('AsyncStorage', () => {
2020
});
2121

2222
type testCases = [
23-
Partial<keyof AsyncStorage<any>>,
23+
Partial<keyof AsyncStorage<any, any>>,
2424
Partial<keyof StorageMock>,
2525
string
2626
][];
2727

2828
describe('main API', () => {
29-
const asyncStorage = new AsyncStorage<any>(mockedStorage, {
29+
const asyncStorage = new AsyncStorage<any, any>(mockedStorage, {
3030
logger: false,
3131
errorHandler: false,
3232
});
@@ -63,7 +63,7 @@ describe('AsyncStorage', () => {
6363
it('uses logger when provided', async () => {
6464
const loggerFunc = jest.fn();
6565

66-
const as = new AsyncStorage(mockedStorage, {
66+
const as = new AsyncStorage<any, any>(mockedStorage, {
6767
logger: loggerFunc,
6868
errorHandler: false,
6969
});
@@ -81,7 +81,7 @@ describe('AsyncStorage', () => {
8181
throw error;
8282
});
8383

84-
const as = new AsyncStorage(mockedStorage, {
84+
const as = new AsyncStorage<any, any>(mockedStorage, {
8585
errorHandler,
8686
logger: false,
8787
});

0 commit comments

Comments
 (0)